Nagios-config
前面解决完了服务器端和客户端的安装,默认在浏览器中只能看到服务器监控nagios服务器本身,也就是localhost,并且在httpd和sshd服务前面显示有个打岔的灯,那表示报警未开启,下面就来配置下监控客户端的服务和设置短信报警等!
进入nagios工作目录w
[root@server objects]# pwd
/usr/local/nagios/etc/objects
将默认的localhost.cfg文件复制一份,修改其中的hostname和ip地址等参数
[root@server objects]# cp localhost.cfg client.cfg (修改下hostname和ip地址等参数)
[root@server objects]# chown nagios:nagios client.cfg 修改下文件的属主和属组,使用cp命令可忽略此步,若是用vi新建的cfg文件,则需要执行这步
在服务器主配置文件中添加下面一行,让其加载我们新建出来的client.cfg配置文件
[root@server ~]# grep 'cfg_file' /usr/local/nagios/etc/nagios.cfg |grep client
cfg_file=/usr/local/nagios/etc/objects/client.cfg
检测配置文件是否存在错误
[root@server objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Checking hosts...
Checked 2 hosts.
Total Warnings: 0
Total Errors: 0
重启nagios服务
[root@server objects]# service nagios restart
Running configuration check...done.
Stopping nagios: done.
Starting nagios: done.
该文件的末尾添加要监控的相关服务,dhcp于ftp为服务器端直接允许测试基本,mysql则为服务器端调用客户端的脚本进行测试;
local-service:为模板,在/usr/local/nagios/etc/objects/templates.cfg文件中定义
check_command:为/usr/local/nagios/libexec/目录下的可执行文件,在服务器端可以直接用来检测客户端状态, check_nrpe!check_mysql:表示服务器端通过nrpe调用客户端上的check_mysql命令
notifications_enabled:此项为0则表示不启用通知,所以要设定为1
[root@server ~]# vi /usr/local/nagios/etc/objects/client.cfg 在
define service{
use local-service ; Name of service template to use
host_name client
service_description dhcp
check_command check_dhcp
notifications_enabled 1
}
define service{
use local-service ; Name of service template to use
host_name client
service_description ftp
check_command check_ftp
notifications_enabled 1
}
define service{
use local-service ; Name of service template to use
host_name client
service_description mysql
check_command check_nrpe!check_mysql
notifications_enabled 1
}
检测语法并重启服务
[root@server ~]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Total Warnings: 0
Total Errors: 0
[root@server ~]# service nagios restart
Running configuration check...done.
Stopping nagios: .done.
Starting nagios: done.
在客户端的主配置文件中添加一行定义check_mysql的参数如下:
需要用-d参数来指定检测的数据库名称
[root@client ~]#grep 'check_mysql' /usr/local/nagios/etc/nrpe.cfg
command[check_mysql]=/usr/local/nagios/libexec/check_mysql -u root -p passwd -d mysql -H 192.168.90.1
[root@server ~]#vim /usr/local/nagios/etc/objects/commands.cfg
define command {
command_name check_Mysql
command_line $USER1$/check_Mysql -H $HOSTADDRESS$ -u nagios -d nagdb
}
测试客户端能否允行
[root@client ~]#/usr/local/nagios/libexec/check_mysql -u root -p passwd -d mysql -H 127.0.0.1
Uptime: 1998 Threads: 1 Questions: 1 Slow queries: 0 Opens: 15 Flush tables: 1 Open tables: 8 Queries per second avg: 0.0
短信监控的实现:
下载并安装linux版飞信
[root@server ~]# cd /usr/local/src/tarbag/
[root@server tarbag]# wget http://d3.766.com/766down/fetion-linux.tar.gz
[root@server tarbag]# tar -zxvf fetion-linux.tar.gz -C ../software/
[root@server tarbag]# cd ../software/fx/
[root@server fx]# mv fetion /usr/local/bin/
[root@server fx]# cp ./* /lib
[root@server fx]# cp ./* /usr/lib
目录可以忽略,Fetion的使用方法详见fetion --help
[root@server ~]# vi /usr/local/nagios/etc/objects/commands.cfg (添加如下内容)
define command{
command_name notify-service-by-sms
command_line /usr/bin/fetion --mobile=15900000001 --pwd=password --to=15900000002 --msg-utf8="主机:IP地址$HOSTADDRESS$,服务器描述: $HOSTALIAS$/$SERVICEDESC$ 目前状态:$SERVICESTATE$"
}
--mobile:表示发送方的手机号;--pwd:表示发送方手机的飞信登录密码;--to:表示接收人的手机号,如果有多个可以用“,”号隔开,也可以定义组来管理;
--msg-utf8:表示信息以utf8的格式发送,这样不会乱码,后面则是一些变量,会从配置文件中读取!
检测配置文件是否存在错误
[root@server objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Total Warnings: 0
Total Errors: 0
重启nagios服务
[root@server objects]# service nagios restart
Running configuration check...done.
Stopping nagios: done.
Starting nagios: done.
[root@client ~]#service mysqld stop
Shutting down MySQL.
[root@server ~]# tail -f /usr/local/nagios/var/nagios.log
[1264014486] SERVICE ALERT: client;mysql;CRITICAL;SOFT;1;Can't connect to MySQL server on '192.168.90.1' (111)
[1264014546] SERVICE ALERT: client;mysql;CRITICAL;SOFT;2;Can't connect to MySQL server on '192.168.90.1' (111)
[1264014606] SERVICE ALERT: client;mysql;CRITICAL;SOFT;3;Can't connect to MySQL server on '192.168.90.1' (111)
[1264014666] SERVICE ALERT: client;mysql;CRITICAL;HARD;4;Can't connect to MySQL server on '192.168.90.1' (111)
[1264014666] SERVICE NOTIFICATION: nagiosadmin;client;mysql;CRITICAL;notify-service-by-sms;Cant connect to MySQL server on 192.168.90.1 (111)