2011年9月2日 星期五

[記事] cacti command line interface

屬於阿宅世界的技術文章,想看的再點開,切莫自誤 !

基本上 cacti 是套 PHP 程式,所有的功能都做的好好的在網頁上呈現, 只是要幫台機器加張圖的流程頗長,閒閒沒事幫兩三台機器裝三兩張圖沒什麼問題,但幾十台下來,長期保受滑鼠折磨的手實在吃不消,好在它內建了一些 script 可以在 command line 達到同樣的目的。以下是用法的簡單筆記。

伺服器 IP 配置
cacti : 192.168.1.1
host : 192.168.1.3
cacti 安裝目錄:
 /home/cacti

cacti 增加一張圖的流程:

在host弄好 snmp 相關設定 -> 建立 host device -> 建立graph -> 建立 tree -> 將 graph 加入 tree
首先對 host 進行 snmp 的設定

shell> vi /etc/snmp/snmpd.conf

rocommunity public 127.0.0.1
rocommunity public 192.168.1.1

disk /


開啟 host 的 snmpd
shell> /etc/init.d/snmpd start
因為被監控的host snmpd 一定要開起來,所以設定成開機後就自動啟動,免得 reboot 後沒開資料就收不到了
shell> chkconfig snmpd on
接著在 cacti 中建立這個 host 的 device 資料,每個device有預定好的 templates 可以選。所有可選的 template 可透過下面指令查詢
shell> php /home/cacti/cli/add_device.php --list-host-templates
Valid Host Templates: (id, name)
0 None
1 Generic SNMP-enabled Host
3 ucd/net SNMP Host
4 Karlnet Wireless Bridge
5 Cisco Router
6 Netware 4/5 Server
7 Windows 2000/XP Host
8 Local Linux Machine
因為只是單純的snmp enabled主機(3 ucd/net SNMP Host),所以建立 device 的指令如下
shell> php /home/cacti/cli/add_device.php --description="Machine - 192.168.1.3" --ip="192.168.1.3" --community=public --template=3;
完成後執行下列指令會列出能掛上 graph 的device,記好最前頭的id,之後會用得到
shell> php /home/cacti/cli/add_graphs.php --list-hosts
Known Hosts: (id, hostname, template, description)
1 192.168.1.3 3 Machine - 192.168.1.3
假設現在要掛上的 graph 是硬碟根目錄的使用量,可以以下列方式建立
shell> php /home/cacti/cli/add_graphs.php --graph-type=ds --graph-template-id=3 --snmp-query-id=2 --snmp-query-type-id=6 --snmp-field=dskIndex --snmp-value=1 --host-id=2
其中graph type 的部份,依照說明
'cg' graphs are for things like CPU temp/fan speed, while
'ds' graphs are for data-source based graphs (interface stats etc.)


查詢 graph template id 的方法如下
shell> php /home/cacti/cli/add_graphs.php --list-graph-templates;
Known Graph Templates:(id, name)
2 Interface - Traffic (bits/sec)
3 ucd/net - Available Disk Space
4 ucd/net - CPU Usage
5 Karlnet - Wireless Levels
6 Karlnet - Wireless Transmissions
7 Unix - Ping Latency
8 Unix - Processes
9 Unix - Load Average
10 Unix - Logged in Users
11 ucd/net - Load Average
12 Linux - Memory Usage
13 ucd/net - Memory Usage
14 Netware - File System Cache
15 Netware - CPU Utilization
16 Netware - File System Activity
17 Netware - Logged In Users
18 Cisco - CPU Usage
19 Netware - Volume Information
20 Netware - Directory Information
21 Unix - Available Disk Space
22 Interface - Errors/Discards
23 Interface - Unicast Packets
24 Interface - Non-Unicast Packets
25 Interface - Traffic (bytes/sec)
26 Host MIB - Available Disk Space
27 Host MIB - CPU Utilization
28 Host MIB - Logged in Users
29 Host MIB - Processes
30 Netware - Open Files
31 Interface - Traffic (bits/sec, 95th Percentile)
32 Interface - Traffic (bits/sec, Total Bandwidth)
33 Interface - Traffic (bytes/sec, Total Bandwidth)
34 SNMP - Generic OID Template
查詢 snmp query id 的方法如下
shell> php /home/cacti/cli/add_graphs.php --list-snmp-queries
Known SNMP Queries:(id, name)
1 SNMP - Interface Statistics
2 ucd/net - Get Monitored Partitions
3 Karlnet - Wireless Bridge Statistics
4 Netware - Get Available Volumes
6 Unix - Get Mounted Partitions
7 Netware - Get Processor Information
8 SNMP - Get Mounted Partitions
9 SNMP - Get Processor Information
查詢 snmp query type id 的方法如下
shell> php /home/cacti/cli/add_graphs.php --list-query-types --snmp-query-id=2
Known SNMP Query Types: (id, name)
6 Available/Used Disk Space
查詢 snmp query fields id 的方法如下
shell> php /home/cacti/cli/add_graphs.php --list-snmp-fields --host-id=1;
Known SNMP Fields for host-id 1: (name)
dskDevice
dskIndex
dskPath
ifAlias
ifDescr
ifHwAddr
ifIndex
ifIP
ifName
ifOperStatus
ifSpeed
ifType
查詢 snmp query value id 的方法如下
shell> php /home/cacti/cli/add_graphs.php --list-snmp-values --host-id=1 --snmp-field=dskIndex
Known values for dskIndex for host 2: (name)
1
device 的 graph 建立完成後,接著建立要掛上的tree (顯示用)
shell> php /home/cacti/cli/add_tree.php --type=tree --name="Available Disk Space" --sort-method=manual
Tree Created - tree-id: (1)
最後是把建好的 graph 掛上 tree
shell> php /home/cacti/cli/add_tree.php --type=node --node-type=graph --tree-id=1 --graph-id=1
查詢 graph id 的方法如下
shell> php /home/cacti/cli/add_tree.php --list-graphs --host-id=1
Known Host Graphs: (id, name, template)
1 cacti - Disk Space - / ucd/net - Available Disk Space
以上步驟只執行一次的話當然比在網頁操作麻煩得多,但如果如果寫成 shell script ,就可以批次進行大量設備的設定,以下是一個簡單的範例,在所有已經建好的device中挑張graph 加入某個tree中 
#!/bin/sh

PHP=/usr/bin/php
CACTI_HOME=/home/cacti

P_HOST_ID=()
P_GRAPH_ID=()

HOST_ID=(`php /home/cacti/cli/add_tree.php --list-hosts | grep -v "Known" | grep -v Localhost | awk '{print $1}'`)

${PHP} ${CACTI_HOME}/cli/add_tree.php --list-trees

printf "Choose the tree you wann append graph to : "
read TREE_ID

for host in ${HOST_ID[@]};
do
    ${PHP} ${CACTI_HOME}/cli/add_tree.php --list-graphs --host-id=$host
    printf "Choose graph (x for skip): "
    read GRAPH_ID

    if [ $GRAPH_ID != "x" ]
    then
        echo "Add graph : ${GRAPH_ID} to ${host}"
        P_HOST_ID=("${P_HOST_ID[@]}" $host)
        P_GRAPH_ID=("${P_GRAPH_ID[@]}" $GRAPH_ID)
    fi
done

for gid in ${P_GRAPH_ID[@]};
do
    ${PHP} ${CACTI_HOME}/cli/add_tree.php --type=node --node-type=graph --tree-id=$TREE_ID --graph-id=${gid}
done

那怎麼批次建 device 跟 graph 呢 .... 好吧, Ref那邊有個Garlic 寫好的範例,就不在這邊拾人牙慧了

Ref :
相關文章 :


0 意見:

張貼留言