|
| 1 | +# |
| 2 | +# |
| 3 | +# Module: apc_status |
| 4 | +# Graphs the status of APC: Another PHP Cache |
| 5 | +# |
| 6 | +# Useage: To use this, you need to copy the apc-json.php file to your document root of the local webserver. |
| 7 | +# The path to the apc-json.php should be set in conf.d/apc_status.pyconf |
| 8 | +# |
| 9 | +# Author: Jacob V. Rasmussen (jacobvrasmussen@gmail.com) |
| 10 | +# Site: http://blackthorne.dk |
| 11 | +# |
| 12 | + |
| 13 | +import urllib2 |
| 14 | +import json |
| 15 | +import traceback |
| 16 | + |
| 17 | +NAME_PREFIX = "apc_" |
| 18 | + |
| 19 | +APC_STATUS_URL = "" |
| 20 | + |
| 21 | +descriptors = list() |
| 22 | +Desc_Skel = {} |
| 23 | +metric_list = { |
| 24 | + NAME_PREFIX + 'num_slots' : { 'type': 'uint', 'format' : '%d', 'unit': 'Slots', 'desc': 'Number of slots' }, |
| 25 | + NAME_PREFIX + 'num_hits' : { 'type': 'uint', 'format' : '%d', 'unit': 'Hits', 'desc': 'Number of cache hits' }, |
| 26 | + NAME_PREFIX + 'num_misses' : { 'type': 'uint', 'format' : '%d', 'unit': 'Misses', 'desc': 'Number of cache misses' }, |
| 27 | + NAME_PREFIX + 'num_inserts' : { 'type': 'uint', 'format' : '%d', 'unit': 'Inserts', 'desc': 'Number of cache inserts' }, |
| 28 | + NAME_PREFIX + 'expunges' : { 'type': 'uint', 'format' : '%d', 'unit': 'Deletes', 'desc': 'Number of cache deletes' }, |
| 29 | + NAME_PREFIX + 'mem_size' : { 'type': 'uint', 'format' : '%d', 'unit': 'Bytes', 'desc': 'Memory size' }, |
| 30 | + NAME_PREFIX + 'num_entries' : { 'type': 'uint', 'format' : '%d', 'unit': 'Entries', 'desc': 'Cached Files' }, |
| 31 | + NAME_PREFIX + 'uptime' : { 'type': 'uint', 'format' : '%d', 'unit': 'seconds', 'desc': 'Uptime' }, |
| 32 | + NAME_PREFIX + 'request_rate' : { 'type': 'float', 'format' : '%f', 'unit': 'requests/sec', 'desc': 'Request Rate (hits, misses)' }, |
| 33 | + NAME_PREFIX + 'hit_rate' : { 'type': 'float', 'format' : '%f', 'unit': 'requests/sec', 'desc': 'Hit Rate' }, |
| 34 | + NAME_PREFIX + 'miss_rate' : { 'type': 'float', 'format' : '%f', 'unit': 'requests/sec', 'desc': 'Miss Rate' }, |
| 35 | + NAME_PREFIX + 'insert_rate' : { 'type': 'float', 'format' : '%f', 'unit': 'requests/sec', 'desc': 'Insert Rate' }, |
| 36 | + NAME_PREFIX + 'num_seg' : { 'type': 'uint', 'format' : '%d', 'unit': 'fragments', 'desc': 'Segments' }, |
| 37 | + NAME_PREFIX + 'mem_avail' : { 'type': 'uint', 'format' : '%d', 'unit': 'bytes', 'desc': 'Free Memory' }, |
| 38 | + NAME_PREFIX + 'mem_used' : { 'type': 'uint', 'format' : '%d', 'unit': 'bytes', 'desc': 'Used Memory' }, |
| 39 | + } |
| 40 | + |
| 41 | +def get_value(name): |
| 42 | + try: |
| 43 | + req = urllib2.Request(APC_STATUS_URL, None, {'user-agent':'ganglia-apc-python'}) |
| 44 | + opener = urllib2.build_opener() |
| 45 | + f = opener.open(req) |
| 46 | + apc_stats = json.load(f) |
| 47 | + |
| 48 | + except urllib2.URLError: |
| 49 | + traceback.print_exc() |
| 50 | + |
| 51 | + return apc_stats[name[len(NAME_PREFIX):]] |
| 52 | + |
| 53 | +def create_desc(prop): |
| 54 | + d = Desc_Skel.copy() |
| 55 | + for k,v in prop.iteritems(): |
| 56 | + d[k] = v |
| 57 | + return d |
| 58 | + |
| 59 | +def metric_init(params): |
| 60 | + global descriptors, Desc_Skel, APC_STATUS_URL |
| 61 | + |
| 62 | + if "metric_group" not in params: |
| 63 | + params["metric_group"] = "apc_cache" |
| 64 | + |
| 65 | + Desc_Skel = { |
| 66 | + 'name' : 'XXX', |
| 67 | + 'call_back' : get_value, |
| 68 | + 'time_max' : 60, |
| 69 | + 'value_type' : 'uint', |
| 70 | + 'units' : 'proc', |
| 71 | + 'slope' : 'both', |
| 72 | + 'format' : '%d', |
| 73 | + 'description' : 'XXX', |
| 74 | + 'groups' : params["metric_group"], |
| 75 | + } |
| 76 | + |
| 77 | + if "refresh_rate" not in params: |
| 78 | + params["refresh_rate"] = 15 |
| 79 | + |
| 80 | + if "url" not in params: |
| 81 | + params["url"] = "http://localhost/apc-json.php" |
| 82 | + |
| 83 | + |
| 84 | + APC_STATUS_URL = params["url"] |
| 85 | + |
| 86 | + if "spoof_host" in params: |
| 87 | + Desc_Skel["spoof_host"] = params["spoof_host"] |
| 88 | + |
| 89 | + for k,v in metric_list.iteritems(): |
| 90 | + descriptors.append(create_desc({ |
| 91 | + "name" : k, |
| 92 | + "call_back" : get_value, |
| 93 | + "value_type" : v["type"], |
| 94 | + "units" : v["unit"], |
| 95 | + "format" : v["format"], |
| 96 | + "description" : v["desc"], |
| 97 | + })) |
| 98 | + |
| 99 | + return descriptors |
| 100 | + |
| 101 | +def metric_cleanup(): |
| 102 | + pass |
| 103 | + |
| 104 | +if __name__ == '__main__': |
| 105 | + metric_init({}) |
| 106 | + for d in descriptors: |
| 107 | + v = d['call_back'](d['name']) |
| 108 | + print 'value for %s is %s' % (d['name'], v) |
| 109 | + |
| 110 | + |
0 commit comments