1 |
< |
# TODO: return values more the python way - like os.stat |
2 |
< |
# deal with failures better (return nothing, or raise error?) |
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$ |
21 |
> |
# |
22 |
|
|
23 |
|
ctypedef long time_t |
24 |
|
|
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 |
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 |
< |
return {'user': s.user, |
166 |
< |
'kernel': s.kernel, |
167 |
< |
'idle': s.idle, |
168 |
< |
'iowait': s.iowait, |
169 |
< |
'swap': s.swap, |
170 |
< |
'nice': s.nice, |
171 |
< |
'total': s.total, |
172 |
< |
'systime': s.systime, |
173 |
< |
} |
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 |
< |
return {'user': s.user, |
183 |
< |
'kernel': s.kernel, |
184 |
< |
'idle': s.idle, |
185 |
< |
'iowait': s.iowait, |
186 |
< |
'swap': s.swap, |
187 |
< |
'nice': s.nice, |
188 |
< |
'total': s.total, |
189 |
< |
'systime': s.systime, |
190 |
< |
} |
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 |
< |
return {'user': s.user, |
200 |
< |
'kernel': s.kernel, |
201 |
< |
'idle': s.idle, |
202 |
< |
'iowait': s.iowait, |
203 |
< |
'swap': s.swap, |
204 |
< |
'nice': s.nice, |
205 |
< |
'time_taken': s.time_taken, |
206 |
< |
} |
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 |
< |
return {'total': s.total, |
216 |
< |
'used': s.used, |
217 |
< |
'free': s.free, |
218 |
< |
'cache': s.cache, |
219 |
< |
} |
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 |
< |
return {'min1': s.min1, |
229 |
< |
'min5': s.min5, |
230 |
< |
'min15': s.min15, |
231 |
< |
} |
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 |
< |
return {'name_list': s.name_list, |
241 |
< |
'num_entries': s.num_entries, |
242 |
< |
} |
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 |
< |
return {'total': s.total, |
252 |
< |
'used': s.used, |
253 |
< |
'free': s.free, |
254 |
< |
} |
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 |
< |
return {'os_name': s.os_name, |
264 |
< |
'os_release': s.os_release, |
265 |
< |
'os_version': s.os_version, |
266 |
< |
'platform': s.platform, |
267 |
< |
'hostname': s.hostname, |
268 |
< |
'uptime': s.uptime, |
269 |
< |
} |
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 |
< |
list = [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({'device_name': s.device_name, |
284 |
< |
'fs_type': s.fs_type, |
285 |
< |
'mnt_point': s.mnt_point, |
286 |
< |
'size': s.size, |
287 |
< |
'used': s.used, |
288 |
< |
'avail': s.avail, |
289 |
< |
'total_inodes': s.total_inodes, |
290 |
< |
'used_inodes': s.used_inodes, |
291 |
< |
'free_inodes': s.free_inodes, |
292 |
< |
}, |
293 |
< |
) |
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 |
|
|
299 |
|
cdef diskio_stat_t *s |
300 |
|
cdef int entries |
301 |
|
s = get_diskio_stats(&entries) |
302 |
< |
list = [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({'disk_name': s.disk_name, |
307 |
< |
'read_bytes': s.read_bytes, |
308 |
< |
'write_bytes': s.write_bytes, |
309 |
< |
'systime': s.systime, |
310 |
< |
}, |
311 |
< |
) |
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 |
|
|
317 |
|
cdef diskio_stat_t *s |
318 |
|
cdef int entries |
319 |
|
s = get_diskio_stats_diff(&entries) |
320 |
< |
list = [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({'disk_name': s.disk_name, |
325 |
< |
'read_bytes': s.read_bytes, |
326 |
< |
'write_bytes': s.write_bytes, |
327 |
< |
'systime': s.systime, |
328 |
< |
}, |
329 |
< |
) |
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 |
< |
return {'total': s.total, |
338 |
< |
'running': s.running, |
339 |
< |
'sleeping': s.sleeping, |
340 |
< |
'stopped': s.stopped, |
341 |
< |
'zombie': s.zombie, |
342 |
< |
} |
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 |
< |
list = [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({'interface_name': s.interface_name, |
357 |
< |
'tx': s.tx, |
358 |
< |
'rx': s.rx, |
359 |
< |
'systime': s.systime, |
360 |
< |
}, |
361 |
< |
) |
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 |
|
|
367 |
|
cdef network_stat_t *s |
368 |
|
cdef int entries |
369 |
|
s = get_network_stats_diff(&entries) |
370 |
< |
list = [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({'interface_name': s.interface_name, |
375 |
< |
'tx': s.tx, |
376 |
< |
'rx': s.rx, |
377 |
< |
'systime': s.systime, |
378 |
< |
}, |
379 |
< |
) |
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 |
< |
return {'pages_pagein': s.pages_pagein, |
406 |
< |
'pages_pageout': s.pages_pageout, |
407 |
< |
} |
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 |
< |
return {'pages_pagein': s.pages_pagein, |
417 |
< |
'pages_pageout': s.pages_pageout, |
418 |
< |
} |
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() |