1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
__author__ = 'Kenny Freeman' |
4
|
|
|
__email__ = '[email protected]' |
5
|
|
|
__license__ = "ISCL" |
6
|
|
|
__docformat__ = 'reStructuredText' |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
import psutil # pip install psutil |
10
|
|
|
|
11
|
|
|
import plumd |
12
|
|
|
import plumd.plugins |
13
|
|
|
|
14
|
|
|
# note: these plugins are currently used for demonstration and testing purposes. |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
class Cpu(plumd.plugins.Reader): |
18
|
|
|
""" |
19
|
|
|
Plugin to measure cpu utilization using the psutil module. |
20
|
|
|
""" |
21
|
|
|
defaults = { |
22
|
|
|
'poll.interval': 10, |
23
|
|
|
'percpu': False |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
def poll(self): |
27
|
|
|
"""Poll for cpu utilization time metrics. |
28
|
|
|
|
29
|
|
|
:rtype: ResultSet |
30
|
|
|
""" |
31
|
|
|
metrics = [] |
32
|
|
|
ctimes = psutil.cpu_times() |
33
|
|
|
for m_name in ctimes._fields: |
34
|
|
|
val = getattr(ctimes, m_name) |
35
|
|
|
metrics.append(plumd.Float(m_name, val)) |
36
|
|
|
return plumd.ResultSet([plumd.Result("cpu", metrics)]) |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
class Mem(plumd.plugins.Reader): |
40
|
|
|
"""Class to measure memory utilization using the psutil module.""" |
41
|
|
|
defaults = { |
42
|
|
|
'poll.interval': 10 |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
def poll(self): |
46
|
|
|
"""Poll for mem memory utilization metrics. |
47
|
|
|
|
48
|
|
|
:rtype: ResultSet |
49
|
|
|
""" |
50
|
|
|
metrics = [] |
51
|
|
|
meminfo = psutil.virtual_memory() |
52
|
|
|
for m in meminfo._fields: |
53
|
|
|
val = getattr(meminfo, m) |
54
|
|
|
metrics.append(plumd.Float(m, val)) |
55
|
|
|
return plumd.ResultSet([plumd.Result("mem", metrics)]) |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
class Swap(plumd.plugins.Reader): |
59
|
|
|
"""Class to measure swap utilization with the psutil module.""" |
60
|
|
|
defaults = { |
61
|
|
|
'poll.interval': 10 |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
def poll(self): |
65
|
|
|
"""Poll for swap utilization metrics. |
66
|
|
|
|
67
|
|
|
:rtype: ResultSet |
68
|
|
|
""" |
69
|
|
|
metrics = [] |
70
|
|
|
s = psutil.swap_memory() |
71
|
|
|
for m in s._fields: |
72
|
|
|
val =getattr(s, m) |
73
|
|
|
metrics.append(plumd.Float(m, val)) |
74
|
|
|
return plumd.ResultSet([plumd.Result("swap", metrics)]) |
75
|
|
|
|
76
|
|
|
|
77
|
|
View Code Duplication |
class Net(plumd.plugins.Reader): |
|
|
|
|
78
|
|
|
"""Class to measure network interface metrics using the psutil module.""" |
79
|
|
|
defaults = { |
80
|
|
|
'poll.interval': 10, |
81
|
|
|
'nics': None # all |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
def poll(self): |
85
|
|
|
"""Poll for net per nic bandwidth metrics. |
86
|
|
|
|
87
|
|
|
:rtype: ResultSet |
88
|
|
|
""" |
89
|
|
|
nicios = psutil.net_io_counters(pernic=True) |
90
|
|
|
rset = plumd.ResultSet() |
91
|
|
|
for nic in nicios.keys(): |
92
|
|
|
metrics = [] |
93
|
|
|
io = nicios[nic] |
94
|
|
|
for f in io._fields: |
95
|
|
|
val = getattr(io, f) |
96
|
|
|
metrics.append(plumd.Float(f, val)) |
97
|
|
|
robj = plumd.Result("net", metrics) |
98
|
|
|
robj.meta.add(plumd.String("nic", nic.replace(" ", "_"))) |
99
|
|
|
rset.add(robj) |
100
|
|
|
return rset |
101
|
|
|
|
102
|
|
|
|
103
|
|
View Code Duplication |
class Disk(plumd.plugins.Reader): |
|
|
|
|
104
|
|
|
""" |
105
|
|
|
Class to measure disk utilization using the psutil module. |
106
|
|
|
""" |
107
|
|
|
defaults = { |
108
|
|
|
'poll.interval': 300, |
109
|
|
|
'devices': None # all |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
def poll(self): |
113
|
|
|
"""Poll for disk space utilization metrics. |
114
|
|
|
|
115
|
|
|
:rtype: ResultSet |
116
|
|
|
""" |
117
|
|
|
rset = plumd.ResultSet() |
118
|
|
|
for disk in psutil.disk_partitions(): |
119
|
|
|
metrics = [] |
120
|
|
|
dev = disk.device.split("/")[-1] |
121
|
|
|
dev = dev.replace(":", "").replace("\\", "") |
122
|
|
|
try: |
123
|
|
|
u = psutil.disk_usage(disk.mountpoint) |
124
|
|
|
except OSError: |
125
|
|
|
continue |
126
|
|
|
for f in u._fields: |
127
|
|
|
val = getattr(u, f) |
128
|
|
|
metrics.append(plumd.Float(f, val)) |
129
|
|
|
robj = plumd.Result("disk", metrics) |
130
|
|
|
robj.meta.add(plumd.String("dev", dev)) |
131
|
|
|
rset.add(robj) |
132
|
|
|
return rset |
133
|
|
|
|
134
|
|
|
|
135
|
|
View Code Duplication |
class DiskIo(plumd.plugins.Reader): |
|
|
|
|
136
|
|
|
"""Class to measure disk io.""" |
137
|
|
|
defaults = { |
138
|
|
|
'poll.interval': 10, |
139
|
|
|
'devices': None # all |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
def poll(self): |
143
|
|
|
"""Poll for diskio metrics. |
144
|
|
|
|
145
|
|
|
:rtype: ResultSet |
146
|
|
|
""" |
147
|
|
|
rset = plumd.ResultSet() |
148
|
|
|
diskio = psutil.disk_io_counters(perdisk=True) |
149
|
|
|
for dev in diskio.keys(): |
150
|
|
|
metrics = [] |
151
|
|
|
io = diskio[dev] |
152
|
|
|
for f in io._fields: |
153
|
|
|
val = getattr(io, f) |
154
|
|
|
metrics.append(plumd.Float(f, val)) |
155
|
|
|
robj = plumd.Result("diskio", metrics) |
156
|
|
|
robj.meta.add(plumd.String("dev", dev)) |
157
|
|
|
rset.add(robj) |
158
|
|
|
return rset |
159
|
|
|
|