ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/pystatgrab/_statgrab.pyx
Revision: 1.15
Committed: Tue Aug 24 20:43:47 2004 UTC (19 years, 8 months ago) by tdb
Branch: MAIN
CVS Tags: PYSTATGRAB_0_3
Changes since 1.14: +11 -2 lines
Log Message:
Catch up to new features in libstatgrab 0.10.2.

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