一、安装前的准备工作
准备lnmp环境(略)
下载最新cacti
# wget http://www.cacti.net/downloads/cacti-0.8.8a.tar.gz
安装snmpwalk等工具集
# yum install net-snmp-utils
安装snmp开发包
#yum install net-snmp-devel
安装rrdtool
# yum install rrdtool
所有被监控端安装net-snm
#yum install net-snmp
二、安装cacti
1、解压
# tar xzvf cacti-0.8.7i-PIA-3.1.tar.gz
2、创建cacti数据库
# mysqladmin -uroot -p create cacti
3、导入cacti的默认数据库结构
# mysql -uroot -p cacti < cacti.sql
4、给cacti用户对cacti数据库的访问进行授权
mysql> GRANT ALL ON cacti.* TO cactiuser@localhost IDENTIFIED BY '123456';
mysql> flush privileges;
5.修改连接cacti数据库的用户名密码
# more /var/www/cacti/include/config.php
$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "localhost";
$database_username = "cactiuser";
$database_password = "123456";
6、给cacti目录设置权限
# chown nginx.nginx /var/www/cacti/ -R
7、创建crontab
# crontab -l
*/5 * * * * php /var/www/cacti/poller.php > /dev/null 2>&1
8、配置nginx
# more /etc/nginx/conf.d/cacti.conf |grep -v "#"
server {
listen 80;
server_name localhost;
root /var/www/cacti;
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/cacti$fastcgi_script_name;
include fastcgi_params;
}
}
9、浏览器访问,进行初始设置cacti
http://192.168.100.158
用户名密码均为admin,初次登陆需要修改密码
10、测试本机snmp运行是否正常
# snmpwalk -Os -c mycommunity -v 1 localhost system
11、开机启动snmpd
# chkconfig snmpd on
12、cacti监控
下载最新模板集
http://www.percona.com/downloads/percona-monitoring-plugins/percona-monitoring-plugins-1.0.0.tar.gz
$ ls percona-monitoring-plugins-1.0.0/cacti/templates/
cacti_host_template_percona_apache_server_ht_0.8.6i-sver1.0.0.xml
cacti_host_template_percona_gnu_linux_server_ht_0.8.6i-sver1.0.0.xml
cacti_host_template_percona_jmx_server_ht_0.8.6i-sver1.0.0.xml
cacti_host_template_percona_memcached_server_ht_0.8.6i-sver1.0.0.xml
cacti_host_template_percona_mongodb_server_ht_0.8.6i-sver1.0.0.xml
cacti_host_template_percona_mysql_server_ht_0.8.6i-sver1.0.0.xml
cacti_host_template_percona_nginx_server_ht_0.8.6i-sver1.0.0.xml
cacti_host_template_percona_openvz_server_ht_0.8.6i-sver1.0.0.xml
cacti_host_template_percona_redis_server_ht_0.8.6i-sver1.0.0.xml
解压后在cacti界面导入相应的xml文件。
安装脚本
#cp percona-monitoring-plugins-1.0.0/cacti/scripts/* /var/www/cacti/cacti/scripts/
修改配置文件中的账号信息:
$ vim /var/www/cacti/cacti/scripts/ss_get_by_ssh.php
$ vim /var/www/cacti/cacti/scripts/ss_get_mysql_stats.php
在被监控机设置监控账号cactiuser(不设置密码,只允许公钥登陆),并且与被监控机之间采用ssh公钥通信
useradd cactiuser
mkdir /home/cactiuser/.ssh/
vim /home/cactiuser/.ssh/authorized_keys
chmod 700 /home/cactiuser/.ssh/
chown cactiuser.cactiuser /home/cactiuser/ -R
把监控机上生成公钥,然后把公钥拷到被监控机上
ssh-copy-id -i /home/cactiuser/.ssh/id_rsa.pub cactiuser@192.168.0.10 或者
scp /home/cactiuser/.ssh/id_rsa.pub cactiuser@192.168.0.16:/home/cactiuser/.ssh/authorized_keys
监控nginx配置
###add by lai monitor nginx status
location /server-status {
stub_status on;
allow 127.0.0.1;
# deny all;
}
监控mysql配置:
GRANT SUPER,PROCESS ON *.* TO 'cactiuser'@'192.168.0.200' IDENTIFIED BY "cactiuser_mysql";
flush privileges;
监控机设置Snmptrapd
[root@admin200 snmp]# more /etc/snmp/snmptrapd.conf
#traphandle default /etc/snmp/lognotify
# the generic traps
traphandle NET-SNMP-AGENT-MIB::nsNotifyShutdown /etc/snmp/lognotify snmpdShutdown
traphandle SNMPv2-MIB::coldStart /etc/snmp/lognotify coldStart
traphandle SNMPv2-MIB::warmStart /etc/snmp/lognotify warmStart
traphandle IF-MIB::linkDown /etc/snmp/lognotify linkDown
traphandle IF-MIB::linkUp /etc/snmp/lognotify linkUp
traphandle SNMPv2-MIB::authenticationFailure /home/nba/bin/traps authFailure
authCommunity log,execute,net WBY_com_cacti
[root@admin200 snmp]# more lognotify
#!/bin/sh
read host
read ip
vars=
while read oid val
do
if [ "$vars" = "" ]
then
vars="$oid = $val"
else
vars="$vars, $oid = $val"
fi
done
echo trap: $1 $host $ip $vars >checkfile
echo "trap: $1 $host $ip $vars" |mail -s zhaowei_snmptrap_alert:"$ip" 362560701@qq.com -- -f trap_admin200@sdwzy.com
#echo “mail content”|mail -s test 362560701@qq.com
被监控机设置trap,在snmpd.conf 的最后添加如下几行:
# vim /etc/snmp/snmpd.conf
###add by for sending v1 traps to the admin server
trapsink 192.168.0.200:162 WBY_com_cacti
/etc/init.d/snmpd restart
交换机设置trap
[H3C254_uplink]snmp-agent trap enable
[H3C254_uplink]snmp-agent target-host trap address udp-domain 192.168.0.200 udp-port 162 params securityname WBY_com_cacti v1
三、批量安装被监控端
虚机
for i in 90 91 92 95 96 97 98 100 101 102 103 106 107 108 110 111 112 113 118;do ssh 192.168.100.$i 'mkdir /root/.ssh';done
for i in 90 91 92 95 96 97 98 100 101 102 103 106 107 108 110 111 112 113 118;do scp /root/.ssh/id_rsa.pub 192.168.100.$i:/root/.ssh/authorized_keys;done
for i in 90 91 92 95 96 97 98 100 101 102 103 106 107 108 110 111 112 113 118;do ssh 192.168.100.$i 'yum -y install net-snmp';done
for i in 90 91 92 95 96 97 98 100 101 102 103 106 107 108 110 111 112 113 118;do scp /etc/snmp/snmpd.conf 192.168.100.$i:/etc/snmp/snmpd.conf;done
for i in 90 91 92 95 96 97 98 100 101 102 103 106 107 108 110 111 112 113 118;do ssh 192.168.100.$i 'echo "#########################重启snmpd 192.168.100."'$i' &&
/etc/init.d/snmpd restart';done
for i in 120 121 122 123 126 127 128 130 131 132 133 138 140 141 142 143 144 145 150 151 152 153 155;do ssh 192.168.100.$i 'mkdir /root/.ssh';done
for i in 120 121 122 123 126 127 128 130 131 132 133 138 140 141 142 143 144 145 150 151 152 153 155;do echo "########################scp 192.168.100."$i && scp
/root/.ssh/id_rsa.pub 192.168.100.$i:/root/.ssh/authorized_keys;done
for i in 120 121 122 123 126 127 128 130 131 132 133 138 140 141 142 143 144 145 150 151 152 153 155;do ssh 192.168.100.$i 'echo "########################install snmp
192.168.100."'$i' && yum -y install net-snmp';done
for i in 120 121 122 123 126 127 128 130 131 132 133 138 140 141 142 143 144 145 150 151 152 153 155;do scp /etc/snmp/snmpd.conf 192.168.100.
$i:/etc/snmp/snmpd.conf;done
for i in 120 121 122 123 126 127 128 130 131 132 133 138 140 141 142 143 144 145 150 151 152 153 155;do ssh 192.168.100.$i 'echo "#########################重启snmpd
192.168.100."'$i' && /etc/init.d/snmpd restart';done
实机
for i in 101 121 123 182 130 131 184 187 ;do ssh 192.168.1.$i 'mkdir /root/.ssh';done
for i in 101 121 123 182 130 131 184 187;do scp /root/.ssh/id_rsa.pub 192.168.1.$i:/root/.ssh/authorized_keys;done
for i in 101 121 123 182 130 131 184 187;do ssh 192.168.1.$i 'yum -y install net-snmp';done
for i in 101 121 123 182 130 131 184 187;do scp /etc/snmp/snmpd.conf 192.168.1.$i:/etc/snmp/snmpd.conf;done
for i in 101 121 123 182 130 131 184 187;do ssh 192.168.1.$i '/etc/init.d/snmpd restart';done
四、批量添加主机
进入/var/www/cacti/cli
列出可用主机模板的ID
# php -q cli/add_device.php --list-host-templates
创建主机
#php add_device.php --description=192.168.100.103_vm_dev100_C103 --ip=192.168.100.103 --template=3 --version=1 --community=mycommunity
批量增加主机脚本
# more auto_bat_add_hosts.sh
#!/bin/bash
php add_device.php --description=192.168.100.106_vm_dev100_C103 --ip=192.168.100.106 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.107_vm_dev100_C103 --ip=192.168.100.107 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.108_vm_dev100_C103 --ip=192.168.100.108 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.111_vm_dev100_C103 --ip=192.168.100.111 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.112_vm_dev100_C103 --ip=192.168.100.112 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.113_vm_dev100_C103 --ip=192.168.100.113 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.118_vm_dev100_C103 --ip=192.168.100.118 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.121_vm_dev100_C103 --ip=192.168.100.121 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.122_vm_dev100_C103 --ip=192.168.100.122 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.123_vm_dev100_C103 --ip=192.168.100.123 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.126_vm_dev100_C103 --ip=192.168.100.126 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.127_vm_dev100_C103 --ip=192.168.100.127 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.128_vm_dev100_C103 --ip=192.168.100.128 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.131_vm_dev100_C103 --ip=192.168.100.131 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.132_vm_dev100_C103 --ip=192.168.100.132 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.133_vm_dev100_C103 --ip=192.168.100.133 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.138_vm_dev100_C103 --ip=192.168.100.138 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.141_vm_dev100_C103 --ip=192.168.100.141 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.142_vm_dev100_C103 --ip=192.168.100.142 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.143_vm_dev100_C103 --ip=192.168.100.143 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.144_vm_dev100_C103 --ip=192.168.100.144 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.145_vm_dev100_C103 --ip=192.168.100.145 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.151_vm_dev100_C103 --ip=192.168.100.151 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.152_vm_dev100_C103 --ip=192.168.100.152 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.153_vm_dev100_C103 --ip=192.168.100.153 --template=3 --version=1 --community=mycommunity
php add_device.php --description=192.168.100.155_vm_dev100_C103 --ip=192.168.100.155 --template=3 --version=1 --community=mycommunity
五、批量增加监控图像
列出存在的主机
# php add_graphs.php --list-hosts
列出可用的图像模板
# php add_graphs.php --list-graph-templates
1、增加cpu图像
php add_graphs.php --host-id=16 --graph-type=cg --graph-template-id=4
2、增加Load Average图像
php add_graphs.php --host-id=16 --graph-type=cg --graph-template-id=11
3、增加Logged in Users图像
php add_graphs.php --host-id=16 --graph-type=cg --graph-template-id=28
4、增加memory图像
php add_graphs.php --host-id=16 --graph-type=cg --graph-template-id=13
5、增加Ping Latency图像
php add_graphs.php --host-id=16 --graph-type=cg --graph-template-id=7
6、增加Processes图像
php add_graphs.php --host-id=16 --graph-type=cg --graph-template-id=29
7、增加Traffic图像(最好手工增加)
php add_graphs.php --host-id=16 --graph-type=cg --graph-template-id=2
8、增加disk图像(最好手工增加)
php add_graphs.php --host-id=16 --graph-type=cg --graph-template-id=3
批量增加图像脚本
# more auto_bat_add_graph.sh
#!/bin/bash
loop=15
while [ "$loop" -le 41 ]
do
loop=`expr $loop + 1`
echo $loop
#log in user
php add_graphs.php --host-id=$loop --graph-type=cg --graph-template-id=28
#ping
php add_graphs.php --host-id=$loop --graph-type=cg --graph-template-id=7
#process
php add_graphs.php --host-id=$loop --graph-type=cg --graph-template-id=29
done
六、批量添加主机到树
查看存在的主机
# php add_tree.php --list-hosts
查看存在的树
# php add_tree.php --list-trees
脚本:
# more auto_bat_add_tree.sh
#!/bin/bash
loop=33
while [ "$loop" -le 41 ]
do
loop=`expr $loop + 1`
echo $loop
#add tree
php add_tree.php --type=node --node-type=host --tree-id=2 --host-id=$loop
done
七、参考
cacti插件安装方法:http://docs.cacti.net/plugins
批量创建host、图像、tree:
http://www.vim-fu.com/using-cacti-to-monitor-a-large-scale-infrastructure-in-amazons-ec2/
http://www.php-oa.com/2011/05/19/cacti-cli.html
http://hi.baidu.com/cavaran/blog/item/a903338a17546508c8fc7a16.html