Nagios监控Mysql及远程主机
环境: 192.168.0.201 mysql主机 rhel6.4
192.186.0.202 nagios主机 rhel6.4
安装相关软件包:
yum install httpd gcc make perl-ExtUtils-Embed.x86_64 -y
yum localinstall gd-devel-2.0.35-11.el6.x86_64.rpm -y
安装nagios:
- useradd nagios
- usermod -G nagios apache
- #编译所需软件包
- #运行 nagios 服务的用户
- #使 apache 用户对 nagios 目录具有写权限,不然 web 页面操作失败
- tar jxf nagios-cn-3.2.3.tar.bz2
- #nagios 软件安装
- cd nagios-cn-3.2.3
- ./configure –enable-embedded-perl
- make all //根据提示完成安装
- make install
- make install-init
- make install-commandmode
- make install-config
- make install-webconf //在apache下通过这条命令可以快速整合
- yum install mysql-devel openssl-devel -y
- tar zxf nagios-plugins-1.4.15.tar.gz
- cd nagios-plugins-1.4.15
- ./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-extra-opts --enable-libtap --enable-perl-modules
- make
- make install
- vi /usr/local/nagios/etc/nagios.cfg
- #cfg_file=/usr/local/nagios/etc/objects/localhost.cfg #注释掉localhost.cfg文件
- cfg_file=/usr/local/nagios/etc/objects/services.cfg #新建 services.cfg 文件,存放服务与服务组定义
- cfg_file=/usr/local/nagios/etc/objects/hosts.cfg #新建 hosts.cfg 文件,存放主机与主机组定义
- vi /usr/local/nagios/etc/objects/hosts.cfg
- define host{ //主机的管理
- use linux-server //使用默认的主机规则
- host_name vm2.example.com //主机名
- alias Nagios vm2 //主机别名
- address 127.0.0.1 //主机ip地址
- icon_image switch.gif //图标
- statusmap_image switch.gd2
- 2d_coords 100,200 //2D图像坐标
- 3d_coords 100,200,100 //3D图像坐标
- }
- define hostgroup{ //主机组的管理
- hostgroup_name linux-servers //主机组名字
- alias Linux Servers //别名
- members * //*代表上面所有主机
- }
- define servicegroup{
- servicegroup_name 系统负荷检查
- alias 负荷检查
- members vm2.example.com,进程总数,vm2.example.com,登录用户数,vm2.example.com,根分区,vm2.example.com,交换空间利用率
- }
- define service{
- use local-service ; Name of service template to use
- host_name *
- service_description PING //表示监控ping服务
- check_command check_ping!100.0,20%!500.0,60%
- }
- define service{
- use local-service ; Name of service template to use
- host_name vm2.example.com
- service_description 根分区
- check_command check_local_disk!20%!10%!/
- }
- define service{
- use local-service ; Name of service template to use
- host_name vm2.example.com
- service_description 登录用户数
- check_command check_local_users!20!50
- }
- define service{
- use local-service ; Name of service template to use
- host_name vm2.example.com
- service_description 进程总数
- check_command check_local_procs!250!400!RSZDT
- }
- define service{
- use local-service ; Name of service template to use
- host_name vm2.example.com
- service_description 系统负荷
- check_command check_local_load!5.0,4.0,3.0!10.0,6.0,4.0
- }
- define service{
- use local-service ; Name of service template to use
- host_name vm2.example.com
- service_description 交换空间利用率
- check_command check_local_swap!20!10
- }
- define service{
- use local-service ; Name of service template to use
- host_name vm2.example.com
- service_description SSH
- check_command check_tcp!22!1.0!10.0
- notifications_enabled 1 //这里表示进行通知
- }
- define service{
- use local-service ; Name of service template to use
- host_name vm2.example.com
- service_description HTTP
- check_command check_http
- notifications_enabled 1
- }
如果没有error的话,开启服务
/etc/init.d/nagios start
/etc/init.d/httpd start
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin nagios 监控页面访问用户和密码
打开浏览器,输入帐号密码:
过一会儿,你就可以看到主机状况了
OK,现在我们来监控Mysql主机 192.168.0.201
在Mysql主机上进行操作
- mysql> create database nagdb;
- mysql> grant select on nagdb.* to nagdb@‘192.168.0.202‘;
- mysql> flush privileges;
[root@vm2 ~]# /usr/local/nagios/libexec/check_mysql -H 192.168.0.201 -u nagdb -d nagdb
Uptime: 19056 Threads: 12 Questions: 102960 Slow queries: 0 Opens: 317 Flush tables: 1 Open tables: 63 Queries per second avg: 5.403
更改配置文件hosts.cfg services.cfg commands.cfg增加对Mysql主机的监控
- vi /usr/local/nagios/etc/objects/hosts.cfg 添加下面内容
- define host{
- use linux-server
- host_name mysql主机
- alias mysql服务器
- address 192.168.0.201
- icon_image server.gif
- statusmap_image server.gd2
- 2d_coords 500,200
- 3d_coords 500,200,100
- }
- vi /usr/local/nagios/etc/objects/services.cfg 添加以下内容
- define service{
- use local-service
- host_name mysql主机
- servce_groups mysql服务组
- service_description mysql服务
- check_command check_mysql
- }
- define servicegroup{
- servicegroup_name mysql服务组
- alias mysql服务器
- }
- vi /usr/local/nagios/etc/objects/commands.cfg 添加下面内容
- define command{
- command_name check_mysql
- command_line $USER1$/check_mysql -H $HOSTADDRESS$ -u nagdb -d nagdb
- }
service nagios reload reload服务
截取的监控图:
利用NRPE和Nagios-plugin监控远程主机:
NRPE是Nagios的一个功能扩展,它可以在远程Linux和Unix主机上执行插件程序。通过在远程服务器上安装NRPE构件及Nagios插件程序来向Nagios监控主机提供该服务器的一些本地情况,例如,CPU负载、内存使用、磁盘使用等。
远程主机配置
- useradd nagios
- tar -xzf 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
- yum install xinetd -y
- tar zxf nrpe-2.12.tar.gz
- cd nrpe-2.12
- ./configure
- make all
- make install-plugin
- make install-daemon
- make install-daemon-config
- make install-xinetd
- vim /etc/xinetd.d/nrpe
-
only_from= 192.168.0.202 //添加Nagios监控主机ip
- vim /etc/services
-
nrpe 5666/tcp //跟/etc/xinetd.d/nrp端口一样
- /etc/init.d/xinetd start
- netstat -anlpte | grep 5666 //检测nrpe是否启动成功
- tar zxf nrpe-2.12.tar.gz
- cd nrpe-2.12
- ./configure --with-nagios-user=nagios --with-nagios-group=nagios
- make all
- make install-plugin
[root@vm2 ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.0.201
NRPE v2.12
看到以上输出说明与远程主机连接正常
修改配置文件 commands.cfg services.cfg
- vi /usr/local/nagios/etc/objects/commands.cfg
- define command{
- command_name check_nrpe
- command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
- }
- vi /usr/local/nagios/etc/objects/services.cfg
- define service{
- use generic-service
- host_name mysql主机
- service_description CPU Load
- check_command check_nrpe!check_load
- }
- define service{
- use generic-service
- host_name mysql主机
- service_description Current Users
- check_command check_nrpe!check_users
- }
- define service{
- use generic-service
- host_name mysql主机
- service_description /Free Space
- check_command check_nrpe!check_disk
- }
没有错误reload服务
service nagios reload
忙活了这么久,看下劳动成果:
明天再来折腾下Nagios报警,今天就到这里啦
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。