当前位置: 首页 > OpenSource, python > 正文

write nagios nrpe plugin

Scripts and executables must do two things (at a minimum) in order to function as Nagios plugins:
1.Exit with one of several possible return values
2.Return at least one line of text output to STDOUT

Plugin Return Code Service State Host State
0 OK UP
1 WARNING UP or DOWN/UNREACHABLE*
2 CRITICAL DOWN/UNREACHABLE
3 UNKNOWN DOWN/UNREACHABLE
Note: If the use_aggressive_host_checking option is enabled, return codes of 1 will result in a host
state of DOWN or UNREACHABLE. Otherwise return codes of 1 will result in a host state of UP.

Plugin Output Spec
At a minimum, plugins should return at least one of text output. Beginning with Nagios 3, plugins can
optionally return multiple lines of output. Plugins may also return optional performance data that can
be processed by external applications. The basic format for plugin output is shown below:
TEXT OUTPUT | OPTIONAL PERFDATA
LONG TEXT LINE 1
LONG TEXT LINE 2

LONG TEXT LINE N | PERFDATA LINE 2
PERFDATA LINE 3

PERFDATA LINE N

this is my python scripts:
#!/usr/bin/evn python
# -*- coding: utf-8 -*-

import sys,getopt
import memcache

memcached_host=’2hei.net’
memcached_port=11211
Warning_item=120
Critical_item=20

def usage():
    print “””
Usage: check_memcached [-h|–help] [-w|–warning curr_items] [-c|–critical curr_items]”
Warning curr_items defaults to 120
Critical curr_items defaults to 20
“””
    sys.exit(3)

#get curr_items from memcache stats
def get_memcache_curr_items(mc):
    #mc = memcache.Client([memcached_host+’:’+str(memcached_port)], debug=0)
    stats = mc.get_stats()[0][1]   
    #for i in xrange(0,100):
    #    mc.set(‘key’+str(i),’value’+str(i))
    #for k,v in stats.items():
    #    print k,v
    items = stats.get(‘curr_items’)
    return items

if __name__ == “__main__”:
    warning_item = 0
    critical_item = 0

    try:
        options, args = getopt.getopt(sys.argv[1:],”h:w:c:”,”–help –warning= –critical=”,)
    except getopt.GetoptError:
        usage()
        sys.exit(3)

    try:
        mc = memcache.Client([memcached_host+’:’+str(memcached_port)], debug=0)
        items = get_memcache_curr_items(mc)
        mc.disconnect_all()
    except Exception:
        print “Cannot get memcache’s curr_items.”,Exception
        sys.exit(3)

    for name, value in options:
        if name in (“-h”, “–help”):
            usage()
            sys.exit(3)
        if name in (“-w”, “–warning”):
            warning_item = value
        if name in (“-c”, “–critical”):
            critical_item = value

    if warning_item == 0:
        warning_item = Warning_item
    if critical_item == 0:
        critical_item = Critical_item

    if int(items) <= int(critical_item):
        print ‘MEMCACHED_ITEM CRITICAL: curr_items is:’,items
        sys.exit(2)
    if int(items) <= int(warning_item):
        print ‘MEMCACHED_ITEM WARNING: curr_items is:’,items
        sys.exit(1)
    else:
        print ‘MEMCACHED_ITEM OK: curr_items is:’,items
        sys.exit(0)

when encounter errors:
CHECK_NRPE: No output returned from daemon.
or
CHECK_NRPE: Received 0 bytes from daemon.  Check the remote server logs for error messages.
this shows your plugins return output is null

本文固定链接: https://www.2hei.net/2010/07/01/write_nagios_nrpe_plugin/ | 2hei.net

该日志由 u2 于2010年07月01日发表在 OpenSource, python 分类下,
原创文章转载请注明: write nagios nrpe plugin | 2hei.net
关键字:

write nagios nrpe plugin:目前有1 条留言