Nagios部署+远程MySQL监控+PNP图表环境配置(3)

三、监控远程MySQL主机,把禁ping功能关闭。
1、远程主机安装mysql服务
[root@mysql ~]# yum install mysql mysql-server -y
[root@mysql ~]# service mysqld start
[root@mysql ~]# /usr/bin/mysqladmin -u root password 'kevin'  (设置密码为kevin)
[root@mysql ~]# mysql -uroot -pkevin
mysql> create database nagiosdb;
Query OK, 1 row affected (0.00 sec)

mysql> grant select on nagiosdb.* to nagiosdb@'192.168.0.114';

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)
2、测试权限
在nagios主机【192.168.0.114】上检测nagiosdb用户是否可以登录远程mysql服务
[root@nagios ~]# /usr/local/nagios/libexec/check_mysql -H 192.168.0.121 -u nagiosdb -d nagiosdb
Uptime: 4307 Threads: 1 Questions: 15 Slow queries: 0 Opens: 15 Flush tables: 1 Open tables: 8 Queries per second avg: 0.3
OK,说明能够监控了,接下来将mysql主机添加进nagios中:
3、定义hosts主机:(在/usr/local/nagios/etc/objects/hosts.cfg 中添加)
[root@nagios ~]# cd /usr/local/nagios/etc/objects/
[root@nagios objects]# vim hosts.cfg
define host{
use linux-server ; Name of host template to use
host_name mysql主机
alias mysql服务器
address 192.168.0.121
}
4、定义服务和服务组
[root@nagios objects]# vim services.cfg (添加下面的内容)
define servicegroup{
servicegroup_name mysql服务组
alias mysql服务器
}

define service{

use local-service ; Name of service template to use
host_name mysql主机
service_groups mysql服务组
service_description mysql服务
check_command check_mysql
}
5、定义mysql监控命令
[root@nagios objects]# vim commands.cfg (添加check_mysql命令)
define command{
command_name check_mysql
command_line $USER1$/check_mysql -H $HOSTADDRESS$ -u nagiosdb -d nagiosdb
}
6、重载nagios服务
[root@nagios objects]#/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
[root@nagios objects]# service nagios reload (这里之前使用restart好像还不能正常加载)
 
7、故障测试

同上面“本地监控故障测试”

四、pnp图表监控
nagios对服务是即时的,可以使用pnp生成图表进行查看一段时间的监控情况
php是一款基于php和perl的开源软件包,php可以利用rrdtool工具将nagios采集的数据绘制成相关的图表,显示主机或服务在一段时间的运行情况

1、安装rrdtool
[root@nagios ~]#tar -zxvf rrdtool-1.4.5.tar.gz
[root@nagios ~]# cd rrdtool-1.4.5
[root@nagios rrdtool-1.4.5]# ./configure --prefix=/usr/local/rrdtool --enable-perl-site-install
----------------------------------------------------------------------------
checking for xmlParseFile in -lxml2... yes
checking libxml/parser.h usability... yes
checking libxml/parser.h presence... yes
checking for libxml/parser.h... yes
configure: error: Please fix the library issues listed above and try again.
出现错误了,解决办法:安装pango-devel
[root@nagios rrdtool-1.4.5]#yum install pango-devel -y
[root@nagios rrdtool-1.4.5]# ./configure --prefix=/usr/local/rrdtool --enable-perl-site-install
[root@nagios rrdtool-1.4.5]# make && make install
2、安装pnp
[root@nagios ~]#tar -zxvf pnp-0.4.13.tar.gz
[root@nagios ~]# cd pnp-0.4.13
[root@nagios pnp-0.4.13]#./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-rrdtool=/usr/local/rrdtool/bin/rrdtool --with-perfdata-dir=/usr/local/nagios/share/perfdate
【“--with-perfdata-dir=” rrd数据的存放位置】

*** Configuration summary for pnp 0.4.13 02-19-2009 ***

General Options:

------------------------- -------------------
Nagios user/group: nagios nagios
Install directory: /usr/local/nagios
HTML Dir: /usr/local/nagios/share/pnp
Config Dir: /usr/local/nagios/etc/pnp
Path to rrdtool: /usr/local/rrdtool/bin/rrdtool (Version 1.4.5)
RRDs Perl Modules: *** NOT FOUND ***
RRD Files stored in: /usr/local/nagios/share/perfdate
process_perfdata.pl Logfile: /usr/local/nagios/var/perfdata.log
Perfdata files (NPCD) stored in: /usr/local/nagios/var/spool/perfdata/

[root@nagios pnp-0.4.13]#make all
[root@nagios pnp-0.4.13]# make install
[root@nagios pnp-0.4.13]# make install-config
[root@nagios pnp-0.4.13]#make install-init
【每一步完成之后都有相关文字,它提示了安装过程的相关信息】
3、配置pnp
[root@nagios pnp-0.4.13]# cd /usr/local/nagios/etc/pnp/pages/
[root@nagios pages]# cp web_traffic.cfg-sample web_traffic.cfg
[root@nagios pages]# cd ../check_commands/
[root@nagios check_commands]# cp check_nwstat.cfg-sample check_nwstat.cfg
[root@nagios pnp-0.4.13]#cd /usr/local/nagios/etc/pnp/
[root@nagios pnp]# pwd
/usr/local/nagios/etc/pnp
[root@nagios pnp]# cp process_perfdata.cfg-sample process_perfdata.cfg
[root@nagios pnp]# cp npcd.cfg-sample npcd.cfg
[root@nagios pnp]# cp rra.cfg-sample rra.cfg
[root@nagios pnp]# chown nagios.nagios * -R
[root@nagios pnp]# vim process_perfdata.cfg
LOG_FILE = /usr/local/nagios/var/perfdata.log
#
# Loglevel 0=silent 1=normal 2=debug
#
LOG_LEVEL = 2
【只用将这里日志等级改为debug模式。0:silent;1:normal;2:debug】

4、更改模版文件,加入pnp监控图标
[root@nagios pnp]#vim /usr/local/nagios/etc/objects/templates.cfg
define host{
name hosts-pnp
register 0
action_url /nagios/pnp/index.php?host=$HOSTNAME$
process_perf_data  1
}

define service{

name services-pnp
register 0
action_url /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
process_perf_data  1
}
5、开启process_performance_data命令,处理数据
[root@nagios pnp]# vim /usr/local/nagios/etc/nagios.cfg  (修改nagios.cfg,约833、845行)
833 process_performance_data=1
845 host_perfdata_command=process-host-perfdata
846 service_perfdata_command=process-service-perfdata

6、定义process_performance_data的相关命令
[root@nagios pnp]# vim /usr/local/nagios/etc/objects/commands.cfg
define command{
command_name process-host-perfdata
command_line /usr/local/nagios/libexec/process_perfdata.pl
}

define command{

command_name process-service-perfdata
command_line /usr/local/nagios/libexec/process_perfdata.pl
}
说明:command.cfg文件的默认设置对process-host-perfdata 和 process-service-perfdata有定义,如果直接添加而不删除默认定义会发生冲突,会导致在检测配置文件时报错。
# 'process-host-perfdata' command definition
#define command{
# command_name process-host-perfdata
# command_line /usr/bin/printf "%b" "$LASTHOSTCHECK$\t$HOSTNAME$\t$HOSTSTATE$\t$HOSTATTEMPT$\t$HOSTSTATETYPE$\t$HOSTEXECUTIONTIME$\t$HOSTOUTPUT$\t$HOSTPERFDATA$\n" >> /usr/local/nagios/var/host-perfdata.out
# }


# 'process-service-perfdata' command definition
#define command{
# command_name process-service-perfdata
# command_line /usr/bin/printf "%b" "$LASTSERVICECHECK$\t$HOSTNAME$\t$SERVICEDESC$\t$SERVICESTATE$\t$SERVICEATTEMPT$\t$SERVICESTATETYPE$\t$SERVICEEXECUTIONTIME$\t$SERVICELATENCY$\t$SERVICEOUTPUT$\t$SERVICEPERFDATA$\n" >> /usr/local/nagios/var/service-perfdata.out
# }

7、定义host文件
[root@nagios pnp]# vim /usr/local/nagios/etc/objects/hosts.cfg
###############################################################################
define host{
use linux-server,hosts-pnp ; Name of host template to use ,添加hosts-pnp
host_name 本地监控主机
alias nagios监控主机
address 127.0.0.1
}

define host{

use linux-server,hosts-pnp ; Name of host template to use ,添加hosts-pnp
host_name mysql主机
alias mysql服务器
address 192.168.0.121
}
###############################################################################
define hostgroup{
hostgroup_name linux-servers ; The name of the hostgroup
alias Linux Servers ; Long name of the group
members * ; Comma separated list of hosts that belong to this group
}
###############################################################################
8、同样修改/usr/local/nagios/etc/objects/services.cfg文件
[root@nagios pnp]# sed -i 's/local-service/local-service,services-pnp/g' /usr/local/nagios/etc/objects/services.cfg
9、检查错误并重启
[root@nagios pnp]#/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
[root@nagios pnp]#service nagios restart 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/9968acbe73ad09fb0c73953daace36d0.html