Skip to content

Instantly share code, notes, and snippets.

@overplumbum
Last active December 17, 2015 10:19
Show Gist options
  • Save overplumbum/5594337 to your computer and use it in GitHub Desktop.
Save overplumbum/5594337 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
Installation (Ubuntu <=12.04):
sudo apt-get install -y python-pip python-dev
sudo pip install -U psutil librato-metrics
cat > /etc/cron.d/librato <<zzz
* * * * * root /usr/local/bin/librato-monitoring --token=TOKEN [email protected] --prefix=lazybitch >/dev/null
zzz
"""
import psutil, librato, os, subprocess, re, argparse
parser = argparse.ArgumentParser()
parser.add_argument('--token', required=True)
parser.add_argument('--user', required=True)
parser.add_argument('--prefix', required=True)
parser.add_argument('--with-io', action='store_true')
parser.add_argument('--fake', action='store_true')
args = parser.parse_args()
api = librato.connect(args.user, args.token)
m = psutil.virtual_memory()
metrics = [
('mem_util', 100.0 * m.available / m.total),
('load_avg', os.getloadavg()[0]),
('cpu_usage', psutil.cpu_percent(interval=5)),
]
if args.with_io:
io = iter(subprocess.check_output(['iostat', '-xkd', '10', '2'], env={'LANG': 'C'}).splitlines())
whole_time = True
while True:
line = next(io)
if line.startswith('Device:'):
if not whole_time:
break
else:
whole_time = False
io_fields = [f for f in re.split(r'\s+', line)[1:]]
io_values = []
while True:
try:
line = next(io)
if not line:
break
except StopIteration:
break
vs = re.split(r'\s+', line)
io_values.append((vs[0], [float(v) for v in vs[1:]]))
for device, values in io_values:
kv = dict(zip(io_fields, values))
for k in ('%util', 'r/s', 'w/s', 'rkB/s', 'wkB/s'):
metrics.append((device + '.' + k.strip(':%').replace('/', '_'), kv[k]))
devs = subprocess.check_output(['/usr/sbin/smartctl', '--scan']).rstrip().splitlines()
devs = [d.split(' ', 1)[0] for d in devs]
for dev in devs:
dev_name = dev.replace('/dev/', '').replace('/', '_')
attrs = subprocess.check_output(['/usr/sbin/smartctl', '-A', dev]).splitlines()
attrs = [re.split(r"\s+", line.strip()) for line in attrs]
attrs = dict((row[1], row[9]) for row in attrs if len(row) == 10)
for attr_name in ('Reallocated_Sector_Ct',):
if attr_name in attrs:
metrics.append((dev_name + '.' + attr_name, int(attrs[attr_name])))
q = api.new_queue()
for name, value in metrics:
print args.prefix + '.' + name, value
if not args.fake:
q.add(args.prefix + '.' + name, value)
if not args.fake:
q.submit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment