近期在做一个Nagios,果然如其名--“难过死”,不过做出来之后,还是蛮爽的,然后感觉不尽兴,就写了两个脚本实现起功能。
系统环境:centos5.7 x86_64
硬件环境:XenServer
内存3G,一颗双核CPU
Nagios是一款开源的免费网络监视工具,能有效监控Windows、Linux和Unix的主机状态,交换机路由器等网络设置,打印机等。在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员,在状态恢复后发出正常的邮件或短信通知。
Nagios具有的功能:
首先要了解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 start
- cp /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
接下来是客户端脚本:
- #!/bin/bash
- #:Title:Nagios client install
- #:Synopsis:
- #:Data:2012-07-18 15:36:32
- #:Version:1.1
- #:Author:LuoWei <luoweiro@126.com>
- #:Options:
- #This is an install Nagios client's scripts
- Pack_path=`pwd`
- ip=192.168.158.219 此处是nagi
- yum install openssl-devel gcc -y
- useradd nagios -s /sbin/nologin
- cd $Pack_path
- tar xf nagios-plugins-1.4.15.tar.gz
- cd nagios-plugins-1.4.15
- ./configure && make && make install
- cd $Pack_path
- tar xf nrpe-2.12.tar.gz
- cd nrpe-2.12
- ./configure
- make && make install
- mkdir /usr/local/nagios/etc
- cp init-script /etc/init.d/nrpe
- chmod +x /etc/init.d/nrpe
- sed -i 's@\(allowed_hosts=127.0.0.1\)@\1,'$ip'@' /usr/local/nagios/etc/nrpe.cfg
- mkdir -pv /usr/local/nagios/bin
- cp src/nrpe /usr/local/nagios/bin/
- chkconfig nrpe on
- cp /usr/local/src/nrpe-2.12/src/check_nrpe /usr/local/nagios/libexec/
- chown -R nagios:nagios /usr/local/nagios
- #############monitor mysql
- mysql -e "grant all on *.* to nagios@'%' identified by 'nagios';"
- mysql -e "flush privileges;"
- echo "command[check_mysql_slave]=/usr/local/nrpe/libexec/check_mysql_slave" >> /usr/local/nagios/etc/nrpe.cfg
- /etc/init.d/nrpe restart
希望在各位部署Nagios的时候不会在难过死了,当然在批量添加和删除监控主机也可以写个脚本,本文就不多加介绍了。
提示:
1.可以把监控的服务器的ip地址,服务类型写在一个文件中批量添加。
2.可以定义一个方法,然后类似ipvsadm那样进行添加和删除,此处不做详细介绍,可以根据自己的配置文件进行修改。
当然大家可以把后面的完成,共同探讨一下!希望对你有所帮助!