| 1 | #! /usr/bin/env python |
|---|
| 2 | ''' |
|---|
| 3 | |
|---|
| 4 | 2007 Kakikubo Teruo |
|---|
| 5 | check_memcache.py |
|---|
| 6 | |
|---|
| 7 | Check Memcached Server plugin. |
|---|
| 8 | This Plugin requires the memcached-Python API. |
|---|
| 9 | This Plugin was tested only in the python 2.5 |
|---|
| 10 | |
|---|
| 11 | ''' |
|---|
| 12 | import memcache |
|---|
| 13 | import signal |
|---|
| 14 | import os,sys |
|---|
| 15 | |
|---|
| 16 | from optparse import OptionParser |
|---|
| 17 | |
|---|
| 18 | pid = str(os.getpid()) |
|---|
| 19 | # option |
|---|
| 20 | parser = OptionParser() |
|---|
| 21 | parser.add_option("-H","--hostname",dest="hostaddress", |
|---|
| 22 | help="Host Address", metavar=" <hostaddr>") |
|---|
| 23 | parser.add_option("-t","--timeout",dest="timeout", |
|---|
| 24 | help="timeout x seconds", metavar=" <timeout>", default=10) |
|---|
| 25 | parser.add_option("-p","--port",dest="port", |
|---|
| 26 | help="port number", metavar=" <port number>" , default=11211) |
|---|
| 27 | (opts,args)=parser.parse_args() |
|---|
| 28 | |
|---|
| 29 | if not opts.hostaddress: |
|---|
| 30 | parser.print_help() |
|---|
| 31 | parser.error("UNKNOWN - Please specify Host") |
|---|
| 32 | exit(2) |
|---|
| 33 | |
|---|
| 34 | svr = u"" |
|---|
| 35 | svr = str(opts.hostaddress) + ":" + str(opts.port) |
|---|
| 36 | mc = memcache.Client([ svr ], debug=0) |
|---|
| 37 | #mc = memcache.Client({opts.hostaddress}:{opts.port}, debug=1) |
|---|
| 38 | #mc = memcache.Client(["ocnblg-tc01-int:112"], debug=1) |
|---|
| 39 | |
|---|
| 40 | def handler(signum, frame): |
|---|
| 41 | #print 'Signal handler called with signal', signum |
|---|
| 42 | #raise IOError, "hogehogehgoehgoe" |
|---|
| 43 | print "CRITICAL - timeout after " + str(opts.timeout) + " seconds" |
|---|
| 44 | sys.exit(2) |
|---|
| 45 | |
|---|
| 46 | ## Set the signal handler |
|---|
| 47 | signal.signal(signal.SIGALRM, handler) |
|---|
| 48 | signal.alarm(int(opts.timeout)) |
|---|
| 49 | |
|---|
| 50 | if not mc.set(pid,"nagios value"): |
|---|
| 51 | print "CRITICAL - cannot set the value" |
|---|
| 52 | sys.exit(2) |
|---|
| 53 | else: |
|---|
| 54 | if not mc.get(pid) == 'nagios value': |
|---|
| 55 | print "CRITICAL - cannot get the value" |
|---|
| 56 | sys.exit(2) |
|---|
| 57 | else: |
|---|
| 58 | mc.delete(pid) |
|---|
| 59 | print "OK - memcached alive" |
|---|
| 60 | sys.exit(0) |
|---|
| 61 | |
|---|