存档

2010年7月 的存档

Get memcached all items and deleted them if needed.

2010年7月14日 2 条评论

    I see there are so many api to operate memcached,such as get and set,but no list all items,so i write a shell just list all the items in memcached and we can delete the items when we don’t know the exactly items key.

#!bin/bash
# get_items_from_memcached.sh
# Usge: sh get_items_from_memcached.sh localhost port
# Exp:  sh get_items_from_memcached.sh 2hei.net 11211
# By: @2hei.net

items=`echo “stats items” | nc $1 $2|grep number|awk -F: ‘{print $2}’|awk ‘{printf(“%s “,$1) }’`
for i in ${items}
  do
    #get delete_items_list
    echo “stats cachedump $i 0” | nc $1 $2|awk -v HOST=$1 -v PORT=$2 ‘{if(length($2)>0) print “echo delete”,$2,” | nc”,HOST,PORT}’ >> $1_$2.txt
    #print all items
    echo “stats cachedump $i 0” | nc $1 $2|awk ‘{if($2) print $2}’
  done

##delete all items by item_list if needed
#/bin/sh $1_$2.txt

##END##

tips:
1. you just can use “flush_all” cmd
   echo “flush_all” | nc $1 $2
   
   “flush_all” is a command with an optional numeric argument. It always
succeeds, and the server sends “OK\r\n” in response (unless “noreply”
is given as the last parameter). Its effect is to invalidate all
existing items immediately (by default) or after the expiration specified.
   flush_all doesn’t actually free all the memory taken up by existing items; that
will happen gradually as new items are stored. The most precise
definition of what flush_all does is the following: it causes all
items whose update time is earlier than the time at which flush_all
was set to be executed to be ignored for retrieval purposes.

2. if your memcached has to many items, this shell will waste a long time, for it will establish a new connection when delete each time.
we can use other tools write by socket and do this in only one connection.

分类: OpenSource 标签:

write nagios nrpe plugin

2010年7月1日 1 条评论

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

分类: OpenSource, python 标签: