ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/pystatgrab/_statgrab.pyx
Revision: 1.21
Committed: Mon Jun 16 21:06:49 2008 UTC (16 years ago) by tdb
Branch: MAIN
CVS Tags: HEAD
Changes since 1.20: +10 -9 lines
Log Message:
Modify the Result class to extend dict. Then overload __getattr__ to still
allow it to be used as if it had attributes. This makes it easier to work
with the data (iteration over values, etc) and maintains compatibility.

File Contents

# Content
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: _statgrab.pyx,v 1.20 2007/07/13 22:26:19 tdb Exp $
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 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
87 long long iowait
88 long long swap
89 long long nice
90 long long total
91 time_t systime
92
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
100 float iowait
101 float swap
102 float nice
103 time_t time_taken
104
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 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 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 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 cdef extern sg_swap_stats *sg_get_swap_stats()
134
135 ctypedef struct sg_fs_stats:
136 char *device_name
137 char *fs_type
138 char *mnt_point
139 long long size
140 long long used
141 long long avail
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 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 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 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 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 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 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 cdef extern sg_process_stats *sg_get_process_stats(int *entries)
224
225 ctypedef struct sg_process_count:
226 int total
227 int running
228 int sleeping
229 int stopped
230 int zombie
231
232 cdef extern sg_process_count *sg_get_process_count()
233
234
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 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 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
277 class Result(dict):
278 def __init__(self, attrs):
279 self.attrs = attrs # to maintain compatibility
280 super(Result, self).__init__(attrs)
281
282 def __getattr__(self, item):
283 try:
284 return self.__getitem__(item)
285 except KeyError:
286 raise AttributeError(item)
287
288 class StatgrabException(Exception):
289 def __init__(self, value):
290 self.value = value
291 def __str__(self):
292 return repr(self.value)
293
294
295 def py_sg_init():
296 if sg_init() == 0:
297 return True
298 else:
299 return False
300
301 def py_sg_shutdown():
302 if sg_shutdown() == 0:
303 return True
304 else:
305 return False
306
307 def py_sg_snapshot():
308 if sg_snapshot() == 0:
309 return True
310 else:
311 return False
312
313 def py_sg_drop_privileges():
314 if sg_drop_privileges() == 0:
315 return True
316 else:
317 return False
318
319 def py_sg_set_error(code, arg):
320 sg_set_error(code, arg)
321
322 def py_sg_set_error_with_errno(code, arg):
323 sg_set_error_with_errno(code, arg)
324
325 def py_sg_get_error():
326 cdef sg_error s
327 s = sg_get_error()
328 return s
329
330 def py_sg_get_error_arg():
331 s = sg_get_error_arg()
332 return s
333
334 def py_sg_get_error_errno():
335 s = sg_get_error_errno()
336 return s
337
338 def py_sg_str_error(code):
339 s = sg_str_error(code)
340 return s
341
342 def py_sg_get_host_info():
343 cdef sg_host_info *s
344 s = sg_get_host_info()
345 if s == NULL:
346 raise StatgrabException, 'sg_get_host_info() returned NULL'
347 return Result(
348 {'os_name': s.os_name,
349 'os_release': s.os_release,
350 'os_version': s.os_version,
351 'platform': s.platform,
352 'hostname': s.hostname,
353 'uptime': s.uptime,
354 }
355 )
356
357 def py_sg_get_cpu_stats():
358 cdef sg_cpu_stats *s
359 s = sg_get_cpu_stats()
360 if s == NULL:
361 raise StatgrabException, 'sg_get_cpu_stats() returned NULL'
362 return Result(
363 {'user': s.user,
364 'kernel': s.kernel,
365 'idle': s.idle,
366 'iowait': s.iowait,
367 'swap': s.swap,
368 'nice': s.nice,
369 'total': s.total,
370 'systime': s.systime,
371 }
372 )
373
374 def py_sg_get_cpu_stats_diff():
375 cdef sg_cpu_stats *s
376 s = sg_get_cpu_stats_diff()
377 if s == NULL:
378 raise StatgrabException, 'sg_get_cpu_stats_diff() returned NULL'
379 return Result(
380 {'user': s.user,
381 'kernel': s.kernel,
382 'idle': s.idle,
383 'iowait': s.iowait,
384 'swap': s.swap,
385 'nice': s.nice,
386 'total': s.total,
387 'systime': s.systime,
388 }
389 )
390
391 def py_sg_get_cpu_percents():
392 cdef sg_cpu_percents *s
393 s = sg_get_cpu_percents()
394 if s == NULL:
395 raise StatgrabException, 'sg_get_cpu_percents() returned NULL'
396 return Result(
397 {'user': s.user,
398 'kernel': s.kernel,
399 'idle': s.idle,
400 'iowait': s.iowait,
401 'swap': s.swap,
402 'nice': s.nice,
403 'time_taken': s.time_taken,
404 }
405 )
406
407 def py_sg_get_mem_stats():
408 cdef sg_mem_stats *s
409 s = sg_get_mem_stats()
410 if s == NULL:
411 raise StatgrabException, 'sg_get_mem_stats() returned NULL'
412 return Result(
413 {'total': s.total,
414 'used': s.used,
415 'free': s.free,
416 'cache': s.cache,
417 }
418 )
419
420 def py_sg_get_load_stats():
421 cdef sg_load_stats *s
422 s = sg_get_load_stats()
423 if s == NULL:
424 raise StatgrabException, 'sg_get_load_stats() returned NULL'
425 return Result(
426 {'min1': s.min1,
427 'min5': s.min5,
428 'min15': s.min15,
429 }
430 )
431
432 def py_sg_get_user_stats():
433 cdef sg_user_stats *s
434 s = sg_get_user_stats()
435 if s == NULL:
436 raise StatgrabException, 'sg_get_user_stats() returned NULL'
437 return Result(
438 {'name_list': s.name_list,
439 'num_entries': s.num_entries,
440 }
441 )
442
443 def py_sg_get_swap_stats():
444 cdef sg_swap_stats *s
445 s = sg_get_swap_stats()
446 if s == NULL:
447 raise StatgrabException, 'sg_get_swap_stats() returned NULL'
448 return Result(
449 {'total': s.total,
450 'used': s.used,
451 'free': s.free,
452 }
453 )
454
455 def py_sg_get_fs_stats():
456 cdef sg_fs_stats *s
457 cdef int entries
458 s = sg_get_fs_stats(&entries)
459 if s == NULL:
460 raise StatgrabException, 'sg_get_fs_stats() returned NULL'
461 list = []
462 for i from 0 <= i < entries:
463 list.append(Result(
464 {'device_name': s.device_name,
465 'fs_type': s.fs_type,
466 'mnt_point': s.mnt_point,
467 'size': s.size,
468 'used': s.used,
469 'avail': s.avail,
470 'total_inodes': s.total_inodes,
471 'used_inodes': s.used_inodes,
472 'free_inodes': s.free_inodes,
473 'avail_inodes': s.avail_inodes,
474 'io_size': s.io_size,
475 'block_size': s.block_size,
476 'total_blocks': s.total_blocks,
477 'free_blocks': s.free_blocks,
478 'used_blocks': s.used_blocks,
479 'avail_blocks': s.avail_blocks,
480 }
481 ))
482 s = s + 1
483 return list
484
485 def py_sg_get_disk_io_stats():
486 cdef sg_disk_io_stats *s
487 cdef int entries
488 s = sg_get_disk_io_stats(&entries)
489 if s == NULL:
490 raise StatgrabException, 'sg_get_disk_io_stats() returned NULL'
491 list = []
492 for i from 0 <= i < entries:
493 list.append(Result(
494 {'disk_name': s.disk_name,
495 'read_bytes': s.read_bytes,
496 'write_bytes': s.write_bytes,
497 'systime': s.systime,
498 }
499 ))
500 s = s + 1
501 return list
502
503 def py_sg_get_disk_io_stats_diff():
504 cdef sg_disk_io_stats *s
505 cdef int entries
506 s = sg_get_disk_io_stats_diff(&entries)
507 if s == NULL:
508 raise StatgrabException, 'sg_get_disk_io_stats_diff() returned NULL'
509 list = []
510 for i from 0 <= i < entries:
511 list.append(Result(
512 {'disk_name': s.disk_name,
513 'read_bytes': s.read_bytes,
514 'write_bytes': s.write_bytes,
515 'systime': s.systime,
516 }
517 ))
518 s = s + 1
519 return list
520
521 def py_sg_get_network_io_stats():
522 cdef sg_network_io_stats *s
523 cdef int entries
524 s = sg_get_network_io_stats(&entries)
525 if s == NULL:
526 raise StatgrabException, 'sg_get_network_io_stats() returned NULL'
527 list = []
528 for i from 0 <= i < entries:
529 list.append(Result(
530 {'interface_name': s.interface_name,
531 'tx': s.tx,
532 'rx': s.rx,
533 'ipackets': s.ipackets,
534 'opackets': s.opackets,
535 'ierrors': s.ierrors,
536 'oerrors': s.oerrors,
537 'collisions': s.collisions,
538 'systime': s.systime,
539 }
540 ))
541 s = s + 1
542 return list
543
544 def py_sg_get_network_io_stats_diff():
545 cdef sg_network_io_stats *s
546 cdef int entries
547 s = sg_get_network_io_stats_diff(&entries)
548 if s == NULL:
549 raise StatgrabException, 'sg_get_network_io_stats_diff() returned NULL'
550 list = []
551 for i from 0 <= i < entries:
552 list.append(Result(
553 {'interface_name': s.interface_name,
554 'tx': s.tx,
555 'rx': s.rx,
556 'ipackets': s.ipackets,
557 'opackets': s.opackets,
558 'ierrors': s.ierrors,
559 'oerrors': s.oerrors,
560 'collisions': s.collisions,
561 'systime': s.systime,
562 }
563 ))
564 s = s + 1
565 return list
566
567 def py_sg_get_network_iface_stats():
568 cdef sg_network_iface_stats *s
569 cdef int entries
570 s = sg_get_network_iface_stats(&entries)
571 if s == NULL:
572 raise StatgrabException, 'sg_get_network_iface_stats() returned NULL'
573 list = []
574 for i from 0 <= i < entries:
575 list.append(Result(
576 {'interface_name': s.interface_name,
577 'speed': s.speed,
578 'duplex': s.duplex,
579 'up' : s.up,
580 }
581 ))
582 s = s + 1
583 return list
584
585 def py_sg_get_page_stats():
586 cdef sg_page_stats *s
587 s = sg_get_page_stats()
588 if s == NULL:
589 raise StatgrabException, 'sg_get_page_stats() returned NULL'
590 return Result(
591 {'pages_pagein': s.pages_pagein,
592 'pages_pageout': s.pages_pageout,
593 }
594 )
595
596 def py_sg_get_page_stats_diff():
597 cdef sg_page_stats *s
598 s = sg_get_page_stats_diff()
599 if s == NULL:
600 raise StatgrabException, 'sg_get_page_stats_diff() returned NULL'
601 return Result(
602 {'pages_pagein': s.pages_pagein,
603 'pages_pageout': s.pages_pageout,
604 }
605 )
606
607 def py_sg_get_process_stats():
608 cdef sg_process_stats *s
609 cdef int entries
610 s = sg_get_process_stats(&entries)
611 if s == NULL:
612 raise StatgrabException, 'sg_get_process_stats() returned NULL'
613 list = []
614 for i from 0 <= i < entries:
615 if s.process_name is NULL:
616 process_name = ''
617 else:
618 process_name = s.process_name
619 if s.proctitle is NULL:
620 proctitle = ''
621 else:
622 proctitle = s.proctitle
623 list.append(Result(
624 {'process_name': process_name,
625 'proctitle' : proctitle,
626 'pid' : s.pid,
627 'parent' : s.parent,
628 'pgid' : s.pgid,
629 'uid' : s.uid,
630 'euid' : s.euid,
631 'gid' : s.gid,
632 'egid' : s.egid,
633 'proc_size' : s.proc_size,
634 'proc_resident' : s.proc_resident,
635 'time_spent' : s.time_spent,
636 'cpu_percent' : s.cpu_percent,
637 'nice' : s.nice,
638 'state' : s.state,
639 }
640 ))
641 s = s + 1
642 return list
643
644 def py_sg_get_process_count():
645 cdef sg_process_count *s
646 s = sg_get_process_count()
647 if s == NULL:
648 raise StatgrabException, 'sg_get_process_count() returned NULL'
649 return Result(
650 {'total': s.total,
651 'running': s.running,
652 'sleeping': s.sleeping,
653 'stopped': s.stopped,
654 'zombie': s.zombie,
655 }
656 )