Skip to content

Commit

Permalink
Merge pull request ganglia#87 from blackthornedk/master
Browse files Browse the repository at this point in the history
Added APC Status
  • Loading branch information
jbuchbinder committed Sep 26, 2012
2 parents 4b228b3 + a6d1e9f commit 9748f51
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 0 deletions.
13 changes: 13 additions & 0 deletions apc_status/README.mkdn
@@ -0,0 +1,13 @@
apc_status
===============

python module for ganglia 3.1.

"apc_status" sends metrics on Another PHP Cache process status refering to
apc-json.php.

To use this you will need to copy apc-json.php to your webdir.

## AUTHOR

Jacob V. Rasmussen <jacobvrasmussen@gmail.com>
97 changes: 97 additions & 0 deletions apc_status/conf.d/apc_status.pyconf
@@ -0,0 +1,97 @@
modules {
module {
name = "apc_status"
language = "python"

# URL of the resident apc-json.php script, which will translate the APC figures to JSON
param url {
value = "http://localhost/apc-json.php"
}

# Which metric group should these metrics be put into
param metric_group {
value = "apc_cache"
}
}
}

collection_group {
collect_every = 30
time_threshold = 90

metric {
name = "apc_mem_size"
title = "Total Memory"
value_threshold = 0
}
metric {
name = "apc_mem_avail"
title = "Free Memory"
value_threshold = 0
}
metric {
name = "apc_mem_used"
title = "Used Memory"
value_threshold = 0
}
metric {
name = "apc_num_slots"
title = "Number of Slots"
value_threshold = 0
}
metric {
name = "apc_num_hits"
title = "Number of Cache Hits"
value_threshold = 0
}
metric {
name = "apc_num_misses"
title = "Number of Cache Misses"
value_threshold = 0
}
metric {
name = "apc_num_inserts"
title = "Number of Cache Inserts"
value_threshold = 0
}
metric {
name = "apc_expunges"
title = "Number of Cache Deletes"
value_threshold = 0
}
metric {
name = "apc_num_entries"
title = "Cached Files"
value_threshold = 0
}
metric {
name = "apc_num_seg"
title = "Segments"
value_threshold = 0
}
metric {
name = "apc_uptime"
title = "Uptime"
value_threshold = 0
}
metric {
name = "apc_request_rate"
title = "Request Rate (hits, misses)"
value_threshold = 0.0
}
metric {
name = "apc_hit_rate"
title = "Hit Rate"
value_threshold = 0.0
}
metric {
name = "apc_miss_rate"
title = "Miss Rate"
value_threshold = 0.0
}
metric {
name = "apc_insert_rate"
title = "Insert Rate"
value_threshold = 0.0
}
}
44 changes: 44 additions & 0 deletions apc_status/document_root/apc-json.php
@@ -0,0 +1,44 @@
<?php
#
# APC JSON builder
# Used for Ganglia APC Status module
#
# Author: Jacob V. Rasmussen (jacobvrasmussen@gmail.com)
# Site: http://blackthorne.dk
#

header("Content-type: text/plain");

function cmp_cache_list($a, $b)
{
return ($b['num_hits'] - $a['num_hits']);
}

if ($_SERVER["REMOTE_ADDR"] == "127.0.0.1" || TRUE)
{
$cache = apc_cache_info();
$mem = apc_sma_info();
$cache['uptime'] = time() - $cache['start_time'];
$cache['request_rate'] = ($cache['num_hits'] + $cache['num_misses']) / $cache['uptime'];
$cache['hit_rate'] = $cache['num_hits'] / $cache['uptime'];
$cache['miss_rate'] = $cache['num_misses'] / $cache['uptime'];
$cache['insert_rate'] = $cache['num_inserts'] / $cache['uptime'];
$cache['num_seg'] = $mem['num_seg'];
$cache['mem_size'] = $mem['num_seg'] * $mem['seg_size'];
$cache['mem_avail'] = $mem['avail_mem'];
$cache['mem_used'] = $cache['mem_size'] - $cache['mem_avail'];

$cache_list = $cache['cache_list'];
usort($cache_list, 'cmp_cache_list');

unset($cache['cache_list']); //lots of info that we don't need for a brief status
unset($cache['deleted_list']); // ditto

if (@$_REQUEST['debug'] == '1')
{
print_r($cache);
print_r($mem);
print_r($cache_list);
}
echo json_encode($cache);
}
110 changes: 110 additions & 0 deletions apc_status/python_modules/apc_status.py
@@ -0,0 +1,110 @@
#
#
# Module: apc_status
# Graphs the status of APC: Another PHP Cache
#
# Useage: To use this, you need to copy the apc-json.php file to your document root of the local webserver.
# The path to the apc-json.php should be set in conf.d/apc_status.pyconf
#
# Author: Jacob V. Rasmussen (jacobvrasmussen@gmail.com)
# Site: http://blackthorne.dk
#

import urllib2
import json
import traceback

NAME_PREFIX = "apc_"

APC_STATUS_URL = ""

descriptors = list()
Desc_Skel = {}
metric_list = {
NAME_PREFIX + 'num_slots' : { 'type': 'uint', 'format' : '%d', 'unit': 'Slots', 'desc': 'Number of slots' },
NAME_PREFIX + 'num_hits' : { 'type': 'uint', 'format' : '%d', 'unit': 'Hits', 'desc': 'Number of cache hits' },
NAME_PREFIX + 'num_misses' : { 'type': 'uint', 'format' : '%d', 'unit': 'Misses', 'desc': 'Number of cache misses' },
NAME_PREFIX + 'num_inserts' : { 'type': 'uint', 'format' : '%d', 'unit': 'Inserts', 'desc': 'Number of cache inserts' },
NAME_PREFIX + 'expunges' : { 'type': 'uint', 'format' : '%d', 'unit': 'Deletes', 'desc': 'Number of cache deletes' },
NAME_PREFIX + 'mem_size' : { 'type': 'uint', 'format' : '%d', 'unit': 'Bytes', 'desc': 'Memory size' },
NAME_PREFIX + 'num_entries' : { 'type': 'uint', 'format' : '%d', 'unit': 'Entries', 'desc': 'Cached Files' },
NAME_PREFIX + 'uptime' : { 'type': 'uint', 'format' : '%d', 'unit': 'seconds', 'desc': 'Uptime' },
NAME_PREFIX + 'request_rate' : { 'type': 'float', 'format' : '%f', 'unit': 'requests/sec', 'desc': 'Request Rate (hits, misses)' },
NAME_PREFIX + 'hit_rate' : { 'type': 'float', 'format' : '%f', 'unit': 'requests/sec', 'desc': 'Hit Rate' },
NAME_PREFIX + 'miss_rate' : { 'type': 'float', 'format' : '%f', 'unit': 'requests/sec', 'desc': 'Miss Rate' },
NAME_PREFIX + 'insert_rate' : { 'type': 'float', 'format' : '%f', 'unit': 'requests/sec', 'desc': 'Insert Rate' },
NAME_PREFIX + 'num_seg' : { 'type': 'uint', 'format' : '%d', 'unit': 'fragments', 'desc': 'Segments' },
NAME_PREFIX + 'mem_avail' : { 'type': 'uint', 'format' : '%d', 'unit': 'bytes', 'desc': 'Free Memory' },
NAME_PREFIX + 'mem_used' : { 'type': 'uint', 'format' : '%d', 'unit': 'bytes', 'desc': 'Used Memory' },
}

def get_value(name):
try:
req = urllib2.Request(APC_STATUS_URL, None, {'user-agent':'ganglia-apc-python'})
opener = urllib2.build_opener()
f = opener.open(req)
apc_stats = json.load(f)

except urllib2.URLError:
traceback.print_exc()

return apc_stats[name[len(NAME_PREFIX):]]

def create_desc(prop):
d = Desc_Skel.copy()
for k,v in prop.iteritems():
d[k] = v
return d

def metric_init(params):
global descriptors, Desc_Skel, APC_STATUS_URL

if "metric_group" not in params:
params["metric_group"] = "apc_cache"

Desc_Skel = {
'name' : 'XXX',
'call_back' : get_value,
'time_max' : 60,
'value_type' : 'uint',
'units' : 'proc',
'slope' : 'both',
'format' : '%d',
'description' : 'XXX',
'groups' : params["metric_group"],
}

if "refresh_rate" not in params:
params["refresh_rate"] = 15

if "url" not in params:
params["url"] = "http://localhost/apc-json.php"


APC_STATUS_URL = params["url"]

if "spoof_host" in params:
Desc_Skel["spoof_host"] = params["spoof_host"]

for k,v in metric_list.iteritems():
descriptors.append(create_desc({
"name" : k,
"call_back" : get_value,
"value_type" : v["type"],
"units" : v["unit"],
"format" : v["format"],
"description" : v["desc"],
}))

return descriptors

def metric_cleanup():
pass

if __name__ == '__main__':
metric_init({})
for d in descriptors:
v = d['call_back'](d['name'])
print 'value for %s is %s' % (d['name'], v)


0 comments on commit 9748f51

Please sign in to comment.