第一段文档简单写了如何搭建nagios/pnp4nagios/

useradd nagiosgroupadd nagcmdusermod -a -G nagcmd nagiossrc=/home/nagios/srcmkdir ${src}cd ${src}wget http://ar2.php.net/distributions/php-5.5.9.tar.gzwget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.5.1.tar.gzwget http://nagios-plugins.org/download/nagios-plugins-2.0.tar.gzwget http://mirror.bit.edu.cn/apache/httpd/httpd-2.4.7.tar.gzwget http://mirror.bit.edu.cn/apache//apr/apr-1.5.0.tar.gzwget http://mirror.bit.edu.cn/apache//apr/apr-util-1.5.3.tar.gzwget http://jaist.dl.sourceforge.net/project/mysql.mirror/MySQL%205.6.16/mysql-5.6.16.tar.gzwget http://mirrors.neusoft.edu.cn/epel/6Server/x86_64/epel-release-6-8.noarch.rpmwget http://oss.oetiker.ch/rrdtool/pub/rrdtool-1.4.8.tar.gzwget http://cznic.dl.sourceforge.net/project/pnp4nagios/PNP-0.6/pnp4nagios-0.6.21.tar.gzrpm -ivh epel-release-6-8.noarch.rpm#FOR RHEL5/CENTOS5x86_64#DOWNLOAD URL IS http://mirrors.neusoft.edu.cn/epel/5Server/x86_64/epel-release-5-4.noarch.rpmyum -y install wget mhash mcrypt libmcrypt-devel cmake cpan make autoconf automake curl curl-devel gcc gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel gd gd-devel kernel keyutils patch perl-core kernel-headers compat* mpfr cpp glibc glibc-common libgomp libstdc++-devel pplcloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel libselinux-develkrb5-devel libXpm* freetype freetype-devel libjpeg* libpng libpng-devel php-common php-gd ncurses* libtool* libxml2 libxml2-develpackagelist=`ls ${src}/*.tar.gz`for package in $packagelist;do tar xvf $package;donecd cd ${src}/apr-1.5.0./configuremake && make installcd ${src}cd cd ${src}/apr-util-1.5.3./configure --with-apr=/usr/local/aprmake && make installcd ${src}chown -R nagios. ${src}su - nagioscd ${src}/httpd-2.4.7./configure --prefix=/home/nagios/httpd --enable-ssl=shared --enable-module=most --enable-shared=most --enable-rewritemake && make installcd modules/generators/~/httpd/bin/apxs -i -a -c mod_cgi.c~/httpd/bin/apxs -i -a -c mod_cgid.ccd ${src}/mysql-5.6.16cmake -DCMAKE_INSTALL_PREFIX=/home/nagios/mysqlmake && make installcd /home/nagios/mysql/./scripts/mysql_install_db -basedir=/home/nagios/mysql -datadir=/home/nagios/mysql/data --user=nagios/home/nagios/mysql/bin/mysqld_safe &./bin/mysqladmin -u root password $passwordcat >>~/.bash_profile<<EOFexport PATH=$HOME/mysql/binEOFcd ${src}/php-5.5.9./configure --prefix=/home/nagios/php \--with-apxs2=/home/nagios/httpd/bin/apxs \--with-mysql=/home/nagios/mysql \--with-mysqli=/home/nagios/mysql/bin/mysql_config \--with-zlib-dir \--with-gd \--with-libxml-dir \--with-freetype-dir \--with-jpeg-dir \--with-png-dir \--with-mhash \--with-mcrypt \--with-iconv \--with-openssl \--with-gettext \--enable-bcmath \--enable-shmop \--enable-fpm \--enable-mbstring \--enable-gd-native-ttf \--enable-gd-jis-conv \--enable-static \--enable-zend-signals \--enable-inline-optimization \--enable-sockets \--enable-soap \--enable-ftp \--enable-zip \--enable-session \--with-curl \--disable-ipv6make && make installcp php.ini-production ~/php/etc/php.ini#vim ~/httpd/conf/httpd.conf#replace User/Group to nagios#ServerName注释127.0.0.1#修改默认80端口#AddType application/x-httpd-php .php#<IfModule dir_module># DirectoryIndex index.html index.php#</IfModule>rm -rf ~/httpd/htdocs/index.htmlcat >>~/httpd/htdocs/index.php<<EOF<?phpphpinfo();?>;EOFcd ${src}/nagios-4.0.2./configure --prefix=/home/nagios/nagios --enable-embedded-perl --with-command-group=nagcmdmake allmake install#must_be_rootmake install-initmake install-configmake install-commandmodecat sample-config/httpd.conf>>~/httpd/conf/httpd.conf~/httpd/bin/htpasswd -c /home/nagios/nagios/etc/htpasswd.users nagioscd ${src}/nagios-plugins-1.5 ./configure --prefix=/home/nagios/nagios/ --with-nagios-user=nagios --with-nagios-group=nagios --enable-libtap --enable-extra-opts --enable-perl-modulesmake && make installsed -i "s/nagiosadmin/nagiosadmin,nagios/g" ~/nagios/etc/cgi.cfg#启动nagios:~/nagios/bin/nagios -d ~/nagios/etc/nagios.cfgexitecho "#BACK TO ROOOT#"yum -y install cjkuni*yum -y install cairo-devel pango pango-devel net-snmp* php-snmp libart_lgpl-devel#install_rrdtool #must_be_rootcd ${src}/rrdtool-1.4.8./configure --prefix=/home/nagios/rrdtool --enable-perl-site-installmake && make installchown -R nagios. /home/nagios/rrdtool#install pnp4nagiossu - nagioscd ${src}/pnp4nagios./configure --prefix=/home/nagios/pnp --with-rrdtool=/home/nagios/rrdtool/bin/rrdtool --with-base-url=/pnp --with-httpd-conf=/home/nagios/httpd/confmake allmake installmake install-webconfmake install-configecho "include conf/pnp4nagios.conf">>~/httpd/conf/httpd.conf#vim ~/nagios/etc/objects/templates.cfgdefine host { name host-pnp action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_ register 0}define service { name srv-pnp action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$ register 0}#vim ~/nagios/etc/objects/commands.cfgdefine command{ command_name process-host-perfdata command_line /usr/bin/perl /home/nagios/pnp/libexec/process_perfdata.pl -d HOSTPERFDATA }define command{ command_name process-service-perfdata command_line /usr/bin/perl /home/nagios/pnp/libexec/process_perfdata.pl }mv /home/nagios/pnp/share/install.php /home/nagios/pnp/share/install.php.`date +%Y%m%d`process_performance_data=1host_perfdata_command=process-host-perfdataservice_perfdata_command=process-service-perfdata

第二部分关于nrpe的安装

#!/bin/bash#INSTALL NRPE DAEMON#src=/home/nagios/srcnagios_home=/home/nagios/nagiosyum install openssl openssl-develuseradd nagiosgroupadd nagcmdusermod -a -G nagcmd nagiosecho "5cr8xOW7DF"|passwd --stdin nagiossu - nagiosmkdir ${src}cd ${src}wget http://nagios-plugins.org/download/nagios-plugins-2.0.tar.gzwget http://jaist.dl.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.15/nrpe-2.15.tar.gzecho "##INSTALL NAGIOS-PLUGINS##"tar xvf nagios-plugins-2.0.tar.gzcd nagios-plugins-2.0./configure --prefix=${nagios_home} --enable-perl-modulesmake && make installcd ${src}echo "##INSTALL NRPE##"tar xvf nrpe-2.15.tar.gzcd nrpe-2.15#编译启用参数传递./configure --prefix=${nagios_home} --with-nrpe-user=nagios --with-nrpe-group=nagios --enable-command-argsmake allmake install-pluginmake install-daemonmake install-daemon-configexitcd ${src}/nrpe-2.15/make install-xinetdsed -i 's/127.0.0.1/192.168.28.241/g' /etc/xinetd.d/nrpeecho "nrpe 5666/tcp # NRPE" >>/etc/serviceschown nagios. /etc/xinetd.d/nrpeservice xinetd restartlsof -i:5666

nrpe.cfg配置文件简单如下

log_facility=daemonpid_file=/var/run/nrpe.pidserver_port=5666server_address=127.0.0.1nrpe_user=nagiosnrpe_group=nagiosallowed_hosts=127.0.0.1 dont_blame_nrpe=1allow_bash_command_substitution=0debug=0command_timeout=60connection_timeout=300command[check_users]=/home/nagios/nagios//libexec/check_users -w $ARG1$ -c $ARG2$command[check_load]=/home/nagios/nagios//libexec/check_load -w $ARG1$ -c $ARG2$command[check_disk]=/home/nagios/nagios//libexec/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$command[check_procs]=/home/nagios/nagios//libexec/check_procs -w $ARG1$ -c $ARG2$ -s $ARG3$

其中server_address/allow_address注意根据实际修改,dont_blame_nrpe=1(默认为0,1为开启参数传递)

nagios server端command.cfg需另外define一个check_remote_nrpe的command

define command{ command_name check_remote_nrpe command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ -a $ARG2$ $ARG3$ $ARG4$ }

这样监控服务,如客户端环境改变需调整参数的话只需要在nagios服务端修改参数即可,例如

define service{ use linux-service hostgroup_name 97f5eafb7a service_description check partition / check_command check_remote_nrpe!check_disk!20%!10%!/ }

第三部分,如果以后添加新监控点,则需去修改每台客户机的nrpe.cfg文件去define command,这样太麻烦了。所以再添加这样一个脚本来传递整个命令,客户端如需添加新监控点只需上传监控脚本,nagios服务端define service即可

#!/bin/shwhile getopts "a:c:" argdo case $arg in a) arg_a=$OPTARG; ;; c) arg_c=$OPTARG; ;; ?) echo "unkonw argument" echo "only -c -a argument" exit 1 ;; esacdoneif [[ "$arg_c" =~ "$HOME/" ]];then if [ ! -x $arg_c ];then echo "[$arg_c] not execute"; exit 3; fi $arg_c $arg_a; exit $?;else echo "cannt execute $arg_c,not allow execute [$HOME] outside scripts"; exit 3;fi

nagios的command文件需添加如下

define command{ command_name check_nrpe_exec command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ -a $ARG2$ }

客户端nrpe.cfg配置文件command部分只需要有

command[exec_command]=/home/nagios/nagios//libexec/exec_command -c exec_command -a "$ARG1$"

添加监控点例子,只需修改exec_command后的监控脚本命令

define service{ use linux-service host_name 887a34d257 service_description check_swap check_command check_nrpe_exec!"/home/nagios/nagios/libexec/check_swap -w 50% -c 30%" }