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