服务器先安装好Apache。
从 http://www.nagios.org 下载以下软件:
- nagios-3.2.3.tar.gz
- nagios-plugins-1.4.15.tar.gz
- nrpe-2.12.tar.gz
安装Nagios Core
#切换到root用户 su -l #添加nagios用户 useradd -m nagios passwd nagios #创建nagcmd组,允许用户在web上执行命令. groupadd nagcmd usermod -a -G nagcmd nagios usermod -a -G nagcmd apache tar xzvf nagios-3.2.3.tar.gz cd nagios-3.2.3 ./configure --with-command-group=nagcmd make all make install make install-init make install-config make install-commandmode
configure完成后会显示如下提示
General Options: ------------------------- Nagios executable: nagios Nagios user/group: nagios,nagios Command user/group: nagios,nagcmd Embedded Perl: no Event Broker: yes Install ${prefix}: /usr/local/nagios Lock file: ${prefix}/var/nagios.lock Check result directory: ${prefix}/var/spool/checkresults Init directory: /etc/rc.d/init.d Apache conf.d directory: /etc/httpd/conf.d Mail program: /bin/mail Host OS: linux-gnu Web Interface Options: ------------------------ HTML URL: http://localhost/nagios/ CGI URL: http://localhost/nagios/cgi-bin/ Traceroute (used by WAP): /bin/traceroute
配置文件目录/usr/local/nagios/etc
修改/usr/local/nagios/etc/objects/contacts.cfg中的邮箱地址为 nagios@xxx.edu.cn。该邮箱设置了过滤规则,所有信件转发到 xxx@139.com
将/root/nagios-3.2.3/sample-config/httpd.conf中的片段拷贝到/usr/local/apache2/conf/httpd.conf中
添加nagiosadmin用户
/usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin安装Nagios Plugins
tar xzvf nagios-plugins-1.4.15.tar.gz cd nagios-plugins-1.4.15 ./configure --with-nagios-user=nagios --with-nagios-group=nagios make make install
命令安装在/usr/local/nagios/libexec/目录下
启动Nagios
chkconfig --add nagios #将nagios添加到服务中 chkconfig nagios on #开启该服务 chkconfig --list nagios #查看服务启动状态 #检查脚本正确性 /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg service nagios start
安装NRPE
先安装Nagios Plugins,如果是在被监控主机上安装,需要先添加nagios用户。
tar xzvf nrpe-2.12.tar.gz cd nrpe-2.12 ./configure make all make install-plugin
安装NRPE Daemon
NRPE Daemon的端口5666
先按照上一步安装NRPE
make install-daemon make install-daemon-config make install-xinetd
安装完毕后NRPE的配置文件在/usr/local/nagios/etc/nrpe.cfg
编辑/etc/xinetd.d/nrpe
only_from = 127.0.0.1 #这里只能加一个IP编辑/etc/services,在最后一行加上
nrpe 5666/tcp # NRPE重启xinetd服务
service xinetd restart执行下面命令,检查服务有无正常启动
netstat -at | grep nrpe #应该出现以下提示 tcp 0 0 *:nrpe *:* LISTEN测试NRPE Deamon服务
#测试本机 /usr/local/nagios/libexec/check_nrpe -H localhost #测试远程 /usr/local/nagios/libexec/check_nrpe -H 202.195.160.46 #正常会显示 NRPE v2.12在监控主机上编辑/usr/local/nagios/etc/objects/commands.cfg,加入下面的command。
define command{ command_name check_nrpe command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ }
重启监控主机的Nagios服务
service nagios restart测试接收邮件
首先需要将nagios监控主机的IP加入到邮件系统的信任主机列表中。否则必须使用外部邮件系统账号发送邮件。
新建/root/testmail文件
/bin/mail -v -s "test" nagios@xxx.edu.cn < /root/testmail -- -f nagios@xxx.edu.cn -F nagios /usr/bin/printf "%b" "test" | /bin/mail -v -s "test Alert" nagios@xxx.edu.cn -- -f nagios@xxx.edu.cn -F nagios
修改commands.cfg
notify-host-by-email 和 notify-service-by-email 末尾均加上以下语句。为了便于通过学校邮箱转139邮箱时不被139邮箱过滤。
-- -f nagios@xxx.edu.cn -F nagioscheck_http
./check_http -H www.xxx.edu.cn -w 5 -c 8 -u /index.php -s "www.xxx.edu.cn" define command{ command_name check_http command_line $USER1$/check_http -H $ARG1$ -w $ARG2$ -c $ARG3$ -u $ARG4$ -s $ARG5$ } define service{ use generic-service host_name server-www-8 service_description website-www-8 check_command check_http!www.xxx.edu.cn!5!8!/index.php!"www.xxx.edu.cn" }
check_dns
./check_http -H www.xxx.edu.cn -a site's ip -w 5 -c 8 define command { command_name check_dns command_line $USER1$/check_dns -s $HOSTADDRESS$ -H $ARG1$ -a $ARG2$ -w $ARG3$ -c $ARG4$ } define service{ use generic-service host_name server-dns-95 service_description network-dns-95 check_command check_dns!www.xxx.edu.cn!site's ip!5!8! }
check_squid
./check_squid.pl -u http://www.xxx.edu.cn -p proxy_server_ip -l 3128 -o username -m password -e 200 ./check_squid.pl -u http://www.xxx.edu.cn -p proxy_server_ip -l 3128 -o username -m password -e 200 ./check_squid.pl -u http://www.xxx.edu.cn -p proxy_server_ip -l 3128 -e 200 define command { command_name check_squid command_line $USER1$/check_squid.pl -u $ARG1$ -p $ARG2$ -l $ARG3$ -o $ARG4$ -m $ARG5$ -e $ARG6$ } define service{ use generic-service host_name server-proxy-68 service_description network-proxy-68 check_command check_squid!http://www.xxx.edu.cn!proxy_server_ip!3128!username!password!200! }
check_dhcp
/usr/local/nagios/libexec/check_dhcp -s dhcp_server_ip -i eth2 -u define command{ command_name check_dhcp command_line $USER1$/check_dhcp -s $ARG1$ -i eth2 -u } define service{ use generic-service host_name server-dhcp-13 service_description network-dhcp-13 check_command check_dhcp!dhcp_server_ip! }
DHCP服务器必须加上配置
host nagios { hardware ethernet 00:d0:b7:c2:76:fd; fixed-address nagios_server_ip; } subnet nagios_server_net netmask 255.255.255.0 { }
check_smtp
./check_smtp -H mailserverip -A LOGIN -U username -P password define command{ command_name check_smtp command_line $USER1$/check_smtp -H $HOSTADDRESS$ -A LOGIN -U $ARG1$ -P $ARG2$ } define service{ use generic-service host_name server-mail3-17 service_description ecampus-mail3-smtp-17 check_command check_smtp!username!password! }