2. 安装完成后修改配置文件NSC.ini把需要的库都打开,
找到配置文件的方法:
这个程序的配置文件默认为C:\Program Files\NSClient++下的NSC.ini文件
打开这个文件进行如下修改:
(1)、打开需要的库文件,将行的最前端带有“;”的,将这个符号删除即为打开。
具体修改结果如下:
[modules]
NRPEListener.dll
NSClientListener.dll
NSCAAgent.dll
CheckWMI.dll
FileLogger.dll
Checksystem.dll
CheckEventLog.dll
CheckHelpers.dll
;# NSCLIENT++ MODULES
;# A list with DLLs to load at startup.
; You will need to enable some of these for NSClient++ to work.
; ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
; * *
; * N O T I C E ! ! ! - Y O U H A V E T O E D I T T H I S *
; * *
; ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
FileLogger.dll
CheckSystem.dll
CheckDisk.dll
NSClientListener.dll
NRPEListener.dll
SysTray.dll
CheckEventLog.dll
CheckHelpers.dll
CheckWMI.dll
(2)、配置nagios服务器的IP地址:
修改结果如下:
;# ALLOWED HOST ADDRESSES
allowed_hosts=192.168.0.8 保存配置文件即可
3. 在监控服务器上修改nagios配置文件nagios.cfg
为了配置的方便进行我们对nagios的主配置文件做一个连接
ln -vs /var/www/nagios/etc/nagios.cfg /etc/nagios.cfg
对配置文件/etc/nagios.cfg进行如下修改
去掉cfg_file=/var/www/nagios/etc/objects/windows.cfg 的注释
定义windows.cfg
修改/var/www/nagios/etc/objects/windows.cfg文件,修改结果如下
define host{
use windows-server ; Inherit default values from a template
host_name winserver ; The name we're giving to this host
alias My Windows Server ; A longer name associated with the host
address 192.168.0.254 ; windows主机的IP,我的为192.168.0.254
}
保存退出即可:
重新启动nagios服务
service nagios restart
查看我们的nagios是否监控到了windows主机:
如图显示则表示监控成功:
一、下面就进入对其它linux主机实现nagios监控的配置
三、在被监控端的linux主机上:
1、先添加nagios用户来执行此服务
useradd nagios
2、为了安装nrpe,先安装nagios-plugins-1.4.15.tar.gz 插件
tar zxf 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
3、编译安装nrpe-2.12.tar.gz
tar -zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
(注:在执行下步之前必须保证这个主机上已经安装了openssl、openssl-devel 否则在编译的时候将提示有不能找到ssl头文件的错误,导致编译无法进行)
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
4、对nrpe的配置文件/usr/local/nagios/etc/nrpe.cfg进行配置
vim /usr/local/nagios/etc/nrpe.cfg
server_address=127.0.0.1 允许监控的IP
5、检测配置文件的语法并启动服务
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
如果没有任何提示则表示语法正确并已经启动了该服务
到此我们在被监控的linux主机上的配置已经完成
四、Nagios服务端配置:
1、安装NRPE
tar xvf nrpe-2.12.tar.gz
cd nrpe-2.12
(注:同样,在执行下步之前必须保证这个主机上已经安装了openssl-devel、openssl,否则编译将出错)
./configure
make all
make install-plugin
2、配置nagios的服务命令文件/var/www/nagios/etc/objects/commands.cfg定义外部构件nrpe
vim /var/www/nagios/etc/objects/commands.cfg
添加如下内容:
#check nrpe
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
3、由于此服务的配置文件中被没有关于监控linux主机的配置文件,所以我们要自己定义并创建
在/etc/nagios.cfg中定义被监控的linux主机的配置文件的名字及位置
vim /etc/nagios.cfg
添加如下内容
cfg_file=/var/www/nagios/etc/objects/mylinux.cfg 指定我们定义的被监控linux主机的配置文件名字和位置
然后我们就要到定义的位置创建这个文件了
vim /var/www/nagios/etc/objects/mylinux.cfg
添加如下内容
define host{
use linux-server
host_name mylinux
alias mylinux
address 192.168.0.200(被监控的linux主机的IP,这个是我的)
}
define service{
use generic-service
host_name mylinux
service_description check-swap
check_command check_nrpe!check_swap
}
define service{
use generic-service
host_name mylinux
service_description check-load
check_command check_nrpe!check_load
}
define service{
use generic-service
host_name mylinux
service_description check-disk
check_command check_nrpe!check_had1
}
define service{
use generic-service
host_name mylinux
service_description check-users
check_command check_nrpe!check_users
}
define service{
use generic-service
host_name mylinux
service_description otal_procs
check_command check_nrpe!check_total_procs
}
保存退出
修改这个文件的属主、属组
chown nagios:nagios mylinux.cfg
检查我们创建的文件是否有语法错误:
nagios -v /var/www/nagios/etc/objects/mylinux.cfg
4、重启nagios服
service ngios restart
好了,关于监控linux主机的配置到此为止
5、我们查看nagios是否监控到了mylinux,显示如图:
好了,我们监控其他linux主机已经实现了,关于nagios对其它linux主机的监控配置到此为止
五、Nagios监控MySQL服务及飞信报警
上面我们进行了相关主机监控的配置,但是我们只能通过查看nagios才能知道我们所监控主机的服务是否在线可用,这和我们在实际应用中的作用还有一定的差距,下面我们就来实现nagios的报警机制,即使我们不查看nagios,只要我们所监控的服务掉线了,nagios就给我们发飞信开提示我们。
我们通过监控本机的mysql服务的在线情况来实现这个实验:
nagios监控mysql服务器:
1、 建立专用数据库:
我们通过让nagios监控mysql服务中的一个特定的数据库的在线状态来实现对mysql服务在线状态的监控,
创建这个数据库,并对其进行相应的配置
mysql
mysql> create database nagios;
mysql> grant select on nagios.* to nagios@'%' identified by 'nagios';
mysql> flush privileges;
2、查看/var/www/nagios/libexec/check_mysql 这个二进制文件是否存在,如果存在就
继续下面的操作,如果不存在就重新编译安装nagios-plugins,具体方法在上一篇有关nagios的文章中,编译时一定要指明--with-mysql.
对check-mysql这个二进制文件进行测试,看是否能够读取数据库状态信息
/var/www/nagios/libexec/check_mysql -H 10.1.1.30 -u nagios -d nagios -p 123qweasd
Uptime: 970348 Threads: 1 Questions: 684584 Slow queries: 3 Opens: 672 Flush tables: 1 Open tables: 369 Queries per second avg: 0.705
如果输出上面的信息,则表示能够读取nagios这个数据库的状态
3、在定义监控命令的文件/var/www/nagios/etc/objects/command.cfg里增加插件命令:
vim /var/www/nagios/etc/objects/command.cfg
添加如下内容:
#check mysql
define command{
command_name check_mysql
command_line $USER1$/check_mysql -H $HOSTADDRESS$ -u nagios -d nagios -p nagios
}
4、定义服务:
在/var/www/nagios/etc/objects/localhost.cfg 中添加如下内容
define service {
use local-service
host_name localhost
service_description msyql
check_command check_mysql 监控这个服务的命令
notifications_enabled 1 对这个服务开启监控
}
OK,我们可以到nagios的“servers”中查看是否监控到了这个服务,显示如下:
既然当服务掉线的时候要使用飞信报警,接下来就对飞信进行配置,来实现飞信报警机制
准备飞信的客户端和库文件:
fetion libACE-5.7.2.so libACE_SSL-5.7.2.so libcrypto.so.4 libssl.so.4
1、将飞信的客户端加入到/usr/bin/中,并添加执行权限
mv fetion /usr/bin
chmod +x /usr/bin/fetion
2、将飞信的库文件加入到系统的库文件中
mv lib* /usr/lib/
3、测试飞信能否成功发送信息:
fetion --mobile 15003880800 --pwd 123qweQWE(飞信密码) --to 15003880800(电话号码) --msg-tpye=0 --msg-utf8 $HOSTNAME
如果出现一大片的乱码信息,表示飞信连接成功,这一大片的乱码是飞信的验证码图片,这个验证码图片在图形界面下才能显示出来,只有第一次使用的时候才会用到验证码,这个图片的位置在/usr/bin下,我们输入相应的验证码即可。
测试成功之后,就继续进行
4、修改nagios的报警配置文件/var/www/nagios/etc/objects/ templates.cfg,修改报警机制,将email改成fetion
vim /var/www/nagios/etc/objects/templates.cfg 修改结果如下
define contact{
name generic-contact ; The name of thiscontact template
service_notification_period 24x7 ; service notifications can be sent anytime
host_notification_period 24x7 ; host notifications can be sent anytime
service_notification_options w,u,c,r,f,s ; send notifications for all service states, flapping events, and scheduled downtime events
host_notification_options d,u,r,f,s ; send notifications for all host states, flapping events, and scheduled downtime events
service_notification_commands notify-service-by-fetion; send service notifications via email
host_notification_commands notify-service-by-fetion; send host notifications via email
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLA
}
5、修改命令文件/var/www/nagios/etc/objects/commands.cfg
vim /var/www/nagios/etc/objects/commands.cfg 添加如下内容
define command{
command_name notify-service-by-fetion
command_line /usr/bin/fetion --mobile 15003880800 --pwd 123qweQWE --to 15003880800 --msg-tpye=0 --msg-utf8 $HOSTNAME
}
6、重启nagios服务
service nagios restart
到此为止我们我们对mysql服务的飞信报警,已经配置完成了,现在我们可以关闭mysql服务,看你是否收到了我们的nagios发送的飞信。