#!/bin/bash
#LANG="en_US.UTF-8"
#CentOS release 6.4 (Final)
#安装EPEL源
rpm -ivh
#安装相关软件包
yum -y install ntpdate net-snmp net-snmp-utils mrtg httpd
#同步时间
ntpdate 218.75.4.130
#SNMP设置
sed -i 's/systemview none/all none/' /etc/snmp/snmpd.conf
sed -i 's/#view all/view all/' /etc/snmp/snmpd.conf
sed -i 's/#view mib2/view mib2/' /etc/snmp/snmpd.conf
#Apache设置
sed -i '9 i\AddDefaultCharset GB2312' /etc/httpd/conf.d/mrtg.conf
sed -i 's/Deny from all/Deny from none/' /etc/httpd/conf.d/mrtg.conf
sed -i 's/127.0.0.1/all/' /etc/httpd/conf.d/mrtg.conf
#启动snmp、httpd
service snmpd restart
chkconfig snmpd on
service httpd restart
chkconfig httpd on
#添加监控主机
cfgmaker --no-down --global "language: chinese" --global "WorkDir: /var/www/mrtg" --global "Options[_]: growright, bits" --output /etc/mrtg/mrtg.cfg public@127.0.0.1
env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg
env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg
env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg
indexmaker /etc/mrtg/mrtg.cfg > /var/www/mrtg/index.html
#计划任务
sed -i 's/#//' /etc/cron.d/mrtg
#批量添加交换机
#所有交换机IP
cat << EOF > 00.txt
127.0.0.1
EOF
for IP in `cat 00.txt`;
do
#创建目录
mkdir /var/www/mrtg/$IP
#创建配置文件
cfgmaker --no-down --global "language: chinese" --global "WorkDir: /var/www/mrtg/$IP" --global "Options[_]: growright, bits" --output /etc/mrtg/$IP.cfg public@$IP
#刷新时间
sed -i '2 i\RunAsDaemon: Yes' /etc/mrtg/$IP.cfg
sed -i '2 i\Interval: 5' /etc/mrtg/$IP.cfg
sed -i '2 i\Refresh:300' /etc/mrtg/$IP.cfg
#启动MRTG
env LANG=C /usr/bin/mrtg /etc/mrtg/$IP.cfg
env LANG=C /usr/bin/mrtg /etc/mrtg/$IP.cfg
env LANG=C /usr/bin/mrtg /etc/mrtg/$IP.cfg
#创建首页
indexmaker /etc/mrtg/$IP.cfg > /var/www/mrtg/$IP/index.html
#创建计划任务
echo "LANG=C LC_ALL=C /usr/bin/mrtg /etc/mrtg/$IP.cfg --logging /var/log/mrtg.log" >> /etc/mrtg/mrtg-cron.sh
done
#允许列出/varwww/mrtg目录内容
sed -i '9 i\Options Indexes' /etc/httpd/conf.d/mrtg.conf
/etc/init.d/httpd restart
#添加执行权限
chmod +x /etc/mrtg/mrtg-cron.sh
#添加到计划任务
(crontab -l;echo '*/5 * * * * /bin/bash /etc/mrtg/mrtg-cron.sh') | crontab
#防火墙开启80
iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
/etc/init.d/iptables save
#测试效果
#wget
#查看
#官方提供的安装文档:
阅读(1555) | 评论(0) | 转发(0) |