1 |
< |
# TODO: 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 |
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, |
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, |
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, |
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, |
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, |
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, |
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, |
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, |
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(Result( |
284 |
|
{'device_name': s.device_name, |
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(Result( |
307 |
|
{'disk_name': s.disk_name, |
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(Result( |
325 |
|
{'disk_name': s.disk_name, |
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, |
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(Result( |
357 |
|
{'interface_name': s.interface_name, |
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(Result( |
375 |
|
{'interface_name': s.interface_name, |
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, |
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, |