-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsystem_alert.py
57 lines (40 loc) · 1.64 KB
/
system_alert.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
from constants import Emoji, CPU_LIMIT, RAM_LIMIT, DISK_LIMIT, LOG_SIZE_THRESHOLD
from tasks import Task
from utils import System, to_gb, run_command
class CpuAlert(Task):
def __init__(self, chain):
super().__init__(chain)
self.name = 'CpuAlert'
def run(self):
cpu_usage = System().get_usage().cpu_usage
if cpu_usage > CPU_LIMIT:
return self.notify('CPU usage is above %d%% (%d%%) %s' % (CPU_LIMIT, cpu_usage, Emoji.CPU))
return False
class RamAlert(Task):
def __init__(self, chain):
super().__init__(chain)
self.name = 'RamAlert'
def run(self):
usage = System().get_usage()
ram_used = round(usage.ram_used / usage.ram_size * 100, 1)
if ram_used > RAM_LIMIT:
return self.notify('Ram usage is above %d%% (%d%%) %s' % (RAM_LIMIT, ram_used, Emoji.RAM))
return False
class DiskAlert(Task):
def __init__(self, chain):
super().__init__(chain)
self.name = 'DiskAlert'
def run(self):
usage = System().get_usage()
if usage.disk_percentage_used <= DISK_LIMIT:
return False
return self.notify(
'Disk usage is above %d%% (%d%%) (/var/log: %.1fG, /: %.1fG) %s' %
(DISK_LIMIT, usage.disk_percentage_used, to_gb(usage.disk_used_by_log), to_gb(usage.disk_used),
Emoji.DISK))
def can_recover(self):
return to_gb(System().get_usage().disk_used_by_log) > LOG_SIZE_THRESHOLD
def recover(self):
run_command('truncate -s 0 /var/log/syslog')
run_command('rm /var/log/syslog.*')
run_command('rm -r /var/log/journal/*')