/
load.py
338 lines (295 loc) · 10.2 KB
/
load.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import utils
import os
class Linux(object):
def __init__(self):
self.memory = '/proc/meminfo'
self.swap = '/proc/swaps'
self.loadavg = '/proc/loadavg'
self.ifstat = 'ip -s -o link'
self.pagesize = os.sysconf('SC_PAGE_SIZE')
def get_uptime(self):
'''
Get machine uptime in seconds as provided by /proc/uptime.
Also see parse_uptime().
'''
f = open('/proc/uptime', 'r', 0)
seconds = float(f.read().split()[0])
f.close()
return seconds
def get_memory(self):
'''
Get memory information from /proc/meminfo.
Also see parse_memory().
'''
f = open(self.memory, 'r', 0)
data = f.read()
f.close()
return data
def get_swap(self):
'''
Get swap information from /proc/swaps.
Also see parse_swap().
'''
f = open(self.swap, 'r', 0)
data = f.read()
f.close()
return data
def get_loadavg(self):
'''Get load information from /proc/loadavg.'''
f = open(self.loadavg, 'r', 0)
data = f.read()
f.close()
return data
def get_ifstat(self):
'''Get interface statistics.'''
return utils.run(self.ifstat)
def parse_uptime(self):
'''
Parse output from get_uptime().
Returned structure is a tuple:
(days, hours, minutes, seconds, seconds since boot)
'''
return utils.parser_uptime(self.get_uptime())
def parse_memory(self, mem_fields=('MemTotal', 'MemFree')):
'''
Parse memory info from output of get_memory().
Returned key, value pairs depend on keywords defined in mem_fields,
which MUST be a tuple:
self.parse_memory(('MemTotal',))
Search is case sensitive.
Returned structure is a dictionary:
{key: value, ...}
'''
return utils.parser_memory(self.get_memory(), mem_fields)
def parse_swap(self):
'''
Parse output of get_swap().
Returned structure is a dictionary of dictionaries:
{device:
{'type': value,
'size': value,
'used', value,
'priority', value}
}
'''
fields = ('type', 'size', 'used', 'priority')
return utils.parser_swap(self.get_swap(), fields)
def parse_loadavg(self):
'''
Parse output of get_loadavg().
Returned structure is a tuple:
(1min, 5min, 15min)
'''
return utils.parser_loadavg(self.get_loadavg())
def parse_ifstat(self):
'''
Parse output of get_ifstat().
Returned structure is a dictionary of dictionaries:
{device:
{'in':
{'bytes': value,
'packets': value,
'errors': value,
'dropped': value},
'out':
{'bytes': value,
'packets': value,
'errors': value,
'dropped': value},
}
}
'''
return utils.parser_ifstat(self.get_ifstat())
class FreeBSD(object):
def __init__(self, libc='/lib/libc.so.7'):
self.memory = 'sysctl vm.stats.vm'
self.swap = 'pstat -s'
self.loadavg = 'sysctl -n vm.loadavg'
self.ifstat = 'netstat -i -d -b -n -W'
self.libc = libc
self.pagesize = os.sysconf('SC_PAGE_SIZE')
def get_uptime(self):
'''
Get machine uptime in seconds as provided by clock_gettime(2).
clock_gettime's first argument is CLOCK_UPTIME_PRECISE, which
is defined in time.h:
#define CLOCK_UPTIME 5 /* FreeBSD-specific. */
#define CLOCK_UPTIME_PRECISE 7 /* FreeBSD-specific. */
#define CLOCK_UPTIME_FAST 8 /* FreeBSD-specific. */
'''
from ctypes import Structure, cdll, byref, c_long
lib = cdll.LoadLibrary(self.libc)
class Data(Structure):
_fields_ = [('sec', c_long), ('nsec', c_long)]
struct = Data()
lib.clock_gettime(7, byref(struct))
return struct.sec
def get_memory(self):
'''Get memory information.'''
return utils.run(self.memory)
def get_swap(self):
'''Get swap information.'''
return utils.run(self.swap)
def get_loadavg(self):
'''Get load average.'''
return utils.run(self.loadavg)
def get_ifstat(self):
'''Get interface statistics.'''
return utils.run(self.ifstat)
def parse_uptime(self):
'''
Parse output from get_uptime().
Returned structure is tuple:
(days, hours, minutes, seconds, seconds since boot)
'''
return utils.parser_uptime(self.get_uptime())
def parse_memory(self, mem_fields=('vm.stats.vm.v_page_count', 'vm.stats.vm.v_free_count', 'vm.stats.vm.v_inactive_count', 'vm.stats.vm.v_cache_count')):
'''
Parse output from get_memory().
Returned key, value pairs depend on keywords defined in mem_fields,
which MUST be a tuple:
self.parse_memory(('vm.stats.vm.v_page_count',))
Search is case sensitive.
By default, returned values are pages of memory.
Returned structure is a dictionary.
{key: value, ...}
free = v_free_count + v_inactive_count + v_cache_count
'''
return utils.parser_memory(self.get_memory(), mem_fields)
def parse_swap(self):
'''
Parse output from get_swap().
Returned structure is a dictionary of dictionaries:
{'device':
{'blocks': value,
'used: value,
'free', value,
'percent', value}
}
'''
fields = ('blocks', 'used', 'free', 'percent')
return utils.parser_swap(self.get_swap(), fields)
def parse_loadavg(self):
'''
Parse output of get_loadavg().
Returned structure is a tuple:
(1min, 5min, 15min)
'''
return utils.parser_loadavg(self.get_loadavg())
def parse_ifstat(self):
'''
Parse output of get_ifstat().
Returned structure is a dictionary of dictionaries of dictionaries of ...:
{device:
{address:
{'in':
{'bytes': value,
'packets': value,
'errors': value,
'dropped'value},
'out':
{'bytes': value,
'packets': value,
'errors': value,
'dropped': value}
}
}
}
'''
return utils.parser_ifstat(self.get_ifstat())
class SunOS(object):
def __init__(self):
self.uptime = 'kstat -p unix:0:system_misc:boot_time'
self.loadavg = 'kstat -p unix:0:system_misc:avenrun*'
self.memory = 'kstat -p unix:0:system_pages'
self.swap = 'swap -l'
self.ifstat = 'dladm show-link -s'
self.pagesize = os.sysconf('SC_PAGE_SIZE')
def get_uptime(self):
'''Get machine uptime.'''
return utils.run(self.uptime)
def get_memory(self):
'''Get memory info.'''
return utils.run(self.memory)
def get_swap(self):
'''Get swap info.'''
return utils.run(self.swap)
def get_loadavg(self):
'''Get load average.'''
return utils.run(self.loadavg)
def get_ifstat(self):
'''Get interface statistisc.'''
return utils.run(self.ifstat)
def parse_uptime(self):
'''Parse output of get_uptime().'''
from time import time
return utils.parser_uptime(int(time()) - int(self.get_uptime().split()[-1]))
def parse_memory(self, mem_fields = ('unix:0:system_pages:physmem', 'unix:0:system_pages:freemem')):
'''
Parse output from get_memory().
Returned key, value pairs depend on keywords defined in mem_fields,
whish MUST be a tuple:
self.parse_memory(('unix:0:system_pages:physmem',))
Search is case sensitive.
By default, returned values are pages of memory.
Returned structure is a dictionary:
{key: value, ...}
'''
return utils.parser_memory(self.get_memory(), mem_fields)
def parse_swap(self):
'''
Parse output of get_swap().
Returned structure is a dictionary of dictionaries:
{path:
{'dev': value,
'swaplo': value,
'blocks': value,
'free': value}
}
path - The path name for the swap area.
dev - The major/minor device number in decimal if it is a block special device; zeroes otherwise.
swaplo - The swaplow value for the area in 512-byte blocks.
blocks - The swaplen value for the area in 512-byte blocks.
free - The number of 512-byte blocks in this area that are not currently allocated.
'''
fields = ('dev', 'swaplo', 'blocks', 'free')
return utils.parser_storage(self.get_swap(), fields)
def parse_loadavg(self):
'''
Parse output of get_loadavg().
Returned structure is a tuple:
(1min, 5min, 15min)
Values returned by get_loadavg() must be divided by the scale factor.
Scale factor is defined in /usr/include/sys/param.h:
/*
* Scale factor for scaled integers used to count
* %cpu time and load averages.
*/
#define FSHIFT 8 /* bits to right of fixed binary point */
#define FSCALE (1<<FSHIFT)
'''
data = self.get_loadavg()
data = data.split('\n')[:-1]
rvalue = []
for item in data:
rvalue.append('%.2f' % (float(item.split()[-1]) / float(256)))
return tuple(rvalue)
def parse_ifstat(self):
'''
Parse output of get_ifstat().
Returned structure is a dictionary of dictionaries:
{device:
{'in':
{'bytes': value,
'packets': value,
'errors': value},
'out':
{'bytes': value,
'packets': value,
'errors': value}
}
}
'''
return utils.parser_ifstat(self.get_ifstat())
if __name__ == '__main__':
print "not yet"