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