collectd-memcached-bucket

Grab stats for individual buckets in the NorthMemCouchScaleBase bucket engine and shove them into collectd
git clone https://code.literati.org/collectd-memcached-bucket.git
Log | Files | Refs

collectd_memcached_buckets.py (3922B)


      1 #!/usr/bin/python
      2 import sys, time, re
      3 sys.path = ['/usr/local/lib/python2.6/site-packages'] + sys.path
      4 
      5 import mc_bin_client
      6 import pycollectd
      7 
      8 """
      9 memcached_command       value:COUNTER:0:U
     10 memcached_connections   value:GAUGE:0:U
     11 memcached_items         value:GAUGE:0:U
     12 memcached_octets        rx:COUNTER:0:4294967295, tx:COUNTER:0:4294967295
     13 memcached_ops           value:COUNTER:0:134217728
     14 
     15 APPEND_STAT("cmd_get", "%"PRIu64, thread_stats.get_cmds);
     16 APPEND_STAT("cmd_set", "%"PRIu64, slab_stats.set_cmds);
     17 APPEND_STAT("cmd_flush", "%"PRIu64, thread_stats.flush_cmds);
     18 APPEND_STAT("get_hits", "%"PRIu64, slab_stats.get_hits);
     19 APPEND_STAT("get_misses", "%"PRIu64, thread_stats.get_misses);
     20 APPEND_STAT("delete_misses", "%"PRIu64, thread_stats.delete_misses);
     21 APPEND_STAT("delete_hits", "%"PRIu64, slab_stats.delete_hits);
     22 APPEND_STAT("incr_misses", "%"PRIu64, thread_stats.incr_misses);
     23 APPEND_STAT("incr_hits", "%"PRIu64, thread_stats.incr_hits);
     24 APPEND_STAT("decr_misses", "%"PRIu64, thread_stats.decr_misses);
     25 APPEND_STAT("decr_hits", "%"PRIu64, thread_stats.decr_hits);
     26 APPEND_STAT("cas_misses", "%"PRIu64, thread_stats.cas_misses);
     27 APPEND_STAT("cas_hits", "%"PRIu64, slab_stats.cas_hits);
     28 APPEND_STAT("cas_badval", "%"PRIu64, slab_stats.cas_badval);
     29 APPEND_STAT("bytes_read", "%"PRIu64, thread_stats.bytes_read);
     30 APPEND_STAT("bytes_written", "%"PRIu64, thread_stats.bytes_written);
     31 
     32 
     33 """
     34 
     35 MASK = 134217727
     36 
     37 def get_hostname_from_collectd_config(fn):
     38     data = open(fn).read()
     39     r = re.compile('^Hostname\s*"([^"]+)"$', re.IGNORECASE|re.MULTILINE)
     40     m = r.search(data)
     41     return m.group(1) if m is not None else None
     42 
     43 
     44 def get_bucket_stats(mc):
     45     ops = ['cmd_get',
     46            'cmd_set',
     47            'cmd_flush',
     48            'get_hits',
     49            'get_misses',
     50            'delete_misses',
     51            'delete_hits',
     52            'incr_misses',
     53            'incr_hits',
     54            'decr_misses',
     55            'decr_hits',
     56            'cas_misses',
     57            'cas_hits',
     58            'cas_badval',
     59            'bytes_read',
     60            'bytes_written',
     61            'evictions',
     62           ]
     63 
     64     stats = mc.stats()
     65     return [ int(stats[op]) & MASK for op in ops ]
     66 
     67 
     68 def put_values(c, hostname, bucket, now, values):
     69     identifier = pycollectd.Identifier(hostname, 'memcached_bucket', None, 'memcached_bucket', bucket)
     70     c.putval(identifier, now, values)
     71 
     72 
     73 def list_buckets(mc):
     74     # return mc.bucket_list()
     75     buckets = []
     76     for line in open('/var/db/memcached.pw'):
     77         buckets.append(line.split()[0])
     78 
     79     return buckets
     80 
     81 def main():
     82     from optparse import OptionParser
     83 
     84     parser = OptionParser()
     85     parser.add_option('-H', '--hostname', dest='hostname')
     86     parser.add_option('-m', '--memcached-host', dest='memcached_host')
     87     parser.add_option('-u', '--memcached-username', dest='memcached_username')
     88     parser.add_option('-p', '--memcached-password', dest='memcached_password')
     89 
     90     options, args = parser.parse_args()
     91 
     92     mc = mc_bin_client.MemcachedClient(options.memcached_host or '127.0.0.1')
     93     if options.memcached_username:
     94         mc.sasl_auth_plain(options.memcached_username, options.memcached_password)
     95 
     96     hostname = options.hostname or get_hostname_from_collectd_config('/etc/collectd/collectd.conf') 
     97     assert hostname
     98     c = pycollectd.Exec()
     99     l = list_buckets(mc)
    100     now = time.time()
    101     totals = None
    102     for bucket in l:
    103         try:
    104             mc.bucket_select(bucket)
    105             values = get_bucket_stats(mc)
    106             if totals is None:
    107                 totals = values[:]
    108             else:
    109                 for i in xrange(len(totals)):
    110                     totals[i] += values[i]
    111                     totals[i] &= MASK
    112 
    113             put_values(c, hostname, bucket, now, values)
    114         except:
    115             #import traceback
    116             #traceback.print_exc()
    117             pass
    118 
    119     put_values(c, hostname, 'ALL', now, totals)
    120 
    121 
    122 if __name__ == '__main__':
    123     main()
    124