forked from ganglia/gmond_python_modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscribe_stats.py
More file actions
96 lines (70 loc) · 2.18 KB
/
scribe_stats.py
File metadata and controls
96 lines (70 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import subprocess
import re
import time
from subprocess import Popen, PIPE, STDOUT
descriptors = list()
Debug = False
last_mps_timestamp = float(0)
last_mps_value = 0
def dprint(f, *v):
if Debug:
print >>sys.stderr, "DEBUG: "+f % v
def GetOverallMessagesPerSecond(name):
dprint("%s", name)
global last_mps_timestamp, last_mps_value
# get the current value
rc, output = run_cmd(["/usr/sbin/scribe_ctrl", "counters"])
# return 0 if command fails
if rc:
return float(0)
match = re.compile(r"^scribe_overall:received good: (\d+)$", re.MULTILINE).search(output)
value = int(match.group(1))
# save current value
value_diff = value - last_mps_value
last_mps_value = value
# calculate seconds that have passed since last call
current_time = time.time()
elapsed = current_time - last_mps_timestamp
# save current timestamp
first_run = last_mps_timestamp is 0
last_mps_timestamp = current_time
if first_run:
return float(0)
return float(value_diff / elapsed)
def run_cmd(arglist):
'''Run a command and capture output.'''
try:
p = Popen(arglist, stdout=PIPE, stderr=PIPE)
output, errors = p.communicate()
except OSError, e:
return (1, '')
return (p.returncode, output)
def metric_init(params):
'''Create the metric definition dictionary object for each metric.'''
global descriptors
d1 = {
'name': 'scribe_overall_messages_per_second',
'call_back': GetOverallMessagesPerSecond,
'time_max': 90,
'value_type': 'float',
'units': 'msg/sec',
'slope': 'both',
'format': '%f',
'description': 'Average number of messages sent per second',
'groups': 'scribe'
}
descriptors = [d1]
return descriptors
def metric_cleanup():
'''Clean up the metric module.'''
pass
if __name__ == '__main__':
metric_init({})
# setup last timestamp as 10 seconds ago
last_mps_timestamp = time.time() - 10
for d in descriptors:
v = d['call_back'](d['name'])
print '%s: %s' % (d['name'], v)