ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/pystatgrab/_statgrab.pyx
(Generate patch)

Comparing projects/pystatgrab/_statgrab.pyx (file contents):
Revision 1.1 by tdb, Fri Feb 6 14:10:08 2004 UTC vs.
Revision 1.20 by tdb, Fri Jul 13 22:26:19 2007 UTC

# Line 1 | Line 1
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 pystatgrab
3 > # http://www.i-scream.org/pystatgrab/
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 + ctypedef int pid_t
25 + ctypedef int uid_t
26 + ctypedef int gid_t
27  
28   cdef extern from "statgrab.h":
29 <    ctypedef struct cpu_states_t:
29 >    cdef extern int sg_init()
30 >    cdef extern int sg_shutdown()
31 >    cdef extern int sg_snapshot()
32 >    cdef extern int sg_drop_privileges()
33 >
34 >    ctypedef enum sg_error:
35 >        SG_ERROR_NONE = 0
36 >        SG_ERROR_ASPRINTF
37 >        SG_ERROR_DEVSTAT_GETDEVS
38 >        SG_ERROR_DEVSTAT_SELECTDEVS
39 >        SG_ERROR_ENOENT
40 >        SG_ERROR_GETIFADDRS
41 >        SG_ERROR_GETMNTINFO
42 >        SG_ERROR_GETPAGESIZE
43 >        SG_ERROR_KSTAT_DATA_LOOKUP
44 >        SG_ERROR_KSTAT_LOOKUP
45 >        SG_ERROR_KSTAT_OPEN
46 >        SG_ERROR_KSTAT_READ
47 >        SG_ERROR_KVM_GETSWAPINFO
48 >        SG_ERROR_KVM_OPENFILES
49 >        SG_ERROR_MALLOC
50 >        SG_ERROR_OPEN
51 >        SG_ERROR_OPENDIR
52 >        SG_ERROR_PARSE
53 >        SG_ERROR_SETEGID
54 >        SG_ERROR_SETEUID
55 >        SG_ERROR_SETMNTENT
56 >        SG_ERROR_SOCKET
57 >        SG_ERROR_SWAPCTL
58 >        SG_ERROR_SYSCONF
59 >        SG_ERROR_SYSCTL
60 >        SG_ERROR_SYSCTLBYNAME
61 >        SG_ERROR_SYSCTLNAMETOMIB
62 >        SG_ERROR_UNAME
63 >        SG_ERROR_UNSUPPORTED
64 >        SG_ERROR_XSW_VER_MISMATCH
65 >
66 >    cdef extern void sg_set_error(sg_error code, char *arg)
67 >    cdef extern void sg_set_error_with_errno(sg_error code, char *arg)
68 >    cdef extern sg_error sg_get_error()
69 >    cdef extern char *sg_get_error_arg()
70 >    cdef extern int sg_get_error_errno()
71 >    cdef extern char *sg_str_error(sg_error code)
72 >
73 >    ctypedef struct sg_host_info:
74 >        char *os_name
75 >        char *os_release
76 >        char *os_version
77 >        char *platform
78 >        char *hostname
79 >        time_t uptime
80 >
81 >    cdef extern sg_host_info *sg_get_host_info()
82 >
83 >    ctypedef struct sg_cpu_stats:
84          long long user
85          long long kernel
86          long long idle
# Line 14 | Line 90 | cdef extern from "statgrab.h":
90          long long total
91          time_t systime
92  
93 <    ctypedef struct cpu_percent_t:
93 >    cdef extern sg_cpu_stats *sg_get_cpu_stats()
94 >    cdef extern sg_cpu_stats *sg_get_cpu_stats_diff()
95 >
96 >    ctypedef struct sg_cpu_percents:
97          float user
98          float kernel
99          float idle
# Line 23 | Line 102 | cdef extern from "statgrab.h":
102          float nice
103          time_t time_taken
104  
105 <    ctypedef struct mem_stat_t:
105 >    cdef extern sg_cpu_percents *sg_get_cpu_percents()
106 >
107 >    ctypedef struct sg_mem_stats:
108          long long total
109          long long free
110          long long used
111          long long cache
112  
113 <    ctypedef struct load_stat_t:
113 >    cdef extern sg_mem_stats *sg_get_mem_stats()
114 >
115 >    ctypedef struct sg_load_stats:
116          double min1
117          double min5
118          double min15
119  
120 <    ctypedef struct user_stat_t:
120 >    cdef extern sg_load_stats *sg_get_load_stats()
121 >
122 >    ctypedef struct sg_user_stats:
123          char *name_list
124          int num_entries
125  
126 <    ctypedef struct swap_stat_t:
126 >    cdef extern sg_user_stats *sg_get_user_stats()
127 >
128 >    ctypedef struct sg_swap_stats:
129          long long total
130          long long used
131          long long free
132  
133 <    ctypedef struct general_stat_t:
47 <        char *os_name
48 <        char *os_release
49 <        char *os_version
50 <        char *platform
51 <        char *hostname
52 <        time_t uptime
133 >    cdef extern sg_swap_stats *sg_get_swap_stats()
134  
135 <    ctypedef struct disk_stat_t:
135 >    ctypedef struct sg_fs_stats:
136          char *device_name
137          char *fs_type
138          char *mnt_point
# Line 61 | Line 142 | cdef extern from "statgrab.h":
142          long long total_inodes
143          long long used_inodes
144          long long free_inodes
145 +        long long avail_inodes
146 +        long long io_size
147 +        long long block_size
148 +        long long total_blocks
149 +        long long free_blocks
150 +        long long used_blocks
151 +        long long avail_blocks
152  
153 <    ctypedef struct diskio_stat_t:
153 >    cdef extern sg_fs_stats *sg_get_fs_stats(int *entries)
154 >
155 >    ctypedef struct sg_disk_io_stats:
156          char *disk_name
157          long long read_bytes
158          long long write_bytes
159          time_t systime
160  
161 <    ctypedef struct process_stat_t:
162 <        int total
73 <        int running
74 <        int sleeping
75 <        int stopped
76 <        int zombie
161 >    cdef extern sg_disk_io_stats *sg_get_disk_io_stats(int *entries)
162 >    cdef extern sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entries)
163  
164 <    ctypedef struct network_stat_t:
164 >    ctypedef struct sg_network_io_stats:
165          char *interface_name
166          long long tx
167          long long rx
168 +        long long ipackets
169 +        long long opackets
170 +        long long ierrors
171 +        long long oerrors
172 +        long long collisions
173          time_t systime
174  
175 <    ctypedef struct page_stat_t:
175 >    cdef extern sg_network_io_stats *sg_get_network_io_stats(int *entries)
176 >    cdef extern sg_network_io_stats *sg_get_network_io_stats_diff(int *entries)
177 >
178 >    ctypedef enum sg_iface_duplex:
179 >        SG_IFACE_DUPLEX_FULL
180 >        SG_IFACE_DUPLEX_HALF
181 >        SG_IFACE_DUPLEX_UNKNOWN
182 >
183 >    ctypedef struct sg_network_iface_stats:
184 >        char *interface_name
185 >        int speed
186 >        sg_iface_duplex duplex
187 >        int up
188 >
189 >    cdef extern sg_network_iface_stats *sg_get_network_iface_stats(int *entries)
190 >
191 >    ctypedef struct sg_page_stats:
192          long long pages_pagein
193          long long pages_pageout
194          time_t systime
195  
196 <    cdef extern cpu_states_t *get_cpu_totals()
197 <    cdef extern cpu_states_t *get_cpu_diff()
91 <    cdef extern cpu_percent_t *cpu_percent_usage()
92 <    cdef extern mem_stat_t *get_memory_stats()
93 <    cdef extern load_stat_t *get_load_stats()
94 <    cdef extern user_stat_t *get_user_stats()
95 <    cdef extern swap_stat_t *get_swap_stats()
96 <    cdef extern general_stat_t *get_general_stats()
97 <    cdef extern disk_stat_t *get_disk_stats(int *entries)
98 <    cdef extern diskio_stat_t *get_diskio_stats(int *entries)
99 <    cdef extern diskio_stat_t *get_diskio_stats_diff(int *entries)
100 <    cdef extern process_stat_t *get_process_stats()
101 <    cdef extern network_stat_t *get_network_stats(int *entries)
102 <    cdef extern network_stat_t *get_network_stats_diff(int *entries)
103 <    cdef extern page_stat_t *get_page_stats()
104 <    cdef extern page_stat_t *get_page_stats_diff()
105 <    cdef extern int statgrab_init()
106 <    cdef extern int statgrab_drop_privileges()
196 >    cdef extern sg_page_stats *sg_get_page_stats()
197 >    cdef extern sg_page_stats *sg_get_page_stats_diff()
198  
199 +    ctypedef enum sg_process_state:
200 +        SG_PROCESS_STATE_RUNNING
201 +        SG_PROCESS_STATE_SLEEPING
202 +        SG_PROCESS_STATE_STOPPED
203 +        SG_PROCESS_STATE_ZOMBIE
204 +        SG_PROCESS_STATE_UNKNOWN
205  
206 < def py_get_cpu_totals():
207 <    cdef cpu_states_t *s
208 <    s = get_cpu_totals()
209 <    return {'user': s.user,
210 <            'kernel': s.kernel,
211 <            'idle': s.idle,
212 <            'iowait': s.iowait,
213 <            'swap': s.swap,
214 <            'nice': s.nice,
215 <            'total': s.total,
216 <            'systime': s.systime,
217 <           }
206 >    ctypedef struct sg_process_stats:
207 >        char *process_name
208 >        char *proctitle
209 >        pid_t pid
210 >        pid_t parent
211 >        pid_t pgid
212 >        uid_t uid
213 >        uid_t euid
214 >        gid_t gid
215 >        gid_t egid
216 >        unsigned long long proc_size
217 >        unsigned long long proc_resident
218 >        time_t time_spent
219 >        double cpu_percent
220 >        int nice
221 >        sg_process_state state
222  
223 < def py_get_cpu_diff():
123 <    cdef cpu_states_t *s
124 <    s = get_cpu_diff()
125 <    return {'user': s.user,
126 <            'kernel': s.kernel,
127 <            'idle': s.idle,
128 <            'iowait': s.iowait,
129 <            'swap': s.swap,
130 <            'nice': s.nice,
131 <            'total': s.total,
132 <            'systime': s.systime,
133 <           }
223 >    cdef extern sg_process_stats *sg_get_process_stats(int *entries)
224  
225 < def py_cpu_percent_usage():
226 <    cdef cpu_percent_t *s
227 <    s = cpu_percent_usage()
228 <    return {'user': s.user,
229 <            'kernel': s.kernel,
230 <            'idle': s.idle,
141 <            'iowait': s.iowait,
142 <            'swap': s.swap,
143 <            'nice': s.nice,
144 <            'time_taken': s.time_taken,
145 <           }
225 >    ctypedef struct sg_process_count:
226 >        int total
227 >        int running
228 >        int sleeping
229 >        int stopped
230 >        int zombie
231  
232 < def py_get_memory_stats():
148 <    cdef mem_stat_t *s
149 <    s = get_memory_stats()
150 <    return {'total': s.total,
151 <            'used': s.used,
152 <            'free': s.free,
153 <            'cache': s.cache,
154 <           }
232 >    cdef extern sg_process_count *sg_get_process_count()
233  
156 def py_get_load_stats():
157    cdef load_stat_t *s
158    s = get_load_stats()
159    return {'min1': s.min1,
160            'min5': s.min5,
161            'min15': s.min15,
162           }
234  
235 < def py_get_user_stats():
236 <    cdef user_stat_t *s
237 <    s = get_user_stats()
238 <    return {'name_list': s.name_list,
239 <            'num_entries': s.num_entries,
240 <           }
235 > py_SG_ERROR_NONE = SG_ERROR_NONE
236 > py_SG_ERROR_ASPRINTF = SG_ERROR_ASPRINTF
237 > py_SG_ERROR_DEVSTAT_GETDEVS = SG_ERROR_DEVSTAT_GETDEVS
238 > py_SG_ERROR_DEVSTAT_SELECTDEVS = SG_ERROR_DEVSTAT_SELECTDEVS
239 > py_SG_ERROR_ENOENT = SG_ERROR_ENOENT
240 > py_SG_ERROR_GETIFADDRS = SG_ERROR_GETIFADDRS
241 > py_SG_ERROR_GETMNTINFO = SG_ERROR_GETMNTINFO
242 > py_SG_ERROR_GETPAGESIZE = SG_ERROR_GETPAGESIZE
243 > py_SG_ERROR_KSTAT_DATA_LOOKUP = SG_ERROR_KSTAT_DATA_LOOKUP
244 > py_SG_ERROR_KSTAT_LOOKUP = SG_ERROR_KSTAT_LOOKUP
245 > py_SG_ERROR_KSTAT_OPEN = SG_ERROR_KSTAT_OPEN
246 > py_SG_ERROR_KSTAT_READ = SG_ERROR_KSTAT_READ
247 > py_SG_ERROR_KVM_GETSWAPINFO = SG_ERROR_KVM_GETSWAPINFO
248 > py_SG_ERROR_KVM_OPENFILES = SG_ERROR_KVM_OPENFILES
249 > py_SG_ERROR_MALLOC = SG_ERROR_MALLOC
250 > py_SG_ERROR_OPEN = SG_ERROR_OPEN
251 > py_SG_ERROR_OPENDIR = SG_ERROR_OPENDIR
252 > py_SG_ERROR_PARSE = SG_ERROR_PARSE
253 > py_SG_ERROR_SETEGID = SG_ERROR_SETEGID
254 > py_SG_ERROR_SETEUID = SG_ERROR_SETEUID
255 > py_SG_ERROR_SETMNTENT = SG_ERROR_SETMNTENT
256 > py_SG_ERROR_SOCKET = SG_ERROR_SOCKET
257 > py_SG_ERROR_SWAPCTL = SG_ERROR_SWAPCTL
258 > py_SG_ERROR_SYSCONF = SG_ERROR_SYSCONF
259 > py_SG_ERROR_SYSCTL = SG_ERROR_SYSCTL
260 > py_SG_ERROR_SYSCTLBYNAME = SG_ERROR_SYSCTLBYNAME
261 > py_SG_ERROR_SYSCTLNAMETOMIB = SG_ERROR_SYSCTLNAMETOMIB
262 > py_SG_ERROR_UNAME = SG_ERROR_UNAME
263 > py_SG_ERROR_UNSUPPORTED = SG_ERROR_UNSUPPORTED
264 > py_SG_ERROR_XSW_VER_MISMATCH = SG_ERROR_XSW_VER_MISMATCH
265  
266 < def py_get_swap_stats():
267 <    cdef swap_stat_t *s
268 <    s = get_swap_stats()
174 <    return {'total': s.total,
175 <            'used': s.used,
176 <            'free': s.free,
177 <           }
266 > py_SG_IFACE_DUPLEX_FULL = SG_IFACE_DUPLEX_FULL
267 > py_SG_IFACE_DUPLEX_HALF = SG_IFACE_DUPLEX_HALF
268 > py_SG_IFACE_DUPLEX_UNKNOWN = SG_IFACE_DUPLEX_UNKNOWN
269  
270 < def py_get_general_stats():
271 <    cdef general_stat_t *s
272 <    s = get_general_stats()
273 <    return {'os_name': s.os_name,
274 <            'os_release': s.os_release,
184 <            'os_version': s.os_version,
185 <            'platform': s.platform,
186 <            'hostname': s.hostname,
187 <            'uptime': s.uptime,
188 <           }
270 > py_SG_PROCESS_STATE_RUNNING = SG_PROCESS_STATE_RUNNING
271 > py_SG_PROCESS_STATE_SLEEPING = SG_PROCESS_STATE_SLEEPING
272 > py_SG_PROCESS_STATE_STOPPED = SG_PROCESS_STATE_STOPPED
273 > py_SG_PROCESS_STATE_ZOMBIE = SG_PROCESS_STATE_ZOMBIE
274 > py_SG_PROCESS_STATE_UNKNOWN = SG_PROCESS_STATE_UNKNOWN
275  
276 < def py_get_disk_stats():
277 <    cdef disk_stat_t *s
276 >
277 > class Result:
278 >    def __init__(self, attrs):
279 >        self.attrs = attrs
280 >        for attr in attrs:
281 >            setattr(self, attr, attrs[attr])
282 >    def __getitem__(self, item):
283 >        return getattr(self, item)
284 >    def __repr__(self):
285 >        return str(self.attrs)
286 >
287 > class StatgrabException(Exception):
288 >    def __init__(self, value):
289 >        self.value = value
290 >    def __str__(self):
291 >        return repr(self.value)
292 >
293 >
294 > def py_sg_init():
295 >    if sg_init() == 0:
296 >        return True
297 >    else:
298 >        return False
299 >
300 > def py_sg_shutdown():
301 >    if sg_shutdown() == 0:
302 >        return True
303 >    else:
304 >        return False
305 >
306 > def py_sg_snapshot():
307 >    if sg_snapshot() == 0:
308 >        return True
309 >    else:
310 >        return False
311 >
312 > def py_sg_drop_privileges():
313 >    if sg_drop_privileges() == 0:
314 >        return True
315 >    else:
316 >        return False
317 >
318 > def py_sg_set_error(code, arg):
319 >    sg_set_error(code, arg)
320 >
321 > def py_sg_set_error_with_errno(code, arg):
322 >    sg_set_error_with_errno(code, arg)
323 >
324 > def py_sg_get_error():
325 >    cdef sg_error s
326 >    s = sg_get_error()
327 >    return s
328 >
329 > def py_sg_get_error_arg():
330 >    s = sg_get_error_arg()
331 >    return s
332 >
333 > def py_sg_get_error_errno():
334 >    s = sg_get_error_errno()
335 >    return s
336 >
337 > def py_sg_str_error(code):
338 >    s = sg_str_error(code)
339 >    return s
340 >
341 > def py_sg_get_host_info():
342 >    cdef sg_host_info *s
343 >    s = sg_get_host_info()
344 >    if s == NULL:
345 >        raise StatgrabException, 'sg_get_host_info() returned NULL'
346 >    return Result(
347 >        {'os_name': s.os_name,
348 >         'os_release': s.os_release,
349 >         'os_version': s.os_version,
350 >         'platform': s.platform,
351 >         'hostname': s.hostname,
352 >         'uptime': s.uptime,
353 >        }
354 >    )
355 >
356 > def py_sg_get_cpu_stats():
357 >    cdef sg_cpu_stats *s
358 >    s = sg_get_cpu_stats()
359 >    if s == NULL:
360 >        raise StatgrabException, 'sg_get_cpu_stats() returned NULL'
361 >    return Result(
362 >        {'user': s.user,
363 >         'kernel': s.kernel,
364 >         'idle': s.idle,
365 >         'iowait': s.iowait,
366 >         'swap': s.swap,
367 >         'nice': s.nice,
368 >         'total': s.total,
369 >         'systime': s.systime,
370 >        }
371 >    )
372 >
373 > def py_sg_get_cpu_stats_diff():
374 >    cdef sg_cpu_stats *s
375 >    s = sg_get_cpu_stats_diff()
376 >    if s == NULL:
377 >        raise StatgrabException, 'sg_get_cpu_stats_diff() returned NULL'
378 >    return Result(
379 >        {'user': s.user,
380 >         'kernel': s.kernel,
381 >         'idle': s.idle,
382 >         'iowait': s.iowait,
383 >         'swap': s.swap,
384 >         'nice': s.nice,
385 >         'total': s.total,
386 >         'systime': s.systime,
387 >        }
388 >    )
389 >
390 > def py_sg_get_cpu_percents():
391 >    cdef sg_cpu_percents *s
392 >    s = sg_get_cpu_percents()
393 >    if s == NULL:
394 >        raise StatgrabException, 'sg_get_cpu_percents() returned NULL'
395 >    return Result(
396 >        {'user': s.user,
397 >         'kernel': s.kernel,
398 >         'idle': s.idle,
399 >         'iowait': s.iowait,
400 >         'swap': s.swap,
401 >         'nice': s.nice,
402 >         'time_taken': s.time_taken,
403 >        }
404 >    )
405 >
406 > def py_sg_get_mem_stats():
407 >    cdef sg_mem_stats *s
408 >    s = sg_get_mem_stats()
409 >    if s == NULL:
410 >        raise StatgrabException, 'sg_get_mem_stats() returned NULL'
411 >    return Result(
412 >        {'total': s.total,
413 >         'used': s.used,
414 >         'free': s.free,
415 >         'cache': s.cache,
416 >        }
417 >    )
418 >
419 > def py_sg_get_load_stats():
420 >    cdef sg_load_stats *s
421 >    s = sg_get_load_stats()
422 >    if s == NULL:
423 >        raise StatgrabException, 'sg_get_load_stats() returned NULL'
424 >    return Result(
425 >        {'min1': s.min1,
426 >         'min5': s.min5,
427 >         'min15': s.min15,
428 >        }
429 >    )
430 >
431 > def py_sg_get_user_stats():
432 >    cdef sg_user_stats *s
433 >    s = sg_get_user_stats()
434 >    if s == NULL:
435 >        raise StatgrabException, 'sg_get_user_stats() returned NULL'
436 >    return Result(
437 >        {'name_list': s.name_list,
438 >         'num_entries': s.num_entries,
439 >        }
440 >    )
441 >
442 > def py_sg_get_swap_stats():
443 >    cdef sg_swap_stats *s
444 >    s = sg_get_swap_stats()
445 >    if s == NULL:
446 >        raise StatgrabException, 'sg_get_swap_stats() returned NULL'
447 >    return Result(
448 >        {'total': s.total,
449 >         'used': s.used,
450 >         'free': s.free,
451 >        }
452 >    )
453 >
454 > def py_sg_get_fs_stats():
455 >    cdef sg_fs_stats *s
456      cdef int entries
457 <    s = get_disk_stats(&entries)
458 <    list = [entries]
457 >    s = sg_get_fs_stats(&entries)
458 >    if s == NULL:
459 >        raise StatgrabException, 'sg_get_fs_stats() returned NULL'
460 >    list = []
461      for i from 0 <= i < entries:
462 <        list.append({'device_name': s.device_name,
463 <                     'fs_type': s.fs_type,
464 <                     'mnt_point': s.mnt_point,
465 <                     'size': s.size,
466 <                     'used': s.used,
467 <                     'avail': s.avail,
468 <                     'total_inodes': s.total_inodes,
469 <                     'used_inodes': s.used_inodes,
470 <                     'free_inodes': s.free_inodes,
471 <                    },
472 <                   )
462 >        list.append(Result(
463 >            {'device_name': s.device_name,
464 >             'fs_type': s.fs_type,
465 >             'mnt_point': s.mnt_point,
466 >             'size': s.size,
467 >             'used': s.used,
468 >             'avail': s.avail,
469 >             'total_inodes': s.total_inodes,
470 >             'used_inodes': s.used_inodes,
471 >             'free_inodes': s.free_inodes,
472 >             'avail_inodes': s.avail_inodes,
473 >             'io_size': s.io_size,
474 >             'block_size': s.block_size,
475 >             'total_blocks': s.total_blocks,
476 >             'free_blocks': s.free_blocks,
477 >             'used_blocks': s.used_blocks,
478 >             'avail_blocks': s.avail_blocks,
479 >            }
480 >        ))
481          s = s + 1
482      return list
483  
484 < def py_get_diskio_stats():
485 <    cdef diskio_stat_t *s
484 > def py_sg_get_disk_io_stats():
485 >    cdef sg_disk_io_stats *s
486      cdef int entries
487 <    s = get_diskio_stats(&entries)
488 <    list = [entries]
487 >    s = sg_get_disk_io_stats(&entries)
488 >    if s == NULL:
489 >        raise StatgrabException, 'sg_get_disk_io_stats() returned NULL'
490 >    list = []
491      for i from 0 <= i < entries:
492 <        list.append({'disk_name': s.disk_name,
493 <                     'read_bytes': s.read_bytes,
494 <                     'write_bytes': s.write_bytes,
495 <                     'systime': s.systime,
496 <                    },
497 <                   )
492 >        list.append(Result(
493 >            {'disk_name': s.disk_name,
494 >             'read_bytes': s.read_bytes,
495 >             'write_bytes': s.write_bytes,
496 >             'systime': s.systime,
497 >            }
498 >        ))
499          s = s + 1
500      return list
501  
502 < def py_get_diskio_stats_diff():
503 <    cdef diskio_stat_t *s
502 > def py_sg_get_disk_io_stats_diff():
503 >    cdef sg_disk_io_stats *s
504      cdef int entries
505 <    s = get_diskio_stats_diff(&entries)
506 <    list = [entries]
505 >    s = sg_get_disk_io_stats_diff(&entries)
506 >    if s == NULL:
507 >        raise StatgrabException, 'sg_get_disk_io_stats_diff() returned NULL'
508 >    list = []
509      for i from 0 <= i < entries:
510 <        list.append({'disk_name': s.disk_name,
511 <                     'read_bytes': s.read_bytes,
512 <                     'write_bytes': s.write_bytes,
513 <                     'systime': s.systime,
514 <                    },
515 <                   )
510 >        list.append(Result(
511 >            {'disk_name': s.disk_name,
512 >             'read_bytes': s.read_bytes,
513 >             'write_bytes': s.write_bytes,
514 >             'systime': s.systime,
515 >            }
516 >        ))
517          s = s + 1
518      return list
519  
520 < def py_get_process_stats():
521 <    cdef process_stat_t *s
522 <    s = get_process_stats()
523 <    return {'total': s.total,
524 <            'running': s.running,
525 <            'sleeping': s.sleeping,
526 <            'stopped': s.stopped,
527 <            'zombie': s.zombie,
528 <           }
520 > def py_sg_get_network_io_stats():
521 >    cdef sg_network_io_stats *s
522 >    cdef int entries
523 >    s = sg_get_network_io_stats(&entries)
524 >    if s == NULL:
525 >        raise StatgrabException, 'sg_get_network_io_stats() returned NULL'
526 >    list = []
527 >    for i from 0 <= i < entries:
528 >        list.append(Result(
529 >            {'interface_name': s.interface_name,
530 >             'tx': s.tx,
531 >             'rx': s.rx,
532 >             'ipackets': s.ipackets,
533 >             'opackets': s.opackets,
534 >             'ierrors': s.ierrors,
535 >             'oerrors': s.oerrors,
536 >             'collisions': s.collisions,
537 >             'systime': s.systime,
538 >            }
539 >        ))
540 >        s = s + 1
541 >    return list
542  
543 < def py_get_network_stats():
544 <    cdef network_stat_t *s
543 > def py_sg_get_network_io_stats_diff():
544 >    cdef sg_network_io_stats *s
545      cdef int entries
546 <    s = get_network_stats(&entries)
547 <    list = [entries]
546 >    s = sg_get_network_io_stats_diff(&entries)
547 >    if s == NULL:
548 >        raise StatgrabException, 'sg_get_network_io_stats_diff() returned NULL'
549 >    list = []
550      for i from 0 <= i < entries:
551 <        list.append({'interface_name': s.interface_name,
552 <                     'tx': s.tx,
553 <                     'rx': s.rx,
554 <                     'systime': s.systime,
555 <                    },
556 <                   )
551 >        list.append(Result(
552 >            {'interface_name': s.interface_name,
553 >             'tx': s.tx,
554 >             'rx': s.rx,
555 >             'ipackets': s.ipackets,
556 >             'opackets': s.opackets,
557 >             'ierrors': s.ierrors,
558 >             'oerrors': s.oerrors,
559 >             'collisions': s.collisions,
560 >             'systime': s.systime,
561 >            }
562 >        ))
563          s = s + 1
564      return list
565  
566 < def py_get_network_stats_diff():
567 <    cdef network_stat_t *s
566 > def py_sg_get_network_iface_stats():
567 >    cdef sg_network_iface_stats *s
568      cdef int entries
569 <    s = get_network_stats_diff(&entries)
570 <    list = [entries]
569 >    s = sg_get_network_iface_stats(&entries)
570 >    if s == NULL:
571 >        raise StatgrabException, 'sg_get_network_iface_stats() returned NULL'
572 >    list = []
573      for i from 0 <= i < entries:
574 <        list.append({'interface_name': s.interface_name,
575 <                     'tx': s.tx,
576 <                     'rx': s.rx,
577 <                     'systime': s.systime,
578 <                    },
579 <                   )
574 >        list.append(Result(
575 >            {'interface_name': s.interface_name,
576 >             'speed': s.speed,
577 >             'duplex': s.duplex,
578 >             'up' : s.up,
579 >            }
580 >        ))
581          s = s + 1
582      return list
583  
584 < def py_get_page_stats():
585 <    cdef page_stat_t *s
586 <    s = get_page_stats()
587 <    return {'pages_pagein': s.pages_pagein,
588 <            'pages_pageout': s.pages_pageout,
589 <           }
584 > def py_sg_get_page_stats():
585 >    cdef sg_page_stats *s
586 >    s = sg_get_page_stats()
587 >    if s == NULL:
588 >        raise StatgrabException, 'sg_get_page_stats() returned NULL'
589 >    return Result(
590 >        {'pages_pagein': s.pages_pagein,
591 >         'pages_pageout': s.pages_pageout,
592 >        }
593 >    )
594  
595 < def py_get_page_stats_diff():
596 <    cdef page_stat_t *s
597 <    s = get_page_stats_diff()
598 <    return {'pages_pagein': s.pages_pagein,
599 <            'pages_pageout': s.pages_pageout,
600 <           }
595 > def py_sg_get_page_stats_diff():
596 >    cdef sg_page_stats *s
597 >    s = sg_get_page_stats_diff()
598 >    if s == NULL:
599 >        raise StatgrabException, 'sg_get_page_stats_diff() returned NULL'
600 >    return Result(
601 >        {'pages_pagein': s.pages_pagein,
602 >         'pages_pageout': s.pages_pageout,
603 >        }
604 >    )
605  
606 < def py_statgrab_init():
607 <    return statgrab_init()
606 > def py_sg_get_process_stats():
607 >    cdef sg_process_stats *s
608 >    cdef int entries
609 >    s = sg_get_process_stats(&entries)
610 >    if s == NULL:
611 >        raise StatgrabException, 'sg_get_process_stats() returned NULL'
612 >    list = []
613 >    for i from 0 <= i < entries:
614 >        if s.process_name is NULL:
615 >            process_name = ''
616 >        else:
617 >            process_name = s.process_name
618 >        if s.proctitle is NULL:
619 >            proctitle = ''
620 >        else:
621 >            proctitle = s.proctitle
622 >        list.append(Result(
623 >            {'process_name': process_name,
624 >             'proctitle' : proctitle,
625 >             'pid' : s.pid,
626 >             'parent' : s.parent,
627 >             'pgid' : s.pgid,
628 >             'uid' : s.uid,
629 >             'euid' : s.euid,
630 >             'gid' : s.gid,
631 >             'egid' : s.egid,
632 >             'proc_size' : s.proc_size,
633 >             'proc_resident' : s.proc_resident,
634 >             'time_spent' : s.time_spent,
635 >             'cpu_percent' : s.cpu_percent,
636 >             'nice' : s.nice,
637 >             'state' : s.state,
638 >            }
639 >        ))
640 >        s = s + 1
641 >    return list
642  
643 < def py_statgrab_drop_privileges():
644 <    return statgrab_drop_privileges()
643 > def py_sg_get_process_count():
644 >    cdef sg_process_count *s
645 >    s = sg_get_process_count()
646 >    if s == NULL:
647 >        raise StatgrabException, 'sg_get_process_count() returned NULL'
648 >    return Result(
649 >        {'total': s.total,
650 >         'running': s.running,
651 >         'sleeping': s.sleeping,
652 >         'stopped': s.stopped,
653 >         'zombie': s.zombie,
654 >        }
655 >    )

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines