commit b3eb4abf67ce6df6fbad264c698666bcd25b98d1 Author: Sven Slootweg Date: Sat Dec 7 03:31:46 2013 +0100 First commit diff --git a/cstatsd/config/machine.yaml b/cstatsd/config/machine.yaml new file mode 100644 index 0000000..7c1e7e5 --- /dev/null +++ b/cstatsd/config/machine.yaml @@ -0,0 +1,4 @@ +interval: 1 + +drives: + - / diff --git a/cstatsd/config/ports.yaml b/cstatsd/config/ports.yaml new file mode 100644 index 0000000..2cd11ef --- /dev/null +++ b/cstatsd/config/ports.yaml @@ -0,0 +1,5 @@ +interval: 5 + +ports: + 6667: UnrealIRCd + 3456: Tahoe-LAFS diff --git a/cstatsd/cstatsd b/cstatsd/cstatsd new file mode 100755 index 0000000..dcc9b63 --- /dev/null +++ b/cstatsd/cstatsd @@ -0,0 +1,12 @@ +#!/usr/bin/env python2 + +import zmq, msgpack + +ctx = zmq.Context() + +collector = ctx.socket(zmq.PULL) +collector.bind("ipc:///tmp/cstatsd") + +while True: + message = msgpack.unpackb(collector.recv()) + print message diff --git a/cstatsd/stats-machine.py b/cstatsd/stats-machine.py new file mode 100755 index 0000000..5c3d94b --- /dev/null +++ b/cstatsd/stats-machine.py @@ -0,0 +1,143 @@ +#!/usr/bin/env python2 + +import zmq, msgpack, time, psutil, yaml, os + +ctx = zmq.Context() + +sock = ctx.socket(zmq.PUSH) +sock.connect("ipc:///tmp/cstatsd") + +with open("config/machine.yaml", "r") as cfile: + config = yaml.safe_load(cfile) + +interval = config["interval"] +old_net_data = {} + +while True: + load_avgs = os.getloadavg() + sock.send(msgpack.packb({ + "service": "machine", + "msg_type": "value", + "resource_type": "load_average", + "unit": "", + "values": { + "1m": load_avgs[0], + "5m": load_avgs[1], + "15m": load_avgs[2] + } + })) + + cpu_loads = psutil.cpu_percent(percpu=True) + + for i in xrange(0, len(cpu_loads)): + sock.send(msgpack.packb({ + "service": "machine", + "msg_type": "value", + "resource_type": "cpu", + "unit": "core%d" % (i + 1), + "values": { + "load": cpu_loads[i] + } + })) + + for drive in config["drives"]: + drive_data = psutil.disk_usage(drive) + sock.send(msgpack.packb({ + "service": "machine", + "msg_type": "value", + "resource_type": "disk", + "unit": drive, + "values": { + "total": drive_data.total, + "used": drive_data.used, + "free": drive_data.free, + "used_percentage": drive_data.percent + } + })) + + ram_data = psutil.virtual_memory() + sock.send(msgpack.packb({ + "service": "machine", + "msg_type": "value", + "resource_type": "memory", + "unit": "physical", + "values": { + "total": ram_data.total, + "used": ram_data.used, + "free": ram_data.available, + "used_percentage": ram_data.percent, + "buffers": ram_data.buffers, + "cache": ram_data.cached + } + })) + + swap_data = psutil.virtual_memory() + sock.send(msgpack.packb({ + "service": "machine", + "msg_type": "value", + "resource_type": "memory", + "unit": "swap", + "values": { + "total": swap_data.total, + "used": swap_data.used, + "free": swap_data.free, + "used_percentage": swap_data.percent + } + })) + + net_data = psutil.net_io_counters(pernic=True) + for nic, data in net_data.iteritems(): + try: + old_in_b = old_net_data[nic].bytes_recv + old_out_b = old_net_data[nic].bytes_sent + old_in_p = old_net_data[nic].packets_recv + old_out_p = old_net_data[nic].packets_sent + except KeyError, e: + # No old data yet, first run? Save and skip to next... + old_net_data[nic] = data + continue + + diff_in_b = data.bytes_recv - old_in_b + diff_out_b = data.bytes_sent - old_out_b + diff_in_p = data.packets_recv - old_in_p + diff_out_p = data.packets_sent - old_out_p + + if diff_in_b < 0: + diff_in_b = 0 + + if diff_out_b < 0: + diff_out_b = 0 + + if diff_in_p < 0: + diff_in_p = 0 + + if diff_out_p < 0: + diff_out_p = 0 + + old_net_data[nic] = data + + sock.send(msgpack.packb({ + "service": "machine", + "msg_type": "value", + "resource_type": "network", + "unit": nic, + "values": { + "bps_in": diff_in_b / interval, + "bps_out": diff_out_b / interval, + "pps_in": diff_in_p / interval, + "pps_out": diff_out_p / interval + } + })) + + sock.send(msgpack.packb({ + "service": "machine", + "msg_type": "value", + "resource_type": "uptime", + "unit": "", + "values": { + "uptime": time.time() - psutil.get_boot_time() + } + })) + + time.sleep(interval) + diff --git a/cstatsd/stats-ports.py b/cstatsd/stats-ports.py new file mode 100644 index 0000000..b503bc1 --- /dev/null +++ b/cstatsd/stats-ports.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python2 + +import zmq, msgpack, time, yaml, socket + +ctx = zmq.Context() + +sock = ctx.socket(zmq.PUSH) +sock.connect("ipc:///tmp/cstatsd") + +with open("config/ports.yaml", "r") as cfile: + config = yaml.safe_load(cfile) + +interval = config["interval"] + +old_status = {} + +while True: + for port, service_name in config["ports"].iteritems(): + try: + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.settimeout(0.5) + s.connect(("127.0.0.1", port)) + s.close() + up = True + except socket.error, e: + up = False + + try: + if up == old_status[port]: + send_notice = False + else: + send_notice = True + initial = False + except KeyError, e: + send_notice = True + initial = True + + old_status[port] = up + + if send_notice: + if up: + msg_type = "up" + else: + msg_type = "down" + + sock.send(msgpack.packb({ + "service": "port", + "msg_type": msg_type, + "unit": "%s (%s)" % (service_name, port), + "initial": initial + })) + + time.sleep(interval) + diff --git a/cstatsd/stats-tahoe.py b/cstatsd/stats-tahoe.py new file mode 100644 index 0000000..80b4160 --- /dev/null +++ b/cstatsd/stats-tahoe.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python2 + +import zmq, msgpack + +ctx = zmq.Context() + +sock = ctx.socket(zmq.PUSH) +sock.connect("ipc:///tmp/cstatsd") + +# Can't write any more code here, Tahoe-LAFS' JSON web API status thingie is broken...