1. Wang Dingwei
  2. df_warn

Source

df_warn / dfsum.py

#!/usr/bin/env python

from subprocess import call, PIPE, Popen
from threading import Thread
import os

def server_name(ip):
    "ip -> server name"
    with open('/root/bin/servers', 'r') as s:
        for line in s:
            if line.find(ip) != -1:
                return line.split()[1]
        return ip

class Probe(Thread):
    "Threadded ssh probe"
    def __init__(self, ip):
        Thread.__init__(self)
        self.ip = ip
    def run(self):
        ssh_cmd = '''ssh -q -o "BatchMode=yes" %s "echo 2>&1"''' % self.ip
        self.status = call(ssh_cmd, stdout=PIPE, stderr=PIPE, shell=True)

def readsrv():
    "server_list -> (online_list, offline_list)"
    online_servers = []
    offline_servers = []
    jobs = []
    for line in open('/root/bin/servers', 'r'):
        s = line.strip().split()
        if s[2].startswith('192'):
            ip = s[2]
            current_job = Probe(ip)
            jobs.append(current_job)
            current_job.start()
    for job in jobs:
        job.join()
        if job.status == 0:
            online_servers.append(job.ip)
        else: offline_servers.append(job.ip)
    return online_servers, offline_servers


LOGNAME = '/var/log/dflog'

online_servers, offline_servers = readsrv()
with open('/tmp/servers', 'w') as f:    # Will be read by fabfile.py
    for s in online_servers:
        f.write('%s\n' % s)

df_cmd = Popen('fab --fabfile=/root/bin/cronfab.py df', 
               stdout=PIPE, shell=True)
df_cmd.wait()
os.remove('/tmp/servers')

full_disks = []

for line in df_cmd.stdout:
    if line.startswith('[192.168.200'):
        for s in ['out:', '[', ']']:
            line = line.replace(s, '')
        ip, dev_name, size, used, avail, percentage, mpoint = tuple(
                line.split())
        used_percentage = int(percentage.replace('%', ''))
        avail_space = float(avail)/1024/1024
        if used_percentage >= 85 and avail_space <= 100:
            entry = [server_name(ip),
                     ip, 
                     dev_name, 
                     ('%.2fGB' % avail_space), 
                     percentage]
            outline = ''.join(map(lambda x: '<td>'+x+'</td>', entry))
            outline = '<tr>' + outline + '</tr>'
            full_disks.append((outline, used_percentage))

with open('/tmp/sum.htm', 'w') as f:
    f.write('<html><body>\n')
    f.write('<b>Disk usage warning (>85% & <100GB):</b><br>\n')
    f.write('<table border=1><tr><th>Server</th><th>IP</th><th>Device</th><th>Free</th><th>Used</th></tr>\n')
    for (d, p) in sorted(full_disks, key=lambda x: x[1], reverse=True):
        f.write(d + '\n')
    f.write('</table><p>\n')
    f.write('<b>Offline Servers:</b><ul>\n')
    for ip in offline_servers:
        f.write('<li>%s (%s)</li>\n' % (ip, server_name(ip)))
    f.write('</ul>\n')
    f.write('<p>%d online servers checked</p>\n' % len(online_servers))
    f.write('</body></html>')

exec_scp = call(['scp /tmp/sum.htm Administrator@192.168.200.222:/tmp/.'],
        shell=True, stdout=PIPE, stderr=PIPE)

if exec_scp == 0:
    exit(0)