#!/usr/bin/python3 import datetime import os import socket import psycopg2 import psycopg2.extras hostname = socket.getfqdn() db = psycopg2.connect('') csr = db.cursor(cursor_factory=psycopg2.extras.DictCursor) csr.execute( """ with recursive t as ( select '2 minutes'::interval as d union select d * 2 from t where d < '1 year'::interval ), fs as ( select distinct hostname, filesystem from df where ts >= now() - '2 minutes'::interval group by hostname, filesystem ), forecast as ( select fs.hostname, fs.filesystem, new.ts + (new.ts - old.ts) as when, new.ts + (new.ts - old.ts) - now() as d, new.f_used + (new.f_used - old.f_used) as used, (new.f_used + (new.f_used - old.f_used)) / old.f_usable * 100 as percent, (new.f_used + (new.f_used - old.f_used)) > old.f_usable as capacity_exceeded from fs, t, lateral ( select * from df where df.hostname = fs.hostname and df.filesystem = fs.filesystem and df.ts >= now() - t.d order by ts limit 1 ) old, lateral ( select * from df where df.hostname = fs.hostname and df.filesystem = fs.filesystem order by ts desc limit 1 ) new ) select * from forecast where capacity_exceeded order by "when" """ ) status = 0 message = "" for r in csr: if r["d"] < datetime.timedelta(hours=24): status = 2 else: if status < 1: status = 1 message += "%s:%s at %s; " % (r["hostname"], r["filesystem"].strftime("%Y-%m-%d %H:%M"), r["when"]) status_str = ["OK", "WARNING", "CRITICAL"] print("df_files", status_str[status], "-", message)