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.5 2004/02/13 00:16:20 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 duplex: |
104 |
FULL_DUPLEX |
105 |
HALF_DUPLEX |
106 |
NO_DUPLEX |
107 |
|
108 |
ctypedef struct network_iface_stat_t: |
109 |
char *interface_name |
110 |
int speed |
111 |
duplex dup |
112 |
|
113 |
ctypedef struct page_stat_t: |
114 |
long long pages_pagein |
115 |
long long pages_pageout |
116 |
time_t systime |
117 |
|
118 |
cdef extern cpu_states_t *get_cpu_totals() |
119 |
cdef extern cpu_states_t *get_cpu_diff() |
120 |
cdef extern cpu_percent_t *cpu_percent_usage() |
121 |
cdef extern mem_stat_t *get_memory_stats() |
122 |
cdef extern load_stat_t *get_load_stats() |
123 |
cdef extern user_stat_t *get_user_stats() |
124 |
cdef extern swap_stat_t *get_swap_stats() |
125 |
cdef extern general_stat_t *get_general_stats() |
126 |
cdef extern disk_stat_t *get_disk_stats(int *entries) |
127 |
cdef extern diskio_stat_t *get_diskio_stats(int *entries) |
128 |
cdef extern diskio_stat_t *get_diskio_stats_diff(int *entries) |
129 |
cdef extern process_stat_t *get_process_stats() |
130 |
cdef extern network_stat_t *get_network_stats(int *entries) |
131 |
cdef extern network_stat_t *get_network_stats_diff(int *entries) |
132 |
cdef extern network_iface_stat_t *get_network_iface_stats(int *entries) |
133 |
cdef extern page_stat_t *get_page_stats() |
134 |
cdef extern page_stat_t *get_page_stats_diff() |
135 |
cdef extern int statgrab_init() |
136 |
cdef extern int statgrab_drop_privileges() |
137 |
|
138 |
|
139 |
class Result: |
140 |
def __init__(self, attrs): |
141 |
self.attrs = attrs |
142 |
for attr in attrs: |
143 |
setattr(self, attr, attrs[attr]) |
144 |
def __getitem__(self, item): |
145 |
return getattr(self, item) |
146 |
def __repr__(self): |
147 |
return str(self.attrs) |
148 |
|
149 |
class StatgrabException(Exception): |
150 |
def __init__(self, value): |
151 |
self.value = value |
152 |
def __str__(self): |
153 |
return repr(self.value) |
154 |
|
155 |
|
156 |
def py_get_cpu_totals(): |
157 |
cdef cpu_states_t *s |
158 |
s = get_cpu_totals() |
159 |
if s == NULL: |
160 |
raise StatgrabException, 'get_cpu_totals() returned NULL' |
161 |
return Result( |
162 |
{'user': s.user, |
163 |
'kernel': s.kernel, |
164 |
'idle': s.idle, |
165 |
'iowait': s.iowait, |
166 |
'swap': s.swap, |
167 |
'nice': s.nice, |
168 |
'total': s.total, |
169 |
'systime': s.systime, |
170 |
} |
171 |
) |
172 |
|
173 |
def py_get_cpu_diff(): |
174 |
cdef cpu_states_t *s |
175 |
s = get_cpu_diff() |
176 |
if s == NULL: |
177 |
raise StatgrabException, 'get_cpu_diff() returned NULL' |
178 |
return Result( |
179 |
{'user': s.user, |
180 |
'kernel': s.kernel, |
181 |
'idle': s.idle, |
182 |
'iowait': s.iowait, |
183 |
'swap': s.swap, |
184 |
'nice': s.nice, |
185 |
'total': s.total, |
186 |
'systime': s.systime, |
187 |
} |
188 |
) |
189 |
|
190 |
def py_cpu_percent_usage(): |
191 |
cdef cpu_percent_t *s |
192 |
s = cpu_percent_usage() |
193 |
if s == NULL: |
194 |
raise StatgrabException, 'cpu_percent_usage() returned NULL' |
195 |
return Result( |
196 |
{'user': s.user, |
197 |
'kernel': s.kernel, |
198 |
'idle': s.idle, |
199 |
'iowait': s.iowait, |
200 |
'swap': s.swap, |
201 |
'nice': s.nice, |
202 |
'time_taken': s.time_taken, |
203 |
} |
204 |
) |
205 |
|
206 |
def py_get_memory_stats(): |
207 |
cdef mem_stat_t *s |
208 |
s = get_memory_stats() |
209 |
if s == NULL: |
210 |
raise StatgrabException, 'get_memory_stats() returned NULL' |
211 |
return Result( |
212 |
{'total': s.total, |
213 |
'used': s.used, |
214 |
'free': s.free, |
215 |
'cache': s.cache, |
216 |
} |
217 |
) |
218 |
|
219 |
def py_get_load_stats(): |
220 |
cdef load_stat_t *s |
221 |
s = get_load_stats() |
222 |
if s == NULL: |
223 |
raise StatgrabException, 'get_load_stats() returned NULL' |
224 |
return Result( |
225 |
{'min1': s.min1, |
226 |
'min5': s.min5, |
227 |
'min15': s.min15, |
228 |
} |
229 |
) |
230 |
|
231 |
def py_get_user_stats(): |
232 |
cdef user_stat_t *s |
233 |
s = get_user_stats() |
234 |
if s == NULL: |
235 |
raise StatgrabException, 'get_user_stats() returned NULL' |
236 |
return Result( |
237 |
{'name_list': s.name_list, |
238 |
'num_entries': s.num_entries, |
239 |
} |
240 |
) |
241 |
|
242 |
def py_get_swap_stats(): |
243 |
cdef swap_stat_t *s |
244 |
s = get_swap_stats() |
245 |
if s == NULL: |
246 |
raise StatgrabException, 'get_swap_stats() returned NULL' |
247 |
return Result( |
248 |
{'total': s.total, |
249 |
'used': s.used, |
250 |
'free': s.free, |
251 |
} |
252 |
) |
253 |
|
254 |
def py_get_general_stats(): |
255 |
cdef general_stat_t *s |
256 |
s = get_general_stats() |
257 |
if s == NULL: |
258 |
raise StatgrabException, 'get_general_stats() returned NULL' |
259 |
return Result( |
260 |
{'os_name': s.os_name, |
261 |
'os_release': s.os_release, |
262 |
'os_version': s.os_version, |
263 |
'platform': s.platform, |
264 |
'hostname': s.hostname, |
265 |
'uptime': s.uptime, |
266 |
} |
267 |
) |
268 |
|
269 |
def py_get_disk_stats(): |
270 |
cdef disk_stat_t *s |
271 |
cdef int entries |
272 |
s = get_disk_stats(&entries) |
273 |
if s == NULL: |
274 |
raise StatgrabException, 'get_disk_stats() returned NULL' |
275 |
list = [] |
276 |
for i from 0 <= i < entries: |
277 |
list.append(Result( |
278 |
{'device_name': s.device_name, |
279 |
'fs_type': s.fs_type, |
280 |
'mnt_point': s.mnt_point, |
281 |
'size': s.size, |
282 |
'used': s.used, |
283 |
'avail': s.avail, |
284 |
'total_inodes': s.total_inodes, |
285 |
'used_inodes': s.used_inodes, |
286 |
'free_inodes': s.free_inodes, |
287 |
} |
288 |
)) |
289 |
s = s + 1 |
290 |
return list |
291 |
|
292 |
def py_get_diskio_stats(): |
293 |
cdef diskio_stat_t *s |
294 |
cdef int entries |
295 |
s = get_diskio_stats(&entries) |
296 |
if s == NULL: |
297 |
raise StatgrabException, 'get_diskio_stats() returned NULL' |
298 |
list = [] |
299 |
for i from 0 <= i < entries: |
300 |
list.append(Result( |
301 |
{'disk_name': s.disk_name, |
302 |
'read_bytes': s.read_bytes, |
303 |
'write_bytes': s.write_bytes, |
304 |
'systime': s.systime, |
305 |
} |
306 |
)) |
307 |
s = s + 1 |
308 |
return list |
309 |
|
310 |
def py_get_diskio_stats_diff(): |
311 |
cdef diskio_stat_t *s |
312 |
cdef int entries |
313 |
s = get_diskio_stats_diff(&entries) |
314 |
if s == NULL: |
315 |
raise StatgrabException, 'get_diskio_stats_diff() returned NULL' |
316 |
list = [] |
317 |
for i from 0 <= i < entries: |
318 |
list.append(Result( |
319 |
{'disk_name': s.disk_name, |
320 |
'read_bytes': s.read_bytes, |
321 |
'write_bytes': s.write_bytes, |
322 |
'systime': s.systime, |
323 |
} |
324 |
)) |
325 |
s = s + 1 |
326 |
return list |
327 |
|
328 |
def py_get_process_stats(): |
329 |
cdef process_stat_t *s |
330 |
s = get_process_stats() |
331 |
if s == NULL: |
332 |
raise StatgrabException, 'get_process_stats() returned NULL' |
333 |
return Result( |
334 |
{'total': s.total, |
335 |
'running': s.running, |
336 |
'sleeping': s.sleeping, |
337 |
'stopped': s.stopped, |
338 |
'zombie': s.zombie, |
339 |
} |
340 |
) |
341 |
|
342 |
def py_get_network_stats(): |
343 |
cdef network_stat_t *s |
344 |
cdef int entries |
345 |
s = get_network_stats(&entries) |
346 |
if s == NULL: |
347 |
raise StatgrabException, 'get_network_stats() returned NULL' |
348 |
list = [] |
349 |
for i from 0 <= i < entries: |
350 |
list.append(Result( |
351 |
{'interface_name': s.interface_name, |
352 |
'tx': s.tx, |
353 |
'rx': s.rx, |
354 |
'systime': s.systime, |
355 |
} |
356 |
)) |
357 |
s = s + 1 |
358 |
return list |
359 |
|
360 |
def py_get_network_stats_diff(): |
361 |
cdef network_stat_t *s |
362 |
cdef int entries |
363 |
s = get_network_stats_diff(&entries) |
364 |
if s == NULL: |
365 |
raise StatgrabException, 'get_network_stats_diff() returned NULL' |
366 |
list = [] |
367 |
for i from 0 <= i < entries: |
368 |
list.append(Result( |
369 |
{'interface_name': s.interface_name, |
370 |
'tx': s.tx, |
371 |
'rx': s.rx, |
372 |
'systime': s.systime, |
373 |
} |
374 |
)) |
375 |
s = s + 1 |
376 |
return list |
377 |
|
378 |
def py_get_network_iface_stats(): |
379 |
cdef network_iface_stat_t *s |
380 |
cdef int entries |
381 |
s = get_network_iface_stats(&entries) |
382 |
if s == NULL: |
383 |
raise StatgrabException, 'get_network_iface_stats() returned NULL' |
384 |
list = [] |
385 |
for i from 0 <= i < entries: |
386 |
list.append(Result( |
387 |
{'interface_name': s.interface_name, |
388 |
'speed': s.speed, |
389 |
'dup': s.dup, |
390 |
} |
391 |
)) |
392 |
s = s + 1 |
393 |
return list |
394 |
|
395 |
def py_get_page_stats(): |
396 |
cdef page_stat_t *s |
397 |
s = get_page_stats() |
398 |
if s == NULL: |
399 |
raise StatgrabException, 'get_page_stats() returned NULL' |
400 |
return Result( |
401 |
{'pages_pagein': s.pages_pagein, |
402 |
'pages_pageout': s.pages_pageout, |
403 |
} |
404 |
) |
405 |
|
406 |
def py_get_page_stats_diff(): |
407 |
cdef page_stat_t *s |
408 |
s = get_page_stats_diff() |
409 |
if s == NULL: |
410 |
raise StatgrabException, 'get_page_stats_diff() returned NULL' |
411 |
return Result( |
412 |
{'pages_pagein': s.pages_pagein, |
413 |
'pages_pageout': s.pages_pageout, |
414 |
} |
415 |
) |
416 |
|
417 |
def py_statgrab_init(): |
418 |
return statgrab_init() |
419 |
|
420 |
def py_statgrab_drop_privileges(): |
421 |
return statgrab_drop_privileges() |