Winse Blog

走走停停都是风景, 熙熙攘攘都向最好, 忙忙碌碌都为明朝, 何畏之.

Cacti批量添加配置

所有机器SNMP配置同步

1
2
3
for h in `cat /etc/hosts| grep hadoop|awk '{print $2}'` ; do scp -r /etc/snmp/snmpd.conf $h:/etc/snmp/ ; done

for h in `cat /etc/hosts| grep hadoop|awk '{print $2}'` ; do ssh $h "service snmpd start" ; done

Cacti批量添加配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
批量添加device。ip也可以为hostname;template为机器模板;version为SNMP的版本
[root@cu-omc1 cacti]# for h in `cat /etc/hosts | grep hadoop | awk '{print $2}'` ; do php cli/add_device.php --description="$h" --ip="$h" --template=9 --version=2 ; done

了解参数
[root@cu-omc1 cacti]# php cli/add_graphs.php --list-hosts

[root@cu-omc1 cacti]# php cli/add_graphs.php --list-graph-templates --host-template-id=9
Known Graph Templates:(id, name)
29      Host MIB - Processes
35      ucd/net - Users Logged On
36      ucd/net - TCP Current Established
37      ucd/net - Uptime
38      ucd/net - TCP Counters
39      ucd/net - Memory Usage (enhanced)
40      ucd/net - Load Average (enhanced)
41      ucd/net - CPU Usage (enhanced)

[root@cu-omc1 cacti]# php 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

[root@cu-omc1 cacti]# php cli/add_graphs.php --list-snmp-fields --host-id=2 --snmp-query-id=1
Known SNMP Fields for host-id 2: (name)
ifAlias
ifDescr
ifHighSpeed
ifHwAddr
ifIndex
ifIP
ifName
ifOperStatus
ifSpeed
ifType

[root@cu-omc1 cacti]# php cli/add_graphs.php --list-snmp-values --host-id=2 --snmp-query-id=1 --snmp-field=ifIP
Known values for ifIP for host 2: (name)
127.0.0.1
192.168.20.11

[root@cu-omc1 cacti]# php 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
35      ucd/net - Users Logged On
36      ucd/net - TCP Current Established
37      ucd/net - Uptime
38      ucd/net - TCP Counters
39      ucd/net - Memory Usage (enhanced)
40      ucd/net - Load Average (enhanced)
41      ucd/net - CPU Usage (enhanced)

[root@cu-omc1 cacti]# php cli/add_graphs.php  --list-query-types  --snmp-query-id=1
Known SNMP Query Types: (id, name)
2       In/Out Errors/Discarded Packets
3       In/Out Non-Unicast Packets
4       In/Out Unicast Packets
9       In/Out Bytes (64-bit Counters)
13      In/Out Bits
14      In/Out Bits (64-bit Counters)
16      In/Out Bytes
20      In/Out Bits with 95th Percentile
21      In/Out Bits with Total Bandwidth
22      In/Out Bytes with Total Bandwidth

先测试单机添加,对应到Device页面点击`Create Graphs for this Host`添加图像的操作
[root@cu-omc1 cacti]# php cli/add_graphs.php --host-id=2 --graph-type=cg --graph-template-id=40
Graph Added - graph-id: (5) - data-source-ids: (8, 9, 10)
[root@cu-omc1 cacti]# php cli/add_graphs.php --host-id=2 --graph-type=cg --graph-template-id=41
Graph Added - graph-id: (6) - data-source-ids: (11, 12, 13, 14)
[root@cu-omc1 cacti]# php cli/add_graphs.php --host-id=2 --graph-type=cg --graph-template-id=39
Graph Added - graph-id: (7) - data-source-ids: (15, 16, 17, 18, 19)
[root@cu-omc1 cacti]# php cli/add_graphs.php --host-id=2 --graph-type=cg --graph-template-id=38

[root@cu-omc1 cacti]# php cli/add_graphs.php --host-id="2" --graph-type=ds  --graph-template-id=2 --snmp-query-id=1 --snmp-query-type-id=16 --snmp-field=ifIP --snmp-value="192.168.20.11"
Graph Added - graph-id: (9) - data-source-ids: (24, 24)

批量操作
添加Graph Templates
[root@cu-omc1 cacti]# php cli/add_graphs.php --list-hosts | awk '{print $1}' | while read line ; do 
>  php cli/add_graphs.php --host-id=$line --graph-type=cg --graph-template-id=41
>  php cli/add_graphs.php --host-id=$line --graph-type=cg --graph-template-id=40
>  php cli/add_graphs.php --host-id=$line --graph-type=cg --graph-template-id=39
>  php cli/add_graphs.php --host-id=$line --graph-type=cg --graph-template-id=38
> done

添加Data Query。比较复杂点,需要查询匹配
php cli/add_graphs.php --list-hosts | awk '{print $1}' | while read line ; do 
  php cli/add_graphs.php --host-id=$line --graph-type=ds  --graph-template-id=2 --snmp-query-id=1 --snmp-query-type-id=16 --snmp-field=ifIP --snmp-value=$(grep "`php cli/add_graphs.php --list-hosts | grep "^$line\s" | awk '{print $2}'`\s" /etc/hosts | awk '{print $1}')
done

其他命令

1
2
3
4
5
6
7
8
9
10
11

dmesg |grep eth0

iftop –i eth0 –B

sar -n DEV 1 100 

ethtool eth0

[omc@cu-omc1 ~]$ sort -k 2 /tmp/cacti.list > /tmp/cacti.sort.list
[omc@cu-omc1 ~]$ grep hadoop /etc/hosts | sort -k 2 | join -j 2 - /tmp/cacti.sort.list 

参考

–END

Comments