监控之nagios的安装部署(监控本机及其他主机的配置+警报机制的配置)
主机环境 redhat6.5 64位
实验环境服务端1 ip 172.25.25.1
服务端2 ip 172.25.25.2
安装包 nagios-plugins-2.1.1.tar.gz
nagios-cn-3.2.3.tar.bz2
gd-devel-2.0.35-11.el6.x86_64.rpm
nrpe-2.15.tar.gz
防火墙状态:关闭
Selinux状态:Disabled
1.安装nagios、监控本地及测试
A)配置安装nagios及插件
[root@sever1 mnt]# ls
nagios-plugins-2.1.1.tar.gz nagios-cn-3.2.3.tar.bz2
gd-devel-2.0.35-11.el6.x86_64.rpm
[root@sever1 mnt]# yum installgd-devel-2.0.35-11.el6.x86_64.rpm -y
[root@sever1 mnt]# useradd Nagios #创建系统用户ngios
[root@sever1 mnt]# usermod -a -G nagcmd Nagios #将nagios添加到nagcmd附加组
[root@sever1 mnt]# usermod -a -G nagcmd apache #将apache也添加到nagcnd附加组
[root@sever1 mnt]# id Nagios #查看
uid=1001(nagios) gid=1001(nagios)groups=1001(nagios),1002(nagcmd)
[root@sever1 mnt]# id apache
uid=48(apache) gid=48(apache) groups=48(apache),1002(nagcmd)
[root@sever1 mnt]# tar jxf nagios-cn-3.2.3.tar.bz2 #解压
[root@sever1 mnt]# cd nagios-cn-3.2.3
[root@sever1 nagios-cn-3.2.3]# ./configure--with-command-group=nagcmd #配置
[root@sever1 nagios-cn-3.2.3]# make all
[root@sever1 nagios-cn-3.2.3]# make install #安装
[root@sever1 nagios-cn-3.2.3]# make install-init #安装脚本文件
[root@sever1 nagios-cn-3.2.3]# make install-config #安装配置文件
[root@sever1 nagios-cn-3.2.3]# make install-webconf
[root@sever1 nagios-cn-3.2.3]# cat /usr/local/nagios/etc/htpasswd.users #查看密码
nagiosadmin:gCWSDnqEHR45c
[root@sever1 nagios-cn-3.2.3]# htpasswd -c/usr/local/nagios/etc/htpasswd.users nagiosadmin #修改密码
New password:
Re-type new password:
Adding password for user nagiosadmin
[root@sever1 nagios-cn-3.2.3]# cat/usr/local/nagios/etc/htpasswd.usersnagiosadmin:QNeDXoEuaEVuI
[root@sever1 nagios-cn-3.2.3]# /etc/init.d/httpd start #开启httpd
Stopping httpd: [OK ]
[root@sever1 nagios-cn-3.2.3]# /usr/local/nagios/bin/nagios -v/usr/local/nagios/etc/nagios.cfg #校验nagios的语法
[root@sever1 nagios-cn-3.2.3]# /etc/init.d/nagios start #开启nagios
Starting nagios: done.
#安装nagios插件
[root@sever1 mnt]# tar zxf nagios-plugins-2.1.1.tar.gz #解压
[root@sever1 mnt]# cd nagios-plugins-2.1.1
[root@sever1 nagios-plugins-2.1.1]# ./config--with-nagios-user=nagios --with-nagios-group=nagios #配置
[root@sever1 nagios-plugins-2.1.1]# make #编译
[root@sever1 nagios-plugins-2.1.1]# make install #安装
B)添加本地服务
[root@server1 etc]# vimnagios.cfg
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg#添加文件cfg_file=/usr/local/nagios/etc/objects/services.cfg#Definitionsformonitoringthelocal(Linux)host#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg#标记localhost文件
[root@server1 etc]# cd objects/
[root@server1 objects]# cp -p localhost.cfg hosts.cfg
[root@server1 objects]# cp -p localhost.cfg services.cfg
[root@server1 objects]# vim hosts.cfg #主机配置文件
definehost{#主机uselinux-server#使用的模板host_nameserver1.example.com#主机名aliasManager#别名address172.25.29.1#ipicon_p_w_picpathserver.gif#下面几行是在拓扑图中的位置statusmap_p_w_picpathserver.gd22d_coords500,2003d_coords500,200,100}definehostgroup{hostgroup_namelinux-servers;ThenameofthehostgroupaliasLinuxServers;Longnameofthegroupmembers*;Commaseparatedlistofhoststhatbelongtothisgroup}
[root@server1objects]# vim service.cfg #服务配置文件
defineservicegroup{#服务组servicegroup_name系统负荷检查alias负荷检查membersserver1.example.com,进程总数,server1.example.com,登录用户数,server1.example.com,根分区,server1.example.com,交换空间利用率}defineservice{#服务uselocal-service#使用的模板host_name*service_descriptionPINGcheck_commandcheck_ping!100.0,20%!500.0,60%#要传的参数}defineservice{uselocal-service;Nameofservicetemplatetousehost_nameserver1.example.comservice_description根分区check_commandcheck_local_disk!20%!10%!/}defineservice{uselocal-service;Nameofservicetemplatetousehost_nameserver1.example.comservice_description登录用户数check_commandcheck_local_users!20!50}defineservice{uselocal-service;Nameofservicetemplatetousehost_nameserver1.example.comservice_description进程总数check_commandcheck_local_procs!250!400!RSZDT}defineservice{uselocal-service;Nameofservicetemplatetousehost_nameserver1.example.comservice_description系统负荷check_commandcheck_local_load!5.0,4.0,3.0!10.0,6.0,4.0}defineservice{uselocal-service;Nameofservicetemplatetousehost_nameserver1.example.comservice_description交换空间利用率check_commandcheck_local_swap!20!10}defineservice{uselocal-service;Nameofservicetemplatetousehost_nameserver1.example.comservice_descriptionSSHcheck_commandcheck_tcp!22!1.0!10.0notifications_enabled0}defineservice{uselocal-service;Nameofservicetemplatetousehost_nameserver1.example.comservice_descriptionHTTPcheck_commandcheck_httpnotifications_enabled0}
[root@server1 objects]# /etc/init.d/nagios reload #刷新
Running configuration check...done.
Reloading nagios configuration...done
C)测试172.25.25.1/Nagios
2.远程监控服务端2mysql及测试
A) 安装mysql及添加授权
服务端2
[root@server2 ~]# yum install mysql-server -y #安装mysql
[root@server2 ~]# /etc/init.d/mysqld start #开启mysql
[root@server2 ~]# mysql_secure_installation #初始化,添加密码redhat
[root@server2 ~]# mysql -predhat #进入mysql
mysql> create database nagdb; #添加nagdb库
Query OK, 1 row affected (0.00 sec)
mysql> grant select on nagdb.* to nagios@'172.25.25.1'identified by 'redhat';
Query OK, 0 rows affected (0.00 sec) #给nagios用户授予nagdb库的查看权限
mysql> Bye #退出
#服务端1,测试
[root@server1 objects]# ./check_mysql -H 172.25.25.2 -u nagios-p redhat -d nagdb
B) 将mysql服务(服务端2)添加到nagios(服务端1)
服务端1
[root@server1 objects]# vim hosts.cfg #定义主机
definehost{uselinux-server#调用的模版host_nameserver2.example.com#主机名(command.cfg中要调用的参数)aliasMYSQL#别名address172.25.25.2#ipicon_p_w_picpathserver.gifstatusmap_p_w_picpathserver.gd2#坐标的定义2d_coords400,2003d_coords400,200,100}[root@server1objects]#vimservices.cfg#定义服务####################check_mysql#######################defineservice{uselocal-servicehost_nameserver2.example.com(调用hosts.cfg中的host_name)service_descriptionMYSQLcheck_commandcheck_mysql!nagios!redhat!nagdb#command.cfg中要调用的参数(命令名字!主机名!密码!数据库的名称)}
[root@server1 objects]# vim commands.cfg #定义命令
#'check_mysql'commanddefinitiondefinecommand{command_namecheck_mysql#命令的名字command_line$USER1$/check_mysql-H$HOSTADDRESS$-u$ARG1$-p$ARG2$-d$ARG3$#调用的脚本绝对路径-H主机名(调用hosts.cfg中的host_name)-umysql的用户名-p用户密码-d数据库的名称}
[root@server1 objects]# /usr/local/nagios/bin/nagios -v/usr/local/nagios/etc/nagios.cfg #检查是否有语法错误
Total Warnings: 0
Total Errors: 0 #无语法错误
Things look okay - No serious problems were detected during thepre-flight check
[root@server1 objects]# /etc/init.d/nagios reload #刷新
Running configuration check...done.
Reloading nagios configuration...done
C)测试
点击拓扑图,添加server2.example.com成功
点击主机,查看两台主机
点击服务,如图
3.远程监控服务端2的根分区和用户数
A)安装nagios插件及nrpe
服务端2
[root@server2 mnt]# ls
nagios-plugins-2.1.1.tar.gznrpe-2.15.tar.gz
[root@server2 mnt]# tar zxf nagios-plugins-2.1.1.tar.gz #解压plugins插件
[root@server2 mnt]# cd nagios-plugins-2.1.1
[root@server2 nagios-plugins-2.1.1]# ./configure
[root@server2 nagios-plugins-2.1.1]# make
[root@server2 nagios-plugins-2.1.1]# make install
[root@server2 mnt]# cd /usr/local/nagios/
[root@server2 nagios]# ls
include libexec share
[root@server2 nagios]# useradd -u 1001 nagios #创建nagios用户
[root@server2 nagios]# chown nagios.nagios * -R #修改文件权限
[root@server2 nagios]# ll #查看
total 12
drwxr-xr-x 2 nagios nagios 4096 Oct 23 10:50 include
drwxr-xr-x 2 nagios nagios 4096 Oct 23 10:50 libexec
drwxr-xr-x 3 nagios nagios 4096 Oct 23 10:50 share
[root@server2 libexec]# cd /mnt/
[root@server2 mnt]# tar zxf nrpe-2.15.tar.gz #解压nrpe
[root@server2 mnt]# cd nrpe-2.15
[root@server2 nrpe-2.15]# ./configure #配置
[root@server2 nrpe-2.15]# make all
[root@server2 nrpe-2.15]# make install-plugin
[root@server2 nrpe-2.15]# make install-daemon
[root@server2 nrpe-2.15]# make install-daemon-config
[root@server2 nrpe-2.15]# yum install -y xinetd
[root@server2 nrpe-2.15]# make install-xinetd
[root@server2 nrpe-2.15]# vim /etc/services #添加nrpe的端口
nrpe 5666/tcp
[root@server2 nrpe-2.15]# cd /etc/xinetd.d/
[root@server2 xinetd.d]# vim nrpe #进入配置文件
15 only_from = 172.25.25.1 #允许172.25.25.1监听
[root@server2 etc]# cd /usr/local/nagios/etc/
[root@server2 etc]# ls
nrpe.cfg
[root@server2 etc]# vim nrpe.cfg #修改命令
221command[check_disk]=/usr/local/nagios/libexec/check_disk-w20%-c10%-p/
[root@server2 etc]# cd ..
[root@server2 nagios]# ls
bin etc includelibexec share
[root@server2 nagios]# cd libexec/
[root@server2 libexec]# ./check_disk -w 20% -c 10% -p / #测试
DISK OK - free space: / 6222 MB (85% inode=93%);|/=1057MB;6135;6902;0;7669
[root@server2 libexec]# /etc/init.d/xinetd start #开启xinetd
Starting xinetd:[ OK ]
[root@server2 libexec]# netstat -antple |grep 5666 #查看端口,开启
tcp 00 :::5666:::* LISTEN 023552 23120/xinetd
[root@server2 libexec]# scp check_nrpe172.25.25.1:/usr/local/nagios/libexec/
root@172.25.25.1's password: #将nrpe传给服务端1
check_nrpe 100% 75KB75.0KB/s 00:00
B) 将根分区和用户数(服务端2)添加到nagios(服务端1)
服务端1
[root@server1 libexec]# pwd
/usr/local/nagios/libexec
[root@server1 libexec]# chown nagios.nagios check_nrpe #修改权限
[root@server1 libexec]# ./check_nrpe -H 172.25.25.2 #测试
NRPE v2.15
[root@server1 libexec]# cd ..
[root@server1 nagios]# cd etc/objects/
[root@server1 objects]# vim services.cfg #添加服务
####################check_nrpe#######################defineservice{uselocal-servicehost_nameserver2.example.comservice_description根分区check_commandcheck_nrpe!check_disk}defineservice{uselocal-servicehost_nameserver2.example.comservice_description登录用户数check_commandcheck_nrpe!check_users}
[root@server1 objects]# vim commands.cfg #添加命令
#'check_nrpe'commanddefinitiondefinecommand{command_namecheck_nrpecommand_line$USER1$/check_nrpe-H$HOSTADDRESS$-c$ARG1$}
[root@server1 objects]# /usr/local/nagios/bin/nagios -v/usr/local/nagios/etc/nagios.cfg
Total Warnings: 0
Total Errors: 0
Things look okay - No serious problems were detected during thepre-flight check
[root@server1 objects]# /etc/init.d/nagios reload #刷新
C)测试
4.nagios警报的配置及测试(邮件联系人)
[root@server1 objects]# vim contacts.cfg #修改邮件地址
35emailxxxxxxxxxx@qq.com
[root@server1 objects]# /etc/init.d/nagios reload #刷新
[root@server1 ~]$ cd /mnt/
[root@server1 mnt]$ ls
alert-agent-4.1.3.1-linux-x64.tar.gz
[root@server1 mnt]$ tar zxfalert-agent-4.1.3.1-linux-x64.tar.gz -C /usr/local/nagios/libexec/
[root@server1 mnt]$ cd /usr/local/nagios/libexec/
[root@server1 libexec]$ cpalert-agent/plugin/nagios-plugin/nagios .
[root@server1 libexec]$ cpalert-agent/plugin/nagios-plugin/110monitor.cfg ../etc/objects/
[root@server1 libexec]$ chmod +x nagios
[root@server1 libexec]$ chmod nagios.nagios * -R
[root@server1 libexec]$ cd ..
[root@server1 nagios]# cd etc/
[root@server1 etc]# vim nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/110monitor.cfg #添加
[root@server1 etc]# cd objects/
[root@server1 objects]# vim 110monitor.cfg
18pager379cd8ca-5700-e2ef-10a2-c72a9158b55a#改成自己生成的key
[root@server1 objects]# /usr/local/nagios/bin/nagios -v/usr/local/nagios/etc/nagios.cfg
Total Warnings: 0
Total Errors: 0
Things look okay - No serious problems were detected during thepre-flight check
[root@server1 objects]# /etc/init.d/nagios reload
B)测试 当把服务端2的mysql停掉之后,如图:
邮件也发过来了,如图
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。