ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/extras/python/statgrab.pyx
Revision: 1.7
Committed: Fri Feb 13 17:53:15 2004 UTC (20 years, 10 months ago) by tdb
Branch: MAIN
Changes since 1.6: +9 -4 lines
Log Message:
Chase statgrab_duplex name changes.
Add variables that can be accessed within python for the duplex enum.

File Contents

# User Rev Content
1 tdb 1.3 #
2     # i-scream central monitoring system
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 tdb 1.7 # $Id: statgrab.pyx,v 1.6 2004/02/13 12:03:29 tdb Exp $
21 tdb 1.3 #
22 tdb 1.1
23     ctypedef long time_t
24    
25     cdef extern from "statgrab.h":
26     ctypedef struct cpu_states_t:
27     long long user
28     long long kernel
29     long long idle
30     long long iowait
31     long long swap
32     long long nice
33     long long total
34     time_t systime
35    
36     ctypedef struct cpu_percent_t:
37     float user
38     float kernel
39     float idle
40     float iowait
41     float swap
42     float nice
43     time_t time_taken
44    
45     ctypedef struct mem_stat_t:
46     long long total
47     long long free
48     long long used
49     long long cache
50    
51     ctypedef struct load_stat_t:
52     double min1
53     double min5
54     double min15
55    
56     ctypedef struct user_stat_t:
57     char *name_list
58     int num_entries
59    
60     ctypedef struct swap_stat_t:
61     long long total
62     long long used
63     long long free
64    
65     ctypedef struct general_stat_t:
66     char *os_name
67     char *os_release
68     char *os_version
69     char *platform
70     char *hostname
71     time_t uptime
72    
73     ctypedef struct disk_stat_t:
74     char *device_name
75     char *fs_type
76     char *mnt_point
77     long long size
78     long long used
79     long long avail
80     long long total_inodes
81     long long used_inodes
82     long long free_inodes
83    
84     ctypedef struct diskio_stat_t:
85     char *disk_name
86     long long read_bytes
87     long long write_bytes
88     time_t systime
89    
90     ctypedef struct process_stat_t:
91     int total
92     int running
93     int sleeping
94     int stopped
95     int zombie
96    
97     ctypedef struct network_stat_t:
98     char *interface_name
99     long long tx
100     long long rx
101     time_t systime
102    
103 tdb 1.7 ctypedef enum statgrab_duplex:
104 tdb 1.5 FULL_DUPLEX
105     HALF_DUPLEX
106 tdb 1.7 UNKNOWN_DUPLEX
107 tdb 1.5
108     ctypedef struct network_iface_stat_t:
109     char *interface_name
110     int speed
111 tdb 1.7 statgrab_duplex dup
112 tdb 1.5
113 tdb 1.1 ctypedef struct page_stat_t:
114     long long pages_pagein
115     long long pages_pageout
116     time_t systime
117    
118     cdef extern cpu_states_t *get_cpu_totals()
119     cdef extern cpu_states_t *get_cpu_diff()
120     cdef extern cpu_percent_t *cpu_percent_usage()
121     cdef extern mem_stat_t *get_memory_stats()
122     cdef extern load_stat_t *get_load_stats()
123     cdef extern user_stat_t *get_user_stats()
124     cdef extern swap_stat_t *get_swap_stats()
125     cdef extern general_stat_t *get_general_stats()
126     cdef extern disk_stat_t *get_disk_stats(int *entries)
127     cdef extern diskio_stat_t *get_diskio_stats(int *entries)
128     cdef extern diskio_stat_t *get_diskio_stats_diff(int *entries)
129     cdef extern process_stat_t *get_process_stats()
130     cdef extern network_stat_t *get_network_stats(int *entries)
131     cdef extern network_stat_t *get_network_stats_diff(int *entries)
132 tdb 1.5 cdef extern network_iface_stat_t *get_network_iface_stats(int *entries)
133 tdb 1.1 cdef extern page_stat_t *get_page_stats()
134     cdef extern page_stat_t *get_page_stats_diff()
135     cdef extern int statgrab_init()
136     cdef extern int statgrab_drop_privileges()
137 tdb 1.7
138    
139     py_FULL_DUPLEX = FULL_DUPLEX
140     py_HALF_DUPLEX = HALF_DUPLEX
141     py_UNKNOWN_DUPLEX = UNKNOWN_DUPLEX
142 tdb 1.1
143    
144 tdb 1.2 class Result:
145     def __init__(self, attrs):
146     self.attrs = attrs
147     for attr in attrs:
148     setattr(self, attr, attrs[attr])
149     def __getitem__(self, item):
150     return getattr(self, item)
151     def __repr__(self):
152     return str(self.attrs)
153    
154 tdb 1.4 class StatgrabException(Exception):
155 tdb 1.3 def __init__(self, value):
156     self.value = value
157     def __str__(self):
158     return repr(self.value)
159    
160 tdb 1.2
161 tdb 1.1 def py_get_cpu_totals():
162     cdef cpu_states_t *s
163     s = get_cpu_totals()
164 tdb 1.3 if s == NULL:
165     raise StatgrabException, 'get_cpu_totals() returned NULL'
166 tdb 1.2 return Result(
167     {'user': s.user,
168     'kernel': s.kernel,
169     'idle': s.idle,
170     'iowait': s.iowait,
171     'swap': s.swap,
172     'nice': s.nice,
173     'total': s.total,
174     'systime': s.systime,
175     }
176     )
177 tdb 1.1
178     def py_get_cpu_diff():
179     cdef cpu_states_t *s
180     s = get_cpu_diff()
181 tdb 1.3 if s == NULL:
182     raise StatgrabException, 'get_cpu_diff() returned NULL'
183 tdb 1.2 return Result(
184     {'user': s.user,
185     'kernel': s.kernel,
186     'idle': s.idle,
187     'iowait': s.iowait,
188     'swap': s.swap,
189     'nice': s.nice,
190     'total': s.total,
191     'systime': s.systime,
192     }
193     )
194 tdb 1.1
195     def py_cpu_percent_usage():
196     cdef cpu_percent_t *s
197     s = cpu_percent_usage()
198 tdb 1.3 if s == NULL:
199     raise StatgrabException, 'cpu_percent_usage() returned NULL'
200 tdb 1.2 return Result(
201     {'user': s.user,
202     'kernel': s.kernel,
203     'idle': s.idle,
204     'iowait': s.iowait,
205     'swap': s.swap,
206     'nice': s.nice,
207     'time_taken': s.time_taken,
208     }
209     )
210 tdb 1.1
211     def py_get_memory_stats():
212     cdef mem_stat_t *s
213     s = get_memory_stats()
214 tdb 1.3 if s == NULL:
215     raise StatgrabException, 'get_memory_stats() returned NULL'
216 tdb 1.2 return Result(
217     {'total': s.total,
218     'used': s.used,
219     'free': s.free,
220     'cache': s.cache,
221     }
222     )
223 tdb 1.1
224     def py_get_load_stats():
225     cdef load_stat_t *s
226     s = get_load_stats()
227 tdb 1.3 if s == NULL:
228     raise StatgrabException, 'get_load_stats() returned NULL'
229 tdb 1.2 return Result(
230     {'min1': s.min1,
231     'min5': s.min5,
232     'min15': s.min15,
233     }
234     )
235 tdb 1.1
236     def py_get_user_stats():
237     cdef user_stat_t *s
238     s = get_user_stats()
239 tdb 1.3 if s == NULL:
240     raise StatgrabException, 'get_user_stats() returned NULL'
241 tdb 1.2 return Result(
242     {'name_list': s.name_list,
243     'num_entries': s.num_entries,
244     }
245     )
246 tdb 1.1
247     def py_get_swap_stats():
248     cdef swap_stat_t *s
249     s = get_swap_stats()
250 tdb 1.3 if s == NULL:
251     raise StatgrabException, 'get_swap_stats() returned NULL'
252 tdb 1.2 return Result(
253     {'total': s.total,
254     'used': s.used,
255     'free': s.free,
256     }
257     )
258 tdb 1.1
259     def py_get_general_stats():
260     cdef general_stat_t *s
261     s = get_general_stats()
262 tdb 1.3 if s == NULL:
263     raise StatgrabException, 'get_general_stats() returned NULL'
264 tdb 1.2 return Result(
265     {'os_name': s.os_name,
266     'os_release': s.os_release,
267     'os_version': s.os_version,
268     'platform': s.platform,
269     'hostname': s.hostname,
270     'uptime': s.uptime,
271     }
272     )
273 tdb 1.1
274     def py_get_disk_stats():
275     cdef disk_stat_t *s
276     cdef int entries
277     s = get_disk_stats(&entries)
278 tdb 1.3 if s == NULL:
279     raise StatgrabException, 'get_disk_stats() returned NULL'
280 tdb 1.6 list = []
281 tdb 1.1 for i from 0 <= i < entries:
282 tdb 1.2 list.append(Result(
283     {'device_name': s.device_name,
284     'fs_type': s.fs_type,
285     'mnt_point': s.mnt_point,
286     'size': s.size,
287     'used': s.used,
288     'avail': s.avail,
289     'total_inodes': s.total_inodes,
290     'used_inodes': s.used_inodes,
291     'free_inodes': s.free_inodes,
292     }
293     ))
294 tdb 1.1 s = s + 1
295     return list
296    
297     def py_get_diskio_stats():
298     cdef diskio_stat_t *s
299     cdef int entries
300     s = get_diskio_stats(&entries)
301 tdb 1.3 if s == NULL:
302     raise StatgrabException, 'get_diskio_stats() returned NULL'
303 tdb 1.6 list = []
304 tdb 1.1 for i from 0 <= i < entries:
305 tdb 1.2 list.append(Result(
306     {'disk_name': s.disk_name,
307     'read_bytes': s.read_bytes,
308     'write_bytes': s.write_bytes,
309     'systime': s.systime,
310     }
311     ))
312 tdb 1.1 s = s + 1
313     return list
314    
315     def py_get_diskio_stats_diff():
316     cdef diskio_stat_t *s
317     cdef int entries
318     s = get_diskio_stats_diff(&entries)
319 tdb 1.3 if s == NULL:
320     raise StatgrabException, 'get_diskio_stats_diff() returned NULL'
321 tdb 1.6 list = []
322 tdb 1.1 for i from 0 <= i < entries:
323 tdb 1.2 list.append(Result(
324     {'disk_name': s.disk_name,
325     'read_bytes': s.read_bytes,
326     'write_bytes': s.write_bytes,
327     'systime': s.systime,
328     }
329     ))
330 tdb 1.1 s = s + 1
331     return list
332    
333     def py_get_process_stats():
334     cdef process_stat_t *s
335     s = get_process_stats()
336 tdb 1.3 if s == NULL:
337     raise StatgrabException, 'get_process_stats() returned NULL'
338 tdb 1.2 return Result(
339     {'total': s.total,
340     'running': s.running,
341     'sleeping': s.sleeping,
342     'stopped': s.stopped,
343     'zombie': s.zombie,
344     }
345     )
346 tdb 1.1
347     def py_get_network_stats():
348     cdef network_stat_t *s
349     cdef int entries
350     s = get_network_stats(&entries)
351 tdb 1.3 if s == NULL:
352     raise StatgrabException, 'get_network_stats() returned NULL'
353 tdb 1.6 list = []
354 tdb 1.1 for i from 0 <= i < entries:
355 tdb 1.2 list.append(Result(
356     {'interface_name': s.interface_name,
357     'tx': s.tx,
358     'rx': s.rx,
359     'systime': s.systime,
360     }
361     ))
362 tdb 1.1 s = s + 1
363     return list
364    
365     def py_get_network_stats_diff():
366     cdef network_stat_t *s
367     cdef int entries
368     s = get_network_stats_diff(&entries)
369 tdb 1.3 if s == NULL:
370     raise StatgrabException, 'get_network_stats_diff() returned NULL'
371 tdb 1.6 list = []
372 tdb 1.1 for i from 0 <= i < entries:
373 tdb 1.2 list.append(Result(
374     {'interface_name': s.interface_name,
375     'tx': s.tx,
376     'rx': s.rx,
377     'systime': s.systime,
378 tdb 1.5 }
379     ))
380     s = s + 1
381     return list
382    
383     def py_get_network_iface_stats():
384     cdef network_iface_stat_t *s
385     cdef int entries
386     s = get_network_iface_stats(&entries)
387     if s == NULL:
388     raise StatgrabException, 'get_network_iface_stats() returned NULL'
389 tdb 1.6 list = []
390 tdb 1.5 for i from 0 <= i < entries:
391     list.append(Result(
392     {'interface_name': s.interface_name,
393     'speed': s.speed,
394     'dup': s.dup,
395 tdb 1.2 }
396     ))
397 tdb 1.1 s = s + 1
398     return list
399    
400     def py_get_page_stats():
401     cdef page_stat_t *s
402     s = get_page_stats()
403 tdb 1.3 if s == NULL:
404     raise StatgrabException, 'get_page_stats() returned NULL'
405 tdb 1.2 return Result(
406     {'pages_pagein': s.pages_pagein,
407     'pages_pageout': s.pages_pageout,
408     }
409     )
410 tdb 1.1
411     def py_get_page_stats_diff():
412     cdef page_stat_t *s
413     s = get_page_stats_diff()
414 tdb 1.3 if s == NULL:
415     raise StatgrabException, 'get_page_stats_diff() returned NULL'
416 tdb 1.2 return Result(
417     {'pages_pagein': s.pages_pagein,
418     'pages_pageout': s.pages_pageout,
419     }
420     )
421 tdb 1.1
422     def py_statgrab_init():
423     return statgrab_init()
424    
425     def py_statgrab_drop_privileges():
426     return statgrab_drop_privileges()