-
Notifications
You must be signed in to change notification settings - Fork 0
/
sizewatcher.py
65 lines (58 loc) · 2.3 KB
/
sizewatcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import json
import subprocess
import os.path
import http.client, urllib
PUSHOVER_TOKEN = None
PUSHOVER_USER = None
THRESHOLD = 80
# --------------------------------------------------------------------------- #
# retrieve config from sizewatcher.json
# --------------------------------------------------------------------------- #
main_base = os.path.dirname(__file__)
config_file = os.path.join(main_base, "sizewatcher.json")
if os.path.exists(config_file):
with open(config_file) as f:
# use safe_load instead load
cfg = json.load(f)
if isinstance(cfg['threshold'], int):
THRESHOLD = cfg['threshold']
if 'pushover' in cfg:
if isinstance(cfg['pushover']['token'], str):
PUSHOVER_TOKEN = cfg['pushover']['token']
if isinstance(cfg['pushover']['user'], str):
PUSHOVER_USER = cfg['pushover']['user']
else:
raise FileNotFoundError("Configuration file not found")
data_percent = None
metadata_percent = None
result = subprocess.run(
[ 'lvs', 'vg-mail/thinpool', '--options=lv_size,lv_metadata_size,data_percent,metadata_percent', '--reportformat=json', '--units=g' ],
stdout=subprocess.PIPE)
data = json.loads(result.stdout)
if 'report' in data:
if len(data['report']) == 1:
if 'lv' in data['report'][0]:
if len(data['report'][0]['lv']) == 1:
data_percent = float(data['report'][0]['lv'][0]['data_percent'])
metadata_percent = float(data['report'][0]['lv'][0]['metadata_percent'])
if data_percent is None or metadata_percent is None:
# Cannot retrieve value!
print('Cannot retrieve value!')
else:
message = ''
if data_percent > THRESHOLD:
message = message + 'data: {}% '.format(data_percent)
if metadata_percent > THRESHOLD:
message = message + 'meta: {}% '.format(data_percent)
if len(message) > 0:
message = 'ALERT ' + message
conn = http.client.HTTPSConnection("api.pushover.net:443")
conn.request("POST", "/1/messages.json",
urllib.parse.urlencode({
"token": PUSHOVER_TOKEN,
"user": PUSHOVER_USER,
"message": message,
}), { "Content-type": "application/x-www-form-urlencoded" })
conn.getresponse()
else:
print('Everything is good!')