您的位置:首页 > 移动开发 > IOS开发

Nagios自定义扩展

2015-11-04 19:00 323 查看
原理:监控端通过check_nrpe把要监控的指令发送给被监控端,被监控端在本机执行监控任务,并把执行的结果发送回监控端。

# -*- coding: utf-8 -*-
#!/usr/bin/python
import mmap
import os
import sys
import getopt

def usage():
print """
check_log is a Nagios monitor logs Script

Usage:

check_log [-h|--help][-l|--log][-s|--string][-w|warning][-c|critical]

Options:
--help|-h)
check_log help.
--log|-l)
sets log file path.
--string|-s)
sets monitor Keywords.
--warning|-w)
sets Keywords quantity.Default is: off
--critical|-c)
sets Keywords quantity.Default is: off
example:
./check_log -l /var/log/nginx.log -s "502 Bad Gateway" -w 5 -c 10 """
sys.exit(3)

try:
options,args = getopt.getopt(sys.argv[1:],"hl:s:w:c:",["--help","log=","string=","warning=","critical="])
except getopt.GetoptError:
usage()

for n,v in options:
if n in ("-h","--help"):
usage()
if n in ("-l","--log"):
log = v
if n in ("-s","--string"):
string = v
if n in ("-w","--warning"):
warning = v
if n in ("-c","--critical"):
critical = v

if 'log' in dir() and 'string' in dir():
try:
file = open(log,"r+")
size = os.path.getsize(log)
data = mmap.mmap(file.fileno(),size)
# 用了mmap模块的功能!
text = data.read(-1)
counts = text.count(string)
counts = str(counts)
data.close()
file.close()
except IOError:
print "No such file or directory:"+log
else:
usage()

if 'warning' in dir() and 'critical' in dir():
if warning < critical:
if counts >= warning and counts < critical:
print 'WARNING - %s views %s' % (string,counts)
sys.exit(2)
elif counts >= critical:
print 'CRITICAL - %s views %s' % (string,counts)
sys.exit(1)
else:
print 'OK - %s views %s' % (string,counts)
sys.exit(0)
else:
print "Must critical > warning"
sys.exit(0)
else:
print 'OK - %s views %s' % (string,counts)
sys.exit(0)


View Code

command[check_mylog]=/usr/bin/python /usr/local/nagios/libexec/check_mylog -l /var/logs/nginx.log -s "No such file or directory" -w 2 -c 5


-------------------------------------------------------------------------------------------------------

查看配置文件 template.cfg,是否为主动检测模式

define service{
name                         passive_service
use                          generic-service
max_check_attempts           1
active_checks_enabled        0 #(关闭主动检测)
passive_checks_enabled       1 #(开启被动检测)
normal_check_interval        5
retry_check_interval         1
check_freshness              1 # (开启强制刷新)
notifications_enabled        1
notification_interval        5
notification_period          24x7
contact_groups               admins
register                     0 #(必须)
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: