Browse Source

First commit

master
Sven Slootweg 8 years ago
commit
b3eb4abf67
  1. 4
      cstatsd/config/machine.yaml
  2. 5
      cstatsd/config/ports.yaml
  3. 12
      cstatsd/cstatsd
  4. 143
      cstatsd/stats-machine.py
  5. 54
      cstatsd/stats-ports.py
  6. 10
      cstatsd/stats-tahoe.py

4
cstatsd/config/machine.yaml

@ -0,0 +1,4 @@
interval: 1
drives:
- /

5
cstatsd/config/ports.yaml

@ -0,0 +1,5 @@
interval: 5
ports:
6667: UnrealIRCd
3456: Tahoe-LAFS

12
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

143
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)

54
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)

10
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...
Loading…
Cancel
Save