ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/pystatgrab/_statgrab.pyx
Revision: 1.20
Committed: Fri Jul 13 22:26:19 2007 UTC (16 years, 9 months ago) by tdb
Branch: MAIN
CVS Tags: PYSTATGRAB_0_5
Changes since 1.19: +3 -1 lines
Log Message:
Properly import sg_shutdown and sg_snapshot.

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.19 2007/07/13 22:19:56 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:
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 = 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(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_sg_get_disk_io_stats():
485 cdef sg_disk_io_stats *s
486 cdef int 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(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_sg_get_disk_io_stats_diff():
503 cdef sg_disk_io_stats *s
504 cdef int 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(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_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_sg_get_network_io_stats_diff():
544 cdef sg_network_io_stats *s
545 cdef int 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(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_sg_get_network_iface_stats():
567 cdef sg_network_iface_stats *s
568 cdef int 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(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_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_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_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_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 )