nagios 超级详细的笔记(本人总结大量心血)

回复 收藏
本帖最后由 summer123 于 2016-6-22 16:44 编辑

nagios的史上最全的笔记,哈哈哈,分享给大家,共同学习。本人花了大量时间总结。
l 硬件配置说明
Nagios 服务端IP 192.168.91.147  主机名字nagios_service

Nagios 客户端IP 192.168.91.140
Nagios 客户端IP 192.168.91.141

Ø Nagios 服务端安装:
参考:nagios服务端安装文档.txt

安装好Nagios的服务端:打开浏览器:IP/nagios  用户是oldboy密码是123456
到此为止nagios的服务端安装完毕也是正常启动。

Ø Nagios 客户端安装:
参考:nagios客户端安装文档.txt
到此nagios的客户端安装完毕

常见错误:
Ø 服务端nagios的目录说明:
[root@bogon nagios]# ll
total 32
drwxrwxr-x.  2 nagios nagios 4096 May 18 06:52 bin # 命令目录
drwxrwxr-x.  3 nagios nagios 4096 May 18 06:52 etc #配置文件
drwxr-xr-x.  2 root   root   4096 May 18 06:52 include #包含文件
drwxrwxr-x.  2 nagios nagios 4096 May 18 06:52 libexec #插件
drwxr-xr-x.  5 root   root   4096 May 18 06:52 perl
drwxrwxr-x.  2 nagios nagios 4096 May 18 06:48 sbin #一些命令
drwxrwxr-x. 11 nagios nagios 4096 May 18 06:52 share #web程序
drwxrwxr-x.  5 nagios nagios 4096 May 18 07:47 var # 日志
[root@bogon nagios]# pwd
/usr/local/nagios
[root@bogon bin]# tree
.
|-- nagios #nagios的启动程序
|-- nagiostats
`-- nrpe
[root@bogon etc]# tree
.
|-- cgi.cfg
|-- htpasswd.users #web密码的验证文件
|-- nagios.cfg #主配置文件相当于httpd.conf/nginx.conf
|-- nrpe.cfg #客户端的配置文件,客户端agent配置,若是不把自身当做客户端就不用配置
|-- objects #相当于apacheextra包含目录
|   |-- commands.cfg
|   |-- contacts.cfg
|   |-- localhost.cfg
|   |-- printer.cfg
|   |-- switch.cfg
|   |-- templates.cfg
|   |-- timeperiods.cfg
|   `-- windows.cfg
`-- resource.cfg
[root@bogon objects]# tree /usr/local/nagios/etc/objects
.
|-- commands.cfg
|-- contacts.cfg
|-- localhost.cfg
|-- printer.cfg
|-- switch.cfg
|-- templates.cfg
|-- timeperiods.cfg
`-- windows.cfg


[root@bogon libexec]# tree
.
|-- check_apt
|-- check_breeze
|-- check_by_ssh
|-- check_clamd -> check_tcp
|-- check_cluster
|-- check_dhcp
|-- check_dig
省略。。主要是一些插件目录
[root@bogon sbin]# tree
.
|-- avail.cgi
|-- cmd.cgi
|-- config.cgi
|-- extinfo.cgi
|-- histogram.cgi
|-- history.cgi
|-- notifications.cgi
|-- outages.cgi
|-- showlog.cgi
|-- status.cgi
|-- statusmap.cgi
|-- statuswml.cgi
|-- statuswrl.cgi
|-- summary.cgi
|-- tac.cgi
`-- trends.cgi
主要是一些cgi的程序

Sharenagiox的界面的展示的php程序的等内容的目录,站点目录
[root@bogon share]# tree
.
|-- config.inc.php
|-- contexthelp
|   |-- A1.html
|   |-- A2.html
|   |-- A3.html
|   |-- A4.html
|   |-- A5.html
省略。。主要是一些插件目录

Varnagios的数据及日志目录
[root@bogon var]# tree
.
|-- archives
|-- nagios.lock
|-- nagios.log
|-- objects.cache
|-- retention.dat
|-- rw
|-- spool
|   `-- checkresults
`-- status.dat
Ø 配置主配置文件
服务端:
1、备份
[root@bogon nagios]# tar zcvf etc.tar.gz ./etc/
生成一个备份etc.tar.gz文件
2、修改配置文件
[root@bogon etc]# vim nagios.cfg +34
增加入下主机和服务的配置文件
cfg_file=/usr/local/nagios/etc/objects/services.cfg
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_dir=/usr/local/nagios/etc/services
#作为备用增加一个services目录,使用目录的有点很多,在目录下的文件只要符合*.cfg就可以被nagios加载,使用脚本批量部署时候非常方便的随机命名配置文件
Localhost这个配置为监控nagios服务端本地服务的配置文件,我们本机也作为客户端,不当做特殊机器。然后统一监控。
  操作完毕后,保存nagios.cfg并建立目录。且对其进行授权
[root@bogon etc]# mkdir -p /usr/local/nagios/etc/services
[root@bogon etc]# chown -R nagios.nagios /usr/local/nagios/etc/services

l 生成hosts.cfg文件。(通过localhost.cfg 模板来生成)
[root@bogon objects]# cd /usr/local/nagios/etc/objects
[root@bogon objects]# head -51 localhost.cfg >hosts.cfg
[root@bogon objects]# chown nagios.nagios /usr/local/nagios/etc/objects/hosts.cfg
l 生成services.cfg文件
[root@bogon objects]# touch  services.cfg
[root@bogon objects]# chown nagios.nagios services.cfg

配置服务2016521日星期六
Ø 配置主机服务
[root@nagios_service ~]# cd /usr/local/nagios/etc/objects/
[root@bogon objects]# vim hosts.cfg
define host{
        use                     linux-server #模板,在模板中定义
        host_name               localhost  #被监控的主机名字
        alias                    localhost
        address                 127.0.0.1  #被监控的ip地址
        }
改为:
define host{
        use                     linux-server
        host_name               128_nagios_client01  #被监控主机+ hostname
        alias                     128_nagios_client01  #保持和host_name一样即可。没多大意义
        address                  192.168.91.128     #被监控主机ip
        }
在下面在添加一个客户端也就是监控本地:
define host{
        use                     linux-server
        host_name               147_server
        alias                    147_server
        address                  192.168.91.147
        }
同时候把 host_name添加在hostgroup里面。也就是如下:
define hostgroup{
        hostgroup_name  linux-servers ; The name of the hostgroup
        alias            Linux Servers ; Long name of the group
        members         128_nagios_client01,147 _server  #用逗号隔开
        }
最后添加命令。否则会报错,找不到该命令
[root@nagios_service objects]# vim /usr/local/nagios/etc/objects/commands.cfg
其实这个命令的最后就是类似 /usr/local/nagios/libexec/check_nrpe -H 192.168.91.128  -c check_disk这个原理
[root@nagios_service objects]# /usr/local/nagios/libexec/check_nrpe -H 192.168.91.128  -c check_disk
DISK OK - free space: / 184413 MB (98% inode=99%);| /=2785MB;157778;181445;0;197223
192.168.91.128是要被监控的主机IP

其中 linux-server在模板中定义也就是在/usr/local/nagios/etc/objects/templates.cfg中定义的
这个就是一个被监控的主机,
检查语法:
[root@localhost objects]# /etc/init.d/nagios checkconfig
Running configuration check... CONFIG ERROR!  Check your Nagios configuration.
或者以下也检查语法错误,更详细显示
[root@localhost objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
但是为了能让 /etc/init.d/nagios checkconfig输出更详细,修改配置文件即可

修改后配置在重新加载配置文件。在检测语法,直到不报错为止:
[root@nagios_service objects]# /etc/init.d/nagios
Usage: nagios {start|stop|restart|reload|force-reload|status|checkconfig}
[root@nagios_service objects]# /etc/init.d/nagios reload
Running configuration check...done.
Stopping nagios: /etc/init.d/nagios: line 72: kill: (22864) - No such process
done.
Starting nagios: done.
[root@nagios_service objects]# /etc/init.d/nagios checkconfig
Running configuration check...
Nagios Core 3.5.1
Copyright (c) 2009-2011 Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 08-30-2013
License: GPL
Reading configuration data...
   Read main config file okay...
Processing object config file '/usr/local/nagios/etc/objects/commands.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/contacts.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/timeperiods.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/templates.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/services.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/hosts.cfg'...
Processing object config directory '/usr/local/nagios/etc/services'...
   Read object config files okay...
Running pre-flight check on configuration data...
Checking services...
        Checked 1 services.
Checking hosts...
Warning: Host '147_nagios_server' has no services associated with it!
        Checked 2 hosts.
Checking host groups...
        Checked 1 host groups.
Checking service groups...
        Checked 0 service groups.
Checking contacts...
        Checked 1 contacts.
Checking contact groups...
        Checked 1 contact groups.
Checking service escalations...
        Checked 0 service escalations.
Checking service dependencies...
        Checked 0 service dependencies.
Checking host escalations...
        Checked 0 host escalations.
Checking host dependencies...
        Checked 0 host dependencies.
Checking commands...
        Checked 25 commands.
Checking time periods...
        Checked 5 time periods.
Checking for circular paths between hosts...
Checking for circular host and service dependencies...
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...
Total Warnings: 1
Total Errors:   0  #没有在报错
Things look okay - No serious problems were detected during the pre-flight check
OK.
[root@nagios_service objects]# /etc/init.d/nagios reload #平滑重启
Running configuration check...done.
Reloading nagios configuration...done
平时我们只关注hostsservices。点击hosts发现报错。继续解决错误:
It appears as though you do not have permission to view information for any of the hosts you requested...
If you believe this is an error, check the HTTP server authentication requirements for accessing this CGI
and check the authorization options in your CGI configuration file.
原因是没有权限。nagios默认的用户nagiosadmin,而我们用的是oldboy,所以修改成oldboy即可。
[root@nagios_service etc]# vim /usr/local/nagios/etc/cgi.cfg
把以下的
authorized_for_all_services=nagiosadmin
authorized_for_all_ hosts =nagiosadmin
修改为:
authorized_for_all_ services =oldboy
authorized_for_all_hosts=oldboy
保存。F5网页。


现在把客户端的其他服务也加进来:
查看客户端上其他的检查项目:
客户端:
[root@nagios_client01 etc]# cat /usr/local/nagios/etc/nrpe.cfg
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,6 -c 30,25,20
command[check_mem]=/usr/local/nagios/libexec/check_memory.pl -w 6% -c 3%
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 8% -p /
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
command[check_iostat]=/usr/local/nagios/libexec/check_iostat -w 6 -c 10
其他省略。
其实就是在服务端的service主机配置文件添加即可,编辑服务端的主配置文件:
服务端:
[root@nagios_service objects]# vim /usr/local/nagios/etc/objects/services.cfg
cat /usr/local/nagios/etc/objects/services.cfg
define service {
       use                                   generic-service
       host_name                             128_nagios_client01
       service_description                       Disk Partition
       check_command                         check_nrpe!check_disk
}
define service {
       use                                   generic-service
       host_name                             128_nagios_client01
       service_description                       Disk load
       check_command                         check_nrpe!check_load
}
define service {
       use                                   generic-service
       host_name                             128_nagios_client01
       service_description                      Disk disk
       check_command                         check_nrpe!check_disk
}
define service {
       use                                   generic-service
       host_name                             128_nagios_client01
       service_description                       Disk memory
       check_command                         check_nrpe!check_mem
}
define service {
       use                                   generic-service
       host_name                             128_nagios_client01
       service_description                       Disk swap
       check_command                         check_nrpe!check_swap
}
define service {
       use                                   generic-service
       host_name                             128_nagios_client01
       service_description                       Disk iostat
       check_command                         check_nrpe!check_iostat
}
备注:
check_nrpe是 服务端的/usr/local/nagios/etc/objects/commands.cfgcommad中命令。

check_iostat是客户端的/usr/local/nagios/etc/nrpe.cfg的红色部分如下
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,6 -c 30,25,20
command[check_mem]=/usr/local/nagios/libexec/check_memory.pl -w 6% -c 3%
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 8% -p /
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
command[check_iostat]=/usr/local/nagios/libexec/check_iostat -w 6 -c 1
检查配置文件:
[root@nagios_service objects]# /etc/init.d/nagios checkconfig
重新加载配置文件:
[root@nagios_service objects]# /etc/init.d/nagios reload
刷新页面:
此时需要等待一会,数据才能刷新。
到目前为止所有数据都出来了
常见问题:
Nagios主程序调用check_nrpe!check_iostat 其实就是调用$USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
而其中$HOSTADDRESS$ nagios固定语法,宏,也就是被监控主机名,
$ARG1$nagios固定语法,宏,也就是调用监控什么即是check_iostat
所有界面是不是OK,需要保证后台执行的
[root@nagios_service libexec]# ./check_nrpe -H 192.168.91.128 -c check_iostat
IOSTAT OK - user 0.04 nice 0.00 sys 0.11 iowait 0.24 idle 0.00  | iowait=0.24%;; idle=0.00%;; user=0.04%;; nice=0.00%;; sys=0.11%;;
是不是OK。才能保证界面是否OK
最后查到是nrpe没启动,且客户端没添加主机:
客户端:
[root@nagios_client01 etc]# vim /usr/local/nagios/etc/nrpe.cfg
allowed_hosts=127.0.0.1,192.168.91.147 #添加的服务端的主机ip,否则握手失败

总结:
1、客户端和服务端nrpe服务是否启动
2、客户端nrep.cfg是否添加的主机ip
3、客户端nrep.cfg是否添加的相对应的命令参数


Ngaios被动模式
[root@nagios_service libexec]# ./check_nrpe --help

NRPE Plugin for Nagios
Copyright (c) 1999-2008 Ethan Galstad (nagios@nagios.org)
Version: 2.12
Last Modified: 03-10-2008
License: GPL v2 with exemptions (-l for more info)
SSL/TLS Available: Anonymous DH Mode, OpenSSL 0.9.6 or higher required
Usage: check_nrpe -H [-n] [-u] [-p ] [-t ] [-c ] [-a ]
#-H 写客户端的主机
#-c 写客户端的配置文件的命令的名字
Options:
-n         = Do no use SSL
-u         = Make socket timeouts return an UNKNOWN state instead of CRITICAL
      = The address of the host running the NRPE daemon
[port]     = The port on which the daemon is running (default=5666)
[timeout]  = Number of seconds before connection times out (default=10)
[command]  = The name of the command that the remote daemon should run
[arglist]  = Optional arguments that should be passed to the command.  Multiple
              arguments should be separated by a space.  If provided, this must be
              the last option supplied on the command line.
Note:
This plugin requires that you have the NRPE daemon running on the remote host.
You must also have configured the daemon to associate a specific plugin command
with the [command] option you are specifying here.  Upon receipt of the
[command] argument, the NRPE daemon will run the appropriate plugin command and
send the plugin output and return code back to *this* plugin.  This allows you
to execute plugins on remote hosts and 'fake' the results to make Nagios think
the plugin is being run locally.

模拟排除过程:
要在服务端排错:
被动模式排除原理。

Ø 在添加一台主机,例如添加自己,也就是自己监控自己:
[root@nagios_service objects]# cat hosts.cfg|grep host_name
        host_name               128_nagios_client01
        host_name               147_nagios_server
找到要添加的主机也就是上面的 147_nagios_server,添加在services.cfg配置文件中128_nagios_client01加逗号即可。
[root@nagios_service objects]# sed -i "s/128_nagios_client01/128_nagios_client01,147_nagios_server/g"  services.cfg
查看下:
[root@nagios_service objects]# cat !$
cat services.cfg
define service {
       use                                   generic-service
       host_name                             128_nagios_client01,147_nagios_server
       service_description                   Disk Partition
       check_command                         check_nrpe!check_disk
}
define service {
       use                                   generic-service
       host_name                             128_nagios_client01,147_nagios_server
       service_description                   Disk load
       check_command                         check_nrpe!check_load
}
define service {
       use                                   generic-service
       host_name                             128_nagios_client01,147_nagios_server
       service_description                   Disk disk
       check_command                         check_nrpe!check_disk
}
define service {
       use                                   generic-service
       host_name                             128_nagios_client01,147_nagios_server
       service_description                   Disk memory
       check_command                         check_nrpe!check_mem
}
define service {
       use                                   generic-service
       host_name                             128_nagios_client01,147_nagios_server
       service_description                   Disk swap
       check_command                         check_nrpe!check_swap
}
define service {
       use                                   generic-service
       host_name                             128_nagios_client01,147_nagios_server
       service_description                   Disk iostat
       check_command                         check_nrpe!check_iostat
}
Ø 检查配置文件是否报错:
[root@nagios_service objects]# /etc/init.d/nagios checkconfig
Ø 重新加载配置文件
[root@nagios_service objects]# /etc/init.d/nagios reload
Running configuration check...done.
Reloading nagios configuration...done
Ø 测试
    1、刷新页面
2、看结果
发现全部是有问题:
排除过程:
拿第一个Disk Partition来排错
1、查服务端
2、查客户端命令
从服务端来看没什么问题就,看客户端有没有check_disk命令。发现没有添加即可
vim /usr/local/nagios/etc/nrpe.cfg
     command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,6 -c 30,25,20
     command[check_mem]=/usr/local/nagios/libexec/check_memory.pl -w 6% -c 3%
     command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 8% -p /
     command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
     command[check_iostat]=/usr/local/nagios/libexec/check_iostat -w 6 -c 10
     修改完毕后检查配置文件,检查配置文件没有问题后重新在加载配置文件
     /etc/init.d/nagios checkconfig
     /etc/init.d/nagios reload
    3、刷新F5  网页查看即可



















主动模式监控
此类服务一般都是开启了对外服务的业务,这样的业务我们一般采用主动模式
查看客户端的web服务:
[root@nagios_client01 libexec]# lsof -i:80
服务端:
[root@nagios_service libexec]# ./check_tcp --help
Usage:
check_tcp -H host -p port [-w ] [-c ] [-s ]
[-e ] [-q ][-m ] [-d ]
[-t ] [-r ] [-M ] [-v] [-4|-6] [-j]
[-D [,]] [-S ] [-E]
Url监控实质:命令行理解监控原理:
[root@nagios_service libexec]#  ./check_tcp -H  115.28.2.103 -p80
TCP OK - 0.016 second response time on port 80|time=0.016084s;;;0.000000;10.000000
[root@nagios_service libexec]#  ./check_tcp -H  115.28.2.103 -p52113 #我的 52113ssh端口
TCP OK - 0.017 second response time on port 52113|time=0.016709s;;;0.000000;10.000000
不经过nrpe,是主动模式
[root@nagios_service libexec]# ./check_http  --help
Usage:
check_http -H  | -I  [-u ] [-p ]
       [-w ] [-c ] [-t ] [-L] [-a auth]
       [-b proxy_auth] [-f ]
       [-e ] [-s string] [-l] [-r  | -R ]
       [-P string] [-m :] [-4|-6] [-N] [-M ]
       [-A string] [-k string] [-S ] [--sni] [-C [,]]
       [-T ] [-j method]
NOTE: One or both of -H and -I must be specified
[root@nagios_service libexec]#  ./check_http -I 115.28.2.103 #实际上我们就靠这个来监控
HTTP OK: HTTP/1.1 200 OK - 12468 bytes in 0.234 second response time |time=0.234373s;;;0.000000 size=12468B;;;0


Ø 对域名,ip,文件夹,进行监控
添加要监控的服务到service.cfg
[root@nagios_service services]# vim web.cfg
#url example http://www.caimz.cn
define service {
       use                                   generic-service
       host_name                             128_nagios_client01
       service_description                        blog_url
       check_command                         check_url!-I 192.168.91.128 #IP进行监控
       max_check_attempts     2
       normal_check_interval    4
       retry_check_interval      4
       check_period           24x7
       notification_interval     1440
       notification_period      24x7
       notification_options     w,u,c,r
       contact_groups         admins
       process_perf_data       1
}
define service {
       use                                   generic-service
       host_name                             128_nagios_client01
       service_description                      blog_url_domian
       check_command                         check_url!-H caim1z1.org #对域名进行监控
       max_check_attempts     3
       normal_check_interval    2
       check_period           24x7
       notification_interval     30
       notification_period      24x7
       contact_groups         admins
}
define service {
       use                                   generic-service
       host_name                             128_nagios_client01
       service_description                       blog_url_file
       check_command                         check_url!-u /test/ #对网站跟下的文件夹进行监控
       max_check_attempts     3
       normal_check_interval    2
       check_period           24x7
       notification_interval     30
       notification_period      24x7
       contact_groups         admins
}

[root@nagios_service services]# vim /etc/hosts #提前做好解析
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.91.128 blog.caim1z1.org
~
[root@nagios_service libexec]# ./check_http -I 192.168.91.128#IP进行监控
HTTP OK: HTTP/1.1 200 OK - 2908 bytes in 0.001 second response time |time=0.001143s;;;0.000000 size=2908B;;;0
[root@nagios_service libexec]# ./check_http -H blog.caim1z1.org #对域名进行监控
HTTP OK: HTTP/1.1 200 OK - 2908 bytes in 0.001 second response time |time=0.001468s;;;0.000000 size=2908B;;;0
[root@nagios_service libexec]# ./check_http -H blog.caim1z1.org -u /test/ #对文件进行监控
HTTP OK: HTTP/1.1 200 OK - 1070 bytes in 0.002 second response time |time=0.001629s;;;0.000000 size=1070B;;;0

最后检查配置文件和重新加载配置文件
[root@nagios_service services]# /etc/init.d/nagios checkconfig
[root@nagios_service services]# /etc/init.d/nagios reload

对特殊的url使用“”括起来。
测试: 把test改名为abc看监控
[root@nagios_client01 html]# ls
1.txt  index.html  test
[root@nagios_client01 html]# mv test  abc
[root@nagios_client01 html]# ll
total 12
-rw-r--r-- 1 root root    4 May 21 04:31 1.txt
drwxr-xr-x 2 root root 4096 May 21 05:13 abc
-rw-r--r-- 1 root root 2637 May 21 04:49 index.html

Ø 对端口进行监控
其实就是
[root@nagios_service services]# cd -
/usr/local/nagios/libexec
[root@nagios_service libexec]# ./check_tcp -H 192.168.91.128 -p 22
[root@nagios_service libexec]# cd -
/usr/local/nagios/etc/services
[root@nagios_service services]# vim web.cfg
TCP OK - 0.001 second response time on port 22|time=0.000538s;;;0.000000;10.000000
define service {
       use                                   generic-service
       host_name                             128_nagios_client01
       service_description                       blog_port_80
       check_command                         check_tcp!80
       max_check_attempts     3
       normal_check_interval  2
       check_period           24x7
       notification_interval  30
       notification_period    24x7
       contact_groups         admins
}
[root@nagios_service services]# /etc/init.d/nagios checkconfig
[root@nagios_service services]# /etc/init.d/nagios reload
Running configuration check...done.
特殊;
     利用别名实现对集群下面同样节点的URL监控:
Web1  www.caimz.cn     www.caimz1.cn
Web1  www.caimz.cn     www.caimz2.cn

小结: 一般客户端对外开启的服务,用于主动监控,如porturl
主动模式的监控配置:
1、在服务端的命令行把要监控的命令先调试好
2、commands.cfg里面定义nagios的命令,同时调用命令行的插件。
3、在服务的配置文件里面定义要监控的服务,调用commands.cfg里面定义nagios的监控命令。

Ø 被动监控监控客户端:
客户端:
[root@nagios_client01 ~]# cd /usr/local/nagios/libexec/
[root@nagios_client01 libexec]# ./check_tcp -H 192.168.91.128 -p 80
TCP OK - 0.000 second response time on port 80|time=0.000172s;;;0.000000;10.000000
[root@nagios_client01 libexec]# vim  /usr/local/nagios/etc/nrpe.cfg  #最后一行添加
command[check_port_80]=/usr/local/nagios/libexec/check_tcp -H 192.168.91.128 -p 80 -w 5 -c 10
客服端测试:
[root@nagios_client01 libexec]# /usr/local/nagios/libexec/check_tcp -H 192.168.91.128 -p 80 -w 5 -c 10
TCP OK - 0.000 second response time on port 80|time=0.000147s;5.000000;10.000000;0.000000;10.000000
[root@nagios_client01 libexec]# ps -ef |grep nrpe
nagios     2219      1  0 May20 ?        00:00:03 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
root       6440   5397  0 06:12 pts/2    00:00:00 grep nrpe
[root@nagios_client01 libexec]# pkill nrpe
[root@nagios_client01 libexec]# ps -ef |grep nrpe
root       6446   5397  0 06:12 pts/2    00:00:00 grep nrpe
[root@nagios_client01 libexec]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@nagios_client01 libexec]# ps -ef |grep nrpe
nagios     6448      1  0 06:12 ?        00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
root       6450   5397  0 06:12 pts/2    00:00:00 grep nrpe

服务端命令行测试:
[root@nagios_service services]# cd /usr/local/nagios/libexec/
[root@nagios_service libexec]# ./check_nrpe -H 192.168.91.128 -c check_port_80
TCP OK - 0.000 second response time on port 80|time=0.000144s;5.000000;10.000000;0.000000;10.000000
服务端修改配置文件
[root@nagios_service libexec]# vim /usr/local/nagios/etc/services/port.cfg
最后添加
define service {
       use                                   generic-service
       host_name                             128_nagios_client01
       service_description                       blog_port_80_beidong  #
       check_command                         check_nrpe!check_port_80#客户端的命令
       max_check_attempts     3
       normal_check_interval  2
       check_period           24x7
       notification_interval  30
       notification_period    24x7
       contact_groups         admins
}
[root@nagios_service libexec]# /etc/init.d/nagios checkconfig
[root@nagios_service libexec]# /etc/init.d/nagios reload

配置模板
[root@nagios_service services]# vim port.cfg

define service {
       use                                   generic-service
       use                                   generic-service
       host_name                             128_nagios_client01
       service_description                   blog_port_80
       check_command                         check_tcp!80
}
define service {
       use                                   generic-service
       host_name                             128_nagios_client01
       service_description                   blog_port_22
       check_command                         check_tcp!22
}
define service {
       use                                   generic-service
       host_name                             128_nagios_client01
       service_description                   blog_port_873
       check_command                         check_tcp!873
}
define service {
       use                                   generic-service
       host_name                             128_nagios_client01
       service_description                   blog_port_80_beidong
       check_command                         check_nrpe!check_port_80
}
generic-service是在模板里面。
[root@nagios_service objects]# vim /usr/local/nagios/etc/objects/templates.cfg



[root@nagios_service libexec]# head -7 utils.sh
#! /bin/sh

STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4

例子:监控客户端的密码文件的变化。被动模式
[root@nagios_client01 ~]# md5sum /etc/passwd >/etc/caimz.md5
[root@nagios_client01 ~]# md5sum -c /etc/caimz.md5
/etc/passwd: OK
[root@nagios_client01 libexec]# vim /usr/local/nagios/libexec/check_passwd


#!/bin/bash
char=$(md5sum -c /etc/caimz.md5  2>/dev/null | grep "OK" | wc -l)
if [ $char -eq 1 ];then
    echo "passwd is ok"
    exit 0
else
    echo "passwd is changed"
    exit 2
Fi
测试:
[root@nagios_client01 libexec]# chmod +x check_passwd
[root@nagios_client01 libexec]# sh  /usr/local/nagios/libexec/check_passwd
passwd is ok
[root@nagios_client01 libexec]# useradd aaaaa
[root@nagios_client01 libexec]# sh  /usr/local/nagios/libexec/check_passwd
passwd is changed
[root@nagios_client01 libexec]# userdel aaaaa
[root@nagios_client01 libexec]# sh  /usr/local/nagios/libexec/check_passwd
passwd is ok
[root@nagios_client01 libexec]# useradd caimzabc
[root@nagios_client01 libexec]# sh  /usr/local/nagios/libexec/check_passwd
passwd is changed
[root@nagios_client01 libexec]# userdel caimzabc
[root@nagios_client01 libexec]# sh  /usr/local/nagios/libexec/check_passwd
passwd is ok
添加到被动模式:
[root@nagios_client01 libexec]# ll /usr/local/nagios/libexec/check_passwd
-rwxr-xr-x 1 root root 184 May 21 07:57 /usr/local/nagios/libexec/check_passwd
[root@nagios_client01 libexec]# vim ../etc/nrpe.cfg
最后添加一行
command[check_passwd]=/usr/local/nagios/libexec/check_passwd

Ø Killnrpe
[root@nagios_client01 libexec]# ps -ef |grep nrpe
nagios     6448      1  0 06:12 ?        00:00:01 /usr/local/nagios/bin/nrpe -c/usr/local/nagios/etc/nrpe.cfg -d
root       7509   7323  0 08:04 pts/1    00:00:00 grep nrpe
[root@nagios_client01 libexec]# pkill nrpe
[root@nagios_client01 libexec]# /usr/local/nagios/bin/nrpe -c/usr/local/nagios/etc/nrpe.cfg -d
[root@nagios_client01 libexec]# ps -ef |grep nrpe
nagios     7516      1  2 08:04 ?        00:00:00 /usr/local/nagios/bin/nrpe -c/usr/local/nagios/etc/nrpe.cfg -d
root       7518   7323  0 08:04 pts/1    00:00:00 grep nrpe
Ø 服务端验证
[root@nagios_service libexec]# ./check_nrpe -H 192.168.91.128 -c check_passwd
passwd is ok
修改配置文件:
[root@nagios_service libexec]# vim /usr/local/nagios/etc/objects/services.cfg
最后添加
define service {
       use                                   generic-service
       host_name                             128_nagios_client01
       service_description                       check_passwd
       check_command                         check_nrpe!check_passwd
}
[root@nagios_service libexec]# /etc/init.d/nagios checkconfig
[root@nagios_service libexec]# /etc/init.d/nagios reload
Running configuration check...done.
Reloading nagios configuration...done
测试:改变密码
客户端修改
[root@nagios_client01 libexec]# useradd caimzabz


被动模式:在客户端操作
1、写好脚本放在libexec目录下
脚本的返回值和要echo的内容
2、给脚本加上x权限,在本地测试脚本
3、nrpe里面加上你command
4、重启nrpe
5、去服务端执行看看是否可以执行,不能执行看客户端的nrpe.cfg是否有allow服务器的地址
6、services.cfg里面加入定义监控的项目调用命令服务端,检测语法
7、重启服务端看看是否可以正常工作

主动模式:在服务端操作
8、写好脚本放在libexec目录下
脚本的返回值和要echo的内容
9、给脚本加上x权限,在本地测试脚本
10、command.cfg里面定义你写的脚本
11、services.cfg里面加入定义监控的项目
12、检测语法,重启服务



nagios图形显示
l Nagios 图形监控显示和管理
1、pnp安装图像监控曲线(服务器端)
2、Yum安装pn软件需要的基本包
pnp 出图软件官方站点为:http://www.pnp4nagios.org
先执行yum安装一下基础pnp软件需要的包,如果是重复的执行下去也不会有问题:
图形显示的依赖
[root@nagios_service tools]# yum install cairo pango zlib zlib-devel freetype freetype-devel gd gd-devel -y

l Install libart_lpl by rrtdtool #轮询的数据库工具
n 安装   libart_lgpl 也是rrdtool的依赖
当然也可以yum install -y libart_lgpl  libart_lgpl-devel,这里用编译安装方式
[root@nagios_service tools]# cd /home/oldboy/tools
Nagios 工具包中已经包含
[root@nagios_service tools]# ls libart_lgpl-2.3.17.tar.gz
libart_lgpl-2.3.17.tar.gz
[root@nagios_service tools]# tar xf libart_lgpl-2.3.17.tar.gz
[root@nagios_service tools]# cd libart_lgpl-2.3.17
[root@nagios_service libart_lgpl-2.3.17]# ./configure
报错:
checking whether g++ accepts -g... no
checking how to run the C++ preprocessor... /lib/cpp
configure: error: C++ preprocessor "/lib/cpp" fails sanity check
See `config.log' for more details.
解决:出现该情况是由于c++编译器的相关package没有安装,以root用户登陆,在终端上执行:
yum install glibc-headers yum install gcc-c++
重新配置
[root@nagios_service libart_lgpl-2.3.17]# ./configure #没报错,继续
[root@nagios_service libart_lgpl-2.3.17]# make && make install
[root@nagios_service libart_lgpl-2.3.17]# /bin/cp -r /usr/local/include/libart-2.0   /usr/include
[root@nagios_service libart_lgpl-2.3.17]# cd ../
n 安装   rrdtools轮询的数据库,专门画图
[root@nagios_service tools]# ls /home/oldboy/tools/rrdtool-1.2.14.tar.gz
/home/oldboy/tools/rrdtool-1.2.14.tar.gz
[root@nagios_service tools]# tar xf rrdtool-1.2.14.tar.gz
[root@nagios_service tools]# cd rrdtool-1.2.14
[root@nagios_service rrdtool-1.2.14]# ./configure --prefix=/usr/local/rrdtool  --disable-python  --disable-tcl
#WARNING: The RRDs Perl Modules are not found on your System
#Using RRDs will speedup things in larger Installtions.
#configure后出现上面的提示可以不用理会。
[root@nagios_service rrdtool-1.2.14]# make && make install
[root@nagios_service rrdtool-1.2.14]# cd ../
[root@nagios_service tools]# ls -l /usr/local/rrdtool/bin
total 116
-rwxr-xr-x 1 root root 55657 May 21 20:48 rrdcgi
-rwxr-xr-x 1 root root  6727 May 21 20:48 rrdtool
-rwxr-xr-x 1 root root 52651 May 21 20:48 rrdupdate
出现上述三个表示配置成功

n 安装   pnp-0.4.14.tar.gz
[root@nagios_service tools]# ls /home/oldboy/tools/pnp-0.4.14.tar.gz
/home/oldboy/tools/pnp-0.4.14.tar.gz
[root@nagios_service tools]# tar zxf pnp-0.4.14.tar.gz
[root@nagios_service tools]# cd pnp-0.4.14
[root@nagios_service pnp-0.4.14]# ./configure \
> --with-rrdtool=/usr/local/rrdtool/bin/rrdtool \             #真正的出图的命令
> --with-perfdata-dir=/usr/local/nagios/share/perfdata/       #出图所用的数据
[root@nagios_service pnp-0.4.14]# make all
[root@nagios_service pnp-0.4.14]# make install
[root@nagios_service pnp-0.4.14]# make install-config
[root@nagios_service pnp-0.4.14]# make install-config
[root@nagios_service pnp-0.4.14]# ll /usr/local/nagios/libexec/ |grep process
-rwxr-xr-x  1 nagios nagios  31827 May 21 20:57 process_perfdata.pl #收集数据
问题:configure报错(一般不会发生)
checking for linker flags for loadable modules... -shared
checking for Perl Module Time::HiRes... no
configure: error: Perl Module Time::HiRes not available
解决:
yum install perl-Time-HiRes -y

l nagios出图相关基本配置
n 编辑主配置文件nagios.cfg
[root@nagios_service tools]# cd /usr/local/nagios/etc
[root@nagios_service etc]# cp nagios.cfg  nagios.cfg.bak
[root@nagios_service etc]# vim nagios.cfg +835
# Values: 1 = process performance data, 0 = do not process performance data
2016-06-22 16:40 举报
已邀请:
0

乐橙306

赞同来自:


互相学习   {:7_184:}
0

summer123

赞同来自:


可以,但是我的word附件太大了,不给上传。咋整
0

summer123

赞同来自:


有cacti的比较全的笔记共享吗
0

乐橙306

赞同来自:

summer123 发表于 2016-6-22 17:45
可以,但是我的word附件太大了,不给上传。咋整

1.多分几份    分开上传

2.修改下 写简单点   
0

乐橙306

赞同来自:

0

summer123

赞同来自:

乐橙306 发表于 2016-6-22 17:56
1.多分几份    分开上传

2.修改下 写简单点

我的是一套的,一个server 一个client,分别安装,整个一套,还包括脚本等等
0

summer123

赞同来自:

乐橙306 发表于 2016-6-22 17:57
cacti
http://note.youdao.com/yws/public/redirect/share?id=50e6e18eb58b4dbe1869057eec628474&type=t ...

我晚上整理下
0

summer123

赞同来自:

乐橙306 发表于 2016-6-22 17:57
cacti
http://note.youdao.com/yws/public/redirect/share?id=50e6e18eb58b4dbe1869057eec628474&type=t ...

我晚上整理下
0

66259568

赞同来自:

挺详细的
0

小熊

赞同来自:

很长,先留着,过段时间看
0

liqian

赞同来自:

赞一个
0

consam

赞同来自:

可以发一份完整的来 邮箱么,谢谢  13126965#qq.com

0

記噫搁浅

赞同来自:

真得很详细  谢谢分享

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
  • 评分区间
  • 学分
  • -30 ~ 30
可选评分理由: