近期在做一个Nagios,果然如其名--“难过死”,不过做出来之后,还是蛮爽的,然后感觉不尽兴,就写了两个脚本实现起功能。
系统环境:CentOS 5.7 x86_64
硬件环境:XenServer
内存3G,一颗双核CPU
Nagios是一款开源的免费网络监视工具,能有效监控Windows、Linux和Unix的主机状态,交换机路由器等网络设置,打印机等。在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员,在状态恢复后发出正常的邮件或短信通知。
Nagios具有的功能:
监控网络服务(SMTP、POP3、HTTP、NNTP、PING等);
监控主机资源(处理器负荷、磁盘利用率等);
简单地插件设计使得用户可以方便地扩展自己服务的检测方法;
并行服务检查机制;
具备定义网络分层结构的能力,用"parent"主机定义来表达网络主机间的关系,这种关系可被用来发现和明晰主机宕机或不可达状态;
当服务或主机问题产生与解决时将告警发送给联系人(通过EMail、短信、用户定义方式);
可以定义一些处理程序,使之能够在服务或者主机发生故障时起到预防作用;
自动的日志滚动功能;
可以支持并实现对主机的冗余监控;
可选的WEB界面用于查看当前的网络状态、通知和故障历史、日志文件等;
首先要了解nagios的工作原理,他是通过插件管理的,这些插件均可以自己编写:
接下来就是简单的脚本实现Nagios+pnp绘图过程,首先是服务端脚本:
#!/bin/bash #:Title:Nagios server install #:Synopsis: #:Data:2012-07-18 13:36:32 #:Version:1.1 #:Author:LuoWei <luoweiro@126.com> #:Options: #This is an install Nagios's script Nagios_user=nagios Nagios_password=baison Pack_path=`pwd` Mail=282127408@qq.com yum -y install httpd gettext mysql mysql-server mysql-devel php53 php53-mysql php-pear yum install perl perl-DBI perl-DBD-MySQL gcc glibc glibc-common gd gd-devel openssl-devel -y useradd $Nagios_user && echo $Nagios_password |passwd --stdin $Nagios_user usermod -s /sbin/nologin $Nagios_password groupadd nagcmd usermod -G nagcmd $Nagios_password usermod -G nagcmd apache #install nagios cd $Pack_path tar xf nagios-cn-3.2.3.tar.bz2 cd nagios-cn-3.2.3 ./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios --with-command-group=nagcmd make all make install make install-init make install-commandmode make install-config make install-webconf make install-commandmode #install nagios-plugins cd $Pack_path tar xf nagios-plugins-1.4.15.tar.gz cd nagios-plugins-1.4.15 sed -i 's/email.*/email '$Mail'/' /usr/local/nagios/etc/objects/contacts.cfg ./configure --with-nagios-user=nagios --with-nagios-group=nagios make && make install #install nagios-snmp-plugins #cd $Pack_path #tar xf nagios-snmp-plugins.1.1.1.tgz #cd nagios_plugins/ ln -sv /usr/local/nagios/bin/nagios /usr/local/bin/ sed -i "/^#ServerName/i\ServerName 127.0.0.1:80" /etc/httpd/conf/httpd.conf chkconfig --add nagios chkconfig nagios on chkconfig httpd on chkconfig mysqld on service nagios start service httpd start service mysqld startcp /usr/local/nagios/etc/nagios.cfg{,.bak} #nrpe install cd $Pack_path tar xf nrpe-2.12.tar.gz cd nrpe-2.12 ./configure make && make install chown -R nagios:nagios /usr/local/nagios #install pnp cd $Pack_path tar xf rrdtool-1.4.5.tar.gz cd rrdtool-1.4.5 yum install -y pango pango-devel freetype freetype-devel libpng libpng-devel gettext gettext-devel libjpeg libjpeg-devel gd gd-devel libxml2 libxml2-devel libiconv libiconv-devel qpixman qpixman-devel glib glib-devel cairo cairo-devel libart* php53-gd ./configure --prefix=/usr/local/rrdtool make && make install cd $Pack_path tar xf pnp4nagios-0.6.6.tar.gz cd pnp4nagios-0.6.6 ./configure --prefix=/usr/local/pnp4nagios --with-rrdtool=/usr/local/rrdtool/bin/rrdtool --with-nagios-user=nagios --with-nagios-group=nagcmd make all make install make install-webconf make install-config make install-init make fullinstall cd /usr/local/pnp4nagios/etc/ mv misccommands.cfg-sample misccommands.cfg mv nagios.cfg-sample nagios.cfg mv rra.cfg-sample rra.cfg cd pages/ mv web_traffic.cfg-sample web_traffic.cfg cd ../check_commands/ mv check_all_local_disks.cfg-sample check_all_local_disks.cfg mv check_nrpe.cfg-sample check_nrpe.cfg mv check_nwstat.cfg-sample check_nwstat.cfg cp /usr/local/nagios/etc/nagios.cfg{,.bak} sed -i 's@#host_perfdata_command=process-host-perfdata@host_perfdata_command=process-host-perfdata@' /usr/local/nagios/etc/nagios.cfg sed -i '/cfg_file=.*localhost.cfg/d' /usr/local/nagios/etc/nagios.cfg cp /usr/local/pnp4nagios/libexec/process_perfdata.pl /usr/local/nagios/libexec/ cp /usr/local/src/pnp4nagios-0.6.6/contrib/ssi/status-header.ssi /usr/local/nagios/share/ssi/ cp /usr/local/nagios/etc/objects/commands.cfg{,.bak} sed -i '227,247d' /usr/local/nagios/etc/objects/commands.cfg cat >>/usr/local/nagios/etc/objects/commands.cfg <<EOF define command { command_name process-host-perfdata command_line /usr/bin/perl /usr/local/pnp4nagios/libexec/process_perfdata.pl -d HOSTPERFDATA } define command { command_name process-service-perfdata command_line /usr/bin/perl /usr/local/pnp4nagios/libexec/process_perfdata.pl } EOF cp /usr/local/nagios/etc/objects/templates.cfg{,.bak} cat >>/usr/local/nagios/etc/objects/templates.cfg <<EOF define host { name host-pnp register 0 action_url /pnp4nagios/index.php?host=\$HOSTNAME\$ } define service { name srv-pnp register 0 action_url /pnp4nagios/index.php?host=\$HOSTNAME\$&srv=\$SERVICEDESC\$ } EOF
mv /usr/local/pnp4nagios/share/install.php /usr/local/pnp4nagios/share/install.php.bak sed -i 's/use_authentication=1/use_authentication=0/' /usr/local/nagios/etc/cgi.cfg mkdir -pv /usr/local/nagios/etc/{hosts,services} sed -i "/#cfg_dir.*servers/i\cfg_dir=/usr/local/nagios/etc/hosts" /usr/local/nagios/etc/nagios.cfg sed -i "/#cfg_dir.*servers/i\cfg_dir=/usr/local/nagios/etc/services" /usr/local/nagios/etc/nagios.cfg #config the commands.cfg to supports check_nrpe command cat >>/usr/local/nagios/etc/objects/commands.cfg <<EOF define command{ command_name check_nrpe command_line \$USER1\$/check_nrpe -H \$HOSTADDRESS\$ -c \$ARG1\$ } EOF
cat >/usr/local/nagios/etc/objects/contacts.cfg <<EOF define contact{ contact_name nagiosadmin use generic-contact alias Nagios Admin service_notification_period 24x7 host_notification_period 24x7 service_notification_options w,u,c,r host_notification_options d,u,r service_notification_commands notify-service-by-email,notify-service-by-sms host_notification_commands notify-host-by-email,notify-host-by-sms email $Mail pager 13524942642 } define contactgroup{ contactgroup_name admins alias Nagios Administrators members nagiosadmin } EOF #install fetion cd $Pack_path cd fetion/lib64/ chmod +x * cp *.so *.so.4 /usr/lib/ cp fetion /usr/local/bin/
cd $Pack_path tar xf check_mysql_health-2.1.tar.gz cd check_mysql_health-2.1 ./configure make make install yum install perl-DBD* -y cat >>/usr/local/nagios/etc/objects/commands.cfg <<EOF define command{ command_name check_mysql_health command_line \$USER1\$/check_mysql_health --hostname \$ARG1\$ --port \$ARG2\$ --username \$ARG3\$ --password \$ARG4\$ --mode \$ARG5\$ --warning \$ARG6\$ --critical \$ARG7\$ } EOF htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin