ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/pystatgrab/_statgrab.pyx
Revision: 1.3
Committed: Mon Feb 9 23:07:25 2004 UTC (20 years, 3 months ago) by tdb
Branch: MAIN
Changes since 1.2: +59 -1 lines
Log Message:
Add error checking to the python bindings. They previously didn't check
for a NULL response from the libstatgrab functions - which resulted in
python core dumping. It now raises an exception instead.
Also add a first stab at a setup.py so people can quickly build/install
this extension if they want.

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     # $Id$
21     #
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     ctypedef struct page_stat_t:
104     long long pages_pagein
105     long long pages_pageout
106     time_t systime
107    
108     cdef extern cpu_states_t *get_cpu_totals()
109     cdef extern cpu_states_t *get_cpu_diff()
110     cdef extern cpu_percent_t *cpu_percent_usage()
111     cdef extern mem_stat_t *get_memory_stats()
112     cdef extern load_stat_t *get_load_stats()
113     cdef extern user_stat_t *get_user_stats()
114     cdef extern swap_stat_t *get_swap_stats()
115     cdef extern general_stat_t *get_general_stats()
116     cdef extern disk_stat_t *get_disk_stats(int *entries)
117     cdef extern diskio_stat_t *get_diskio_stats(int *entries)
118     cdef extern diskio_stat_t *get_diskio_stats_diff(int *entries)
119     cdef extern process_stat_t *get_process_stats()
120     cdef extern network_stat_t *get_network_stats(int *entries)
121     cdef extern network_stat_t *get_network_stats_diff(int *entries)
122     cdef extern page_stat_t *get_page_stats()
123     cdef extern page_stat_t *get_page_stats_diff()
124     cdef extern int statgrab_init()
125     cdef extern int statgrab_drop_privileges()
126    
127    
128 tdb 1.2 class Result:
129     def __init__(self, attrs):
130     self.attrs = attrs
131     for attr in attrs:
132     setattr(self, attr, attrs[attr])
133     def __getitem__(self, item):
134     return getattr(self, item)
135     def __repr__(self):
136     return str(self.attrs)
137    
138 tdb 1.3 class StatgrabException:
139     def __init__(self, value):
140     self.value = value
141     def __str__(self):
142     return repr(self.value)
143    
144 tdb 1.2
145 tdb 1.1 def py_get_cpu_totals():
146     cdef cpu_states_t *s
147     s = get_cpu_totals()
148 tdb 1.3 if s == NULL:
149     raise StatgrabException, 'get_cpu_totals() returned NULL'
150 tdb 1.2 return Result(
151     {'user': s.user,
152     'kernel': s.kernel,
153     'idle': s.idle,
154     'iowait': s.iowait,
155     'swap': s.swap,
156     'nice': s.nice,
157     'total': s.total,
158     'systime': s.systime,
159     }
160     )
161 tdb 1.1
162     def py_get_cpu_diff():
163     cdef cpu_states_t *s
164     s = get_cpu_diff()
165 tdb 1.3 if s == NULL:
166     raise StatgrabException, 'get_cpu_diff() returned NULL'
167 tdb 1.2 return Result(
168     {'user': s.user,
169     'kernel': s.kernel,
170     'idle': s.idle,
171     'iowait': s.iowait,
172     'swap': s.swap,
173     'nice': s.nice,
174     'total': s.total,
175     'systime': s.systime,
176     }
177     )
178 tdb 1.1
179     def py_cpu_percent_usage():
180     cdef cpu_percent_t *s
181     s = cpu_percent_usage()
182 tdb 1.3 if s == NULL:
183     raise StatgrabException, 'cpu_percent_usage() returned NULL'
184 tdb 1.2 return Result(
185     {'user': s.user,
186     'kernel': s.kernel,
187     'idle': s.idle,
188     'iowait': s.iowait,
189     'swap': s.swap,
190     'nice': s.nice,
191     'time_taken': s.time_taken,
192     }
193     )
194 tdb 1.1
195     def py_get_memory_stats():
196     cdef mem_stat_t *s
197     s = get_memory_stats()
198 tdb 1.3 if s == NULL:
199     raise StatgrabException, 'get_memory_stats() returned NULL'
200 tdb 1.2 return Result(
201     {'total': s.total,
202     'used': s.used,
203     'free': s.free,
204     'cache': s.cache,
205     }
206     )
207 tdb 1.1
208     def py_get_load_stats():
209     cdef load_stat_t *s
210     s = get_load_stats()
211 tdb 1.3 if s == NULL:
212     raise StatgrabException, 'get_load_stats() returned NULL'
213 tdb 1.2 return Result(
214     {'min1': s.min1,
215     'min5': s.min5,
216     'min15': s.min15,
217     }
218     )
219 tdb 1.1
220     def py_get_user_stats():
221     cdef user_stat_t *s
222     s = get_user_stats()
223 tdb 1.3 if s == NULL:
224     raise StatgrabException, 'get_user_stats() returned NULL'
225 tdb 1.2 return Result(
226     {'name_list': s.name_list,
227     'num_entries': s.num_entries,
228     }
229     )
230 tdb 1.1
231     def py_get_swap_stats():
232     cdef swap_stat_t *s
233     s = get_swap_stats()
234 tdb 1.3 if s == NULL:
235     raise StatgrabException, 'get_swap_stats() returned NULL'
236 tdb 1.2 return Result(
237     {'total': s.total,
238     'used': s.used,
239     'free': s.free,
240     }
241     )
242 tdb 1.1
243     def py_get_general_stats():
244     cdef general_stat_t *s
245     s = get_general_stats()
246 tdb 1.3 if s == NULL:
247     raise StatgrabException, 'get_general_stats() returned NULL'
248 tdb 1.2 return Result(
249     {'os_name': s.os_name,
250     'os_release': s.os_release,
251     'os_version': s.os_version,
252     'platform': s.platform,
253     'hostname': s.hostname,
254     'uptime': s.uptime,
255     }
256     )
257 tdb 1.1
258     def py_get_disk_stats():
259     cdef disk_stat_t *s
260     cdef int entries
261     s = get_disk_stats(&entries)
262 tdb 1.3 if s == NULL:
263     raise StatgrabException, 'get_disk_stats() returned NULL'
264 tdb 1.1 list = [entries]
265     for i from 0 <= i < entries:
266 tdb 1.2 list.append(Result(
267     {'device_name': s.device_name,
268     'fs_type': s.fs_type,
269     'mnt_point': s.mnt_point,
270     'size': s.size,
271     'used': s.used,
272     'avail': s.avail,
273     'total_inodes': s.total_inodes,
274     'used_inodes': s.used_inodes,
275     'free_inodes': s.free_inodes,
276     }
277     ))
278 tdb 1.1 s = s + 1
279     return list
280    
281     def py_get_diskio_stats():
282     cdef diskio_stat_t *s
283     cdef int entries
284     s = get_diskio_stats(&entries)
285 tdb 1.3 if s == NULL:
286     raise StatgrabException, 'get_diskio_stats() returned NULL'
287 tdb 1.1 list = [entries]
288     for i from 0 <= i < entries:
289 tdb 1.2 list.append(Result(
290     {'disk_name': s.disk_name,
291     'read_bytes': s.read_bytes,
292     'write_bytes': s.write_bytes,
293     'systime': s.systime,
294     }
295     ))
296 tdb 1.1 s = s + 1
297     return list
298    
299     def py_get_diskio_stats_diff():
300     cdef diskio_stat_t *s
301     cdef int entries
302     s = get_diskio_stats_diff(&entries)
303 tdb 1.3 if s == NULL:
304     raise StatgrabException, 'get_diskio_stats_diff() returned NULL'
305 tdb 1.1 list = [entries]
306     for i from 0 <= i < entries:
307 tdb 1.2 list.append(Result(
308     {'disk_name': s.disk_name,
309     'read_bytes': s.read_bytes,
310     'write_bytes': s.write_bytes,
311     'systime': s.systime,
312     }
313     ))
314 tdb 1.1 s = s + 1
315     return list
316    
317     def py_get_process_stats():
318     cdef process_stat_t *s
319     s = get_process_stats()
320 tdb 1.3 if s == NULL:
321     raise StatgrabException, 'get_process_stats() returned NULL'
322 tdb 1.2 return Result(
323     {'total': s.total,
324     'running': s.running,
325     'sleeping': s.sleeping,
326     'stopped': s.stopped,
327     'zombie': s.zombie,
328     }
329     )
330 tdb 1.1
331     def py_get_network_stats():
332     cdef network_stat_t *s
333     cdef int entries
334     s = get_network_stats(&entries)
335 tdb 1.3 if s == NULL:
336     raise StatgrabException, 'get_network_stats() returned NULL'
337 tdb 1.1 list = [entries]
338     for i from 0 <= i < entries:
339 tdb 1.2 list.append(Result(
340     {'interface_name': s.interface_name,
341     'tx': s.tx,
342     'rx': s.rx,
343     'systime': s.systime,
344     }
345     ))
346 tdb 1.1 s = s + 1
347     return list
348    
349     def py_get_network_stats_diff():
350     cdef network_stat_t *s
351     cdef int entries
352     s = get_network_stats_diff(&entries)
353 tdb 1.3 if s == NULL:
354     raise StatgrabException, 'get_network_stats_diff() returned NULL'
355 tdb 1.1 list = [entries]
356     for i from 0 <= i < entries:
357 tdb 1.2 list.append(Result(
358     {'interface_name': s.interface_name,
359     'tx': s.tx,
360     'rx': s.rx,
361     'systime': s.systime,
362     }
363     ))
364 tdb 1.1 s = s + 1
365     return list
366    
367     def py_get_page_stats():
368     cdef page_stat_t *s
369     s = get_page_stats()
370 tdb 1.3 if s == NULL:
371     raise StatgrabException, 'get_page_stats() returned NULL'
372 tdb 1.2 return Result(
373     {'pages_pagein': s.pages_pagein,
374     'pages_pageout': s.pages_pageout,
375     }
376     )
377 tdb 1.1
378     def py_get_page_stats_diff():
379     cdef page_stat_t *s
380     s = get_page_stats_diff()
381 tdb 1.3 if s == NULL:
382     raise StatgrabException, 'get_page_stats_diff() returned NULL'
383 tdb 1.2 return Result(
384     {'pages_pagein': s.pages_pagein,
385     'pages_pageout': s.pages_pageout,
386     }
387     )
388 tdb 1.1
389     def py_statgrab_init():
390     return statgrab_init()
391    
392     def py_statgrab_drop_privileges():
393     return statgrab_drop_privileges()