搜诶符合你 发表于 2019-1-12 12:28:40

Nagios客户端安装

  1.更新yum源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos  
.d/CentOS-Base.repo.backup
  
wget -O /etc/yum.repos.d/CentOS-Base.repohttp://mirrors.aliyun.com/repo/Centos-6.repo
  2.调整字符集
echo 'export LC_ALL=C'>>/etc/profile  
source /etc/profile
  3.关闭iptables与selinux
  4.更新系统时间
/usr/sbin/ntpdate pool.ntp.org  然后放入定时任务
  5.安装gcc(可以省略)
yum install gcc glibc glibc-common mysql* -y  6.添加用户
/usr/sbin/useradd -m nagios -s /sbin/nologin  7.安装插件
yum -y install perl-devel  
tar xf nagios-plugins-1.4.16.tar.gz
  
cd nagios-plugins-1.4.16
  
./configure --prefix=/usr/local/nagios --enable-perl-modules --enable-redhat-pthread-workaround
  
make && make install
  
cd ../
  
ls /usr/local/nagios/libexec/|wc -l
  8.安装nrpe
tar xf nrpe-2.12.tar.gz  
cd nrpe-2.12
  
./configure
  
make all
  
make install-plugin
  
make install-daemon
  
make install-daemon-config
  
cd ../
  9.安装soft与iostat
tar xf Params-Validate-0.91.tar.gz  
cd Params-Validate-0.91
  
perl Makefile.PL
  
make
  
make install
  
cd ../
tar xf Class-Accessor-0.31.tar.gz  
cd Class-Accessor-0.31
  
perl Makefile.PL
  
make
  
make install
  
cd ../
tar xf Config-Tiny-2.12.tar.gz  
cd Config-Tiny-2.12
  
perl Makefile.PL
  
make
  
make install
  
cd ../
tar xf Math-Calc-Units-1.07.tar.gz  
cd Math-Calc-Units-1.07
  
perl Makefile.PL
  
make
  
make install
  
cd ../
tar xf Regexp-Common-2010010201.tar.gz  
cd Regexp-Common-2010010201
  
perl Makefile.PL
  
make
  
make install
  
cd ../
tar xf Nagios-Plugin-0.34.tar.gz  
cd Nagios-Plugin-0.34
  
perl Makefile.PL
  
make
  
make install
  
cd ../
yum install sysstat -y  
cp check_memory.pl /usr/local/nagios/libexec
  
cp check_iostat /usr/local/nagios/libexec
  
chmod 755 /usr/local/nagios/libexec/check_memory.pl
  
chmod 755 /usr/local/nagios/libexec/check_iostat
  
dos2unix /usr/local/nagios/libexec/check_memory.pl
  
dos2unix /usr/local/nagios/libexec/check_iostat
  10.修改nrpe.cfg
  # vim /usr/local/nagios/etc/nrpe.cfg +79
  修改79行:allowed_hosts=127.0.0.1
  为:allowed_hosts=127.0.0.1,192.168.0.150 #允许哪个服务端(192.168.0.150)对它发送命令
  删除199行到203行:
  199 command=/usr/local/nagios/libexec/check
  _users -w 5 -c 10
  200 command=/usr/local/nagios/libexec/check_
  load -w 15,10,5 -c 30,25,20
  201 command=/usr/local/nagios/libexec/check_
  disk -w 20% -c 10% -p /dev/hda1
  202 command=/usr/local/nagios/libexe
  c/check_procs -w 5 -c 10 -s Z
  203 command=/usr/local/nagios/libexec
  /check_procs -w 150 -c 200
  echo "command=/usr/local/nagios/libexec/check_load -w 15,10,6 -c 30,25,20">>/usr/local/nagios/etc/nrpe.cfg
  echo "command=/usr/local/nagios/libexec/check_memory.pl -w 6% -c 3%">>/usr/local/nagios/etc/nrpe.cfg
  echo "command=/usr/local/nagios/libexec/check_disk -w 20% -c 8% -p /">>/usr/local/nagios/etc/nrpe.cfg
  echo "command=/usr/local/nagios/libexec/check_swap -w 20% -c 10%">>/usr/local/nagios/etc/nrpe.cfg
  echo "command=/usr/local/nagios/libexec/check_iostat -w 6 -c 10">>/usr/local/nagios/etc/nrpe.cfg
  11.启动nagios客户端
  pkill nrpe
  /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
  echo"#nagios nrpe process cmd by oldboy 2012-6-7" >> /etc/rc.local
  echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d">>/etc/rc.local
  # netstat -lnt|grep 5666 && echo "nagios client is ok"
  tcp      0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN
  nagios client is ok
  配置服务端主配置文件:
  # vim nagios.cfg
  在34行增加如下两行:
  cfg_file=/usr/local/nagios/etc/objects/services.cfg
  cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
  注释掉38行:
  cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
  在38行最前面输入"#"
  #cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
  按服务的目录功能分(演示):
  54行:#cfg_dir=/usr/local/nagios/etc/services
  修改为:cfg_dir=/usr/local/nagios/etc/services
  # mkdir services
  # chown -R nagios.nagios services/
  # cd objects
  # ls
  commands.cfglocalhost.cfgswitch.cfg   timeperiods.cfg
  contacts.cfgprinter.cfg    templates.cfgwindows.cfg
  # head -51 localhost.cfg>hosts.cfg
  # chown nagios.nagios hosts.cfg
  # ls
  commands.cfglocalhost.cfgtemplates.cfg
  contacts.cfgprinter.cfg    timeperiods.cfg
  hosts.cfg   switch.cfg   windows.cfg
  # touch services.cfg
  # chown nagios.nagios services.cfg
  Nagios监控模式的定义和约定:
  1.按监控行为分类:
  主动监控
  和nrpe无关,就是利用服务端本地插件直接获取信息。
  被动监控
  主程序通过check_nrpe插件,和客户端nrpe进程沟通,调用本地插件获取数据。
  2.如何选择主动模式和被动模式
  1)对于本地的资源性能等的监控,一般用被动模式,如负载,内存,硬盘,虚拟内存,磁盘IO,温度,风扇等的监控(我们也可以通过snmp实现监控)
  2)对于web服务,数据库服务这种能对外提供服务的监控,一般用主动模式,如监控httpd,sshd,mysqld,rsyncd等的服务
  3)主动模式和被动模式是相对的,并且是可以互相转换的,即主动模式的服务,可以改成被动模式,被动模式的服务有时也可以改为主动模式。
  实战配置Nagios主机及服务实战:
  # cdobjects
  # vim hosts.cfg
  define host{   #定义主机
  use                     linux-server#使用的模板
  host_name               151-web01   #被监控的主机名
  alias                   151-web01   #被监控的主机别名
  address               192.168.0.151 #被监控的主机IP地址
  }
  define host{
  use                     linux-server
  host_name               150-nagios_server
  alias                   150-nagios_server
  address               192.168.0.150
  }
  define hostgroup{#定义主机组
  hostgroup_namelinux-servers
  alias         Linux Servers
  members         151-web01,150-nagios_server#把上面定义的主机加到这一行,多个主机之间用逗号(,)分隔。
  }
  # /etc/init.d/nagios checkconfig #检查配置语法
  Running configuration check... CONFIG ERROR!Check your Nagios configuration.
  # /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg#检查配置语法
  。。。。。。。。。。。。。。。。。。。。。。。。。
  Read object config files okay...
  Running pre-flight check on configuration data...
  Checking services...
  Error: There are no services defined!#错误原因,没有定义服务
  Checked 0 services.
  。。。。。。。。。。。。。。。。。。。。。。。。。
  Total Warnings: 2
  Total Errors:   1
  。。。。。。。。。。。。。。。。。。。。。。。。。。
  # vim /etc/init.d/nagios +183
  183               $NagiosBin -v $NagiosCfgFile > /dev/null 2>&1;
  修改为:
  $NagiosBin -v $NagiosCfgFile
  # /etc/init.d/nagios checkconfig#检查配置语法
  。。。。。。。。。。。。。。。。。。。。。。。。。
  Read object config files okay...
  Running pre-flight check on configuration data...
  Checking services...
  Error: There are no services defined!
  Checked 0 services.
  。。。。。。。。。。。。。。。。。。。。。。。。。
  Total Warnings: 2
  Total Errors:   1
  。。。。。。。。。。。。。。。。。。。。。。。。。。
  # vim services.cfg #编辑services.cfg服务配置文件,定义一个服务
  define service {
  use                  generic-service
  host_name            151-web01
  service_descriptionDisk Partition
  check_command      check_nrpe!check_disk
  }
  # /etc/init.d/nagios checkconfig
  。。。。。。。。。。。。。。。。。。。。。。。。。
  Checking services...
  Error: Service check command 'check_nrpe' specified in service 'Disk Partition' for host '151-web01' not defined anywhere! #错误原因,没有定义服务检测(check_nrpe)命令
  Checked 1 services.
  Checking hosts...
  Warning: Host '150-nagios_server' has no services associated with it!
  Checked 2 hosts.
  Checking host groups...
  Checked 1 host groups.
  Checking service groups...
  。。。。。。。。。。。。。。。。。。。。。。。。。
  Total Warnings: 1
  Total Errors:   1
  。。。。。。。。。。。。。。。。。。。。。。。。。。
  CONFIG ERROR!Check your Nagios configuration.
  # vim commands.cfg
  在39行增加如下内容,定义服务检测命令
  39 # 'check_nrpe' command definition
  40 define command {
  41         command_name    check_nrpe
  42         command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
  43         }
  # /etc/init.d/nagios checkconfig
  。。。。。。。。。。。。。。。。。。。。。。。。。。
  Read object config files okay...
  Running pre-flight check on configuration data...
  Checking services...
  Checked 1 services.
  Checking hosts...
  Warning: Host '150-nagios_server' has no services associated with it!
  Checked 2 hosts.
  Checking host groups...
  Checked 1 host groups.
  。。。。。。。。。。。。。。。。。。。。。。。。。。。。
  Total Warnings: 1
  Total Errors:   0
  Things look okay - No serious problems were detected during the pre-flight check
  OK.
  # /usr/local/nagios/libexec/check_nrpe -H 192.168.0.151 -c check_disk
  DISK OK - free space: / 8970 MB (69% inode=92%);| /=3855MB;10814;12436;0;13518

  # /etc/init.d/nagios>  Running configuration check...done.
  Reloading nagios configuration...done
  在服务端的web界面查看主机和服务时出现如下错误:
  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.
  或
  It appears as though you do not have permission to view information for any of the services 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.
  解决方法:
  # vim cgi.cfg
  修改:
  157 authorized_for_all_services=nagiosadmin
  158 authorized_for_all_hosts=nagiosadmin
  为:
  157 authorized_for_all_services=oldboy
  158 authorized_for_all_hosts=oldboy
  因为前面定义验证用户时,定义成oldboy用户了

  # /etc/init.d/nagios>  Running configuration check...done.
  Reloading nagios configuration...done
  以下为web正常结果:
  Host Status Details For All Host Groups
  Limit Results:
  Host Sort by host name (ascending)Sort by host name (descending)Status Sort by host status (ascending)Sort by host status (descending)Last Check Sort by last check time (ascending)Sort by last check time (descending)Duration Sort by state duration (ascending)Sort by state duration time (descending)Status Information
  150-nagios_server
  View Service Details For This Host
  UP 01-02-2016 21:46:40 0d 0h 1m 46s+ PING OK - Packet loss = 0%, RTA = 0.05 ms
  151-web01
  View Service Details For This Host
  UP 01-02-2016 21:49:00 0d 0h 3m 12s PING OK - Packet loss = 0%, RTA = 0.27 ms
  Service Status Details For All Hosts
  Limit Results:
  Host Sort by host name (ascending)Sort by host name (descending)Service Sort by service name (ascending)Sort by service name (descending)Status Sort by service status (ascending)Sort by service status (descending)Last Check Sort by last check time (ascending)Sort by last check time (descending)Duration Sort by state duration (ascending)Sort by state duration time (descending)Attempt Sort by current attempt (ascending)Sort by current attempt (descending)Status Information
  151-web01
  Disk Partition
  OK 01-02-2016 21:46:10 0d 0h 4m 18s 1/3 DISK OK - free space: / 8970 MB (69% inode=92%):
  Nagios排错及监控Nagios常用本地资源:
  # vim services.cfg #增加如下服务
  define service { #http服务自己增加的
  use                  generic-service
  host_name            150-nagios_server
  service_descriptionhttp
  check_command      check_http!check_http
  }
  define service {
  use                  generic-service
  host_name            150-nagios_server
  service_descriptionload
  check_command      check_nrpe!check_load
  }
  define service {
  use                  generic-service
  host_name            150-nagios_server
  service_descriptionmem
  check_command      check_nrpe!check_mem
  }
  define service {
  use                  generic-service
  host_name            150-nagios_server
  service_descriptionswap
  check_command      check_nrpe!check_swap
  }
  define service {
  use                  generic-service
  host_name            150-nagios_server
  service_descriptioniostat
  check_command      check_nrpe!check_iostat
  }
  # /etc/init.d/nagios checkconfig
  。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
  Total Warnings: 0
  Total Errors:   0
  Things look okay - No serious problems were detected during the pre-flight check
  OK.

  # /etc/init.d/nagios>  Reloading nagios configuration...done
  查看web中服务的状态(在浏览器中):
  serviceStatus
  150-nagios_server   http    PENDING
  出现如上错误,等一会就好了
  如下出现如下错误:
  150-nagios_serverhttp   CRITICAL 01-03-2016 21:18:19 0d 0h 2m 51s 2/3 NRPE: Command 'check_http' not defined
  解决方法如下:
  # cd ..
  # ls
  cgi.cfg         nagios.cfgnrpe.cfg.oriresource.cfg
  htpasswd.usersnrpe.cfg    objects       services
  # vim nrpe.cfg
  在最后一行后面加入如下内容:
  command=/usr/local/nagios/libexec/check_http -w $ARG1$ -c $ARG2$
  # pkill nrpe
  # ps -ef|grep nrpe
  root   13431 128230 21:29 pts/0    00:00:00 grep nrpe
  # /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
  # ps -ef|grep nrpe                   nagios   13433   10 21:29 ?      00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
  root   13435 128230 21:29 pts/0    00:00:00 grep nrpe
  出现如下错误:
  Status
  CRITICAL
  Status Information
  HTTP CRITICAL: HTTP/1.1 403 Forbidden - 5159 bytes in 0.013 second response time
  HTTP CRITICAL: HTTP/1.1 200 OK - 254 bytes in 0.001 second response time
  解决方法如下:
  http服务用后面的主动模式监控!!!!!!!!
  Nagios(被动模式)实战配置过程总结说明:
  1.添加主机和主机组,如果有多台机器,用逗号分隔
  2.添加服务,可以定义多个服务(被动模式,用check_nrpe!check_http)
  3.检查语法
  4.重启nagios服务(重新加载配置,可以reload)
  端口及url地址(主动模式)监控企业案例实战:
  # cd libexec
  # ./check_tcp --help
  。。。。。。。。。。。。。。。
  Usage:
  check_tcp -H host -p port
  # ./check_tcp -H 192.168.0.151 -p 80
  TCP OK - 0.001 second response time on port 80|time=0.000811s;;;0.000000;10.000000
  # ./check_http -H 192.168.0.151 -p 80

  HTTP OK: HTTP/1.1 200 OK - 254 bytes in 0.001 second response time |time=0.000744s;;;0.000000>  # ./check_http --help
  。。。。。。。。。。。。。。。。。。。。。。。。
  Usage:
  check_http -H| -I[-u ] [-p ]
  # cd services
  # vim http.cfg
define service {  
      use                  generic-service
  
      host_name            150-nagios_server
  
      service_descriptionhttp
  
      check_command      check_weburl!-I 192.168.0.150
  
      max_check_attempts    3
  
      normal_check_interval 2
  
      retry_check_interval1
  
      check_period          24x7
  
      notification_interval 30
  
      notification_period   24x7
  
      notification_optionsw,u,c,r
  
      contact_groups      admins
  
   }
  
define service {
  
      use                  generic-service
  
      host_name            151-web01
  
      service_descriptionhttp
  
      check_command      check_weburl!-I 192.168.0.151
  
      max_check_attempts    3
  
      normal_check_interval 2
  
      retry_check_interval1
  
      check_period          24x7
  
      notification_interval 30
  
      notification_period   24x7
  
      notification_optionsw,u,c,r
  
      contact_groups      admins
  
   }
  # vim commands.cfg
  在'check_http' command 下面紧接着加入以下内容:
  # 'check_weburl' command definition
  define command{
  command_name    check_weburl
  command_line    $USER1$/check_http $ARG1$ -w 10 -c 30
  }
  # /etc/init.d/nagios checkconfig
  .................................................
  Total Warnings: 0
  Total Errors:   0
  Things look okay - No serious problems were detected during the pre-flight check
  OK.

  # /etc/init.d/nagios>  Running configuration check...done.
  Reloading nagios configuration...done
  nagios 服务分组定义:
  1.# vim nagios.cfg
  cfg_file=/usr/local/nagios/etc/objects/servicegroups.cfg#添加这一行
  2.在/usr/local/nagios/etc/objects/下建立servicegroups.cfg文件
  3.# vim servicegroups.cfg
  # check web
  define servicegroup{
  servicegroup_name       check_web
  alias                   check_web
  members               155,check_w1,156,check_w2,157,check_w3
  }
  #注意:members这里要写成:主机1,服务,主机2,服务...的形式,我之前就是这里出错了。
  4.# checknagios   #检测配置文件
  5.# service nagios restart
  url高级监控及MySQL-Rsync监控实战:
  # vim http.cfg
  在最后面增加如下监控端口的服务:
define service {  
      use                   generic-service
  
      host_name             151-web01
  
      service_description   port_80
  
      check_command         check_tcp!80
  
      max_check_attempts    3
  
      normal_check_interval 2
  
      retry_check_interval1
  
      check_period          24x7
  
      notification_interval 30
  
      notification_period   24x7
  
      notification_optionsw,u,c,r
  
      contact_groups      admins
  
   }
  
define service {
  
      use                   generic-service
  
      host_name             151-web01
  
      service_description   port_3306
  
      check_command         check_tcp!3306
  
      max_check_attempts    3
  
      normal_check_interval 2
  
      retry_check_interval1
  
      check_period          24x7
  
      notification_interval 30
  
      notification_period   24x7
  
      notification_optionsw,u,c,r
  
      contact_groups      admins
  
   }
  URL监控:
define service {  
      use                   generic-service
  
      host_name             151-web01
  
      service_description   URL
  
      check_command         check_weburl!-H www.test.com #此处定义的check_weburl和Nagios服务端自带的check_http等价,因此也可以在这用check_http
  
      max_check_attempts    3
  
      normal_check_interval 2
  
      retry_check_interval1
  
      check_period          24x7
  
      notification_interval 30
  
      notification_period   24x7
  
      notification_optionsw,u,c,r
  
      contact_groups      admins
  
   }
  # vi /etc/hosts
  添加解析www.test.com的主机,即做hosts映射(DNS解析)
  192.168.0.151 www.test.com
  保存退出
  # curl www.test.com
  192.168.0.151:test1-web
  # /usr/local/nagios/libexec/check_http -H www.test.com

  HTTP OK: HTTP/1.1 200 OK - 254 bytes in 0.001 second response time |time=0.000978s;;;0.000000>
  # /etc/init.d/nagios>  重新加载服务端的配置后,监控www.test.com成功
  高级URL监控:
  # vim http.cfg
  define service {
  use                   generic-service
  host_name             151-web01
  service_description   URL_3
  check_command         check_http!-H www.test.com -u "/test/55.html"
  max_check_attempts    3
  normal_check_interval 2
  retry_check_interval1
  check_period          24x7
  notification_interval 30
  notification_period   24x7
  notification_optionsw,u,c,r
  contact_groups      admins
  }
  # /usr/local/nagios/libexec/check_http -H www.test.com -u "/test/55.html"

  HTTP OK: HTTP/1.1 200 OK - 236 bytes in 0.001 second response time |time=0.000924s;;;0.000000>
  # /etc/init.d/nagios>  Running configuration check...done.
  Reloading nagios configuration...done
  重新加载服务端的配置后,监控www.test.com/test/55.html成功
  集群节点监控-服务分组实例-被动监控实战详解一例:
  方案:
  利用别名实现对集群下面同样节点的URL监控
  如:
  web1blog.etiantian.org,blog1.etiantian.org
  web2blog.etiantian.org,blog2.etiantian.org
  被动模式监控80端口:
  # cd /usr/local/nagios
  # ls
  binetcincludelibexecperlshare
  # cd libexec
  # ./check_tcp -H 192.168.0.151 -p 80
  TCP OK - 0.000 second response time on port 80|time=0.000187s;;;0.000000;10.000000
  # pwd
  /usr/local/nagios/libexec
  # /usr/local/nagios/libexec/check_tcp -H 192.168.0.151 -p 80
  TCP OK - 0.000 second response time on port 80|time=0.000216s;;;0.000000;10.000000
  # cd etc
  # ls
  nrpe.cfg
  # ls
  nrpe.cfg
  # vim nrpe.cfg
  在最后面追加:
  command=/usr/local/nagios/libexec/
  check_tcp -H 192.168.0.151 -p 80 -w 5 -c 10
  # /usr/local/nagios/libexec/check_tcp -H 192.168.0.151 -p 80 -w 5 -c 10
  TCP OK - 0.000 second response time on port 80|time=0.000214s;5.000000;10.000000;0.000000;10.000000
  # ps -ef|grep nrpe
  nagios   21090   10 04:20 ?      00:00:01 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
  root   21649 212010 07:05 pts/0    00:00:00 grep nrpe
  # pkill nrpe
  #/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
  # ps -ef|grep nrpe            nagios   21652   10 07:06 ?      00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
  root   21654 212010 07:06 pts/0    00:00:00 grep nrpe
  # cd /usr/local/nagios/libexec
  # ./check_nrpe -H 192.168.0.151 -c check_port
  TCP OK - 0.000 second response time on port 80|time=0.000181s;5.000000;10.000000;0.000000;10.000000
  # vim http.cfg
define service {  
      use                   generic-service
  
      host_name             151-web01
  
      service_description   port_80_beidong
  
      check_command         check_nrpe!check_port
  
      max_check_attempts    3
  
      normal_check_interval 2
  
      retry_check_interval1
  
      check_period          24x7
  
      notification_interval 30
  
      notification_period   24x7
  
      notification_optionsw,u,c,r
  
      contact_groups      admins
  
   }

  # /etc/init.d/nagios>  Running configuration check...done.
  Reloading nagios configuration...done
  呵呵,被动模式监控80端口成功
  作业:监控mysql主从同步(check_mysql),请用主动及被动分别监控主从同步
  自定义插件开发规范及监控密码文件变化案例实战详解:
  给Nagios插件程序提供两个返回值:一个是插件的退出状态码,另一个是插件在控制台打印的第一行数据。
  Nagios主程序可识别的状态码和说明如下:
  OK      ---退出代码 0-表示服务正常的工作
  WARNING   ---退出代码 1-表示服务处于警告状态
  CRITICAL---退出代码 2-表示服务处于紧急,严重状态
  UNKNOWN   ---退出代码 3-表示服务处于未知状态
  # head -7 utils.sh
  #! /bin/sh
  STATE_OK=0
  STATE_WARNING=1
  STATE_CRITICAL=2
  STATE_UNKNOWN=3
  STATE_DEPENDENT=4
  Nagios被动模式自定义插件开发及应用:
  # md5sum /etc/passwd
  97fac7fd2707bfd9a0edddf7083afc55/etc/passwd
  # md5sum /etc/passwd >/etc/zkq.md5
  # md5sum -c /etc/zkq.md5
  /etc/passwd: OK
  # vim check_passwd
  #!/bin/sh
  char=`md5sum -c /etc/zkq.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
  # chmod +x check_passwd
  # ll /usr/local/nagios/libexec/check_passwd
  -rwxr-xr-x 1 root root 167 Jan3 09:52 /usr/local/nagios/libexec/check_passwd
  # cd ..
  # cd etc
  # ls
  nrpe.cfg
  # vim nrpe.cfg
  在最后一行加入如下内容:
  command=/usr/local/nagios/libexec/check_
  passwd
  # pkill nrpe
  # /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
  # ps -ef|grep nrpe
  nagios   22735   10 10:00 ?      00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
  root   22737 222690 10:00 pts/0    00:00:00 grep nrpe
  # cd libexec
  # ./check_nrpe -H 192.168.0.151 -c check_passwd
  passwd is ok
  # cd ..
  # cd etc
  # ls
  cgi.cfg         nrpe.cfg      resource.cfg
  htpasswd.usersnrpe.cfg.oriservices
  nagios.cfg      objects
  # cd objects
  # ls
  commands.cfg   printer.cfg    timeperiods.cfg
  contacts.cfg   services.cfg   windows.cfg
  hosts.cfg      switch.cfg
  localhost.cfgtemplates.cfg
  # vim services.cfg
  在最后加入以下内容:
  define service {
  use                  generic-service
  host_name            151-web01
  service_descriptioncheck_passwd
  check_command      check_nrpe!check_passwd
  }
  # /etc/init.d/nagios checkconfig
  。。。。。。。。。。。。。。。。。。。。。。。。
  Total Warnings: 0
  Total Errors:   0
  Things look okay - No serious problems were detected during the pre-flight check
  OK.
  嘿嘿,被动模式自定义插件应用成功
  Nagios图形显示介绍及实战安装:
  #1、图形显示管理的依赖库
  yum install cairo pango zlib zlib-devel freetype freetype-devel gd gd-devel -y
  #2、rrdtools的依赖
  # tar xf libart_lgpl-2.3.17.tar.gz
  cd libart_lgpl-2.3.17
  ./configure
  make
  make install
  /bin/cp -r /usr/local/include/libart-2.0 /usr/include
  cd ../
  #3、rrdtools轮询的数据库,专门画图
  # tar xf rrdtool-1.2.14.tar.gz
  cd 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后出现上面的提示可以不用理会。
  make
  make install
  cd ../
  ls -l /usr/local/rrdtool/bin
  #4、PNP收集数据告诉rrdtools画图,PHP负责展示
  tar zxf pnp-0.4.14.tar.gz
  cd pnp-0.4.14
  ./configure \
  --with-rrdtool=/usr/local/rrdtool/bin/rrdtool --with-perfdata-dir=/usr/local/nagios/share/perfdata/
  #################
  #WARNING: The RRDs Perl Modules are not found on your System
  #         Using RRDs will speedup things in larger Installtions.
  #####################
  make all
  make install
  make install-config
  make install-init
  ll /usr/local/nagios/libexec/ |grep process
  问题: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
  配置出图
  # cd /usr/local/nagios/etc
  # vim nagios.cfg
  833 process_performance_data=1> /usr/local/nagios/var/service-perfdata.out
  }
  修改成如下配置
  # 'process-host-perfdata' command definition
  define command{
  command_name    process-host-perfdata
  command_line    /usr/local/nagios/libexec/process_perfdata.pl
  }
  # 'process-service-perfdata' command definition
  define command{
  command_name    process-service-perfdata
  command_line    /usr/local/nagios/libexec/process_perfdata.pl
  }
  也可以用nagios变量$USER1$替代/usr/local/nagios/libexec/路径
  执行检查语法命令/etc/init.d/nagios checkconfig
  Total Warnings: 0
  Total Errors:   0
  根据提示,配置通过。重启。
  3、主机出图的配置,模板或主机配置里hosts.cfg
  action_url            /nagios/pnp/index.php?host=$HOSTNAME$
  4、服务出图,模板或服务配置里hosts.cfg
  action_url            /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
  snmp协议(cacti),客户端安装或开启SNMP
  nrpe(nlient++)
  网络设备(路由器,交换机),开启SNMP
  短信报警的脚本开发及实战配置:
  1、添加联系人及联系组contacts.cfg
define contact{  
      contact_name                  oldboy-pager
  
      use                           generic-contact
  
      alias                            Nagios users
  
      pager                            18901398229
  
}
  2、添加报警的命令commands.cfg
  #command.cfg
  # 'notify-host-by-pager' command definition
define command{  
      command_name    notify-host-by-pager
  
      command_line    $USER1$/sms_send "Host $HOSTSTATE$ alert for $HOSTNAME$"$CONTACTPAGER$
  
      }
  
# 'notify-service-by-pager' command definition
  
define command{
  
      command_name    notify-service-by-pager
  
      command_line    $USER1$/sms_send "$HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$"$CONTACTPAGER$
  
      }
  3、调整联系人模板,添加报警的命令(来自于commands.cfg)
  编辑templates.cfg里的定义内容为:
define contact{  
      name                            generic-contact
  
      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,notify-service-by-fetion,notify-service-by-msn, notify-service-by-pager
  
      host_notification_commands      notify-host-by-email,notify-service-by-fetion,notify-host-by-msn, notify-host-by-pager
  
      register                        0
  
}
  4、hosts.cfg,services.cfg添加报警联系人及组,或者对应模板加
  contact_groups                  admins,groups1,groups2,user01
  开发短信报警脚本(短信网关是收费的)
  # pwd
  /usr/local/nagios/libexec
  # cat sms_send
  #!/bin/sh
  alert_date=$(date +%y-%m-%d" "%H:%M)
  TITLE=$1   #FORMAT "Host $HOSTSTATE$ alert for $HOSTNAME$"
  CONTACT=$2 #$2为手机号
  #curl方式
  curl-d cdkey=3RTY-EMY-0980-MTUQ2 -d password=189162 -d phone=$CONTACT -d message="$TITLE[${alert_date} oldboysa]" http://sdkhttp.eucp.b2m.cn/sdkproxy/sendsms.action
  #wget --quiet "http://s.ccme.cc/qxt/send.jsp?circle=159net_131&pwd=oldboy123&mobile=18911718229&service=f1fb0546-ebb6-0987-8f20-560524c1f88d&msgid=3956724&message=$TITLE[${alert_date} oldboysa n]"
  1、添加联系人及联系组contacts.cfg
define contact{  
      contact_name                  oldboy-pager
  
      use                           generic-contact
  
      alias                            Nagios users
  
      pager                            18901398229
  
}
  2、添加报警的命令commands.cfg
  #command.cfg
  # 'notify-host-by-pager' command definition
define command{  
      command_name    notify-host-by-pager
  
      command_line    $USER1$/sms_send "Host $HOSTSTATE$ alert for $HOSTNAME$"$CONTACTPAGER$
  
      }
  
# 'notify-service-by-pager' command definition
  
define command{
  
      command_name    notify-service-by-pager
  
      command_line    $USER1$/sms_send "$HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$"$CONTACTPAGER$
  
      }
  3、调整联系人模板,添加报警的命令(来自于commands.cfg)
  编辑templates.cfg里的定义内容为:
define contact{  
      name                            generic-contact
  
      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,notify-service-by-fetion,notify-service-by-msn, notify-service-by-pager
  
      host_notification_commands      notify-host-by-email,notify-service-by-fetion,notify-host-by-msn, notify-service-by-pager
  
      register                        0
  
}
  4、hosts.cfg,services.cfg添加报警联系人及组,或者对应模板加
  contact_groups                  admins,groups1,groups2,user01
  开发脚本批量生成Nagios配置文件实战:
  ########################
  批量建立nagios配置文件:
  ########################
  好多兄弟们都说nagios配置麻烦,都来问过我如何管理,为了提高解答效率,所以这里统一总结下,供大家参考。呵呵。
  这是 以前的一个例子,抛砖引玉下,你可以拓展做成菜单式等更详细的配置,如果你足够努力,可以做到一键 部署几百
  台服务器的服务监控,是没有问题的(包括host.cfg,service.cfg等),如果配置支持目录就更简单了。。欢迎随时交流。
  1.建立 机器名和IP对应列表
cat >host.list >new.host
  
echo "      host_name               `echo $line|awk '{print $1}'`" >>new.host
  
echo "      alias                   `echo $line|awk '{print $1}'` " >>new.host
  
echo "      address               `echo $line|awk '{print $2}'` " >>new.host
  
echo "}" >>new.host
  
done
  3.最终host内容
$ cat new.host  
define host {
  
      use                     linux-server
  
      host_name               oldboyVM-001
  
      alias                   oldboyVM-001
  
      address               192.168.1.1
  
}
  
define host {
  
      use                     linux-server
  
      host_name               oldboyVM-002
  
      alias                   oldboyVM-002
  
      address               192.168.1.2
  
}
  
define host {
  
      use                     linux-server
  
      host_name               oldboyVM-003
  
      alias                   oldboyVM-003
  
      address               192.168.1.3
  
}
  
define host {
  
      use                     linux-server
  
      host_name               oldboyVM-004
  
      alias                   oldboyVM-004
  
      address               192.168.1.4
  
}


页: [1]
查看完整版本: Nagios客户端安装