|
Install nagios manual1. Install OS2. Software package no full install, according system default to install software packages.3. Assign to IP address4. Set up YUM source4.1 [root@localhost ~]# cd /etc/yum.repos.d/4.2 Set up mount directory[root@localhost ~]# mkdir /aaa直接挂载光盘,不用查看[root@localhost ~]# mount /dev/cdrom /aaa/查看挂载好的光盘[root@localhost ~]# mount[root@localhost mnt]# df -hFilesystem >/dev/mapper/VolGroup00-LogVol00 11G 1.9G 8.1G 19% //dev/sda1 99M 12M 82M 13% /boottmpfs 420M 0 420M 0% /dev/shm/dev/hdc 2.8G 2.8G 0 100% /mnt4.3 编辑yum配置文件[root@localhost yum.repos.d]# cp -rap rhel-debuginfo.repo rhel-debuginfo.repo.bk[root@localhost Server]# vim /etc/yum.repos.d/rhel-debuginfo.repo[rhel-suibianqiming]name=Sui bian qi mingbaseurl=file:///aaa/Serverenabled=1gpgcheck=04.4 YUM source 已经安装成功,下面可以用YUM命令安装所有光盘里面的文件了4.5 清空yum缓存[root@localhost yum.repos.d]# yum clean allLoaded plugins: rhnplugin, securityCleaning up Everything[root@localhost yum.repos.d]4.6 [root@localhost yum.repos.d]# yum update –y4.7 查看已安装的软件包和未安装的软件包[root@localhost Server]# yum grouplist4.8 安装Available Groups:下面的所有软件包:Available Groups: Authoring and Publishing DNS Name Server Development Libraries Engineering and Scientific GNOME Desktop Environment GNOME Software Development Java Development KDE (K Desktop Environment) KDE Software Development MySQL Database News Server OpenFabrics Enterprise Distribution PostgreSQL Database Web Server X Software DevelopmentDone4.9 安装举例[root@localhost Server]# yum groupinstall –y "X Software Development"4.10接下来安装所有你需要的可安装软件包。5. 以rpm方式安装LAMP环境6. [root@localhost yum.repos.d]# yum install *gd*7. [root@localhost yum.repos.d]# yum install -y *http*8. [root@localhost yum.repos.d]# yum install -y *mysql*9. [root@localhost yum.repos.d]# yum install -y *php*10. [root@localhost yum.repos.d]# yum install *perl* -y11. [root@localhost yum.repos.d]# yum install -y *zlib*12. [root@localhost yum.repos.d]# yum install *lib* -y13. [root@localhost nagios-3.2.0]# yum install *c++*14. [root@localhost nagios-3.2.0]# yum install *gcc*15. [root@localhost yum.repos.d]# yum install gcc gcc-c++ wget bison mysql-devel mysql-server php php-mysql php-pear php-pear-DB php-mbstring nano tftp-server httpd make ncurses-devel libtermcap-devel sendmail sendmail-cf caching-nameserver sox newt-devel libxml2-devel libtiff-devel php-gd audiofile-devel gtk2-devel subversion kernel-devel –y16. [root@localhost yum.repos.d]# yum install e2fsprogs-devel keyutils-libs-devel krb5-devel libogg libselinux-devel libsepol-devel libxml2-devel libtiff-devel gmp php-pear php-pear-DB php-gd php-mysql php-pdo kernel-devel ncurses-devel audiofile-devel libogg-devel openssl-devel mysql-devel zlib-devel perl-DateManip sendmail-cf sox –y17. [root@localhost yum.repos.d]# yum install *apr* -y18. [root@localhost yum.repos.d]# yum install freetype*19. [root@localhost yum.repos.d]# mkdir /var/lib/mysql20. [root@localhost yum.repos.d]# chown -R mysql.mysql /var/lib/mysql21. [root@localhost yum.repos.d]# /etc/init.d/httpd start22. [root@localhost yum.repos.d]# chkconfig --level 35 httpd on23. [root@localhost yum.repos.d]# mysql_install_db24. [root@localhost yum.repos.d]# chown -R mysql.mysql /var/lib/mysql25. [root@localhost yum.repos.d]# /etc/init.d/mysqld start26. Starting MySQL: [ OK ]27. [root@localhost yum.repos.d]# mysqladmin -uroot password 123456 \\设置mysql密码为12345628. [root@localhost yum.repos.d]# cp -rap /usr/share/doc/mysql-server-5.0.77/my-medium.cnf /etc/my.cnf29. [root@localhost yum.repos.d]# /etc/init.d/httpd restart30. Stopping httpd: [ OK ]31. Starting httpd: [ OK ]32. [root@localhost yum.repos.d]# vim /var/www/html/index.php33. [root@localhost yum.repos.d]# vim /var/www/html/mysql.php34. 用浏览器访问,会出现ok的界面。35. 此时,以rpm方式安装的lamp环境已经搭建完成了!这是nagios的基础组成部分,下面进行安装。36.安装nagios服务器37. 把nagios-3.2.0.tar.gz nagios-plugins-1.4.13.tar.gz nrpe-2.12.tar.gz 这三个文件copy到 /var/local/src下。38. [root@localhost nagios-3.2.0]# useradd nagios39. [root@localhost nagios-3.2.0]# mkdir /usr/local/nagios40. [root@localhost nagios-3.2.0]# chown nagios.nagios /usr/local/nagios41. [root@localhost nagios-3.2.0]# ll /usr/local42. 43. [root@localhost src]# cd nagios-3.2.0/44. [root@localhost nagios-3.2.0]# ./configure --prefix=/usr/local/nagios45. [root@localhost nagios-3.2.0]# make all46. [root@localhost nagios-3.2.0]# make install47. [root@localhost nagios-3.2.0]# make install-init48. [root@localhost nagios-3.2.0]# make install-commandmode49. [root@localhost nagios-3.2.0]# make install-config50. [root@localhost nagios-3.2.0]# cd ..51. [root@localhost src]# ls52. nagios-3.2.0 nagios-plugins-1.4.13 nrpe-2.1253. [root@localhost src]# cd nagios-plugins-1.4.13/54. [root@localhost nagios-plugins-1.4.13]# ./configure --prefix=/usr/local/nagios/55. [root@localhost nagios-plugins-1.4.13]# make56. [root@localhost nagios-plugins-1.4.13]# make install57. [root@localhost nagios-plugins-1.4.13]# ls /usr/local/nagios/libexec/58. 会显示安装的插件文件,即所有的插件都安装在libexec这个目录下 59. 将apache的运行用户加到nagios组里面从httpd.conf中过滤出当前的apache运行用户grep ^User /usr/local/apache2/conf/httpd.conf我的是apache,下面将这个用户加入nagios组60. [root@localhost nagios-plugins-1.4.13]# grep ^User /etc/httpd/conf/httpd.confUser apache61. [root@localhost nagios-plugins-1.4.13]# usermod -G nagios apache62. 修改apache配置修改apache的配置文件,增加nagios的目录,并且访问此目录需要进行身份验证63. [root@localhost nagios-plugins-1.4.13]# vim /etc/httpd/conf/httpd.conf在最下面添加:ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbinOptions ExecCGIAllowOverride NoneOrder allow,denyAllow from allAuthName "Nagios Access"AuthType BasicAuthUserFile /usr/local/nagios/etc/htpasswd##//authenRequire valid-userAlias /nagios /usr/local/nagios/shareOptions NoneAllowOverride NoneOrder allow,denyAllow from allAuthName "Nagios Access"AuthType BasicAuthUserFile /usr/local/nagios/etc/htpasswd##//authenRequire valid-user 增加验证用户也就是通过web访问nagios的时候,必须要用这个用户登陆.在这里我们增加用户test:密码为1234564. [root@localhost nagios-plugins-1.4.13]# htpasswd -c /usr/local/nagios/etc/htpasswd test可以用 which命令查看 htpasswd命令的位置,如果是源码安装的apache这里就写htpasswd的全路径。用户是test,命令结束会提示你输入密码,这里输入123456即可。65. 查看认证文件的内容 66. [root@localhost nagios-plugins-1.4.13]# less /usr/local/nagios/etc/htpasswd67. 到这里nagios的安装也就基本完成了,你可以通过web来访问了. 下面来配置nagios在server上在Nagios里面定义了一些基本的对象,一般用到的有:联系人contact出了问题像谁报告?一般当然是系统管理员了监控时间段timeperiod7X24小时不间断还是周一至周五,或是自定义的其他时间段被监控主机host所需要监控的服务器,当然可以是监控机自己监控命令commandnagios发出的哪个指令来执行某个监控,这也是自己定义的被监控的服务service例如主机是否存活,80端口是否开,磁盘使用情况或者自定义的服务等另外,多个被监控主机可以定义为一个主机组,多个联系人可以被定义为一个联系人组,多个服务还能定义成一个服务组呢.所有这些对象绝对多数都是需要我们手动定义的,这就是nagios的安装显得复杂的地方.其实了解了原理,做一遍之后余下的工作就是复制粘贴了.下面就开始动手 68. 进入配置目录[root@localhost etc]# lscgi.cfg htpasswd nagios.cfg objects resource.cfg69. 编辑主配置文件[root@localhost etc]# vim /usr/local/nagios/etc/nagios.cfg70. 注释这一行cfg_file=/usr/local/nagios/etc/objects/localhost.cfg#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg71. 加上如下几行cfg_file=/usr/local/nagios/etc/objects/contactgroups.cfgcfg_file=/usr/local/nagios/etc/objects/hosts.cfgcfg_file=/usr/local/nagios/etc/objects/hostgroups.cfgcfg_file=/usr/local/nagios/etc/objects/services.cfg最后形成如下配置log_file=/usr/local/nagios/var/nagios.log # OBJECT CONFIGURATION FILE(S)# These are the object configuration files in which you define hosts,# host groups, contacts, contact groups, services, etc.# You can split your object definitions across several config files# if you wish (as shown below), or keep them all in a single config file. # You can specify individual object config files as shown below:cfg_file=/usr/local/nagios/etc/objects/commands.cfgcfg_file=/usr/local/nagios/etc/objects/contacts.cfgcfg_file=/usr/local/nagios/etc/objects/contactgroups.cfgcfg_file=/usr/local/nagios/etc/objects/hosts.cfgcfg_file=/usr/local/nagios/etc/objects/hostgroups.cfgcfg_file=/usr/local/nagios/etc/objects/services.cfgcfg_file=/usr/local/nagios/etc/objects/timeperiods.cfgcfg_file=/usr/local/nagios/etc/objects/templates.cfg # Definitions for monitoring the local (Linux) host#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg # Definitions for monitoring a Windows machine#cfg_file=/usr/local/nagios/etc/objects/windows.cfg # Definitions for monitoring a router/switch#cfg_file=/usr/local/nagios/etc/objects/switch.cfg # Definitions for monitoring a network printer#cfg_file=/usr/local/nagios/etc/objects/printer.cfg72. 改check_external_commands=0为check_external_commands=1 .这行的作用是允许在web界面下执行重启nagios、停止主机/服务检查等操作。 73. 把command_check_interval的值从默认的1改成command_check_interval=10s(根据自己的情况定这个命令检查时间间隔,不要太长也不要太短)。 74. 修改 CGI脚本控制文件cgi.cfg75. [root@localhost etc]# vim cgi.cfg76. 把下列项都添加上用户test,如下:authorized_for_system_information=nagiosadmin,testauthorized_for_configuration_information=nagiosadmin,testauthorized_for_system_commands=nagiosadmin,adminauthorized_for_all_services=nagiosadmin,testauthorized_for_all_hosts=nagiosadmin,testauthorized_for_all_service_commands=nagiosadmin,testauthorized_for_all_host_commands=nagiosadmin,test77. 那么上述用户名打那里来的呢?是执行命令 /usr/local/apache/bin/htpasswd –c /usr/local/nagios/etc/htpasswd test 所生成的,这个要注意,不能随便加没有存在的验证用户,为了安全起见,不要添加过多的验证用户。 78. [root@localhost etc]# cd objects/79. 编辑联系人(主要查看邮件地址是否正确,可以编辑多个联系人)[root@localhost objects]# vim contacts.cfgdefine contact{contact_name test //联系人的名称,这个地方不要有空格alias sys adminservice_notification_period 24x7host_notification_period 24x7service_notification_options w,u,c,rhost_notification_options d,u,rservice_notification_commands notify-service-by-emailhost_notification_commands notify-host-by-emailemail yahoon@test.compager 1338757xxxxaddress1 xxxxx.xyyy@icq.comaddress2 555-555-5555}创建了一个名为test的联系人,下面列出其中重要的几个选项做说明 service_notification_period 24x7服务出了状况通知的时间段,这个时间段就是上面在timeperiods.cfg中定义的.host_notification_period 24x7主机出了状况通知的时间段, 这个时间段就是上面在timeperiods.cfg中定义的service_notification_options w,u,c,r当服务出现w—报警(warning),u—未知(unkown),c—严重(critical),或者r—从异常情况恢复正常,在这四种情况下通知联系人.host_notification_options d,u,r当主机出现d­­­­—当机(down),u—返回不可达(unreachable),r—从异常情况恢复正常,在这3种情况下通知联系人service_notification_commands notify-by-email服务出问题通知采用的命令notify-by-email,这个命令是在commands.cfg中定义的,作用是给联系人发邮件.至于commands.cfg之后将专门介绍host_notification_commands host-notify-by-email同上,主机出问题时采用的也是发邮件的方式通知联系人email yahoon@test.com很明显,联系的人email地址pager 1338757xxxx联系人的手机,如果支持短信的通知的话,这个就很有用了.alias是联系人别名,address是地址 意义不大.按照上面的方式简单的复制修改就可以创建多个联系人了. 80. 定义监控时间段 ,创建配置文件timeperiods.cfg81. Vim /usr/local/nagios/etc/objects/timeperiods.cfg查找有没有如下定义:define timeperiod{timeperiod_name 24x7alias 24 Hours A Day,7Days A Weeksunday 00:00-24:00monday 00:00-24:00tuesday 00:00-24:00wednesday 00:00-24:00thursday 00:00-24:00friday 00:00-24:00saturday 00:00-24:00}下面就可以将多个联系人组成一个联系人组,创建文件contactgroups.cfg[root@localhost etc]# vi contactgroups.cfgdefine contactgroup{contactgroup_name sagroup//联系人组的名称,同样不能空格alias System Administrators //别名members test//组的成员,来自于上面定义的contacts.cfg,如果有多个联系人则以逗号相隔} 如下:define contactgroup{ contactgroup_name sagroup alias System Administrators members test,nagiosadmin }定义被监控主机,创建文件hosts.cfg[root@localhost etc]# vi hosts.cfgdefine host{host_name nagios-server//被监控主机的名称,最好别带空格alias nagios server//别名address 192.168.0.111//被监控主机的IP地址,我现在暂时先填本机的IPcheck_command check-host-alive//监控的命令check-host-alive,这个命令来自commands.cfg,用来监控主机是否存活max_check_attempts 5//检查失败后重试的次数check_period 24x7//检查的时间段24x7,同样来自于我们之前在timeperiods.cfg中定义的contact_groups sagroup//联系人组,上面在contactgroups.cfg中定义的sagroupnotification_interval 10//提醒的间隔,每隔10秒提醒一次notification_period 24x7//提醒的周期, 24x7,同样来自于我们之前在timeperiods.cfg中定义的notification_options d,u,r//指定什么情况下提醒,具体含义见之前contacts.cfg部分的介绍} 如下:define host{ host_name nagios-server alias nagios server address 30.232.120.40 check_command check-host-alive max_check_attempts 5 check_period 24x7contact_groups sagroupnotification_interval 10notification_period 24x7notification_options d,u,r }通过简单的复制修改就可以定义多个主机了.我们在这加上另外两台机器:名为dbpi的linux主机,ip为192.168.0.111名为yahoon的xp主机,ip为192.168.0.28 与联系人可以组成联系人组一样,多个主机也可以组成主机组.创建文件hostgroups.cfg[root@localhost etc]# vi hostgroups.cfgdefine hostgroup{hostgroup_name sa-servers //主机组名称alias sa Servers //别名members nagios-server//组的成员主机,多个主机以逗号相隔,必须是上面hosts.cfg中定义的}如下:define hostgroup{hostgroup_name sa-serversalias sa Serversmembers nagios-server}82.下面是最关键的了,用nagios主要是监控一台主机的各种信息,包括本机资源,对外的服务等等.这些在nagios里面都是被定义为一个个的项目(nagios称之为服务,为了与主机提供的服务相区别,我这里用项目这个词),而实现每个监控项目,则需要通过commands.cfg文件中定义的命令.83.例如我们现在有一个监控项目是监控一台机器的web服务是否正常, 我们需要哪些元素呢?最重要的有下面三点:首先是监控哪台机,然后是这个监控要用什么命令实现,最后就是出了问题的时候要通知哪个联系人?84. 定义监控的项目,也叫服务,创建services.cfg85.[root@localhost etc]# vi services.cfg#service definitiondefine service{host_name nagios-server//被监控的主机,hosts.cfg中定义的service_description check-host-alive//这个监控项目的描述(也可以说是这个项目的名称),可以空格,我们这里定义的是监控这个主机是不是存活check_command check-host-alive//所用的命令,是commands.cfg中定义的max_check_attempts 5normal_check_interval 3retry_check_interval 2check_period 24x7//监控的时间段,是timeperiods.cfg中定义的notification_interval 10notification_period 24x7//通知的时间段, ,是timeperiods.cfg中定义的notification_options w,u,c,r//在监控的结果是wucr时通知联系人,具体含义看前文.contact_groups sagroup//联系人组,是contactgroups.cfg中定义的}如下: #service definitiondefine service{ host_name nagios-server service_description check-host-alive check_command check-host-alive max_check_attempts 5 normal_check_interval 3 retry_check_interval 2 check_period 24x7 notification_interval 10 notification_period 24x7 notification_options w,u,c,r contact_groups sagroup }86.可以看到基本上所有的成员都是已经定义的.同样的将上面的内容复制修改,就可以加上另外两个监控项目:分别监控yahoon和dbpi是否存活87.这样整个的配置过程就结束了.虽然功能很简单,但是已经为以后扩展打下了良好的基础.可以放心的告诉大家,以后的工作基本都是复制粘贴啦!!!测试nagios server88. 在运行nagios之前首先做测试89./usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg90.看到下面这些信息就说明没问题了Total Warnings: 0Total Errors: 0Things look okay - No serious problems were detected during the pre-flight check91.如果有问题的话就可以按照输出信息来排查92. 作为守护进程后台启动nagios93./usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg94.切换到插件目录[root@localhost libexec]# cd /usr/local/nagios/libexec/95.这个目录有所有的插件,也就是命令,就是monitor的功能,查看帮助文档用命令:[root@localhost libexec]# ./check_disk –h96.只要是 –h就可以查看,内容很详细,举例:[root@localhost libexec]# ./check_disk -w 10% -c 5% /DISK OK - free space: / 6064 MB (59% inode=93%);| /=4077MB;9623;10158;0;10693[root@localhost libexec]# df -hFilesystem Size Used Avail Use% Mounted on/dev/mapper/VolGroup00-LogVol00 11G 4.0G 6.0G 41% //dev/sda1 99M 12M 82M 13% /boottmpfs 420M 0 420M 0% /dev/shm[root@localhost libexec]#97.这是插件的手动调用方式,下面讲monitor自动调用插件的使用方式。首先理解自动调用命令的过程:/usr/local/nagios/libexec/下面的所有文件 ------ 提供了所有的插件就是命令库文件,就是命令程序/usr/local/nagios/etc/objects/commands.cfg --- 提供了如何使用这个命令,就是把所有参数都加上,就是monitor具体怎么用这条命令来执行操作,比如说:define command{command_name check-host-alivecommand_line $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 1这里有命令名和命令执行方式。最后就是谁来调用这个命令了,就在services.cfg这个文件里调用这个命令名称啦。看下图:services.cfg定义监控项目用某个命令↓这个命令必须在commands.cfg中定义↓定义这个命令时使用了libexec下的插件98.下面监控nagios-server 的ftp编辑services.cfg 增加下面的内容,基本上就是copy上节我们定义监控主机存活的代码.略做修改.define service{host_name nagios-server要监控的机器,给出机器名,注意必须是hosts.cfg中定义的service_description check ftp给这个监控项目起个名字吧,任意起,你自己懂就行check_command check_ftp所用的命令,当然必须是commands.cfg中定义了的max_check_attempts 5normal_check_interval 3retry_check_interval 2check_period 24x7notification_interval 10notification_period 24x7notification_options w,u,c,rcontact_groups sagroup}99. 监控dbpi的sshdefine service{host_name dbpi意义同上service_description check-ssh意义同上check_command check_tcp!22ssh所用的tcp的22号端口,我就用commands中定义的check_tcp命令.至于!22的意思不用我说了吧.max_check_attempts 5normal_check_interval 3retry_check_interval 2check_period 24x7notification_interval 10notification_period 24x7notification_options w,u,c,rcontact_groups sagroup}100. 下面自己定义一个,根据上面的关系表。就以查看http服务举个例子。101. [root@localhost objects]# vim commands.cfg102. 找到相关字段:# 'check_http' command definitiondefine command{ command_name check_http command_line $USER1$/check_http -I $HOSTADDRESS$ $ARG1$ }发现名字是check_http用法得到/usr/local/nagios/libexec/目录下执行命令:[root@localhost libexec]# ./check_http –h可以查看到具体用法,规则如下:我现在来独立执行它,例如查看根分区的使用情况,执行[root@server1 libexec]# ./check_disk -w 10% -c 5% /命令的含义是检查分区/的使用情况,若剩余10%以下,为警告状态(warning),5%以下为严重状态(critical),执行后我们会看到下面这条信息DISK WARNING - free space: / 487 MB (6% inode=78%);| /=7449MB;7524;7942;0;8361说明当前是warning的状态,空闲空间只有6%了.如果nagios收到这些状态结果就会采取报警等措施了 或许在这里大家又迷糊了,我们在定义某个监控项目时,所用的监控命令都是来自commands.cfg的,这和这些插件有什么关系???想到了吧,commands.cfg中定义的监控命令就是使用的这些插件.举个例子,之前我们已经不止一次用到了check-host-alive这个命令,打开commands.cfg就可以看到这个命令的定义,如下:################################################################################## SAMPLE HOST CHECK COMMANDS################################################################################## This command checks to see if a host is "alive" by pinging it# The check must result in a 100% packet loss or 5 second (5000ms) round trip# average time to produce a critical error.# Note: Only one ICMP echo packet is sent (determined by the '-p 1' argument)# 'check-host-alive' command definitiondefine command{command_name check-host-alivecommand_line $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 1}command_name check-host-alive这句话的意思是定义的命令名是check-host-alive,也就是我们在services.cfg中使用的名称执行的操作是$USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 1其中$USER1$是在resource.cfg文件中定义的,代表插件的安装路径.就如我们上面看到的那样$USER1$=/usr/local/nagios/libexec,至于$HOSTADDRESS$,则默认被定义为监控主机的地址.简单的说,我们在services.cfg中定义了对dbpi执行check-host-alive命令,实际上就是执行了/usr/local/nagios/libexec/ check_ping -H dbpi 的ip地址 -w 3000.0,80% -c 5000.0,100% -p 1实际上check-host-alive只是这一长串命令的简称而已,而在services.cfg中都是使用简称的.在commands.cfg中定义了很多这样的命令简称.基本上我们常用的监控项目都包含了,例如ftp,http,本地的磁盘,负载等等.我们再看一个命令,check_local_disk定义如下# 'check_local_disk' command definitiondefine command{command_name check_local_diskcommand_line $USER1$/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$}check_local_disk实际上是执行的check_disk插件.这里的$ARG1$, $ARG2$, $ARG3$是什么意思呢?在之前我们已经提到了这个check_disk这个插件的用法,-w的参数指定磁盘剩了多少是警告状态,-c的参数指定剩多少是严重状态,-p用来指定路径.在使用check-host-alive的时候,只需要在services.cfg中直接写上这个命令名check-host-alive.后面没任何的参数.而使用check_local_disk则不同,在services.cfg中这要这么写check_local_disk!10%!5%!/在命令名后面用!分隔出了3个参数,10%是$ARG1$的值,5%是$ARG2$的值,/ 是$ARG3$的值, 103. [root@localhost objects]# vim services.cfg104. 随便复制一段,然后修改,比如复制这一段:define service{ host_name nagios-server service_description check ftp check_command check_ftp max_check_attempts 5 normal_check_interval 3 retry_check_interval 2 check_period 24x7 notification_interval 10 notification_period 24x7 notification_options w,u,c,r contact_groups sagroup }105. 根据每一项逐一修改。106. 命令调用就在check_command ,这一字段,最后改成:define service{ host_name nagios-server service_description check http check_command check_http max_check_attempts 5 normal_check_interval 3 retry_check_interval 2 check_period 24x7 notification_interval 10 notification_period 24x7 notification_options w,u,c,r contact_groups sagroup }107. 重启服务108. [root@localhost objects]# ps aux | grep nagiosnagios 2555 0.0 0.1 12908 1200 ? Ssl 20:53 0:01 /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg109. [root@localhost objects]# kill 2555110. [root@localhost objects]# /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg111. 过几分钟在查看服务,就会多出刚才定义的那几个服务了,yeah!!!112. 这样一个自动监控http的服务就添加好了,根据如上描述可以添加所有服务。通过nrpe获得本地信息113. 在nagios server上安装check_nrpe插件, 在客户机上安装nrpe插件和nagios-plugs114. 在被监控主机上 115. 增加用户116. [root@dbpi root]# useradd nagios117. 设置密码118. [root@dbpi root]# passwd nagios119. [root@localhost src]# cd nagios-plugins-1.4.13/120. [root@localhost nagios-plugins-1.4.13]# ./configure121. [root@localhost nagios-plugins-1.4.13]# make122. [root@localhost nagios-plugins-1.4.13]# make install123. 这一步完成后会在/usr/local/nagios/下生成两个目录libexec和share 124. [root@localhost nagios-plugins-1.4.13]# ls /usr/local/nagios/libexec share125. 修改目录权限 126. [root@localhost nagios-plugins-1.4.13]# chown nagios.nagios /usr/local/nagios/127. [root@localhost nagios-plugins-1.4.13]# chown -R nagios.nagios /usr/local/nagios/libexec/128. 安装nrpe129. [root@localhost src]# cd nrpe-2.12/130. [root@localhost nrpe-2.12]# ./configure131. [root@localhost nrpe-2.12]# make all132. [root@localhost nrpe-2.12]# make install-plugin133. [root@localhost nrpe-2.12]# make install-daemon134. [root@localhost nrpe-2.12]# make install-daemon-config135. 现在再查看nagios目录就会发现有4个目录了 136. [root@localhost nrpe-2.12]# ls /usr/local/nagios/bin etc libexec share137. [root@localhost nrpe-2.12]# make install-xinetd138. [root@localhost nrpe-2.12]# vim /etc/xinetd.d/nrpe139.# default: on140.# description: NRPE (Nagios Remote Plugin Executor)141.service nrpe142.{143.flags = REUSE144.socket_type = stream145.port = 5666146.wait = no147.user = nagios148.group = nagios149.server = /usr/local/nagios/bin/nrpe150.server_args = -c /usr/local/nagios/etc/nrpe.cfg --inetd151.log_on_failure += USERID152.disable = no153.only_from = 127.0.0.1在后面增加监控主机的地址0.111,以空格间隔154.}改后only_from = 127.0.0.1 30.203.222.90155. 保存退出。156. 编辑 /etc/services文件,增加NRPE服务157. [root@localhost nrpe-2.12]# vim /etc/services158. 增加如下# Local servicesnrpe 5666/tcp # nrpe159. 重启xientd服务[root@localhost nrpe-2.12]# service xinetd restartStopping xinetd: [ OK ]Starting xinetd: [ OK ]160. 查看 NRPE是否已经启动[root@localhost nrpe-2.12]# netstat -at | grep nrpegetnameinfo failedgetnameinfo failedtcp 0 0 *:nrpe *:* LISTEN [root@localhost nrpe-2.12]# netstat -an | grep 5666tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN [root@localhost nrpe-2.12]#161. 测试nrpe插件是否正常工作162. [root@localhost nrpe-2.12]# /usr/local/nagios/libexec/check_nrpe -H localhostNRPE v2.12163. 返回版本信息则证明: 在本地用check_nrpe连接 nrpe daemon是正常的。164. /usr/local/nagios/libexec/check_nrpe –h查看这个命令的用法可以看到用法是check_nrpe –H 被监控的主机 -c要执行的监控命令注意:-c后面接的监控命令必须是nrpe.cfg文件中定义的.也就是NRPE daemon只运行nrpe.cfg中所定义的命令165. 查看 NRPE的监控命令(文件写出了他所能执行的命令,以及命令名)166. [root@localhost nrpe-2.12]# cd /usr/local/nagios/etc/167. [root@localhost etc]# vim nrpe.cfg找到下面这段话# The following examples use hardcoded command arguments...command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/hda1command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Zcommand[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200168. 红色部分是命令名,也就是check_nrpe 的-c参数可以接的内容,等号=后面是实际执行的插件程序(这与commands.cfg中定义命令的形式十分相似,只不过是写在了一行).也就是说check_users就是等号后面/usr/local/nagios/libexec/check_users -w 5 -c 10的简称.169. 我们可以很容易知道上面这5行定义的命令分别是检测登陆用户数,cpu负载,hda1的容量,僵尸进程,总进程数.各条命令具体的含义见插件用法(执行”插件程序名 –h”)170. 由于-c后面只能接nrpe.cfg中定义的命令,也就是说现在我们只能用上面定义的这五条命令.我们可以在本机实验一下.执行/usr/local/nagios/libexec/check_nrpe -H localhost -c check_users/usr/local/nagios/libexec/check_nrpe -H localhost -c check_load/usr/local/nagios/libexec/check_nrpe -H localhost -c check_hda1/usr/local/nagios/libexec/check_nrpe -H localhost -c check_zombie_procs/usr/local/nagios/libexec/check_nrpe -H localhost -c check_total_procs在监控主机上(nagios server) 171. 安装check_nrpe插件172. 在commands.cfg中创建check_nrpe的命令定义,因为只有在commands.cfg中定义过的命令才能在services.cfg中使用 173. 创建对被监控主机的监控项目 174. [root@localhost src]# cd nrpe-2.12/175. [root@localhost nrpe-2.12]# ./configure176. [root@localhost nrpe-2.12]# make all177. [root@localhost nrpe-2.12]# make install-plugin178. 在监控主机上测试与被监控主机的通信179. [root@localhost nrpe-2.12]# /usr/local/nagios/libexec/check_nrpe -H 30.233.222.70NRPE v2.12180. 返回版本证明通信正常。181. 在 commands.cfg中增加对check_nrpe的定义182. [root@localhost nrpe-2.12]# vim /usr/local/nagios/etc/objects/commands.cfg183. 在最后面增加如下内容########################################################################## 2007.9.5 add by yahoon# NRPE COMMAND########################################################################## 'check_nrpe ' command definitiondefine command{command_name check_nrpecommand_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$}184. 意义如下command_name check_nrpe定义命令名称为check_nrpe,在services.cfg中要使用这个名称.command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$这是定义实际运行的插件程序.这个命令行的书写要完全按照check_nrpe这个命令的用法.不知道用法的就用check_nrpe –h查看185. -c后面带的$ARG1$参数是传给nrpe daemon执行的检测命令,之前说过了它必须是nrpe.cfg中所定义的那5条命令中的其中一条.在services.cfg中使用check_nrpe的时候要用!带上这个参数186. 下面就可以在services.cfg中定义对dbpi主机cpu负载的监控 187.define service{188.host_name dbpi189.被监控的主机名,这里注意必须是linux且运行着nrpe,而且必须是hosts.cfg中定义的190.service_description check-load191.监控项目的名称192.check_command check_nrpe!check_load193.监控命令是check_nrpe,是在commands.cfg中定义的,带的参数是check_load,是在nrpe.cfg中定义的194.max_check_attempts 5195.normal_check_interval 3196.retry_check_interval 2197.check_period 24x7198.notification_interval 10199.notification_period 24x7200.notification_options w,u,c,r201.contact_groups sagroup202.}像这样将其余四个监控项目加进来.203. [root@localhost nrpe-2.12]# vim /usr/local/nagios/etc/objects/services.cfg204. 说明: 这里就监控了load这一项,其余还有很多项,到nagios的客户机上,找文件/usr/local/nagios/etc/nrpe.cfg,在文件的最下面就有很多的定义,并且把定义执行了命令,只要在这个文件里(services.cfg)使用这些命令就行了。 注意所使用的命令实在客户机上而不是在主机上。这个之前有说明的,但是没有说的这么明白。205. 下面是我的配置文件:define service{ host_name nagios-server service_description check-load check_command check_nrpe!check_load max_check_attempts 5 normal_check_interval 3 retry_check_interval 2 check_period 24x7 notification_interval 10 notification_period 24x7 notification_options w,u,c,r contact_groups sagroup } define service{ host_name nagios-server service_description check-users check_command check_nrpe!check_users max_check_attempts 5 normal_check_interval 3 retry_check_interval 2 check_period 24x7 notification_interval 10 notification_period 24x7 notification_options w,u,c,r contact_groups sagroup }define service{ host_name nagios-server service_description check-zombie-procs check_command check_nrpe!check_zombie_procs max_check_attempts 5 normal_check_interval 3 retry_check_interval 2 check_period 24x7 notification_interval 10 notification_period 24x7 notification_options w,u,c,r contact_groups sagroup }define service{ host_name nagios-server service_description check-total-procs check_command check_nrpe!check_total_procs max_check_attempts 5 normal_check_interval 3 retry_check_interval 2 check_period 24x7 notification_interval 10 notification_period 24x7 notification_options w,u,c,r contact_groups sagroup }define service{ host_name nagios-server service_description check-sda1 check_command check_nrpe!check_sda1 max_check_attempts 5 normal_check_interval 3 retry_check_interval 2 check_period 24x7 notification_interval 10 notification_period 24x7 notification_options w,u,c,r contact_groups sagroup }206. 之前我们说过了,今天还有一个任务是要监控dbpi的swap使用情况.但是很遗憾,在nrpe.cfg中默认没有定义这个监控功能的命令.怎么办?手动在nrpe.cfg中添加,也就是自定义NRPE命令.207. 现在我们要监控swap分区,如果空闲空间小于20%则为警告状态—warning;如果小于10%则为严重状态—critical.我们可以查得需要使用check_swap插件,完整的命令行应该是下面这样.208. /usr/local/nagios/libexec/check_swap -w 20% -c 10%209. 增加一个没有提前默认配置好的功能:210. 思路:在被监控机上,找到命令,在这个目录下/usr/local/nagios/libexec查看命令如何使用:[root@localhost libexec]# ./check_swap –h手动使用命令:[root@localhost libexec]# /usr/local/nagios/libexec/check_swap -w 20% -c 10%SWAP OK - 100% free (1151 MB out of 1151 MB) |swap=1151MB;230;115;0;1151[root@localhost libexec]#命令执行成功。把命令定义到nrpe.cfg文件中。(被监控机上操作)[root@localhost libexec]# vim /usr/local/nagios/etc/nrpe.cfg增加下面这一行:(command[check_swpa]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%)command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%我们知道check_swap现在就可以作为check_nrpe的-c的参数使用了 修改了配置文件,当然要重启.但是如果你是以独立的daemon运行的nrpe,那么需要手动重启.如果你是在xinetd或者inetd下面运行的,则不需要.由于我们是xinetd下运行的,所以不需要重启服务在监控服务器上增加这个监控项目:[root@localhost nrpe-2.12]# vim /usr/local/nagios/etc/objects/services.cfgdefine service{ host_name nagios-server service_description check-swap check_command check_nrpe!check_swap max_check_attempts 5 normal_check_interval 3 retry_check_interval 2 check_period 24x7 notification_interval 10 notification_period 24x7 notification_options w,u,c,r contact_groups sagroup }现在服务器上手动执行以下,看看swap这个功能好不好用。 211. 所有的配置文件已经修改好了,现在重启nagios.杀掉nagios进程,然后再重启,就OK啦!!!212. 重启nagios服务器/etc/init.d/nagios restart总结一下文章的调用关系:本地信息的调用关系:通过客户端软件调用信息(nrpe)Server端的services.cfg(vim /usr/local/nagios/etc/objects/services.cfg) --调用-- server端的commands.cfg文件(vim /usr/local/nagios/etc/objects/commands.cfg)—-调用—client端的nrpe.cfg文件(vim /usr/local/nagios/etc/nrpe.cfg) ---调用—client端的libexec目录(cd /usr/local/nagios/libexec)下的真实程序---调用---具体执行程序了。远程信息的调用关系:(不使用客户端软件(nrpe))Server端的services.cfg --调用-- server端的commands.cfg文件---调用—server端的libexec目录下的真实程序----具体执行程序了命令名称必须一致!!!附录:1.重启nagios的方法之前我说重启nagios的时候都是用的杀进程的方式,其实也可以不这么做.如果在安装nagios的时候安装了启动脚本就可以使用/etc/init.d/nagios restart 还可以带的参数有stop, start,status如果报错了,有可能是脚本里面的路径设置错误,解决办法vi /etc/init.d/nagios将prefix=/usr/local/nagiosaa改为安装的目录/etc/init.d/nagios注:在nagios安装的时候说是将脚本安装到了/etc/rc.d/init.d,其实这和/etc/init.d是一个目录 2.不以xinetd的方式运行nrpe因为我们按照nrpe的安装文档安装下来,nrpe是在xinetd下面运行的,个人比较喜欢像nagios那样以单独的daemon来运行.这样比较好控制.方法:编辑 /etc/services将nrpe注释掉# Local services#nrpe 5666/tcp # nrpe编辑 nrpe.cfg,增加监控主机的地址# NOTE: This option is ignored if NRPE is running under either inetd or xinetdallowed_hosts=127.0.0.1,192.168.0.111注意两个地址以逗号隔开以单独的daemon启动nrpe[root@dbpi etc]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d 查看[root@dbpi etc]# ps -ef|grep nrpenagios 22125 1 0 14:04 ? 00:00:00 [nrpe][root@dbpi nagios]# netstat -an|grep 5666tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN说明已经正常启动了在/etc/rc.d/rc.local里面加入下面一行就实现开机启动nrpe了/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d同理要开机运行nagios就在/etc/rc.d/rc.local里面增加下面这行/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg 3.有关于check_load的用法及意义这个插件是用来检测系统当前的cpu负载,使用的方法为check_load [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15在unix里面负载的均值通常表示是1分钟,5分钟,15分钟内平均有多少进程处于等待状态.例如check_load -w 15,10,5 -c 30,25,20这个命令的意义如下当1分钟多于15个进程等待,5分钟多于10个,15分钟多于5个则为warning状态当1分钟多于30个进程等待,5分钟多于25个,15分钟多于20个则为critical状态 Nagios monitoring windows server说明:监控Windows,不用services.cfg这个配置文件,而是使用windows.cfg这个配置文件,也不使用hosts.cfg和hostgroups.cfg配置文件,都在windows.cfg这个配置文件里面定义。打开它即可。配置如下:[root@corshetlpro01 objects]# cd /usr/local/nagios/etc/objects[root@corshetlpro01 objects]# vim commands.cfg查看是否有如下项:define command{command_name check_ntcommand_line $USER1$/check_nt -H $HOSTADDRESS$ -p 12489 -v $ARG1$ $ARG2$}保存退出。vim /usr/local/nagios/etc/nagios.cfg# Definitions for monitoring the local (Linux) hostcfg_file=/usr/local/nagios/etc/objects/localhost.cfg# Definitions for monitoring a Windows machinecfg_file=/usr/local/nagios/etc/objects/windows.cfg #去掉这句话的注释 打开模块后配置windows.cfgvim /usr/local/nagios/etc/objects/windows.cfg修改这里的配置,把host_name修改成需要监控的windows机器名。然后配置host和hostgroup。define host{ use windows-server ; Inherit default values from a template host_name corshetlstg01 ; The name we're giving to this host alias My Windows Server ; A longer name associated with the host address 3.242.120.10 ; IP address of the host }define hostgroup{ hostgroup_name windows-servers ; The name of the hostgroup alias Win servers ; Long name of the group }define service{ use generic-service host_name corshetlstg01 service_description Uptime check_command check_nt!UPTIME } # Create a service for monitoring CPU load# Change the host_name to match the name of the host you defined above define service{ use generic-service host_name corshetlstg01 service_description CPU Load check_command check_nt!CPULOAD!-l 5,80,90 } # Create a service for monitoring memory usage# Change the host_name to match the name of the host you defined above define service{ use generic-service host_name corshetlstg01 service_description Memory Usage check_command check_nt!MEMUSE!-w 80 -c 90 # Create a service for monitoring C:\ disk usage# Change the host_name to match the name of the host you defined above define service{ use generic-service host_name corshetlstg01 service_description C:\ Drive Space check_command check_nt!USEDDISKSPACE!-l c -w 80 -c 90 }define service{ use generic-service host_name corshetlstg01 service_description D:\ Drive Space check_command check_nt!USEDDISKSPACE!-l d -w 80 -c 90 }define service{ use generic-service host_name corshetlstg01 service_description E:\ Drive Space check_command check_nt!USEDDISKSPACE!-l e -w 80 -c 90 }define service{ host_name corshetlstg01 service_description PING check_command check_ping!100.0,20%!500.0,60% }保存退出即可。[root@corshetlpro01 libexec]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg检查下语法,然后进行修改。重启:/etc/init.d/nagios restart/etc/init.d/apachectl restartkillall -HUP nagios
重启命令server端:killall -HUP nagios/etc/init.d/nagios restart /etc/init.d/apachectl restart client端:/etc/init.d/xinetd restart/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d测试nagios服务器语法:[root@corshetlpro01 objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
|
|