chenqb 发表于 2015-11-23 07:46:12

2014年第一篇哦,nagios插件开发demo版

  2014年元旦,下班回来,洗涮完毕,床上一躺,腿一盘,笔记本一放,开搞,必须整明白nagios的很多方面的东西。昨晚搞定安装配置,报警等东西,今天搞定了自定义添加服务用的是nagios已有的插件check_mysql,接下来是自定义插件,扯淡完毕,开搞~~~
  nagios自带了一些监控服务,但是有的时候因为业务或者是特殊的需求,需要自己去写,下面是我写的一个demo,不断的google,百度去搜索去学习,基本弄明白了,然后就是怎么写出牛逼的监控脚本了,以后要多逼迫自己写python的而不是shell的
  首先,看看nagios自定义脚本的样式:
  

# cd /usr/lib/nagios/plugins/
check_breeze       check_file_age   check_ircd         check_mysql_querycheck_nwstat       check_simap      check_ups
check_by_ssh       check_flexlm       check_jabber       check_nagios       check_oracle       check_smtp         check_users
check_clamd      check_fping      check_ldap         check_nntp         check_overcr       check_snmp         check_wave
check_cluster      check_ftp          check_ldaps      check_nntps      check_pgsql      check_spop         eventhandlers/
check_dhcp         check_game         check_load         check_nrpe         check_ping         check_ssh          negate
check_dig          check_hpjd         check_log          check_nt         check_pop          check_ssmtp      netstat.sh
check_disk         check_http         check_mailq      check_ntp          check_procs      check_swap         urlize
check_disk_smb   check_icmp         check_mrtg         check_ntp_peer   check_real         check_tcp          utils.pm
check_dns          check_ide_smart    check_mrtgtraf   check_ntp.pl       check_rpc          check_time         utils.sh
check_dummy      check_imap         check_mysql      check_ntp_time   check_sensors      check_udp         
# cd /usr/lib/nagios/plugins/

目录下有个utils.sh的脚本,这就是一个nagios自带的简单的demo,netstat.sh是我写的一个demo  
  声明nagios的stat value
  

#! /bin/sh
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4然后就是普通的脚本了,需要在退出的时候,exit 相应的value  
  一个监控连接的脚本
  

#!/bin/bash
path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
total=`netstat -n | awk '/^tcp/ {++S[$NF]}END {for(a in S) print a, S}' |grep "ESTABLISHED" | awk '{print$NF}'`
if [ $total -lt 100 ];then
echo "TEST OK : The established status id $total"
exit $STATE_OK
elif [ $total -gt 100 -a $total -lt 200];then
echo "TEST WARNING : The established status id $total"
exit $STATE_WARNING
elif [ $total -gt 201 ];then
echo "TEST CRITICAL : The established status id $total"
exit $STATE_CRITICAL
else
echo "UNKNOWN STATE"
exit $STATE_UNKNOWN
fi
netstat.sh (END)

很简单的一个测试脚本。  
  写完了之后放到和各种check_XXXX的东西在一起,名字自定义,我就用的原始脚本的名字
  接下来是如何使用,其实这个使用和上一篇文章的check_mysql的使用是一样的,还是在墨迹的写一次吧
  1.添加到commands.cfg里面
  

# ll
total 44
-rw-rw-r-- 1 root root7982 Jan1 00:33 commands.cfg
-rw-rw-r-- 1 root root2275 Dec 31 18:40 contacts.cfg
drwxr-xr-x 2 root root4096 Jan1 00:34 hosts
-rw-rw-r-- 1 root root3124 Aug 31 06:28 printer.cfg
-rw-rw-r-- 1 root root3293 Aug 31 06:28 switch.cfg
-rw-rw-r-- 1 root root 11247 Dec 31 22:44 templates.cfg
-rw-rw-r-- 1 root root3208 Aug 31 06:28 timeperiods.cfg
-rw-rw-r-- 1 root root4019 Aug 31 06:28 windows.cfg
# vim commands.cfg 添加到文件的最后:  
  

#check netstat
define command{
command_name   netstat_check
command_line   $USER1$/netstat.sh
}
"commands.cfg" 252L, 7982Ccommand_name自定义,别重复  
  command就是如何使用这条命令
  2.使用命令
  

# cd hosts/
# ll
total 20
-rw-r--r-- 1 root root 5212 Jan1 00:34 front.cfg
-rw-r--r-- 1 root root143 Dec 30 22:37 group.cfg
-rw-r--r-- 1 root root 5403 Dec 30 21:49 localhost.cfg
#front.cfg 和localhost.cfg对应的是两台机器  
  修改front.cfg作为测试吧
  

define service{
use             local-service
host_name       Front1.Webserver
service_description   netstat_check
check_command         netstat_check
}
"front.cfg" 153L, 5212C在最后添加一个service,名字要和command.cfg里面的相符合  
  然后 重启nagios服务,这时候在nagios管理界面会看到相应的服务,但是显示的是pending,等一会就好了
  这样一个简单的netstat插件写好了。真的很简单啊。
  特别补充:
  1.自定义的监控脚本放在server机器的plugin下面
  2.默认的监控方式主动的监控
  3.脚本必须chmod a+x sh 可执行

-------------------------------------------------------------------------------------------------------------------------------------------------------------
  添加一个新的被监控节点
  1.首先子节点安装nrpe-cient
  # yum install nagios-plugins-nrpe.i686 nrpe.i686 2.修改nrpe.cfg
  allowed_hosts=192.168.74.143

3.nagios-server,添加一个clientc.cfg
  和其他的一样,cp一份已经存在的
  把下面对应的修改掉就可以了          host_name            Front2.Webserver
alias                  Front2.Webserver
address                192.168.74.128

然后重启nagios,这样就添加上了




  
页: [1]
查看完整版本: 2014年第一篇哦,nagios插件开发demo版