1 |
# |
2 |
# i-scream central monitoring system |
3 |
# http://www.i-scream.org |
4 |
# Copyright (C) 2000-2004 i-scream |
5 |
# |
6 |
# This program is free software; you can redistribute it and/or |
7 |
# modify it under the terms of the GNU General Public License |
8 |
# as published by the Free Software Foundation; either version 2 |
9 |
# of the License, or (at your option) any later version. |
10 |
# |
11 |
# This program is distributed in the hope that it will be useful, |
12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
# GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License |
17 |
# along with this program; if not, write to the Free Software |
18 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
# |
20 |
# $Id: statgrab.pyx,v 1.7 2004/02/13 17:53:15 tdb Exp $ |
21 |
# |
22 |
|
23 |
ctypedef long time_t |
24 |
|
25 |
cdef extern from "statgrab.h": |
26 |
ctypedef struct cpu_states_t: |
27 |
long long user |
28 |
long long kernel |
29 |
long long idle |
30 |
long long iowait |
31 |
long long swap |
32 |
long long nice |
33 |
long long total |
34 |
time_t systime |
35 |
|
36 |
ctypedef struct cpu_percent_t: |
37 |
float user |
38 |
float kernel |
39 |
float idle |
40 |
float iowait |
41 |
float swap |
42 |
float nice |
43 |
time_t time_taken |
44 |
|
45 |
ctypedef struct mem_stat_t: |
46 |
long long total |
47 |
long long free |
48 |
long long used |
49 |
long long cache |
50 |
|
51 |
ctypedef struct load_stat_t: |
52 |
double min1 |
53 |
double min5 |
54 |
double min15 |
55 |
|
56 |
ctypedef struct user_stat_t: |
57 |
char *name_list |
58 |
int num_entries |
59 |
|
60 |
ctypedef struct swap_stat_t: |
61 |
long long total |
62 |
long long used |
63 |
long long free |
64 |
|
65 |
ctypedef struct general_stat_t: |
66 |
char *os_name |
67 |
char *os_release |
68 |
char *os_version |
69 |
char *platform |
70 |
char *hostname |
71 |
time_t uptime |
72 |
|
73 |
ctypedef struct disk_stat_t: |
74 |
char *device_name |
75 |
char *fs_type |
76 |
char *mnt_point |
77 |
long long size |
78 |
long long used |
79 |
long long avail |
80 |
long long total_inodes |
81 |
long long used_inodes |
82 |
long long free_inodes |
83 |
|
84 |
ctypedef struct diskio_stat_t: |
85 |
char *disk_name |
86 |
long long read_bytes |
87 |
long long write_bytes |
88 |
time_t systime |
89 |
|
90 |
ctypedef struct process_stat_t: |
91 |
int total |
92 |
int running |
93 |
int sleeping |
94 |
int stopped |
95 |
int zombie |
96 |
|
97 |
ctypedef struct network_stat_t: |
98 |
char *interface_name |
99 |
long long tx |
100 |
long long rx |
101 |
time_t systime |
102 |
|
103 |
ctypedef enum statgrab_duplex: |
104 |
FULL_DUPLEX |
105 |
HALF_DUPLEX |
106 |
UNKNOWN_DUPLEX |
107 |
|
108 |
ctypedef struct network_iface_stat_t: |
109 |
char *interface_name |
110 |
int speed |
111 |
statgrab_duplex dup |
112 |
int up |
113 |
|
114 |
ctypedef struct page_stat_t: |
115 |
long long pages_pagein |
116 |
long long pages_pageout |
117 |
time_t systime |
118 |
|
119 |
cdef extern cpu_states_t *get_cpu_totals() |
120 |
cdef extern cpu_states_t *get_cpu_diff() |
121 |
cdef extern cpu_percent_t *cpu_percent_usage() |
122 |
cdef extern mem_stat_t *get_memory_stats() |
123 |
cdef extern load_stat_t *get_load_stats() |
124 |
cdef extern user_stat_t *get_user_stats() |
125 |
cdef extern swap_stat_t *get_swap_stats() |
126 |
cdef extern general_stat_t *get_general_stats() |
127 |
cdef extern disk_stat_t *get_disk_stats(int *entries) |
128 |
cdef extern diskio_stat_t *get_diskio_stats(int *entries) |
129 |
cdef extern diskio_stat_t *get_diskio_stats_diff(int *entries) |
130 |
cdef extern process_stat_t *get_process_stats() |
131 |
cdef extern network_stat_t *get_network_stats(int *entries) |
132 |
cdef extern network_stat_t *get_network_stats_diff(int *entries) |
133 |
cdef extern network_iface_stat_t *get_network_iface_stats(int *entries) |
134 |
cdef extern page_stat_t *get_page_stats() |
135 |
cdef extern page_stat_t *get_page_stats_diff() |
136 |
cdef extern int statgrab_init() |
137 |
cdef extern int statgrab_drop_privileges() |
138 |
|
139 |
|
140 |
py_FULL_DUPLEX = FULL_DUPLEX |
141 |
py_HALF_DUPLEX = HALF_DUPLEX |
142 |
py_UNKNOWN_DUPLEX = UNKNOWN_DUPLEX |
143 |
|
144 |
|
145 |
class Result: |
146 |
def __init__(self, attrs): |
147 |
self.attrs = attrs |
148 |
for attr in attrs: |
149 |
setattr(self, attr, attrs[attr]) |
150 |
def __getitem__(self, item): |
151 |
return getattr(self, item) |
152 |
def __repr__(self): |
153 |
return str(self.attrs) |
154 |
|
155 |
class StatgrabException(Exception): |
156 |
def __init__(self, value): |
157 |
self.value = value |
158 |
def __str__(self): |
159 |
return repr(self.value) |
160 |
|
161 |
|
162 |
def py_get_cpu_totals(): |
163 |
cdef cpu_states_t *s |
164 |
s = get_cpu_totals() |
165 |
if s == NULL: |
166 |
raise StatgrabException, 'get_cpu_totals() returned NULL' |
167 |
return Result( |
168 |
{'user': s.user, |
169 |
'kernel': s.kernel, |
170 |
'idle': s.idle, |
171 |
'iowait': s.iowait, |
172 |
'swap': s.swap, |
173 |
'nice': s.nice, |
174 |
'total': s.total, |
175 |
'systime': s.systime, |
176 |
} |
177 |
) |
178 |
|
179 |
def py_get_cpu_diff(): |
180 |
cdef cpu_states_t *s |
181 |
s = get_cpu_diff() |
182 |
if s == NULL: |
183 |
raise StatgrabException, 'get_cpu_diff() returned NULL' |
184 |
return Result( |
185 |
{'user': s.user, |
186 |
'kernel': s.kernel, |
187 |
'idle': s.idle, |
188 |
'iowait': s.iowait, |
189 |
'swap': s.swap, |
190 |
'nice': s.nice, |
191 |
'total': s.total, |
192 |
'systime': s.systime, |
193 |
} |
194 |
) |
195 |
|
196 |
def py_cpu_percent_usage(): |
197 |
cdef cpu_percent_t *s |
198 |
s = cpu_percent_usage() |
199 |
if s == NULL: |
200 |
raise StatgrabException, 'cpu_percent_usage() returned NULL' |
201 |
return Result( |
202 |
{'user': s.user, |
203 |
'kernel': s.kernel, |
204 |
'idle': s.idle, |
205 |
'iowait': s.iowait, |
206 |
'swap': s.swap, |
207 |
'nice': s.nice, |
208 |
'time_taken': s.time_taken, |
209 |
} |
210 |
) |
211 |
|
212 |
def py_get_memory_stats(): |
213 |
cdef mem_stat_t *s |
214 |
s = get_memory_stats() |
215 |
if s == NULL: |
216 |
raise StatgrabException, 'get_memory_stats() returned NULL' |
217 |
return Result( |
218 |
{'total': s.total, |
219 |
'used': s.used, |
220 |
'free': s.free, |
221 |
'cache': s.cache, |
222 |
} |
223 |
) |
224 |
|
225 |
def py_get_load_stats(): |
226 |
cdef load_stat_t *s |
227 |
s = get_load_stats() |
228 |
if s == NULL: |
229 |
raise StatgrabException, 'get_load_stats() returned NULL' |
230 |
return Result( |
231 |
{'min1': s.min1, |
232 |
'min5': s.min5, |
233 |
'min15': s.min15, |
234 |
} |
235 |
) |
236 |
|
237 |
def py_get_user_stats(): |
238 |
cdef user_stat_t *s |
239 |
s = get_user_stats() |
240 |
if s == NULL: |
241 |
raise StatgrabException, 'get_user_stats() returned NULL' |
242 |
return Result( |
243 |
{'name_list': s.name_list, |
244 |
'num_entries': s.num_entries, |
245 |
} |
246 |
) |
247 |
|
248 |
def py_get_swap_stats(): |
249 |
cdef swap_stat_t *s |
250 |
s = get_swap_stats() |
251 |
if s == NULL: |
252 |
raise StatgrabException, 'get_swap_stats() returned NULL' |
253 |
return Result( |
254 |
{'total': s.total, |
255 |
'used': s.used, |
256 |
'free': s.free, |
257 |
} |
258 |
) |
259 |
|
260 |
def py_get_general_stats(): |
261 |
cdef general_stat_t *s |
262 |
s = get_general_stats() |
263 |
if s == NULL: |
264 |
raise StatgrabException, 'get_general_stats() returned NULL' |
265 |
return Result( |
266 |
{'os_name': s.os_name, |
267 |
'os_release': s.os_release, |
268 |
'os_version': s.os_version, |
269 |
'platform': s.platform, |
270 |
'hostname': s.hostname, |
271 |
'uptime': s.uptime, |
272 |
} |
273 |
) |
274 |
|
275 |
def py_get_disk_stats(): |
276 |
cdef disk_stat_t *s |
277 |
cdef int entries |
278 |
s = get_disk_stats(&entries) |
279 |
if s == NULL: |
280 |
raise StatgrabException, 'get_disk_stats() returned NULL' |
281 |
list = [] |
282 |
for i from 0 <= i < entries: |
283 |
list.append(Result( |
284 |
{'device_name': s.device_name, |
285 |
'fs_type': s.fs_type, |
286 |
'mnt_point': s.mnt_point, |
287 |
'size': s.size, |
288 |
'used': s.used, |
289 |
'avail': s.avail, |
290 |
'total_inodes': s.total_inodes, |
291 |
'used_inodes': s.used_inodes, |
292 |
'free_inodes': s.free_inodes, |
293 |
} |
294 |
)) |
295 |
s = s + 1 |
296 |
return list |
297 |
|
298 |
def py_get_diskio_stats(): |
299 |
cdef diskio_stat_t *s |
300 |
cdef int entries |
301 |
s = get_diskio_stats(&entries) |
302 |
if s == NULL: |
303 |
raise StatgrabException, 'get_diskio_stats() returned NULL' |
304 |
list = [] |
305 |
for i from 0 <= i < entries: |
306 |
list.append(Result( |
307 |
{'disk_name': s.disk_name, |
308 |
'read_bytes': s.read_bytes, |
309 |
'write_bytes': s.write_bytes, |
310 |
'systime': s.systime, |
311 |
} |
312 |
)) |
313 |
s = s + 1 |
314 |
return list |
315 |
|
316 |
def py_get_diskio_stats_diff(): |
317 |
cdef diskio_stat_t *s |
318 |
cdef int entries |
319 |
s = get_diskio_stats_diff(&entries) |
320 |
if s == NULL: |
321 |
raise StatgrabException, 'get_diskio_stats_diff() returned NULL' |
322 |
list = [] |
323 |
for i from 0 <= i < entries: |
324 |
list.append(Result( |
325 |
{'disk_name': s.disk_name, |
326 |
'read_bytes': s.read_bytes, |
327 |
'write_bytes': s.write_bytes, |
328 |
'systime': s.systime, |
329 |
} |
330 |
)) |
331 |
s = s + 1 |
332 |
return list |
333 |
|
334 |
def py_get_process_stats(): |
335 |
cdef process_stat_t *s |
336 |
s = get_process_stats() |
337 |
if s == NULL: |
338 |
raise StatgrabException, 'get_process_stats() returned NULL' |
339 |
return Result( |
340 |
{'total': s.total, |
341 |
'running': s.running, |
342 |
'sleeping': s.sleeping, |
343 |
'stopped': s.stopped, |
344 |
'zombie': s.zombie, |
345 |
} |
346 |
) |
347 |
|
348 |
def py_get_network_stats(): |
349 |
cdef network_stat_t *s |
350 |
cdef int entries |
351 |
s = get_network_stats(&entries) |
352 |
if s == NULL: |
353 |
raise StatgrabException, 'get_network_stats() returned NULL' |
354 |
list = [] |
355 |
for i from 0 <= i < entries: |
356 |
list.append(Result( |
357 |
{'interface_name': s.interface_name, |
358 |
'tx': s.tx, |
359 |
'rx': s.rx, |
360 |
'systime': s.systime, |
361 |
} |
362 |
)) |
363 |
s = s + 1 |
364 |
return list |
365 |
|
366 |
def py_get_network_stats_diff(): |
367 |
cdef network_stat_t *s |
368 |
cdef int entries |
369 |
s = get_network_stats_diff(&entries) |
370 |
if s == NULL: |
371 |
raise StatgrabException, 'get_network_stats_diff() returned NULL' |
372 |
list = [] |
373 |
for i from 0 <= i < entries: |
374 |
list.append(Result( |
375 |
{'interface_name': s.interface_name, |
376 |
'tx': s.tx, |
377 |
'rx': s.rx, |
378 |
'systime': s.systime, |
379 |
} |
380 |
)) |
381 |
s = s + 1 |
382 |
return list |
383 |
|
384 |
def py_get_network_iface_stats(): |
385 |
cdef network_iface_stat_t *s |
386 |
cdef int entries |
387 |
s = get_network_iface_stats(&entries) |
388 |
if s == NULL: |
389 |
raise StatgrabException, 'get_network_iface_stats() returned NULL' |
390 |
list = [] |
391 |
for i from 0 <= i < entries: |
392 |
list.append(Result( |
393 |
{'interface_name': s.interface_name, |
394 |
'speed': s.speed, |
395 |
'dup': s.dup, |
396 |
'up' : s.up, |
397 |
} |
398 |
)) |
399 |
s = s + 1 |
400 |
return list |
401 |
|
402 |
def py_get_page_stats(): |
403 |
cdef page_stat_t *s |
404 |
s = get_page_stats() |
405 |
if s == NULL: |
406 |
raise StatgrabException, 'get_page_stats() returned NULL' |
407 |
return Result( |
408 |
{'pages_pagein': s.pages_pagein, |
409 |
'pages_pageout': s.pages_pageout, |
410 |
} |
411 |
) |
412 |
|
413 |
def py_get_page_stats_diff(): |
414 |
cdef page_stat_t *s |
415 |
s = get_page_stats_diff() |
416 |
if s == NULL: |
417 |
raise StatgrabException, 'get_page_stats_diff() returned NULL' |
418 |
return Result( |
419 |
{'pages_pagein': s.pages_pagein, |
420 |
'pages_pageout': s.pages_pageout, |
421 |
} |
422 |
) |
423 |
|
424 |
def py_statgrab_init(): |
425 |
return statgrab_init() |
426 |
|
427 |
def py_statgrab_drop_privileges(): |
428 |
return statgrab_drop_privileges() |