forked from deploytoprod/PyGeckoZabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
68 lines (56 loc) · 1.65 KB
/
helpers.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
66
67
68
def formatunit(value, unit):
if unit == 'bps':
return bit_to_humanreadable(value, unit)
elif unit == 'int':
return int_to_smallformat(value, unit)
elif unit == '%':
return percent(value)
def percent(num):
return str(round(num))+'%'
def bit_to_humanreadable(num, type):
for x in ['bits', 'Kb', 'Mb', 'Gb', 'Tb']:
if num < 1024.0:
return "%3.2f %sps" % (num, x)
num /= 1024.0
def int_to_smallformat(num, type):
for x in ['', 'K', 'M', 'G', 'T']:
if num < 1000.0:
return "%3.1f %s" % (num, x)
num /= 1000.0
def priority_to_humanreadable(priority):
notclassified='#ffffff'
information='#63CF5F'
warning='#C0D100'
average='#FF9900'
high='#D17C28'
disaster='#FF0000'
label = {}
priority = int(priority)
if priority == 0:
label['txt'] = 'Not Classified'
label['color'] = notclassified
if priority == 1:
label['txt'] = 'Information'
label['color'] = information
if priority == 2:
label['txt'] = 'Warning'
label['color'] = warning
if priority == 3:
label['txt'] = 'Average'
label['color'] = average
if priority == 4:
label['txt'] = 'High'
label['color'] = high
if priority == 5:
label['txt'] = 'Disaster'
label['color'] = disaster
return label
def calculate_age(unixtimestamp):
import datetime
import time
now = time.mktime(datetime.datetime.now().timetuple())
seconds = int(now) - int(unixtimestamp)
agesec = datetime.timedelta(seconds=seconds)
return agesec
def sec_to_ms(sec):
return sec*1000