注:本文有感谢程建提供
一、需求
nagios 更新速度是很快的,本公司采用最新的稳定版本nagios-4.0.8,插件采用是的nagios-plugins-2.0.3,新版本经过使用和测试,发现速度和报警的速度比以前快了许多,
并且占用操作系统资源不多问题及时,界面干净,如下图:

由于网上下载包不稳定,所以我整理后放到网盘中,所有的下载包全在360网盘地址中如下:
http://yunpan.cn/cHjtN2f9Kz3FM 访问密码 f9af
二、PHP环境的搭建
本公司采用lamp的环境来做为nagios的基础环境,当然mysql是可以不用装的只要PHP环境就行了。
1、apr的安装
APR(Apache portable Run-time libraries,Apache可移植运行库)的目的如其名称一样,主要为上层的应用程序提供一个可以跨越多操作系统平台使用的底层支持接口库。
在早期的Apache版本中,应用程序本身必须能够处理各种具体操作系统平台的细节,并针对不同的平台调用不同的处理函数。随着Apache的进一步开发,Apache组织决定将这些通用的函数独立出来并发展成为一个新的项目。这样,APR的开发就从Apache中独立出来,Apache仅仅是使用APR而已。
一般情况下,APR开发包很容易理解为仅仅是一个开发包,不过事实上并不是。目前,完整的APR实际上包含了三个开发包:apr、apr-util以及apr-iconv,每一个开发包分别独立开发,并拥有自己的版本。
现在新版本的http中apr集成到了一个httpd-2.4.9-deps.tar 类似于这样的包中,解压后的结果如下:

集成后安装和管理方便多了,不用再担心apache版本和apr的对应该关系了。
安装如下:
1
2
3
|
./configure--prefix=/mnt/cellar/httpd/apr
make
make install
|
2、.apr-utils安装
1
2
3
4
|
./configure --prefix=/mnt/cellar/httpd/apr-util -- with -apr=/mnt/cellar/httpd/apr
make
makeinstall
|
apr和api-utils 安装好后,目录如下:

3、PCRE的安装
1
2
3
|
./configure --prefix=/mnt/cellar/httpd/pcre
make
make install
|
如果报以下错误:
configure: error: You need a C++ compiler for C++ support
则需要安装yum install -y gcc gcc-c++
4、apache的安装
apache的版本是2.4.9
1
2
3
4
5
|
./configure--prefix=/mnt/cellar/httpd/apache -- with -apr=/mnt/cellar/httpd/apr-- with -apr-util=/mnt/cellar/httpd/apr-util/bin/apu- 1 -config-- with -pcre=/mnt/cellar/httpd/pcre/ --enable-so --enable-rewrite
make
make install
|
apache 的启动如下
1
|
[root@kaifabin]# ./apachectl start
|

说明apache 运行正常
由于是源码包安装apache 所以启动和关闭apache比较麻烦,所以写了一个apache的启动脚本如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#!/bin/bash
#Startup script for the Apache2. 0 .X Web Server
# chkconfig:- 85 15
# Sourcefunction library.
./etc/rc.d/init.d/functions
if [ -f/etc/sysconfig/httpd ]; then
./etc/sysconfig/httpd
fi
INITLOG_ARGS= ""
apachectl=/mnt/cellar/httpd/apache/bin/apachectl
httpd=${HTTPD-/mnt/cellar/httpd/apache/bin/httpd}
prog=httpd
RETVAL= 0
start(){
echo -n$ "Starting $prog: "
daemon$httpd $OPTIONS
RETVAL=$?
echo
[$RETVAL = 0 ] && touch / var /lock/subsys/httpd
return $RETVAL
}
stop() {
echo -n$ "Stopping $prog: "
killproc$httpd
RETVAL=$?
echo
[$RETVAL = 0 ] && rm -f / var /lock/subsys/httpd / var /run/httpd.pid
}
reload(){
echo -n$ "Reloading $prog: "
killproc$httpd -HUP
RETVAL=$?
echo
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status$httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart)
if [ -f/ var /run/httpd.pid ] ; then
stop
start
fi
;;
reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl$@
RETVAL=$?
;;
*)
echo$"Usage: $prog
{start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
exit 1
esac
exit$RETVAL
|
以上脚本如果需要,只需要修改红色的部份即可(apache的安装目录)
5、PHP安装
5.1 PHP版本是php-5.6.4
1
2
3
|
./configure --prefix=/mnt/cellar/httpd/php-- with -libdir=/usr/lib64 -- with -config-file-path=/mnt/cellar/httpd/php/etc -- with -config-file-scan-dir=/mnt/cellar/httpd/php/etc/php.d -- with -apxs2=/mnt/cellar/httpd/apache/bin/apxs
make
make install
|
PHP安装的时候make的时候会报错 undefinedreference to `libiconv'
解决方法如下:
Makefile 大概 77 行左右的地方:
EXTRA_LIBS = ….. -lcrypt
在最後加上 -liconv,例如:
EXTRA_LIBS = ….. -lcrypt -liconv
验证有效!
5.2 PHP 环境配置如下:
把php.ini 拷贝一份到下面的目录

然后在apache的httpd.conf中添加
AddType application/x-httpd-php .php
然后重启apache 再访问如下:

至此基础环境配置完成
如果不知道configure时候的参数可以用以下命令来查看:
查看nginx编译参数:/usr/local/nginx/sbin/nginx-V
查看apache编译参数:cat/usr/local/apache2/build/config.nice
查看mysql编译参数:cat /usr/local/mysql/bin/mysqlbug |grep CONFIGURE_LINE
查看php编译参数:/usr/local/php/bin/php -i | grepconfigure
三、nagios 的安装
1、创建用户
1
|
useradd -M -s/sbin/nologin nagios
|
2、nagios 主程序的安装
cd nagios-4.0.8
1
2
3
4
5
6
7
|
./configure --prefix=/mnt/cellar/nagios -- with -command-group=nagios -- with -nagios-group=nagios
makeall
makeinstall
makeinstall-init
makeinstall-config
makeinstall-commandmode
makeinstall-webconf
|
安装完成
1
|
cp -R contrib/eventhandlers/ /mnt/cellar/nagios/libexec/
|
1
|
chown-R nagios.nagios eventhandlers/
|
3、启动nagios
./nagios -v/mnt/cellar/nagios/etc/nagios.cfg 如下图,先验证配置文件的正确性

下面启动,如下图,启动正确

4、apache相关的配置
1
|
./htpasswd -c /mnt/cellar/nagios/etc/htpasswd letang
|
在apache的httpd.conf的最后面添加如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#setting for nagios
ScriptAlias /nagios/cgi-bin "/mnt/cellar/nagios/sbin"
<Directory "/mnt/cellar/nagios/sbin" >
AuthType Basic
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthUserFile /mnt/cellar/nagios/etc/htpasswd
Require valid-user
</Directory>
Alias /nagios "/mnt/cellar/nagios/share"
<Directory "/mnt/cellar/nagios/share" >
AuthType Basic
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "nagios Access"
AuthUserFile /mnt/cellar/nagios/etc/htpasswd
Require valid-user
</Directory>
|
然后找到
user apache
group apache
修改为
user nagios
group nagios
最后重启apache 登陆验证如下:

5、错误排除
输入用户名和密码之后点击services出现下面的乱码情况如下:

出现上面的原因是apache没有开启cgi脚本的缘故
进入apache的主配置文件httpd.conf
1
2
|
#LoadModule cgid_module modules/mod_cgid.so
#LoadModule actions_module modules/mod_actions.so
|
将上面2行的#去掉,重启apache就OK了,然后关掉浏览器重新登陆如下:

好像还是什么也没有?并且有错误提示
这是认证的问题,修改如下
use_authentication=1改为 use_authentication=0 然后重启nagios即可

然后看到如下说明有监控项目了但有红色提示:


再看上面的错,很明显,提示找到命令,这是因为没有安装插件的原因。
6、nagios 插件的安装
nagios 之所以强大,就是因为功能强大的插件而已
1
2
3
4
5
6
7
8
9
|
tar
-zxvf nagios-plugins- 2.0 . 3 .tar.gz
cd
nagios-plugins- 2 .
cd
nagios-plugins- 2.0 . 3
./configure --prefix=/mnt/cellar/nagios/ -- with -nagios-user=nagios
-- with -nagios-group=nagios -- with -command-user=nagios -- with -command-group=nagios
make
|
安装完成后这个/mnt/cellar/nagios/libexec 下会有许多如下的命令

然后重启一下nagios 然再观察如下:

当然这是本机localhost 的监控
7. windows 机器的监控
windows 机器的监控依靠的是

如果是通过snmp来监控的则不需要
本公司采用是的第一种通过安装插件来监控
安装如下:





这里allowed host 是客户端的IP是服务器nagios的IP 密码要为空,不然很麻烦,椭圆里面就这样选择就行了


已经启动起来了。
然后在nagios端定义要监控的项目
localhost.cfg是监控nagios本身的。为了方便管理,我们建了几个目录
database,fdfs,linux,widows 之类的目录

如下图所示是windows 目录下的监控项目
起名规则如下:用途+ip地址

下面以一台windows 机器app_server_10.129.102.53.cfg为类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
#define hostgroup{
#
hostgroup_name
windows-servers
#
alias Windows
Servers
#
}
define host{
use windows-server
host_name app-server
alias
testapp-server
address 10.129 . 102.53
}
########## nsclient++ version
define service{
use
generic-service
host_name app-server
service_description NSClient++
version
check_command
check_nt!CLIENTVERSION
}
######### uptime
define service{
use
generic-service
host_name app-server
service_description uptime
check_command
check_nt!UPTIME
}
######### ping
define service{
use
generic-service
host_name app-server
service_description ping
check_command
check_ping! 100.0 , 20 %! 500.0 , 60 %
}
#########IIS
define service{
use generic-service
host_name app-server
service_description IIS
check_command
check_nt!SERVICESTATE!-d SHOWALL -l W3SVC
}
########## cpu load
define service{
use generic-service
host_name app-server
service_description cpu_load
check_command
check_nt!CPULOAD!-l 5 , 80 , 90
}
########## memory
define service{
use
generic-service
host_name app-server
service_description memory
check_command
check_nt!MEMUSE!-w 80 -c 90
}
########## C:\ space
define service{
use
generic-service
host_name
app-server
service_description
C:\space
check_command
check_nt!USEDDISKSPACE!-l c -w 80 -c 90
}
########## D:\ space
define service{
use generic-service
host_name app-server
service_description D:\space
check_command
check_nt!USEDDISKSPACE!-l d -w 80 -c 90
}
##########
define service{
use
generic-service
host_name app-server
service_description
Explorer.exe
check_command
check_nt!PROCSTATE!-d SHOWALL -l explorer.exe
}
|
定义好之后,在nagios.cfg中定义要监控主机的文件位置如下第二张图所示。


然后重启nagios,再观察如下:
windows 机器已经正常。

8、监控linux 机器
在客户机上操作
同样添加用户如下:
1
|
useradd -M -s /sbin/nologin nagios
|
安装nagios-plugins 插件
cd nagios-plugins-2.0.3
1
2
3
|
/configure --prefix=/mnt/cellar/nagios-plus -- with -nagios-user=nagios-- with -nagios-group=nagios
make all
make install
|
安装nrpe 服务
cd nrpe-2.15
1
2
3
4
5
|
./configure--prefix=/mnt/cellar/nrpe -- with -nrpe-user=nagios -- with -nrpe-group=nagios -- with -nagios-user=nagios -- with -nagios-group=nagios
# make all
# make install-plugin
# make install-daemon
# make install-daemon-config
|
安装成功后,目录如下:

然后进入到etc下面修改nrpe.cfg配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
|
log_facility=daemon
pid_file=/ var /run/nrpe.pid
server_port= 5666
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts= 127.0 . 0.1 , 192.168 . 1.127
dont_blame_nrpe= 0
allow_bash_command_substitution= 0
debug= 0
command_timeout= 60
connection_timeout= 300
command[check_users]=/mnt/cellar/nagios-plus/libexec/check_users -w 5 -c 10
|
// 这里要注意路径,这个路径是nagios-plus安装后生成的libexec下面的文件,当然也可以copy到你想要的目录下面,-w和-c表示WARNING, CRITICAL,即报警状态,具体的理论可以到官网上面查看
ommand[check_load]=/mnt/cellar/nagios-plus/libexec/check_load -w15,10,5 -c 30,25,20
command[check_hda1]=/mnt/cellar/nrpe/libexec/check_disk -w 20% -c 10%-p /dev/hda1
command[check_zombie_procs]=/mnt/cellar/nrpe/libexec/check_procs -w 5-c 10 -s Z
command[check_total_procs]=/mnt/cellar/nrpe/libexec/check_procs -w 150-c 200
客户端当然也要开启动nrpe服务器才行,当然为了方便,这边还是写了一个启动脚本,放到/etc/init.d下即可,别忘记给执行权限就行(chmod+x nrpe), 也可以通过xinit 来启动nrpe 当然我认为独立启动更方便管理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
#!/bin/bash
# chkconfig: 2345 88 12
# description: NRPE DAEMON
NRPE=/mnt/cellar/nrpe/bin/nrpe
NRPECONF=/mnt/cellar/nrpe/etc/nrpe.cfg
case "$1" in
start)
echo -n "Staring NRPE daemon...."
$NRPE -c$NRPECONF -d
echo "done.."
;;
stop)
echo -n "Stopping NRPE daemon...."
pkill -u nagiosnrpe
echo "done.."
;;
restart)
$ 0 stop
sleep 1
$ 0 start
;;
*)
echo "Usage:$0 start|stop|restart"
esac
exit 0
|
最后就是如果防火墙开了的别忘记放行5666端口了。
客户端配置好后,还要在服务端配置相关的文件才行
下面是在nagios的配置
由于是通过nrpe来监控linux客户端的,所以在nagios上原则上也是要安装nrpe的,但经验证,不用安装,只要从客户机上面的

如上图,把check_nrpe 拷贝到nagios的下面路径即可。

nagios的主要的配置文件全在下面的目录下:

由于nrpe只是一个插件,nagios识别不了这个命令,所以要在commands.cfg中定义一下这个命令,编辑command.cfg 在最下面添加如下,这也就是为何要拷贝check_nrpe命令到nagios端的原因
1
2
3
4
5
6
|
############
# 'check_nrpe ' command definition
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
|
然后在下面路径定义要监控的项目,linux这个目录是要自己创建的

具体的配置如下:更具体可以的看网盘中的配置文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
##########define hostname and ipaddress
define host{
use linux-server
host_name linux1-server
alias linux1-server
address 192.168 . 1.109
}
########## ping
define service{
use generic-service
host_name linux1-server
service_description ping
check_command check_ping! 100.0 , 20 %! 500.0 , 60 %
}
########## check_users
define service{
use generic-service
host_name linux1-server
service_description login_user
check_command check_nrpe!check_users
}
########## check_cpu_load
define service{
use generic-service
host_name linux1-server
service_description CPU_load
check_command check_nrpe!check_load
}
|
最后当然还是要配置nagios.cfg文件,要让nagios知道到哪个路径下去找要监控的项目

最后重启一下nagios服务即可,如果配置有错,是不能启动成功的
当然可以在nagios上先验证一下如下:

如果能返回结果,说明配置成功
过几分钟可以看到结果:

至此windows和linux的监控基本的就这样了,复杂的一些的,要通过脚本来监控了,具体的可以看网盘
9. 短信报警和邮件报警
这个功能最nagios最强大的功能,报警功能速度之快
当然为了系统安全,建议安装sendmail服务
yum -yinstall sendmail

然后编辑/etc/mail.rc在最后面加入
1
2
3
4
5
|
set from=cheng7223@ 163 .comsmtp=smtp. 163 .com
setsmtp-auth-user=cheng7223@ 163 .com smtp-auth-password=*******
set smtp-auth=login
|
这里是定义用哪个邮箱来发送报警邮件
最后定义收件人,也就接收人
编辑

1
2
3
4
5
6
7
|
definecontact{
contact_name nagiosadmin ; Short name of user
use generic-contact ; Inheritdefault values from generic-contact template (defined above)
alias Nagios Admin ; Full name of user
email chengjian@ 114995 .com ; <<***** CHANGE THIS TO YOUR EMAILADDRESS ******
}
|
如上红色的就是接收人的邮箱,如果是多个人,要用逗号隔开才行
配置完以后,一定要重启sendmail和nagios服务器
下面把客户机的nrpe服务给停掉来验证邮件的功能

已经显示红色,说明有问题了,再查看邮箱,速度挺快的

这是有问题的报警。再验证一下,恢复后的报警

至此已经OK。至于通过图像显示的功能,这里不做介绍,因为zabbix 在图像显示这一块已经足够。
本文转自 zouqingyun 51CTO博客,原文链接:http://blog.51cto.com/zouqingyun/1712100,如需转载请自行联系原作者