-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.py
38 lines (28 loc) · 1.04 KB
/
Module.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
from core import Log
from circuits import Component, Event, Timer
import os
import time
class load_alert(Event):
""" Event raised when load is to high
"""
def __init__(self, load, load_max, timestamp):
super().__init__(load, load_max, timestamp)
class Module(Component):
channel = 'load_avg'
delay = 60
max_avg = 0
def get_load_avg(self):
avg = os.getloadavg()[0]
if avg >= self.max_avg:
Log.warning('Average at %f (max: %d)' % (avg, self.max_avg))
self.fire(load_alert(avg, self.max_avg, time.time()))
def started(self, c):
if hasattr(os, 'cpu_count'): # python 3.4 min
self.max_avg = os.cpu_count()
else:
import subprocess
process_cmd = ['grep', '-c', '^processor', '/proc/cpuinfo']
cpu_count = subprocess.check_output(process_cmd)
self.max_avg = int(cpu_count)
load_avg_evt = Event.create('get_load_avg')
Timer(self.delay, load_avg_evt, self.channel, persist=True).register(self)