-
Notifications
You must be signed in to change notification settings - Fork 0
/
rrd_cpu.py
104 lines (93 loc) · 3.67 KB
/
rrd_cpu.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import os
import rrdtool
from period import period_conv
#rrdtool create termserver2_cpu.rrd \
# --start $(date +%s --date="-2years") \
# --step 900 \
# DS:load:GAUGE:1200:0:5000 \
# DS:loadavg:GAUGE:1200:0:5000 \
# DS:cores:GAUGE:1200:0:5000 \
# RRA:AVERAGE:0.5:1:1200 \
# RRA:AVERAGE:0.5:6:1200 \
# RRA:AVERAGE:0.5:24:1200 \
# RRA:MIN:0.5:1:1200 \
# RRA:MIN:0.5:6:1200 \
# RRA:MIN:0.5:24:1200 \
# RRA:MAX:0.5:1:1200 \
# RRA:MAX:0.5:6:1200 \
# RRA:MAX:0.5:24:1200 \
# RRA:LAST:0.5:1:1200 \
# RRA:LAST:0.5:6:1200 \
# RRA:LAST:0.5:24:1200
def graph1(hostname, period):
test = rrdtool.graphv("-", "--start", period_conv(period), "-w 800", "--title=Load %s" % hostname,
"DEF:load=rrds/%s_cpu.rrd:load:MAX" % (hostname),
"CDEF:load100=load,100,/",
"LINE2:load#0000FF:load",
"CDEF:unavailable=load,UN,INF,0,IF",
"AREA:unavailable#f0f0f0",
)
return test['image']
def graph2(hostname, period):
test = rrdtool.graphv("-", "--start", period_conv(period), "-w 800", "--title=Load %s" % hostname,
"DEF:cores=rrds/%s_cpu.rrd:cores:LAST" % (hostname),
"LINE2:cores#00FFFF:cores",
"CDEF:unavailable=cores,UN,INF,0,IF",
"AREA:unavailable#f0f0f0",
)
return test['image']
def graph3(hostname, period):
test = rrdtool.graphv("-", "--start", period_conv(period), "-w 800", "--title=Load %s" % hostname,
"DEF:loadavg=rrds/%s_cpu.rrd:loadavg:LAST" % (hostname),
"LINE2:loadavg#FF00FF:loadavg",
"CDEF:unavailable=loadavg,UN,INF,0,IF",
"AREA:unavailable#f0f0f0",
)
return test['image']
def insert(hostname, data, timestamp="N"):
if not exists(hostname):
create(hostname)
rrdname = "rrds/" + hostname + "_cpu.rrd"
rrdtool.update(rrdname, '%s:%s:%s:%s' % (timestamp, data[0], data[1], data[2]))
def exists(hostname):
rrdname = "rrds/" + hostname + "_cpu.rrd"
return os.path.exists(rrdname)
def create(hostname):
if not exists(hostname):
rrdname = "rrds/" + hostname + "_cpu.rrd"
rrdtool.create(rrdname, '--start', '-2years',
'--step', '900',
'DS:load:GAUGE:1200:0:5000',
'DS:loadavg:GAUGE:1200:0:5000',
'DS:cores:GAUGE:1200:0:5000',
'RRA:AVERAGE:0.5:1:1200',
'RRA:AVERAGE:0.5:6:1200',
'RRA:AVERAGE:0.5:24:1200',
'RRA:MIN:0.5:1:1200',
'RRA:MIN:0.5:6:1200',
'RRA:MIN:0.5:24:1200',
'RRA:MAX:0.5:1:1200',
'RRA:MAX:0.5:6:1200',
'RRA:MAX:0.5:24:1200',
'RRA:LAST:0.5:1:1200',
'RRA:LAST:0.5:6:1200',
'RRA:LAST:0.5:24:1200'
)
return True
else:
return False
def last(hostname):
rrdname = "rrds/" + hostname + "_cpu.rrd"
try:
last = rrdtool.last(rrdname)
except:
return None
return last
def latest(hostname):
rrdname = "rrds/" + hostname + "_cpu.rrd"
try:
info = rrdtool.info(rrdname)
lastupdate = [info['last_update'], float(info['ds[load].last_ds']), float(info['ds[loadavg].last_ds'], float(info['ds[cores].last_ds']))]
return lastupdate
except:
return None