设为首页 收藏本站
查看: 1747|回复: 0

监控之nagios的安装部署(监控本机及其他主机的配置+警报机制的配置)

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-11-3 08:43:29 | 显示全部楼层 |阅读模式
主机环境   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及插件
[iyunv@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
[iyunv@sever1 mnt]# yum installgd-devel-2.0.35-11.el6.x86_64.rpm -y
[iyunv@sever1 mnt]# useradd Nagios                  #创建系统用户ngios
[iyunv@sever1 mnt]# usermod -a -G nagcmd Nagios      #将nagios添加到nagcmd附加组
[iyunv@sever1 mnt]# usermod -a -G nagcmd apache      #将apache也添加到nagcnd附加组
[iyunv@sever1 mnt]# id Nagios                       #查看
uid=1001(nagios) gid=1001(nagios)groups=1001(nagios),1002(nagcmd)
[iyunv@sever1 mnt]# id apache
uid=48(apache) gid=48(apache) groups=48(apache),1002(nagcmd)
[iyunv@sever1 mnt]# tar jxf nagios-cn-3.2.3.tar.bz2  #解压
[iyunv@sever1 mnt]# cd nagios-cn-3.2.3
[iyunv@sever1 nagios-cn-3.2.3]# ./configure--with-command-group=nagcmd  #配置
[iyunv@sever1 nagios-cn-3.2.3]# make all
[iyunv@sever1 nagios-cn-3.2.3]# make install         #安装
[iyunv@sever1 nagios-cn-3.2.3]# make install-init    #安装脚本文件
[iyunv@sever1 nagios-cn-3.2.3]# make install-config  #安装配置文件
[iyunv@sever1 nagios-cn-3.2.3]# make install-webconf
[iyunv@sever1 nagios-cn-3.2.3]# cat /usr/local/nagios/etc/htpasswd.users #查看密码
nagiosadmin:gCWSDnqEHR45c
[iyunv@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
[iyunv@sever1 nagios-cn-3.2.3]# cat/usr/local/nagios/etc/htpasswd.usersnagiosadmin:QNeDXoEuaEVuI
[iyunv@sever1 nagios-cn-3.2.3]# /etc/init.d/httpd start      #开启httpd
Stopping httpd:                                           [ OK  ]
[iyunv@sever1 nagios-cn-3.2.3]# /usr/local/nagios/bin/nagios -v/usr/local/nagios/etc/nagios.cfg                           #校验nagios的语法
[iyunv@sever1 nagios-cn-3.2.3]# /etc/init.d/nagios start     #开启nagios
Starting nagios: done.
#安装nagios插件
[iyunv@sever1 mnt]# tar zxf nagios-plugins-2.1.1.tar.gz      #解压
[iyunv@sever1 mnt]# cd nagios-plugins-2.1.1
[iyunv@sever1 nagios-plugins-2.1.1]# ./config--with-nagios-user=nagios --with-nagios-group=nagios                                                #配置
[iyunv@sever1 nagios-plugins-2.1.1]# make                    #编译
[iyunv@sever1 nagios-plugins-2.1.1]# make install            #安装
B)添加本地服务
[iyunv@server1 etc]# vimnagios.cfg
1
2
3
4
  cfg_file=/usr/local/nagios/etc/objects/hosts.cfg        #添加文件
  cfg_file=/usr/local/nagios/etc/objects/services.cfg   
  # Definitions formonitoring the local (Linux) host
  #cfg_file=/usr/local/nagios/etc/objects/localhost.cfg   #标记localhost文件




[iyunv@server1 etc]# cd objects/
[iyunv@server1 objects]# cp -p localhost.cfg hosts.cfg
[iyunv@server1 objects]# cp -p localhost.cfg services.cfg
[iyunv@server1 objects]# vim hosts.cfg                       #主机配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
define host{                                               #主机
        use                     linux-server                #使用的模板
        host_name               server1.example.com         #主机名
        alias                   Manager                     #别名
        address                 172.25.29.1                 #ip
        icon_image              server.gif                  #下面几行是在拓扑图中的位置
       statusmap_image         server.gd2
        2d_coords               500,200
        3d_coords               500,200,100
        }
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
        }




[iyunv@server1 etc]# vim resource.cfg                         #服务配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
define servicegroup{                                        #服务组
        servicegroup_name系统负荷检查               
        alias 负荷检查                                
        membersserver1.example.com,进程总数,server1.example.com,登录用户数,server1.example.com,根分区,server1.example.com,交换空间利用率
        }
  
define service{                                            #服务
        use                             local-service      #使用的模板  
        host_name                       *
       service_description            PING
       check_command                  check_ping!100.0,20%!500.0,60% #要传的参数
        }
  
define service{
        use                             local-service         ; Name of service template to use
        host_name                       server1.example.com
       service_description             根分区
       check_command                  check_local_disk!20%!10%!/
        }
define service{
        use                             local-service         ; Name of service template to use
        host_name                       server1.example.com
       service_description             登录用户数
       check_command                  check_local_users!20!50
        }
  
define service{
        use                             local-service         ; Name of service template to use
        host_name                       server1.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                       server1.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                       server1.example.com
       service_description             交换空间利用率
       check_command                  check_local_swap!20!10
        }
  
define service{
        use                             local-service         ; Name of service template to use
        host_name                       server1.example.com
       service_description            SSH
       check_command                  check_tcp!22!1.0!10.0
       notifications_enabled           0
        }
define service{
        use                             local-service         ; Name of service template to use
        host_name                       server1.example.com
       service_description            HTTP
       check_command                  check_http
       notifications_enabled           0
        }




[iyunv@server1 objects]# /etc/init.d/nagios reload       #刷新
Running configuration check...done.
Reloading nagios configuration...done
C)测试172.25.25.1/Nagios
wKiom1gZzLzAvX-TAAJ381IdDDw607.jpg


2.远程监控服务端2mysql及测试
  A)     安装mysql及添加授权
服务端2
[iyunv@server2 ~]# yum install mysql-server -y       #安装mysql
[iyunv@server2 ~]# /etc/init.d/mysqld start          #开启mysql
[iyunv@server2 ~]# mysql_secure_installation         #初始化,添加密码redhat
[iyunv@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,测试
[iyunv@server1 objects]# ./check_mysql -H 172.25.25.2 -u nagios-p redhat -d nagdb
  B)     将mysql服务(服务端2)添加到nagios(服务端1)
服务端1
[iyunv@server1 objects]# vim hosts.cfg                #定义主机
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
define host{
        use                     linux-server        #调用的模版
        host_name              server2.example.com  #主机名(command.cfg中要调用的参数)
        alias                    MYSQL              #别名
        address                 172.25.25.2         #ip
        icon_image              server.gif
       statusmap_image         server.gd2           #坐标的定义
        2d_coords               400,200
        3d_coords               400,200,100
        }
[iyunv@server1 objects]# vim services.cfg            #定义服务
####################check_mysql#######################
define service{
        use                             local-service      
        host_name                       server2.example.com(调用hosts.cfg中的host_name)
       service_description            MYSQL
       check_command                  check_mysql!nagios!redhat!nagdb    #command.cfg中要调用的参数(命令名字!主机名!密码!数据库的名称)
        }




[iyunv@server1 objects]# vim commands.cfg            #定义命令
1
2
3
4
5
# 'check_mysql' command definition
define command{
        command_name    check_mysql                #命令的名字
        command_line    $USER1$/check_mysql -H $HOSTADDRESS$ -u$ARG1$ -p $ARG2$ -d  $ARG3$       #调用的脚本绝对路径 -H 主机名(调用hosts.cfg中的host_name) -u mysql的用户名 -p 用户密码 -d 数据库的名称
        }




[iyunv@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
[iyunv@server1 objects]# /etc/init.d/nagios reload    #刷新
Running configuration check...done.
Reloading nagios configuration...done
C)测试
点击拓扑图,添加server2.example.com成功
wKiom1gZzL3yybEQAAF6_rgCalA523.jpg
点击主机,查看两台主机
wKioL1gZzL2yyk7cAAHg59_LZ7o376.jpg
点击服务,如图
wKioL1gZzL-SaIHcAAIzgrOAfs8964.jpg


3.远程监控服务端2的根分区和用户数
A)安装nagios插件及nrpe
服务端2
[iyunv@server2 mnt]# ls
nagios-plugins-2.1.1.tar.gz nrpe-2.15.tar.gz
[iyunv@server2 mnt]# tar zxf nagios-plugins-2.1.1.tar.gz #解压plugins插件
[iyunv@server2 mnt]# cd nagios-plugins-2.1.1
[iyunv@server2 nagios-plugins-2.1.1]# ./configure            
[iyunv@server2 nagios-plugins-2.1.1]# make                  
[iyunv@server2 nagios-plugins-2.1.1]# make install         
[iyunv@server2 mnt]# cd /usr/local/nagios/                  
[iyunv@server2 nagios]# ls
include  libexec  share
[iyunv@server2 nagios]# useradd -u 1001 nagios           #创建nagios用户
[iyunv@server2 nagios]# chown nagios.nagios * -R         #修改文件权限
[iyunv@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
[iyunv@server2 libexec]# cd /mnt/
[iyunv@server2 mnt]# tar zxf nrpe-2.15.tar.gz            #解压nrpe
[iyunv@server2 mnt]# cd nrpe-2.15
[iyunv@server2 nrpe-2.15]# ./configure                  #配置
[iyunv@server2 nrpe-2.15]# make all
[iyunv@server2 nrpe-2.15]# make install-plugin
[iyunv@server2 nrpe-2.15]# make install-daemon
[iyunv@server2 nrpe-2.15]# make install-daemon-config
[iyunv@server2 nrpe-2.15]# yum install -y xinetd
[iyunv@server2 nrpe-2.15]# make install-xinetd
[iyunv@server2 nrpe-2.15]# vim /etc/services             #添加nrpe的端口
nrpe            5666/tcp
[iyunv@server2 nrpe-2.15]# cd /etc/xinetd.d/
[iyunv@server2 xinetd.d]# vim nrpe                  #进入配置文件
15         only_from       = 172.25.25.1          #允许172.25.25.1监听
[iyunv@server2 etc]# cd /usr/local/nagios/etc/
[iyunv@server2 etc]# ls
nrpe.cfg
[iyunv@server2 etc]# vim nrpe.cfg                   #修改命令
1
221 command[check_disk]=/usr/local/nagios/libexec/check_disk -w20% -c 10% -p /




[iyunv@server2 etc]# cd ..
[iyunv@server2 nagios]# ls
bin  etc  include libexec  share
[iyunv@server2 nagios]# cd libexec/
[iyunv@server2 libexec]# ./check_disk -w 20% -c 10% -p /     #测试
DISK OK - free space: / 6222 MB (85% inode=93%);|/=1057MB;6135;6902;0;7669
[iyunv@server2 libexec]# /etc/init.d/xinetd start            #开启xinetd
Starting xinetd:                                          [  OK  ]
[iyunv@server2 libexec]# netstat -antple |grep 5666          #查看端口,开启
tcp        0     0 :::5666                    :::*                        LISTEN      0         23552      23120/xinetd        
[iyunv@server2 libexec]# scp check_nrpe172.25.25.1:/usr/local/nagios/libexec/
root@172.25.25.1's password:                               #将nrpe传给服务端1
check_nrpe                                   100%   75KB 75.0KB/s   00:00  
B) 将根分区和用户数(服务端2)添加到nagios(服务端1)  
服务端1
[iyunv@server1 libexec]# pwd
/usr/local/nagios/libexec
[iyunv@server1 libexec]# chown nagios.nagios check_nrpe      #修改权限
[iyunv@server1 libexec]# ./check_nrpe -H 172.25.25.2        #测试
NRPE v2.15
[iyunv@server1 libexec]# cd ..
[iyunv@server1 nagios]# cd etc/objects/
[iyunv@server1 objects]# vim services.cfg                   #添加服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
####################check_nrpe#######################
define service{
        use                             local-service      
        host_name                       server2.example.com
       service_description             根分区
       check_command                  check_nrpe!check_disk
        }
  
define service{
        use                             local-service        
        host_name                       server2.example.com
       service_description             登录用户数
       check_command                  check_nrpe!check_users
        }




[iyunv@server1 objects]# vim commands.cfg                   #添加命令
1
2
3
4
5
# 'check_nrpe' command definition
define command{
        command_name    check_nrpe
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c$ARG1$
        }




[iyunv@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
[iyunv@server1 objects]# /etc/init.d/nagios reload           #刷新
C)测试
wKiom1gZzMDitvdLAAKTUzkmO-s130.jpg

4.nagios警报的配置及测试(邮件联系人)
[iyunv@server1 objects]# vim contacts.cfg                           #修改邮件地址
1
35         email                           xxxxxxxxxx@qq.com




[iyunv@server1 objects]# /etc/init.d/nagios reload                  #刷新
[iyunv@server1 ~]$ cd /mnt/
[iyunv@server1 mnt]$ ls
alert-agent-4.1.3.1-linux-x64.tar.gz
[iyunv@server1 mnt]$ tar zxfalert-agent-4.1.3.1-linux-x64.tar.gz -C /usr/local/nagios/libexec/
[iyunv@server1 mnt]$ cd /usr/local/nagios/libexec/
[iyunv@server1 libexec]$ cpalert-agent/plugin/nagios-plugin/nagios .
[iyunv@server1 libexec]$ cpalert-agent/plugin/nagios-plugin/110monitor.cfg ../etc/objects/
[iyunv@server1 libexec]$ chmod +x nagios
[iyunv@server1 libexec]$ chmod nagios.nagios * -R
[iyunv@server1 libexec]$ cd ..
[iyunv@server1 nagios]# cd etc/
[iyunv@server1 etc]# vim nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/110monitor.cfg      #添加
[iyunv@server1 etc]# cd objects/
[iyunv@server1 objects]# vim 110monitor.cfg
1
18         pager                           379cd8ca-5700-e2ef-10a2-c72a9158b55a                            #改成自己生成的key




[iyunv@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
[iyunv@server1 objects]# /etc/init.d/nagios reload

  B)测试  当把服务端2的mysql停掉之后,如图:
wKiom1gZzMGReI2ZAAJxr2jXF1g813.jpg
邮件也发过来了,如图
wKioL1gZzMOSlD88AADEXcokqj8150.jpg



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-294906-1-1.html 上篇帖子: nagios通过check_http监控tomcat 下篇帖子: nagios使用check_traffic.sh监控网卡流量 监控 主机
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表