#!/usr/bin/env python
#
# Simple script to plot today's system load for all known Linux servers.
# See 'hostnames' below.
# Example:
#  ./plot_sys_load.py
#  # Then look at sys_load*.png file

from pylab import *
import paramiko
from datetime import datetime
import pandas as pd
import multiprocessing as mp

hostnames = ['meson','hubble','higgs','fermion','donald','daffy','quark','lepton','electron','proton']

def get_data(hostname, day):
    print("Processing %s..."%hostname)
    d = []
    cols = []
    t0 = datetime.strptime('00:00:00','%H:%M:%S')
    with paramiko.SSHClient() as client:
        client.load_system_host_keys()
        client.connect(hostname)
        for base in ['/var/log/sa','/var/log/sysstat']:
            _, stdout, stderr = client.exec_command("LC_TIME=en_UK.utf8 sar -f %s/sa%.2d"%(base,day))
            if len(stderr.read()) == 0:
                break
        for line in stdout:
            w = line.rstrip().split()
            if len(w) < 2:
                continue
            if w[0].startswith('Average'):
                continue
            if w[1] == 'CPU':
                cols = ['hrs'] + [x.strip('%') for x in w[2:]]
            if w[1] != 'all':
                continue
            dt_hrs = (datetime.strptime(w[0],'%H:%M:%S') - t0).total_seconds()/(60.*60.)
            d.append( [dt_hrs]+[float(x) for x in w[2:]] )
    print(" done with %s - # of points %d"%(hostname,len(d)))
    return (hostname,pd.DataFrame(d,columns=cols))

now = datetime.now()

pool = mp.Pool(processes=len(hostnames))
results = [pool.apply_async(get_data, args=(h,now.day,)) for h in hostnames]
for data in results:
    hostname, d = data.get()
    f = figure()
    plot( d.hrs, 100-d.idle, label='used %' )
    plot( d.hrs, d.iowait, label='iowait %' )
    legend()
    title("%s %d-%d-%d"%(hostname,now.year,now.month,now.day))
    xlabel("Hours in day")
    ylabel("Percentage")
    ylim([0,100])
    grid()
    filename = "sys_load_%s.png"%hostname
    print("Saving "+filename)
    savefig(filename)
    close(f)
