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