ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/pystatgrab/_statgrab.pyx
Revision: 1.14
Committed: Sun May 30 16:41:03 2004 UTC (19 years, 11 months ago) by tdb
Branch: MAIN
CVS Tags: PYSTATGRAB_0_2
Changes since 1.13: +153 -1 lines
Log Message:
Stage 3: Add new functions.

File Contents

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