df2db/check_df_space

74 lines
2.3 KiB
Python
Executable File

#!/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.s_used + (new.s_used - old.s_used) as used,
(new.s_used + (new.s_used - old.s_used)) / old.s_usable * 100 as percent,
(new.s_used + (new.s_used - old.s_used)) > old.s_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"], r["when"].strftime("%Y-%m-%d %H:%M"))
status_str = ["OK", "WARNING", "CRITICAL"]
print("df_space", status_str[status], "-", message)