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

nagios及其插件部署安装并使用方法

[复制链接]

尚未签到

发表于 2015-9-8 09:46:11 | 显示全部楼层 |阅读模式
  ...
  http://blog.51yip.com/server/1391.html
  agios最主要优点就是在于它的报警功能,下面讲解一下用nagios来发邮件警告。
  
  一,配置contacts.cfg

  • vim /etc/nagios/objects/contacts.cfg  
  •   
  • define contact{                   //30行  
  •         contact_name                    nagiosadmin  
  •         use                             generic-contact  
  •         alias                           Nagios Admin  
  •         email                           xxxx@163.com     //这里改成自己的邮箱  
  •         }  
  二,配置nagios.cfg和localhost.cfg

  • vim /etc/nagios/nagios.cfg   
  •   
  • enable_notifications=1                //807行,开启报警  
  •   
  • 上面的总的开启也就是nagios装的所有插件,出现问题都会报警  
  •   
  • vim /etc/nagios/objects/localhost.cfg   
  •   
  • define service{                   //以processes为例  
  •         use                             local-service         ; Name of service template to use  
  •         host_name                       localhost  
  •         service_description             Total Processes  
  •         check_command                   check_local_procs!250!400!RSZDT  
  •         notifications_enabled           1  
  •         }  
  如果总的没有开启的话,localhost.cfg里面可以一个一个配置,这样比较麻烦。如果总的开启了,里面有些不想开启就可以notifications_enabled设置成0这样,这个就不用发报警,其他的发报警。
  三,添加发报警用户
  为什么要添加一个邮件用户nagios呢?可以看一下nagios发邮件的配置文件commands.cfg

  • define command{  
  •         command_name    notify-host-by-email  
  •         command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress:
  •     $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **"  
  •     $CONTACTEMAIL$  
  •         }  
  从上面/bin/mail我们可以看出,它是用了系统mail命令来发邮件的,并且没有指定用户,发邮件时会当前的默认用户,nagios的启动用户默认是nagios,可以在主配置nagios.cfg的123行找到nagios_user=nagios,所以要加一个nagios邮件用户,当然你可以把启动用户改掉。
  四,重启动一下nagios
  五,测试nagios是不是可以发邮件
  如果服务器一切正常,当然是不会发报警邮件的,但是重起肯定会发邮件的。重启一下看看是不是能收到邮件。如果收不到邮件,是什么原因呢?您可以按照以下几步来排查。
  1,查看邮件服务器是不是启动了,nagios服务器和客户端是不是启动了。你可以通过netstat,或者ps去查。
  2,如果都已经正常启动的话,手动敲命令来发封邮件是不是能收到,因为nagios也是调用了mail命令来发邮件的,确保这一步是通的,收件方是能收到邮件的。例如:

  • mail -s '看一下' 95219454@qq.com  < /etc/imapd.conf  
  3,如果用mail命令来发邮件能收到邮件,而nagios发的报警邮件还是收不到怎么办呢。这种情比较郁闷,并且我遇到了。查看一下mail的log日志文件/var/log/maillog,我当时用的是qq邮箱来收邮件的,发现qq的邮件服务器会拒绝。看下图

DSC0000.jpg qq拒绝nagios的报警邮件
  拒绝也不是全部拒绝,能收到邮件,但是延迟比较严重。建议不要用qq邮箱来收邮件。

DSC0001.jpg qq收到的nagios报警邮件

  转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/server/1391.html

  
  
  http://yytian.blog.iyunv.com/535845/564942
  
  1 安装sendmail组件
首先要确保sendmail相关组件的完整安装,我们可以使用如下的命令来完成sendmail的安装:
# yum install -y sendmail*
然后重新启动sendmail服务:
# service sendmail restart
然后发送测试邮件,验证sendmail的可用性:
# echo "Hello World" | mail abc@abc.com
  2 邮件报警的配置
    我们只需要编辑/usr/local/nagios/etc/object下的contact.cfg文件,在email后添加管理员的邮箱即可。一般而言,如果监控项目的分工不是太细的话~~就是管理员可以负责所有的监控、并对其进行处理的话,可以直接将一个或者多个管理员的邮件地址写上,使用空格或者逗号隔开。
    但是如果监控的内容中服务器有单独的管理员,网络有单独的管理员的话,我们就可以定义多个contact(联系人),然后再用contactgroup(联系组)对各contact进行分组。
    例如管理网络的有两人,管理服务器的有两个人,我们就可以定义两个contactgroup,然后定义四个管理员的contact,如下例是当前我正在使用的contact.cfg,服务器管理员有两名,网络管理员有两名:
2.1 contact.cfg的配置
#######################################################################################
#######################################################################################
##############   NETWORK ADMINISTRATOR MEMBERS
#######################################################################################
#######################################################################################
define contact{
        contact_name                        zhang1
        use                                        generic-contact
        alias                                       zhang1
        service_notification_period     24x7
        host_notification_period         24x7
        service_notification_options    w,u,c,r,f,s
        host_notification_options        d,u,r,f,s
        service_notification_commands   notify-service-by-email                           
        host_notification_commands        notify-host-by-email
       email                                         zhang1@text.com
        }
  define contact{
        contact_name                        zhang2
        use                                        generic-contact
        alias                                       zhang2
        service_notification_period     24x7
        host_notification_period         24x7
        service_notification_options    w,u,c,r,f,s
        host_notification_options        d,u,r,f,s
        service_notification_commands   notify-service-by-email
        host_notification_commands      notify-host-by-email
        email                                       zhang2@test.com
        }
  #######################################################################################
#######################################################################################
##############   SYSTEM ADMINISTRATOR MEMBERS
#######################################################################################
#######################################################################################
  define contact{
        contact_name                        li1
        use                                        generic-contact
        alias                                       li1
        service_notification_period     24x7
        host_notification_period          24x7
        service_notification_options    w,u,c,r,f,s
        host_notification_options         d,u,r,f,s
        service_notification_commands   notify-service-by-email
        host_notification_commands      notify-host-by-email
        email                                       li1@test.com
        }
  define contact{
        contact_name                           li1
        use                                          generic-contact
        alias                                         li1
        service_notification_period      24x7
        host_notification_period          24x7
        service_notification_options     w,u,c,r,f,s
        host_notification_options         d,u,r,f,s
        service_notification_commands   notify-service-by-email
        host_notification_commands      notify-host-by-email
        email                                          li2@test.com
        }
  
#######################################################################################
#######################################################################################
##############   NETWORK ADMINISTRATOR GROUP
#######################################################################################
#######################################################################################
define contactgroup{
        contactgroup_name           network
        alias                                  network
        members                           zhang1,zhang2
        }
  
  
  #######################################################################################
#######################################################################################
##############   SYSTEM ADMINISTRATOR GROUP
#######################################################################################
#######################################################################################
define contactgroup{
        contactgroup_name              system
        alias                                     system
        members                              li1,li2
        }
2.2 主机及监控内容的配置
相应的联系人和联系给已经创建好了,接下来的就是在被监控的服务中添加故障的联系人了,以下面定义的监控主机和服务为例
define host{
        use                                     linux-server
        host_name                          SKLuDB1
        alias                                    skludb1
        address                               192.168.19.142
        }
define service{
        use                                    generic-service          ; Name of service template to use
        host_name                         SKLuDB1
        service_description            PING
        check_command               check_ping!100.0,20%!500.0,60%
        contact_groups                  network
        }
define service{
        use                                     generic-service
        host_name                          SKLuDB1
        service_description             Uptime
        check_command                check_nt!UPTIME
        contact_groups                   system
         }

  如上面配置所示,当监控主机的ping出现问题的时候,nagios就会查看contact.cfg中定义的联系人组network中的联系人的信息,然后读取各联系人的邮件地址,这样的话,网络中出现故障时就可以直接给zhang1和zhang2二人发邮件了;同理,当服务器出现问题的时候就会给system组的相关人员发送邮件了
  
  
  
  ===================================================
  http://my.oschina.net/u/615185/blog/69699
  nagios发送报警邮件
  (特别说明:本文档介绍的方法基于本公司测试机环境中不能连接到外部网络所作出的修改,要是能直接链接www的话可以直接在localhost.cfg文件中对应的地方修改一处即可)
  
  
  nagios发警告邮件是采用本机的smtp服务,可以查看commands.cfg中关于发邮件的命令的定义,使用本机的mail命令,这就需要开启本机的smtp服务,为了安全可以在防火墙上设置拒绝其他的机器连本机的25号端口
  现在我们的网络里面有一个邮件服务器,所以要求使用这台现有的邮件服务器,不开启本机的smtp服务,这就需要重新定义命令使用第三方软件sendEmail.
  
  首先我们当然要在邮件服务器上新建一个账户用来做发邮件的账户
  这里邮件服务器的地址为mail.test.com
  用来发邮件的帐号nagios@test.com
  SMTP验证的用户名 nagios 密码 p#3isoda
  
  以下就来介绍一下sendEmail这个软件的使用.
  sendEmail的主页http://caspian.dotconf.net/menu/Software/SendEmail/
  下载地址http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.55.tar.gz
  软件十分小,是一个通过命令来发smtp邮件的程序.安装也十分简单(查看其README文件即可).
  解压缩tar –zxvf sendEmail-v1.55.tar.gz
  cd sendEmail-v1.55
  将可执行程序复制cp sendEmail /usr/local/bin
  然后给确认确实它具有执行权限
  ll /usr/local/bin/sendEmail
  -rwxr-xr-x 1 root root 77882 11-03 14:23 /usr/local/bin/sendEmail
  这样程序就装好了,使用也很简单.直接运行sendEmail就会显示详细的用法
  先看一个典型的例子
  /usr/local/bin/sendEmail –f nagios@test.com –t yahoon@test.com –s mail.test.com –u “from nagios” –xu nagios –xp p#3isoda –m happy
  解释:
  -f 表示发送者的邮箱
  -t 表示接收者的邮箱
  -s 表示SMTP服务器的域名或者ip
  -u 表示邮件的主题
  -xu 表示SMTP验证的用户名
  -xp 表示SMTP验证的密码(注意,这个密码貌似有限制,例如我用d!5neyland就不能被正确识别)
  -m 表示邮件的内容
  
  如果你不带-m参数的话,就会提示你自行输入
  Reading message body from STDIN because the ‘-m’ option was not used.
  If you are manually typing in a message:
  - First line must be received within 60 seconds.
  - End manual input with a CTRL-D on its own line
  输入完成后使用CTRL-D来结束
  当然我们也可以将一个文件的内容作为邮件的正文发出去的
  那么就可以使用:
  cat 文件名| /usr/local/bin/sendEmail –f nagios@test.com –t yahoon@test.com –s mail.test.com –u “from nagios” –xu nagios –xp p#3isoda
  有关sendEmail的用法就讲到这里
  
  既然nagios要使用sendEmail来发警告邮件,那么就要修改commands.cfg中关于发邮件的命令的定义,我们现在来修改notify-by-email这个命令,如下(注意其中粗体的部分)
  # 'notify-by-email' command definition
  define command{
          command_name    notify-by-email
          command_line    /usr/bin/printf "%b" "***** Nagios 2.9 *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | /usr/local/bin/sendEmail -f nagios@test.com -t $CONTACTEMAIL$ -s mail.test.com -u "** $NOTIFICATIONTYPE$ alert - $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -xu nagios -xp p#3isoda
          }
  (注意:上几行中彩色字体的部分要根据具体情况修改,例如我针对本公司平台作出的修改如下:/usr/local/bin/sendEmail -f nagios@onecloud.com.cn -t nagios@onecloud.com.cn -s 192.168.4.26 -u "$HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$" )然后就报警信息就会自动发送到https://192.168.4.26:446/mail/中
  
  注:其实sendEmail是一个十分有用的程序,我们在这个地方用了它,其实别的地方也可以用,典型的好处就是你不需要每台机器都装sendmail,开启smtp服务.直接用现成的一台邮件服务器就行了,这无疑很大的加强了系统的安全性,也节约了资源.
  
  
  
  
  
  
  
  
  
  
  
  
  接下来介绍一下在sendmail中的一些基本的设置来定制个性化服务:
  看local-service在templete.cfg中的定义:
  define service{
name                            local-service           ; The name of this service template
use                             generic-service         ; Inherit default values from the generic-service definition
max_check_attempts              4                       ; Re-check the service up to 4 times in order to determine its final (hard) state
normal_check_interval           5                       ;(#每多长时间检测一次) Check the service every 5 minutes under normal conditions
retry_check_interval            1                       ; Re-check the service every minute until a hard state can be determined
register                        0                       ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!
}
看generic-service在templete.cfg中的定义:
  define service{
name                           generic-service         ; The 'name' of this service template
active_checks_enabled           1                       ; Active service checks are enabled
passive_checks_enabled          1                       ; Passive service checks are enabled/accepted
parallelize_check               1                       ; Active service checks should be parallelized (disabling this can lead to major performance problems)
obsess_over_service             1                       ; We should obsess over this service (if necessary)
check_freshness                 0                       ; Default is to NOT check service 'freshness'
notifications_enabled           1                       ; Service notifications are enabled
event_handler_enabled           1                       ; Service event handler is enabled
flap_detection_enabled          1                       ; Flap detection is enabled
failure_prediction_enabled      1                       ; Failure prediction is enabled
process_perf_data               1                       ; Process performance data
retain_status_information       1                       ; Retain status information across program restarts
retain_nonstatus_information    1                       ; Retain non-status information across program restarts
is_volatile                     0                       ; The service is not volatile
check_period                    24x7                    ; The service can be checked at any time of the day
max_check_attempts              3                       ; Re-check the service up to 3 times in order to determine its final (hard) state
normal_check_interval           10              ; Check the service every 10 minutes under normal conditions
retry_check_interval            2                       ; Re-check the service every two minutes until a hard state can be determined
contact_groups                 admingroup      #联系组                ; Notifications get sent out to everyone in the 'admins' group
notification_options           w,u,c,r                 ; Send notifications about warning, unknown, critical, and recovery events
notification_interval           60                      #通知频率 ; Re-notify about service problems every hour
notification_period             24x7                    ; Notifications can be sent out at any time
register                        0                      ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!
}
  配置文件/etc/nagios/nrpe.cfg
  
command[check_load]=/usr/lib/nagios/plugins/check_load -w 15,10,5 -c 30,25,20
command[check_disk]=/usr/lib/nagios/plugins/check_disk -w 15% -c 10% -p /dev/sda1
#command[check_disk2]=/usr/lib/nagios/plugins/check_disk -w 20 -c 10 -p /dev/hdb1
command[check_zombie_procs]=/usr/lib/nagios/plugins/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/lib/nagios/plugins/check_procs -w 250 -c 400
command[check_cpu]=/usr/lib/nagios/plugins/check_procs -w 50 -c 70 --metric=CPU
command[check_mem]=/usr/lib/nagios/plugins/check_mem 450 500
  这个就是调用的命令,各种命令通过安装插件得到,在远程机器上只需要 check_nrpe -H IP地址 -c 命令(如check_mem)就可以了
check_nrpe需要安装nagios-nrpe_2.12.orig.tar.gz默认的插件中是没有的。
  /etc/rc.d/init.d/nrpe start启动在5666端口
  如果调用提示“no xxxxx output“注意属主和是否有可执行权限
  关于自己写插件(我用shell简单O(&cap;_&cap;)O):
  远程也是check_nrpe -H IP地址 -c 命令(如check_mem)调用,
  脚本 返回给服务器的东西 通过 echo
  通过脚本程序退出的值0正常,1警告,2错误            来获取远程服务器健康情况
  ############################
  $USER1是在resource.cfg中定义,$ARG1$ , $ARG2$这些就是定义在service中调用服务命令后面的参数,$HOSTADDRESS$是系统的宏 具体见手册
  说明:定义为use XXXXXXXX 的都定义在template.cfg中
  通知选项 w,u,c,r这些:
  notification_options: This directive is used to determine when notifications for the host should be sent out. Valid options are a combination of one or more of the following: d = send notifications on a DOWN state, u = send notifications on an UNREACHABLE state, r = send notifications on recoveries (OK state), f = send notifications when the host starts and stops flapping, and s = send notifications when scheduled downtimestarts and ends. If you specify n (none) as an option, no host notifications will be sent out. If you do not specify any notification options, Nagios will assume that you want notifications to be sent out for all possible states. Example: If you specify d,r in this field, notifications will only be sent out when the host goes DOWN and when it recovers from a DOWN state.
  手册上有说明
  #notification_interval          120 单位分钟(当检测到错误服务时 通知后 隔多少分钟再通知)
  (centos请关闭selinux,要不无法使用cgi,日志等(出现无法读,或者是白页) | chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin)
  max_check_attempts   normal_check_interval
notification_interval

  第一个就是检测故障的次数 第二个是检测的时间间隔 报警时间=检测故障次数*检测时间间隔 当报警之后还没有处理就按notification_interval这个来进行多长时间的报警
  web页面刷新:refresh_rate=90(在cfg.cgi中)
  邮件通知:
  define command{
command_name    notify-host-by-email
command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
}
  短信通知:(可以用飞信,安装smstools)
  # 'notify-service-by-sms' command definition
define command{
command_name    notify-service-by-sms
command_line    /usr/local/bin/sendsms 1589534xxxx "**Nagios**\n$NOTIFICATIONTYPE$: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\nInfo:\n$SERVICEOUTPUT$"
}
  
  
  参考链接
  nagios中文手册:http://www.itnms.net/docs/nagios/cn/build/html/index.html
  ...............
  http://blog.iyunv.com/maidanm/article/details/7845701
  【关于Nagios
  Nagios是一款用于监控系统和网络的开源应用软件,它的模式是服务器—客户端,也就是说首先要在在一台服务器上(server)部署相应的主要套件,然后在要监控的服务器上部署客户端程序,这样server会和client通信,从而监控client端的各项资源。Nagios功能十分强大几乎所有的项目都可以监控,大到服务器的存活状态,小到服务器上的某一个服务(web)。这些功能都是通过自定义插件(或者叫做脚本)来实现。
  当Nagios监控到某项资源发生异常会通知到用户,你可以接入手机短信接口也可以接入邮件接口。我们可以通过web页面来查看Nagios所监控的各项资源,默认搭建的Nagios服务器只能监控简单的几个项目,而其他服务之类的监控项目都是由我们自己开发的插件来实现的。
  
  【需要下载的软件
  nagios-3.0.5  
  nagios-plugins-1.4.13  
  nrpe-2.12.tar.gz
  apache-2.2.11   
  php-5.2.10   
  // 以上软件版本可以不一样
  
  【监控中心Server端的配置
  1. 安装apache + php(略,请参考第16章中相关内容,只需安装apache 和 php,到后边再配置)
  
  2. 建立nagios账户
  useradd  nagios
  
  3. 下载软件
  cd  /usr/local/src/
  wget  http://syslab.comsenz.com/downloads/linux/nagios-3.0.5.tar.gz
  wget  http://syslab.comsenz.com/downloads/linux/nagios-plugins-1.4.13.tar.gz
  wget  http://syslab.comsenz.com/downloads/linux/nrpe-2.12.tar.gz
  
  4. 编译安装nagios
  cd  /usr/local/src/
  tar  zxvf  nagios-3.0.5.tar.gz
  cd  nagios-3.0.5
  ./configure  --prefix=/usr/local/nagios
  make all
  make install
  make install-init # 把nagios做成一个运行脚本,使nagios随系统开机启动
  make install-config  # 把配置文件样例复制到nagios的安装目录
  make install-commandmode # 给外部命令访问nagios配置文件的权限
  chown -R nagios:nagios /usr/local/nagios
  
  5. 编译安装nagios-plugins
  cd  /usr/local/src/
  tar zxvf nagios-plugins-1.4.13.tar.gz
  cd  nagios-plugins-1.4.13
  ./configure  --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
  make && make install
  查看是否安装成功的方法是:
  ls /usr/local/nagios/libexec/
  看这个目录下是否有插件文件
  
  6. 安装nrpe
  cd /usr/local/src/
  tar zxvf  nrpe-2.12.tar.gz
  cd nrpe-2.12
  ./configure --enable-ssl --enable-command-args
  make all
  make install-plugin
  make install-daemon
  make install-daemon-config
  
  7. 配置web接口
  vim  /usr/local/apache2/conf/httpd.conf
  在最后加入以下内容:
  ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin
  <Directory "/usr/local/nagios/sbin/">
  AllowOverride AuthConfig
  Options ExecCGI
  Order allow,deny
  Allow from all
  </Directory>
  Alias /nagios/ /usr/local/nagios/share/
  <Directory "/usr/local/nagios/share">
  Options None
  AllowOverride AuthConfig
  Order allow,deny
  Allow from all
  </Directory>
  
  8. 配置nagios
  cd  /usr/local/nagios/etc/
  vim cgi.cfg
  把 use_authentication=1  改成  use_authentication=0  意思是不用用户验证
  
  9. 启动nagios
  在启动前先检测一下:
  /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
  如果最后显示如下,则说明配置没有问题了。
  Total Warnings: 0
  Total Errors:   0
  启动命令:
  /etc/init.d/nagios start
  或者:
  /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
  
  此时,就可以访问web页面的nagios了,在浏览器中输入:
  http://IP/nagios/ 看看吧。
  
  【在要监控的机器上client部署nagios
  如果你打开了web页面,点击左栏的Host Detail 会在右栏看到一行数据,其中Host 名为 “localhost” ,Status显示为”up”,并且显示为绿色,如果是其他颜色就说明你的localhost出了问题。目前只有一行数据,也就是说只监控了监控中心(localhost)一台主机,那么如何添加其他机器被它监控呢?这就需要在要被监控的机器上也部署nagios软件。
  1. 添加账户
  useradd nagios
  
  2. 安装nrpe
  cd /usr/local/src/
  wget  http://syslab.comsenz.com/downloads/linux/nrpe-2.12.tar.gz
  tar zxvf  nrpe-2.12.tar.gz
  cd nrpe-2.12
  ./configure --enable-ssl --enable-command-args
  make all
  make install-plugin
  make install-daemon
  make install-daemon-config
  
  3. 安装nagios-plugin
  cd  /usr/local/src/
  wget http://syslab.comsenz.com/downloads/linux/nagios-plugins-1.4.13.tar.gz
  tar zxvf nagios-plugins-1.4.13.tar.gz
  cd  nagios-plugins-1.4.13
  ./configure  --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
  make && make install
  到此就算安装完成了,请查看/usr/local/nagios/目录下是否有四个目录分别为:bin  etc  libexec  share 另外在libexec目录下会有很多check_开头的文件。如果你的机器上没有,就请重新安装吧。
  
  4. 配置
  vim /usr/local/nagios/etc/nrpe.cfg
  找到”allowed_hosts=127.0.0.1” 改成 “allowed_hosts=127.0.0.1,10.0.4.67”
  // 后边的IP是server的IP
  找到” dont_blame_nrpe=0” 改成 “dont_blame_nrpe=1”
  
  5. 启动nrpe
  /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
  
  【在监控中心添加被监控主机
  添加主机当然是要到server端(监控中心)修改配置文件了。
  1. 修改主配置文件
  cd  /usr/local/nagios/etc/
  vim nagios.cfg
  增加内容:
  cfg_dir=/usr/local/nagios/etc/services   ##定义一个目录,以后把新增加的主机信息文件全部放到这里
  
  2. 添加被监控主机信息
  mkdir /usr/local/nagios/etc/services
  cd  /usr/local/nagios/etc/services
  vim  10.0.4.56.cfg 加入如下内容:
  define host{
  use     linux-server
  host_name       10.0.4.56
  alias           10.0.4.56
  address         10.0.4.56
  }
  define service{
  use     generic-service
  host_name       10.0.4.56
  service_description     check_ping
  check_command           check_ping!100.0,20%!200.0,50%
  max_check_attempts 5
  normal_check_interval 1
  }
  define service{
  use     generic-service
  host_name       10.0.4.56
  service_description     check_ssh
  check_command           check_ssh
  max_check_attempts 5
  normal_check_interval 1
  }
  define service{
  use     generic-service
  host_name       10.0.4.56
  service_description     check_http
  check_command           check_http
  max_check_attempts 5
  normal_check_interval 1
  }
  // 注意,这里的IP是client端的IP,监控的项目有三个ping, ssh, http。其实这三个项目使用的脚本都为本地脚本,也就是说,即使远程主机没有安装nagios和nrpe同样可以监控这些项目。但是如果想监控load,disk,等等就需要通过nrpe服务来搞定了,道理很简单,load和disk都需要登录到远程主机上去获得信息,而ping,ssh,http都不需要的。这个到远程主机获取相关的信息的过程是由nrpe完成的。如果你的client上没有启动nrpe服务那么我们是无法获取远程主机的load和disk等信息的。下面笔者配置一下使用nrpe来监控远程主机的相关项目。
  在server端编辑/usr/local/nagios/etc/objects/commands.cfg
  vim  /usr/local/nagios/etc/objects/commands.cfg  # 在最后面添加如下内容
  define command{
  command_name    check_nrpe
  command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
  }
  然后编辑10.0.4.56.cfg (还是server上)
  cd   /usr/local/nagios/etc/services
  vim  10.0.4.56.cfg   # 加入如下内容:
  define service{
  use     generic-service
  host_name       10.0.4.56
  service_description     check_load
  check_command           check_nrpe!check_load
  max_check_attempts 5
  normal_check_interval 1
  }
  
  define service{
  use     generic-service
  host_name       10.0.4.56
  service_description     check_disk_hda1
  check_command           check_nrpe!check_hda1
  max_check_attempts 5
  normal_check_interval 1
  }
  
  define service{
  use     generic-service
  host_name       10.0.4.56
  service_description     check_disk_hda2
  check_command           check_nrpe!check_hda2
  max_check_attempts 5
  normal_check_interval 1
  }
  // 这里需要解释一下相关的”check_command”, 先看这个” check_nrpe!check_load” 这里的check_nrpe就是上面/usr/local/nagios/etc/objects/commands.cfg中刚刚定义的,后面的check_load是在远程主机上定义的一个命令脚本。具体在哪里定义稍后介绍。为什么中间加一个”!”,这个是nagios特有的形式,无需关心。下面需要到远程主机上去定义上面用到的脚本了。
  在远程主机上编辑/usr/local/nagios/etc/nrpe.cfg 文件
  vim  /usr/local/nagios/etc/nrpe.cfg (client上)
  把” command[check_hda1]”那行改成:
  command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1
  然后再增加一行:
  command[check_hda2]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda2
  // 这里的check_hda1 和 check_hda2 都是自定义的,和server端的定义的service中的check_command对应。也就是说,如果在server端定义了一个service(通过nrpe方式)那么必须要在客户端上的nrpe.cfg中定义相应的脚本。保存这个文件后,需要重新启动一下nrpe服务。
  killall nrpe ; /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d (client上)
  
  3. 重启nagios服务
  修改了配置需要重启服务才能使添加的监控主机生效。
  /etc/init.d/nagios  restart  (server上)
  此时再到web页面去观察是否多了一台10.0.4.56
  
  【在nagios客户端上自定义监控脚本
  在开始,笔者就讲过,我们可以自定义写监控脚步,从上面的例子中也可以看到监控磁盘状态时,是根据磁盘分区来监控的。这样不免有些麻烦,因为每台主机的磁盘分区状况都不一样(一样还好),而且有多少个分区就需要定义多少个命令。所以笔者就自定义写一个shell脚本来监控所有的磁盘分区:
  1. 在客户端上创建脚本/usr/local/nagios/libexec/check_disk.sh
  vim  /usr/local/nagios/libexec/check_disk.sh 写入如下内容:(client上)
  #!/bin/bash
  row=`df -h -P|wc -l`
  status=0
  for i in `seq 2 $row`
  do
  spare=`df -h -P|sed -n "$i"p|awk '{print $4}'`
  use_percentage=`df -h -P|sed -n "$i"p|sed -n "s/\%//"p|awk '{print $5}'`
  spare_percentage=`expr 100 - $use_percentage`
  partition_name=`df -h -P|sed -n "$i"p|awk '{print $6}'`
  if [ "$spare_percentage" -lt "3"  ];then
  echo -n "$partition_name CRITICAL ${spare_percentage}% $spare  "
  status[$i]=2
  elif [ "$spare_percentage" -lt "5" ];then
  echo -n  "$partition_name WARNING! ${spare_percentage}% $spare  "
  status[$i]=1
  else
  echo -n  "$partition_name OK ${spare_percentage}% $spare  "
  status[$i]=0
  fi
  done
  zhuangtai=0
  for j in `seq 2 $row`
  do
  if [ "${status[$j]}" -gt "$zhuangtai"  ];then
  zhuangtai=${status[$j]}
  fi
  done
  exit $zhuangtai
  2. 保存后,修改该脚本的权限
  chmod +x  /usr/local/nagios/libexec/check_disk.sh (client上)
  3. 然后编辑/usr/local/nagios/etc/nrpe.cfg文件
  vim /usr/local/nagios/etc/nrpe.cfg  # 加入一行:(client上)
  command[check_disk]=/usr/local/nagios/libexec/check_disk.sh
  保存,重启nrpe服务
  killall nrpe ; /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d (client上)
  4. 检测刚才的脚本是否正常运行的方法是,到server端执行如下命令:
  /usr/local/nagios/libexec/check_nrpe -H 10.0.4.56 -c check_disk (server上)
  如果正常的话,会输出一行磁盘检测的数据,否则可能会报错。
  5. 到server上添加相应的service
  cd   /usr/local/nagios/etc/services  (server上)
  vim  10.0.4.56.cfg   # 加入如下内容:
  define service{
  use     generic-service
  host_name       10.0.4.56
  service_description     check_disk
  check_command           check_nrpe!check_disk
  max_check_attempts 5
  normal_check_interval 1
  }
  6. 重启nagios服务
  /etc/init.d/nagios restart   (server上)
  
  【配置nagios报警邮件
  现在139邮箱有顺便发短信的功能,所以当有报警时,只需发送到你的139邮箱你就同样会收到一条报警短信。这样做的优势就是不用再去买短信网关了,节省了很大一笔钱。
  vim /usr/local/nagios/etc/objects/contacts.cfg
  把” email         nagios@localhost”  修改成 “email   你的139邮箱”
  vim /usr/local/nagios/etc/objects/templates.cfg
  找到:
  define service{
          name                            generic-service
  之所以看这一段,是因为在上面添加的10.0.4.56.cfg 定义了很多generic-service所以要关注这段的配置。
  define service{
  name     generic-service               
  active_checks_enabled          1                             
  passive_checks_enabled          1                     
  parallelize_check               1                     
  obsess_over_service             1                              
  check_freshness                 0                     
  notifications_enabled           1                             
  event_handler_enabled           1                     
  flap_detection_enabled          1                     
  failure_prediction_enabled      1                     
  process_perf_data               1                     
  retain_status_information        1                              
  retain_nonstatus_information    1                     
  is_volatile                     0                     
  check_period                    24x7        
  max_check_attempts              3                     
  normal_check_interval           10                     
  retry_check_interval            2                     
  ontact_groups                  admins                 
  notification_options            w,u,c,r               
  notification_interval           60                     
  notification_period             24x7                  
  register                        0                     
  }
  其中有几个参数需要你注意:
  notifications_enabled : 是否开启提醒功能。1为开启,0为禁用。一般,这个选项会在主配置文件(nagios.cfg)中定义,效果相同。
  notification_interval: 重复发送提醒信息的最短间隔时间。默认间隔时间是60分钟。如果这个值设置为0,将不会发送重复提醒。
  notification_period: 发送提醒的时间段。非常重要的主机(服务)我定义为7×24,一般的主机(服务)就定义为上班时间。如果不在定义的时间段内,无论什么问题发生,都不会发送提醒。
  notification_options: 这个参数定义了发送提醒包括的情况:d = 状态为DOWN, u = 状态为UNREACHABLE , r = 状态恢复为OK ,  f = flapping。,n=不发送提醒。
  要想正确发送邮件,上面的参数得配置合理才行。
  http://zhhmj.blog.iyunv.com/1666742/929631
  前提先自行安装好Apache+php
测试环境
主监控机:CentOS 6.2  172.16.1.8
被监控机:CentOS 6.2  172.16.5.11
主监控机设置:
1、安装epel
# cat /etc/issue
CentOS release 6.2 (Final)
Kernel \r on an \m
# rpm -ivh rpm -ivh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/x86_64/epel-release-6-5.noarch.rpm                         这里应该对应你服务器的版本
2、安装nagios、nagios-plugin与nrpe (nagios 3.3.1版本)
# yum install nagios nagios-plugins nagios-plugins-all nagios-plugins-nrpe nrpe
3、创建一个nagiosadmin 的用户用于Nagios的WEB接口登录,用户认证配置useradd nagiosadmin
# htpasswd -c /etc/nagios/passwd nagiosadmin
New password:
Re-type new password:
Adding password for user nagiosadmin
4、修改cgi.cfg文件,找到use_authentication=1 这行,把1改成0
#find -name cgi.cfg -print    查找
#vi /etc/nagios/cgi.cfg
5、启动apache与nagios
# /etc/init.d/httpd restart
# /etc/init.d/nagios start
6、登陆http://localhost/nagios/,输入用户密码后查看是否正常运行
注:别忘记了nagios后面那个/
被监控机设置:
1、安装需要软件
# yum install nagios-plugins nagios-plugins-nrpe nrpe
2、配置nrpe
# vi /etc/nagios/nrpe.cfg 修改监控机的地址或域名
allowed_hosts=127.0.0.1,192.168.1.8
3、修改/etc/hosts.allow增加监控机ip
# echo ‘nrpe:192.168.1.8&prime; >> /etc/hosts.allow
4、检查 NRPE 是否正常:
监控机上:
# /usr/lib64/nagios/plugins/check_nrpe -H 192.168.5.11
NRPE v2.12
查看相应的端口:netstat -an |grep 5666
防火墙开启5666 允许局域网IP或固定IP连接
5、启动 NRPE 守护进程:
# /usr/sbin/nrpe -c /etc/nagios/nrpe.cfg –d     或者
#/etc/init.d/nrpe start
6、关闭selinux:
#setenforce 0
在回到监控机:
添加nrpe的定义
# vi commands.cfg
# ‘check_nrpe’ command definition
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
增加主机监控配置项www.cfg
  

  • ###### define hosts ###################################
  • define host {
  •         host_name               cancer-ts4
  •         address                 172.16.5.4
  •         check_command           check-host-alive
  •         check_interval          5
  •         max_check_attempts      2
  •         contact_groups          admins
  • }

  • define host {
  •         host_name               cancer-ts11
  •         address                 172.16.5.11
  •         check_command           check-host-alive
  •         check_interval          5
  •         max_check_attempts      2
  •         contact_groups          admins
  • }

  • define host {
  •         host_name               cancer-ts12
  •         address                 172.16.5.12
  •         check_command           check-host-alive
  •         check_interval          5
  •         max_check_attempts      2
  •         contact_groups          admins
  • }

  • define host {
  •         host_name               cancer-hz4
  •         address                 172.16.3.4
  •         check_command           check-host-alive
  •         check_interval          5
  •         max_check_attempts      2
  •         contact_groups          admins
  • }

  • define host {
  •         host_name               cancer-hz5
  •         address                 172.16.3.5
  •         check_command           check-host-alive
  •         check_interval          5
  •         max_check_attempts      2
  •         contact_groups          admins
  • }

  • define host {
  •         host_name               cancer-hz12
  •         address                 172.16.3.12
  •         check_command           check-host-alive
  •         check_interval          5
  •         max_check_attempts      2
  •         contact_groups          admins
  • }

  • define host {
  •         host_name               cancer-hz13
  •         address                 172.16.3.13
  •         check_command           check-host-alive
  •         check_interval          1
  •         max_check_attempts      2
  •         contact_groups          admins
  • }

  • ######## define service ######################
  • define service{
  •         host_name                       cancer-ts4,cancer-ts11,cancer-ts12,cancer-hz4,cancer-hz5,cancer-hz12,cancer-hz13
  •         service_description             Users
  •         check_period                    24x7
  •         normal_check_interval           5
  •         retry_check_interval            2
  •         max_check_attempts              5  
  •         notification_period             24x7
  •         contact_groups                  admins
  •         check_command                   check_nrpe!check_users  
  •         }
  • define service{
  •         host_name                       cancer-ts4,cancer-ts11,cancer-ts12,cancer-hz4,cancer-hz5,cancer-hz12,cancer-hz13
  •         service_description             Load
  •         check_period                    24x7
  •         normal_check_interval           5
  •         retry_check_interval            2
  •         max_check_attempts              5
  •         notification_period             24x7  
  •         contact_groups                  admins
  •         check_command                   check_nrpe!check_load  
  •         }
  • define service{
  •         host_name                       cancer-ts4,cancer-ts11,cancer-ts12,cancer-hz4,cancer-hz5,cancer-hz12,cancer-hz13
  •         service_description             disk
  •         check_period                    24x7
  •         normal_check_interval           5
  •         retry_check_interval            2
  •         max_check_attempts              5
  •         notification_period             24x7  
  •         contact_groups                  admins
  •         check_command                   check_nrpe!check_hda1  
  •         }

  • define service{
  •         host_name                       cancer-ts11,cancer-ts12,cancer-ts4,cancer-hz4,cancer-hz5,cancer-hz13,cancer-hz12
  •         service_description             nginx
  •         check_period                    24x7
  •         normal_check_interval           5
  •         retry_check_interval            2
  •         max_check_attempts              5
  •         notification_period             24x7  
  •         contact_groups                  admins
  •         check_command                   check_nrpe!check_nginx
  •         }
  并把www.cfg添加到nagios主配置项
# echo “cfg_file=/etc/nagios/objects/www.cfg” >> /etc/nagios/nagios.cfg
其他机器如法炮制即可
  
  
  http://chenjiuling.blog.iyunv.com/3057028/935417

1、nagios简介:         
Nagios是一款用于系统和网络监控的应用程序并遵循GPL协定。它可以在你设定的条件下对主机和服务进行监控,在状态变差和变好的时候给出告警信息
Nagios通常由一个主程序(Nagios)、一个插件程序(Nagios-plugins)和四个可选的ADDON(NRPE、NSCA、NSClient++和NDOUtils)组成。Nagios的监控工作都是通过插件实现的,因此,Nagios和Nagios-plugins是服务器端工作所必须的组件。而四个ADDON中
(1)NRPE:用来在监控的远程Linux/Unix主机上执行脚本插件以实现对这些主机资源的监控。
(2)NSCA:用来让被监控的远程Linux/Unix主机主动将监控信息发送给Nagios服务器(这在冗余监控模式中特别要用到)。
(3)NSClient++:用来监控 Windows主机时安装在Windows主机上的组件。
(4)NDOUtils:则用来将Nagios的配置信息和各event产生的数据存入数据库,以实现这些数据的快速检索和处理。
这四个ADDON中,NRPE和NSClient++工作于客户端,NDOUtils工作于服务器端,而NSCA则需要同时安装在服务器端和客户端。
2、Nagios的特征包括:
1、监控网络服务(SMTP、POP3、HTTP、NNTP、PING 等);
2、监控主机资源(处理器负荷、磁盘利用率等);
3、简单地插件设计使得用户可以方便地扩展自己服务的检测方法;
4、并行服务检查机制;
5、具备定义网络分层结构的能力,用"parent"主机定义来表达网络主机间的关系,这种关系可被用来发现和明晰主机宕机或不可达状态;
6、当服务或主机问题产生与解决时将告警发送给联系人(通过 EMail、短信、用户定义方式);
7、具备定义事件句柄功能,它可以在主机或服务的事件发生时获取更多问题定位;
8、自动的日志回滚;
9、可以支持并实现对主机的冗余监控;
10、可选的 WEB 界面用于查看当前的网络状态、通知和故障历史、日志文件等。
3、nagios的优缺点:
优点:
(1)良好的稳定的系统管理代码
(2)在服务事件和主机事件之间良好的相关性
(3)命令检查配置文件的验证
(4)命令重新加载配置文件无需打扰Nagios的运行
(5)良好的文档
缺点:
(1)没有自动发现
(2)很弱的事件控制台
(3)无OOTB采集或性能数据的阈值
(4)没有简单的方法来接收和解释的SNMP TRAP
(5)没有MIB编译器或浏览器
一、安装前的准备工作
(1)、解决安装Nagios的依赖关系:
Nagios基本组件的运行依赖于httpd、gcc和gd。可以通过以下命令来检查nagios所依赖的rpm包是否已经完全安装:
# yum -y install httpd gcc glibc glibc-common gd gd-devel php php-mysql mysql mysql-devel mysql-server
说明:以上软件包您也可以通过编译源代码的方式安装,只是后面许多要用到的相关文件的路径等需要按照您的源代码安装时的配置逐一修改。此外,您还得按需启动必要的服务,如httpd等。
(2)、添加nagios运行所需要的用户和组:
# groupadd nagcmd
# useradd -G nagcmd nagios
# passwd nagios
把apache加入到nagcmd组,以便于在通过web Interface操作nagios时能够具有足够的权限:# usermod -a -G nagcmd apache
二、编译安装nagios:10.100.10.11
(1)、# wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.3.1.tar.gz
# tar zxf nagios-3.3.1.tar.gz
# cd nagios-3.3.1
#./configure --with-command-group=nagcmd \
--enable-event-broker \
--with-httpd-conf=/etc/httpd/conf/ \
--prefix=/usr/local/nagios
# make all
# make install
# make install-init
# make install-commandmode
# make install-config
(2)、为email指定您想用来接收nagios警告信息的邮件地址,默认是本机的nagios用户: # vi /usr/local/nagios/etc/objects/contacts.cfg
email        nagios@localhost       #这个是默认设置
(3)、在httpd的配置文件目录(conf.d)中创建Nagios的Web程序配置文件:
nagios# make install-webconf
(4)、创建一个登录nagios web程序的用户,这个用户帐号在以后通过web登录nagios认证时所用:
# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
以上过程配置结束以后需要重新启动httpd:
# service httpd restart
三、编译安装nagios-plugins
nagios的所有监控工作都是通过插件完成的,因此,在启动nagios之前还需要为其安装官方提供的插件。
#wget    http://nchc.dl.sourceforge.net/project/nagiosplug/nagiosplug/1.4.16/
nagios-plugins-1.4.16.tar.gz
# tar zxf nagios-plugins-1.4.16.tar.gz
# cd nagios-plugins-1.4.16
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
# make
# make install
四、配置并启动Nagios
(1)、把nagios添加为系统服务并将之加入到自动启动服务队列:
# chkconfig --add nagios
# chkconfig nagios on
(2)、检查其主配置文件的语法是否正确:
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
(3)、如果上面的语法检查没有问题,接下来就可以正式启动nagios服务了:
# service nagios start
(4)、配置selinux
如果您的系统开启了selinux服务,则默认为拒绝nagios web cgi程序的运行。您可以通过下面的命令来检查您的系统是否开启了selinux:
#getenforce
如果上面命令的结果显示开启了selinux服务,您可以通过下面的命令暂时性的将其关闭:#setenforce 0
如果您想在以后完全关闭selinux,可以通过编辑/etc/sysconfig/selinux文件,将其中的selinux后面的值“force”修改为“disable”即可。
当然,您也可以通过以下方式将nagios的CGI程序运行于SELinux/targeted模式而不用关闭selinux:
# chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin
# chcon -R -t httpd_sys_content_t /usr/local/nagios/share
(5)、通过web界面查看nagios:
http://10.100.10.11/nagios
登录时需要指定前面设定的web认证帐号和密码。 DSC0002.png

此时,监控本机的服务页面如下: DSC0003.png

五、基于NRPE监控远程Linux主机
1、NRPE简介:
Nagios监控远程主机的方法有多种,其方式包括SNMP、NRPE、SSH和NCSA等。这里介绍其通过NRPE监控远程Linux主机的方式。
NRPE(Nagios Remote Plugin Executor)是用于在远端服务器上运行检测命令的守护进程,
它用于让Nagios监控端基于安装的方式触发远端主机上的检测命令,并将检测结果输出至监控端。而其执行的开销远低于基于SSH的检测方式,而且检测过程并不需要远程主机上的系统帐号等信息,其安全性也高于SSH的检测方式。
2、安装配置被监控端10.100.10.10和10.100.10.12,分别在两个机器上进行配置。
1)先添加nagios用户
# useradd -s /sbin/nologin nagios
2)NRPE依赖于nagios-plugins,因此,需要先安装之
#wget    http://nchc.dl.sourceforge.net/project/nagiosplug/nagiosplug/1.4.16/
nagios-plugins-1.4.16.tar.gz
# tar zxf nagios-plugins-1.4.16.tar.gz
# cd nagios-plugins-1.4.16
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
# make all
# make install
3)安装NRPE
#wget http://nchc.dl.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.13/nrpe-2.13.tar.gz
# tar -zxvf nrpe-2.13.tar.gz
# cd nrpe-2.13
# ./configure --with-nrpe-user=nagios \
     --with-nrpe-group=nagios \
     --with-nagios-user=nagios \
     --with-nagios-group=nagios \
     --enable-command-args \
     --enable-ssl
# make all
# make install-plugin
# make install-daemon
# make install-daemon-config
4)配置NRPE
# vi /usr/local/nagios/etc/nrpe.cfg

log_facility=daemon
pid_file=/var/run/nrpe.pid
server_address=127.0.0.1
server_port=5666
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts=10.100.10.11
command_timeout=60
connection_timeout=300
debug=0

上述配置指令可以做到见名知义,因此,配置过程中根据实际需要进行修改即可。其中,需要特定说明的是allowed_hosts指令用于定义本机所允许的监控端的IP地址。
5)启动NRPE
# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d
为了便于NRPE服务的启动,可以将如下内容定义为/etc/init.d/nrped脚本:
# vi /etc/init.d/nrped

#!/bin/bash
# chkconfig: 2345 88 12
# description: NRPE DAEMON
NRPE=/usr/local/nagios/bin/nrpe
NRPECONF=/usr/local/nagios/etc/nrpe.cfg
case "$1" in
         start)
                   echo -n "Starting NRPE daemon..."
                   $NRPE -c $NRPECONF -d
                   echo " done."
                   ;;
         stop)
                   echo -n "Stopping NRPE daemon..."
                   pkill -u nagios nrpe
                   echo " done."
         ;;
         restart)
                   $0 stop
                   sleep 2
                   $0 start
                   ;;
         *)
                   echo "Usage: $0 start|stop|restart"
                   ;;
         esac
exit 0
或者,也可以在/etc/xinetd.d目录中创建nrpe文件,使其成为一个基于非独立守护进程的服务,文件内容如下:
service nrpe
{
         flags = REUSE
         socket_type = stream
         wait = no
         user = nagios
         group = nagios
         server = /opt/nagios/bin/nrpe
         server_args = -c /etc/nagios/nrpe.cfg -i
         log_on_failure += USERID
         disable = no
}
此种情况下启动NRPE进程需要通过重启xinetd来实现。
6)配置允许远程主机监控的对象
在被监控端,可以通过NRPE监控的服务或资源需要通过nrpe.cfg文件使用命令进行定义,定义命令的语法格式为:command[<command_name>]=<command_to_execute>。
比如:
command[check_rootdisk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p
command[check_swap]=/usr/local/nagios/libexec/check_disk -w 40% -c 20%
command[check_sensors]=/usr/local/nagios/libexec/check_sensors
command[check_users]=/usr/local/nagios/libexec/check_users -w 10 -c 20
command[check_load]=/usr/local/nagios/libexec/check_load -w 10,8,5 -c 20,18,15
command[check_zombies]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_all_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
六、配置监控端所要监控的服务和资源
手动添加一些监控的配置文件,在监控端(10.100.10.11)的/usr/local/nagios/etc/objects/添加:
首先是添加主机和服务:
①   vi /usr/local/nagios/etc/objects/host10.cfg
define host {
        host_name idc010vm010
        alias idc010vm010
        address 10.100.10.10
        check_command check-host-alive
        max_check_attempts 5
        notification_interval 10
        notification_period 24x7
        notification_options d,u,r
        }
define service {
        host_name idc010vm010
        service_description check-host-alive
#       check_command check_nrpe! check_load
        check_period 24x7
        max_check_attempts 4
        normal_check_interval 3
        retry_check_interval 2
        contact_groups admins
        notification_interval 10
        notification_period 24x7
        notification_options w,u,c,r
        check_command check-host-alive
        }
define service {
        host_name idc010vm010
        service_description check-disk
        check_command check_nrpe! check_df
        check_period 24x7
        max_check_attempts 4
        normal_check_interval 3
        retry_check_interval 2
        contact_groups admins
        notification_interval 10
        notification_period 24x7
        notification_options w,u,c,r
#       check_command check-host-alive
        }
define service{
host_name idc010vm010
service_description total_procs
check_command check_nrpe!check_total_procs
max_check_attempts 4
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 admins
}
define service{
host_name idc010vm010
service_description ip_connets
check_command check_nrpe! check_ip_connets
max_check_attempts 4
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 admins
}
define service{
host_name idc010vm010
service_description check-load
check_command check_nrpe!check_load
max_check_attempts 4
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 admins
}
②   Vi /usr/local/nagios/etc/objects/host12.cfg
define host {
        host_name idc010vm012
        alias idc010vm012
        address 10.100.10.12
        check_command check-host-alive
        max_check_attempts 5
        notification_interval 10
        notification_period 24x7
        notification_options d,u,r

        }
define service {
        host_name idc010vm012
        service_description check-host-alive
#       check_command check_nrpe! check_load
        check_period 24x7
        max_check_attempts 4
        normal_check_interval 3
        retry_check_interval 2
        contact_groups admins
        notification_interval 10
        notification_period 24x7
        notification_options w,u,c,r
        check_command check-host-alive

        }
define service {
        host_name idc010vm012
        service_description check-disk
        check_command check_nrpe! check_df
        check_period 24x7
        max_check_attempts 4
        normal_check_interval 3
        retry_check_interval 2
        contact_groups admins
        notification_interval 10
        notification_period 24x7
        notification_options w,u,c,r
#       check_command check-host-alive

        }
define service{
host_name idc010vm012
service_description total_procs
check_command check_nrpe!check_total_procs
max_check_attempts 4
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 admins
}
define service{
host_name idc010vm012
service_description ip_connets
check_command check_nrpe! check_ip_connets
max_check_attempts 4
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 admins
}
define service{
host_name idc010vm012
service_description check-load
check_command check_nrpe!check_load
max_check_attempts 4
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 admins
}
然后是在/usr/local/nagios/etc/nagios.cfg里添加两行内容,如下所示:
# vi /usr/local/nagios/etc/nagios.cfg

cfg_file=/usr/local/nagios/etc/objects/host10.cfg
cfg_file=/usr/local/nagios/etc/objects/host12.cfg

     文件保存后,先测试一下nagios是否有语法错误,若没有,则重新启动nagios。
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
#service nagios restart
http://10.100.10.11/nagios
检测结果如下: DSC0004.png
  本文出自 “陈玖玲-无辜de心碎” 博客,请务必保留此出处http://chenjiuling.blog.iyunv.com/3057028/935417
  http://zhaohaolin.iteye.com/blog/1585209
  Nagios网上的教程百度一下一大把,发现有些教程讲法有点出入,估计有些都是拷贝乱贴乱飞,按有些说法去做,根本就做不下去,今天我亲自实践,现场抓图让你看得清楚看得明白,以及中间遇到什么问题,怎么解决,都会分享给大家,抓图再写出来有点辛苦,关键能学到东西一切都是值得滴,Follow me~!
  下载centOS 6.0然后安装好系统,最好用vmware来做最好.
  CentOS 6.0最新版国内下载地址:http://mirrors.163.com/centos/6.0/isos/i386/
  自行装好系统以后现在开始装备安装前的环境准备
我把命令贴出来,为了方便大家操作你只要跟着我把命令拷贝和粘贴即可,省去敲打命令的麻烦也避免失误出错(小技巧提醒:装好centOS以后进入x-windows里面装个vmware tools就可以实现物理机和虚拟机之间的拷贝和粘贴,方法很简单这里就不说了)
yum -y install httpd gettext mysql mysql-server mysql-devel php php-mysql php-pear
DSC0005.png
  yum install php-mysql mysql perl mysql-server perl-DBI perl-DBD-MySQL httpd gcc glibc glibc-common gd gd-devel php
DSC0006.png
  在线下载nagios程序和插件
  wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz (现在最新版本的插件)
wget http://sourceforge.net/projects/nagios-cn/files/sourcecode/zh_CN%203.2.3/nagios-cn-3.2.3.tar.bz2/download(最新版为3.3.1,装上去觉得界面变得不好看,顺便就找了个中文版的)
DSC0007.png
DSC0008.png
  [iyunv@centos nagios]# ls
  nagios-cn-3.2.3.tar.bz2  nagios-plugins-1.4.15.tar.gz
  
  用户和组的准备
  [iyunv@centos nagios]# useradd nagios
  [iyunv@centos nagios]# passwd nagios
  changing password for user nagios.
  New password:
  BAD PASSWORD:it is based on a dictionary word
  BAD PASSWORD:is too simple
  Retype new password:
  passwd:all authentication tokens updated successfully.
  
DSC0009.png
  开始安装主程序
DSC00010.png
  ./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios --with-command-group=nagcmd
DSC00011.png
  完成以后发现执行make all命令的时候出现以下错误
DSC00012.png
  貌似CentOS下默认系统没有安装编译器,安装一下:
  yum -y install gcc automake autoconf libtool make
DSC00013.png
  继续做make all操作,发现最后报错了,
DSC00014.png
  先执行make clean操作,然后再执行 ./configure && make all 操作通过(问题分享)接着再执行以下命令即可,没有出现任何状况.
DSC00015.png
DSC00016.png
DSC00017.png
DSC00018.png
DSC00019.png
  接下来安装插件
DSC00020.png
  ./configure --with-nagios-user=nagios --with-nagios-group=nagios
DSC00021.png
DSC00022.png
  添加访问页面用户nagiosadmin及密码,这里要记住此密码不要忘记了.
  htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
DSC00023.png
  接下来启动相关服务
DSC00024.png
DSC00025.png
  检查配置文件是否正确
  /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
DSC00026.png
  [经验分享:由于这一串检查的命令很长,应用率也比较高,这里我们可以自定义一个命令来代替这一长串的命令
  编辑.bashrc这个文件
DSC00027.png
  在里面用alias 来自定义一个命令来代替,这里我用check
DSC00028.png
  用source命令在当前bash环境下读取并执行bashrc中的命令
DSC00029.png
  这样我们自定义的命令就生效了,以后只要输入check就可以检查nagios配置文件是否存在错误了,简单方便.]
  发现没有警告和错误就表示通过了
DSC00030.png
  现在就可以用http://serverip/nagios 方式访问nagios web访问控制台了.
  开了防火墙记得要开放80端口,要不然连接不到,或者直接关闭防火墙用命令service iptables stop关闭即可.
DSC00031.png
  问题分享:进入页面的时候点主机或其它选项的时候报此错误,后面百度一下原来是开启了selinux强制访问控制安全模块所致
DSC00032.png
  注意:如果你开启了selinux 需要配置如下二步:
chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin/
chcon -R -t httpd_sys_content_t /usr/local/nagios/share/
DSC00033.png
  这样正宗的中文版出来了,总算迈出了成功的一小步,耐心坚持让我们继续吧~~!
DSC00034.png
  [经验分享:我这里用虚拟机做的,记得系统和网络相关参数配置好以后做一个系统的快照,配到这里nagios安装成功以后我们再做一个快照,以免后面配乱出问题的时候我们可以返回到这里重新再配置,方便快捷,记住做快照的时候先把虚拟机关闭以后再做,要不然快照出来的文件大得吓人,而关闭以后做快照文件才几百KB.]
DSC00035.png
  ok下面我们接着来
  [nrpe_plugins指标采集程序主要是用来分析nrpe客户端的plugin脚本采集性能指标数据。目前实现的有cpu、memory、disk、buffer、singleprocess、oracle、sybase等方面的性能指标的分析,这个插件需要分别安装在主控端和被控端上,这个插件需要openssl的支持,没有就直接在线安装一下(yum install openssl-devel)所以还需在nagios主机和被控端安装,感觉挺麻烦,没办法它需要咱们就跟着做哈.]
  先把nrpe下载下来先.
  wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
DSC00036.png
  解压文件
DSC00037.png
  编译安装
DSC00038.png
DSC00039.png
  完成以后没有出错就可以看到以下生成的文件了
DSC00040.png
  二:配置PNP
  [PNP是一个基于php和perl,用rrdtool将nagios采集的数据绘制图表的工具,所以安装pnp之前必须先安装php perl  and rrdtool这里用yum直接来安装rrdtool,单独下载安装有很多依存关系,挺麻烦.]
DSC00041.png
  我这里找到最新的版本是0.6.15
  wget http://sourceforge.net/projects/pnp4nagios/files/PNP-0.6/pnp4nagios-0.6.15.tar.gz/download
DSC00042.png
DSC00043.png
DSC00044.png
  编译的时候报错了.
DSC00045.png
  yum install perl-Time-HiRes
DSC00046.png
  继续编译发现有个perl modules没有发现,好像提示用来加速什么的,暂时不管它了
DSC00047.png
DSC00048.png
  根据提示需要继续
DSC00049.png



  然后把相关后缀带sample文件变更成无sample后缀

  配置Nagios来启用PNP

  enable_environment_macros=1默认是开启了的

  pnp4nagios有很多种工作模式,这里我们选用同步模式,想选其它模式可以看说明都有详细配置教程(不同模式配置不同)

  这里我们按说明的设定方法来做

  [这里先把pnp4nagios执行数据收集的文件拷贝到nagios的libexec下,方便统一命令的执行路径.]
cp /usr/local/pnp4nagios/libexec/process_perfdata.pl /usr/local/nagios/libexec/

  然后在commands.cfg里面加入如下(这里是同步模式设定方法)

  #process-host-perfdata' command definition
define command{
        command_name    process-host-perfdata
        command_line    $USER1$/process_perfdata.pl -d HOSTPERFDATA
        }
  #process-service-perfdata' command definition
define command{
        command_name    process-service-perfdata
        command_line    $USER1$/process_perfdata.pl
        }

  然后添加小太阳模版,镶嵌在nagios页面上
[这里有两种查看图的方法,第一种方法是你当太阳的图标的时候会在新窗口中打开图形查看,第二种方法是你只需要把鼠标移动到太阳图标上面就会自动弹出图形出来,以下两种方法你可以根据自己喜好选其中一种即可]
第一种方法在templates.cfg里面加入如下

  define host {
   name       host-pnp
   action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_
   register   0
}
  define service {
   name       srv-pnp
   action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$
   register   0
}

  第二种方法按照说明加入如下

  拷贝status-header.ssi到/usr/local/nagios/share/ssi/目录下面来(这一步很重要,要不然移动到太阳标记上出不来图)

  define host {
   name       host-pnp
   action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_' class='tips' rel='/pnp4nagios/index.php/popup?host=$HOSTNAME$&srv=_HOST_
   register   0
}
  define service {
   name       srv-pnp
   action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$' class='tips' rel='/pnp4nagios/index.php/popup?host=$HOSTNAME$&srv=$SERVICEDESC$
   register   0
}
第二种方法最终效果图

  为目标主机图标旁边添加一个小太阳标记


  为相关服务添加一个小太阳标记


  先做一下pnp4nagios环境测试,发现php-gd模块无效,用命令yum install php-gd安装成功以后还是不行,后来经老男孩指点用yum install php* -y安装然后重启系统成功通过,



  按要求重命名install.php

  做好相关配置以后请用命令重启一下nagios的服务

  ok,到此总算配置好了PNP,配置了这么多有点累了,但我们还要继续,配置本章最后一个Nagios WEB管理工具叫NagiosQL(注:还有同样一个Nagios WEB管理工具叫Nconf)
  wget http://sourceforge.net/projects/nagiosql/files/nagiosql/NagiosQL%203.1.1/nagiosql_311.tar.gz/download

  NagiosQL是一个WEB管理工具,只要把其放到下Apache即可。因为这里它要和Nagios整合到一起,为了方便管理,把其放在Nagios目录下


  建立nagiosQL导出nagios配置文件的目录,并修改权限
  建目录和改权限
mkdir -p /etc/nagiosql/{hosts,services,backup/{,hosts,services},import}
chown -R apache:nagios /etc/nagiosql/
chmod -R 755 /etc/nagiosql
chmod 777 /usr/local/nagios/nagiosql/config

  Nagios相关文件权限的设定
chown nagios:apache /usr/local/nagios/etc
chown nagios:apache /usr/local/nagios/etc/{nagios.cfg,cgi.cfg}
chown nagios:apache /usr/local/nagios/etc/*
chmod 664 /usr/local/nagios/etc/{nagios.cfg,cgi.cfg}
chmod 775 /usr/local/nagios/etc

  设定nagios二进制文件的权限,以便其能够通过Web程序执行
chown nagios:apache /usr/local/nagios/bin/nagios
chmod 750 /usr/local/nagios/bin/nagios
chown -R apache.nagios /usr/local/nagios/var/spool/
chown nagios:apache /usr/local/nagios/var/rw/nagios.cmd
chmod 660 /usr/local/nagios/var/rw/nagios.cmd

  为apache添加nagiosQL虚拟目录

  Alias /nagiosQL "/usr/local/nagios/nagiosql"
<Directory "/usr/local/nagios/nagiosql">
# SSLRequireSSL
Options None
AllowOverride None
Order allow,deny
Allow from all
</Directory>

  重新加载httpd服务和启动mysqld服务

  建立nagiosql安装文件
touch /usr/local/nagios/nagiosql/install/ENABLE_INSTALLER

  访问http://ip/nagiosQL进行安装
  问题分享:我在访问的时候提示没有权限访问,搞了半天原来是SElinux惹的祸,修改SELinux的实时运行模式,setenforce 0 (设置SELinux 成为permissive模式)这个只是临时解决方法,优点是不需重启系统生效,但如果要彻底禁用SELinux 需要在/etc/sysconfig/selinux中设置参数selinux=0

  选取中文然后点开始安装按钮

  测试所需环境通过,直接按下一步即可

  删除安装目录,这里使用改权限:chmod 000 /usr/local/nagios/nagiosql/install


  到这里安装步骤总算结束了,下面我们还需要进行相关配置工作.

  管理-域-localhost---点“修改”

  按如图所示更改相应的路径即可

  工具-导入数据 右边会列出一些配置文件,选择导入即可。
导入只是把配置文件内容导入到Mysql数据库,而使用其配置文件时,是一个个cfg文件,所以下面要进行写入过程

  工具-nagios控制-写入检测数据-写入其他数据

  检查配置文件成功


  写入之后就可以看到相应的cfg文件了,查看写入是否成功,由下面的输出可以看出写入是成功滴.

  修改nagios的配置文件,将原有以cfg_file开头项全部注释掉,加入上面新写入的配置文件

  注释掉

  加入新的配置文件路径
cfg_dir=/etc/nagiosql/hosts/
cfg_dir=/etc/nagiosql/services/
cfg_file=/etc/nagiosql/commands.cfg
cfg_file=/etc/nagiosql/contactgroups.cfg
cfg_file=/etc/nagiosql/contacts.cfg
cfg_file=/etc/nagiosql/contacttemplates.cfg
cfg_file=/etc/nagiosql/hostdependencies.cfg
cfg_file=/etc/nagiosql/hostescalations.cfg
cfg_file=/etc/nagiosql/hostextinfo.cfg
cfg_file=/etc/nagiosql/hostgroups.cfg
cfg_file=/etc/nagiosql/hosttemplates.cfg
cfg_file=/etc/nagiosql/servicedependencies.cfg
cfg_file=/etc/nagiosql/serviceescalations.cfg
cfg_file=/etc/nagiosql/serviceextinfo.cfg
cfg_file=/etc/nagiosql/servicegroups.cfg
cfg_file=/etc/nagiosql/servicetemplates.cfg
cfg_file=/etc/nagiosql/timeperiods.cfg

  检查nagios的配置文件是否有问题,然后生重启nagios服务,至此整个搭建过程终于完成了,下一节将讲解重头戏,怎么用nagiosQL添加需要监控的主机以及启用nagios报警机制,待续~~!

运维网声明 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-110933-1-1.html 上篇帖子: 监控系统-nagios 下篇帖子: Nagios-配置版
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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