Skip to content

Commit 9748f51

Browse files
committed
Merge pull request #87 from blackthornedk/master
Added APC Status
2 parents 4b228b3 + a6d1e9f commit 9748f51

File tree

4 files changed

+264
-0
lines changed

4 files changed

+264
-0
lines changed

apc_status/README.mkdn

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apc_status
2+
===============
3+
4+
python module for ganglia 3.1.
5+
6+
"apc_status" sends metrics on Another PHP Cache process status refering to
7+
apc-json.php.
8+
9+
To use this you will need to copy apc-json.php to your webdir.
10+
11+
## AUTHOR
12+
13+
Jacob V. Rasmussen <jacobvrasmussen@gmail.com>

apc_status/conf.d/apc_status.pyconf

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
modules {
2+
module {
3+
name = "apc_status"
4+
language = "python"
5+
6+
# URL of the resident apc-json.php script, which will translate the APC figures to JSON
7+
param url {
8+
value = "http://localhost/apc-json.php"
9+
}
10+
11+
# Which metric group should these metrics be put into
12+
param metric_group {
13+
value = "apc_cache"
14+
}
15+
}
16+
}
17+
18+
collection_group {
19+
collect_every = 30
20+
time_threshold = 90
21+
22+
metric {
23+
name = "apc_mem_size"
24+
title = "Total Memory"
25+
value_threshold = 0
26+
}
27+
metric {
28+
name = "apc_mem_avail"
29+
title = "Free Memory"
30+
value_threshold = 0
31+
}
32+
metric {
33+
name = "apc_mem_used"
34+
title = "Used Memory"
35+
value_threshold = 0
36+
}
37+
metric {
38+
name = "apc_num_slots"
39+
title = "Number of Slots"
40+
value_threshold = 0
41+
}
42+
metric {
43+
name = "apc_num_hits"
44+
title = "Number of Cache Hits"
45+
value_threshold = 0
46+
}
47+
metric {
48+
name = "apc_num_misses"
49+
title = "Number of Cache Misses"
50+
value_threshold = 0
51+
}
52+
metric {
53+
name = "apc_num_inserts"
54+
title = "Number of Cache Inserts"
55+
value_threshold = 0
56+
}
57+
metric {
58+
name = "apc_expunges"
59+
title = "Number of Cache Deletes"
60+
value_threshold = 0
61+
}
62+
metric {
63+
name = "apc_num_entries"
64+
title = "Cached Files"
65+
value_threshold = 0
66+
}
67+
metric {
68+
name = "apc_num_seg"
69+
title = "Segments"
70+
value_threshold = 0
71+
}
72+
metric {
73+
name = "apc_uptime"
74+
title = "Uptime"
75+
value_threshold = 0
76+
}
77+
metric {
78+
name = "apc_request_rate"
79+
title = "Request Rate (hits, misses)"
80+
value_threshold = 0.0
81+
}
82+
metric {
83+
name = "apc_hit_rate"
84+
title = "Hit Rate"
85+
value_threshold = 0.0
86+
}
87+
metric {
88+
name = "apc_miss_rate"
89+
title = "Miss Rate"
90+
value_threshold = 0.0
91+
}
92+
metric {
93+
name = "apc_insert_rate"
94+
title = "Insert Rate"
95+
value_threshold = 0.0
96+
}
97+
}

apc_status/document_root/apc-json.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
#
3+
# APC JSON builder
4+
# Used for Ganglia APC Status module
5+
#
6+
# Author: Jacob V. Rasmussen (jacobvrasmussen@gmail.com)
7+
# Site: http://blackthorne.dk
8+
#
9+
10+
header("Content-type: text/plain");
11+
12+
function cmp_cache_list($a, $b)
13+
{
14+
return ($b['num_hits'] - $a['num_hits']);
15+
}
16+
17+
if ($_SERVER["REMOTE_ADDR"] == "127.0.0.1" || TRUE)
18+
{
19+
$cache = apc_cache_info();
20+
$mem = apc_sma_info();
21+
$cache['uptime'] = time() - $cache['start_time'];
22+
$cache['request_rate'] = ($cache['num_hits'] + $cache['num_misses']) / $cache['uptime'];
23+
$cache['hit_rate'] = $cache['num_hits'] / $cache['uptime'];
24+
$cache['miss_rate'] = $cache['num_misses'] / $cache['uptime'];
25+
$cache['insert_rate'] = $cache['num_inserts'] / $cache['uptime'];
26+
$cache['num_seg'] = $mem['num_seg'];
27+
$cache['mem_size'] = $mem['num_seg'] * $mem['seg_size'];
28+
$cache['mem_avail'] = $mem['avail_mem'];
29+
$cache['mem_used'] = $cache['mem_size'] - $cache['mem_avail'];
30+
31+
$cache_list = $cache['cache_list'];
32+
usort($cache_list, 'cmp_cache_list');
33+
34+
unset($cache['cache_list']); //lots of info that we don't need for a brief status
35+
unset($cache['deleted_list']); // ditto
36+
37+
if (@$_REQUEST['debug'] == '1')
38+
{
39+
print_r($cache);
40+
print_r($mem);
41+
print_r($cache_list);
42+
}
43+
echo json_encode($cache);
44+
}
+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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

Comments
 (0)