ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/pystatgrab/_statgrab.pyx
Revision: 1.12
Committed: Sun May 30 15:21:19 2004 UTC (20 years, 5 months ago) by tdb
Branch: MAIN
Changes since 1.11: +115 -115 lines
Log Message:
Stage 1: Update all existing functions to have new names.

File Contents

# User Rev Content
1 tdb 1.3 #
2 tdb 1.11 # i-scream pystatgrab
3 tdb 1.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.12 # $Id: _statgrab.pyx,v 1.11 2004/04/06 14:53:01 tdb Exp $
21 tdb 1.3 #
22 tdb 1.1
23     ctypedef long time_t
24    
25     cdef extern from "statgrab.h":
26 tdb 1.12 ctypedef struct sg_cpu_stats:
27 tdb 1.1 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 tdb 1.12 ctypedef struct sg_cpu_percents:
37 tdb 1.1 float user
38     float kernel
39     float idle
40     float iowait
41     float swap
42     float nice
43     time_t time_taken
44    
45 tdb 1.12 ctypedef struct sg_mem_stats:
46 tdb 1.1 long long total
47     long long free
48     long long used
49     long long cache
50    
51 tdb 1.12 ctypedef struct sg_load_stats:
52 tdb 1.1 double min1
53     double min5
54     double min15
55    
56 tdb 1.12 ctypedef struct sg_user_stats:
57 tdb 1.1 char *name_list
58     int num_entries
59    
60 tdb 1.12 ctypedef struct sg_swap_stats:
61 tdb 1.1 long long total
62     long long used
63     long long free
64    
65 tdb 1.12 ctypedef struct sg_host_info:
66 tdb 1.1 char *os_name
67     char *os_release
68     char *os_version
69     char *platform
70     char *hostname
71     time_t uptime
72    
73 tdb 1.12 ctypedef struct sg_fs_stats:
74 tdb 1.1 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 tdb 1.12 ctypedef struct sg_disk_io_stats:
85 tdb 1.1 char *disk_name
86     long long read_bytes
87     long long write_bytes
88     time_t systime
89    
90 tdb 1.12 ctypedef struct sg_process_count:
91 tdb 1.1 int total
92     int running
93     int sleeping
94     int stopped
95     int zombie
96    
97 tdb 1.12 ctypedef struct sg_network_io_stats:
98 tdb 1.1 char *interface_name
99     long long tx
100     long long rx
101 tdb 1.9 long long ipackets
102     long long opackets
103     long long ierrors
104     long long oerrors
105     long long collisions
106 tdb 1.1 time_t systime
107    
108 tdb 1.12 ctypedef enum sg_iface_duplex:
109     SG_IFACE_DUPLEX_FULL
110     SG_IFACE_DUPLEX_HALF
111     SG_IFACE_DUPLEX_UNKNOWN
112 tdb 1.5
113 tdb 1.12 ctypedef struct sg_network_iface_stats:
114 tdb 1.5 char *interface_name
115     int speed
116 tdb 1.12 sg_iface_duplex dup
117 tdb 1.8 int up
118 tdb 1.5
119 tdb 1.12 ctypedef struct sg_page_stats:
120 tdb 1.1 long long pages_pagein
121     long long pages_pageout
122     time_t systime
123    
124 tdb 1.12 cdef extern sg_cpu_stats *sg_get_cpu_stats()
125     cdef extern sg_cpu_stats *sg_get_cpu_stats_diff()
126     cdef extern sg_cpu_percents *sg_get_cpu_percents()
127     cdef extern sg_mem_stats *sg_get_mem_stats()
128     cdef extern sg_load_stats *sg_get_load_stats()
129     cdef extern sg_user_stats *sg_get_user_stats()
130     cdef extern sg_swap_stats *sg_get_swap_stats()
131     cdef extern sg_host_info *sg_get_host_info()
132     cdef extern sg_fs_stats *sg_get_fs_stats(int *entries)
133     cdef extern sg_disk_io_stats *sg_get_disk_io_stats(int *entries)
134     cdef extern sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entries)
135     cdef extern sg_process_count *sg_get_process_count()
136     cdef extern sg_network_io_stats *sg_get_network_io_stats(int *entries)
137     cdef extern sg_network_io_stats *sg_get_network_io_stats_diff(int *entries)
138     cdef extern sg_network_iface_stats *sg_get_network_iface_stats(int *entries)
139     cdef extern sg_page_stats *sg_get_page_stats()
140     cdef extern sg_page_stats *sg_get_page_stats_diff()
141     cdef extern int sg_init()
142     cdef extern int sg_drop_privileges()
143    
144    
145     py_SG_IFACE_DUPLEX_FULL = SG_IFACE_DUPLEX_FULL
146     py_SG_IFACE_DUPLEX_HALF = SG_IFACE_DUPLEX_HALF
147     py_SG_IFACE_DUPLEX_UNKNOWN = SG_IFACE_DUPLEX_UNKNOWN
148 tdb 1.1
149    
150 tdb 1.2 class Result:
151     def __init__(self, attrs):
152     self.attrs = attrs
153     for attr in attrs:
154     setattr(self, attr, attrs[attr])
155     def __getitem__(self, item):
156     return getattr(self, item)
157     def __repr__(self):
158     return str(self.attrs)
159    
160 tdb 1.4 class StatgrabException(Exception):
161 tdb 1.3 def __init__(self, value):
162     self.value = value
163     def __str__(self):
164     return repr(self.value)
165    
166 tdb 1.2
167 tdb 1.12 def py_sg_get_cpu_stats():
168     cdef sg_cpu_stats *s
169     s = sg_get_cpu_stats()
170 tdb 1.3 if s == NULL:
171 tdb 1.12 raise StatgrabException, 'sg_get_cpu_stats() returned NULL'
172 tdb 1.2 return Result(
173     {'user': s.user,
174     'kernel': s.kernel,
175     'idle': s.idle,
176     'iowait': s.iowait,
177     'swap': s.swap,
178     'nice': s.nice,
179     'total': s.total,
180     'systime': s.systime,
181     }
182     )
183 tdb 1.1
184 tdb 1.12 def py_sg_get_cpu_stats_diff():
185     cdef sg_cpu_stats *s
186     s = sg_get_cpu_stats_diff()
187 tdb 1.3 if s == NULL:
188 tdb 1.12 raise StatgrabException, 'sg_get_cpu_stats_diff() returned NULL'
189 tdb 1.2 return Result(
190     {'user': s.user,
191     'kernel': s.kernel,
192     'idle': s.idle,
193     'iowait': s.iowait,
194     'swap': s.swap,
195     'nice': s.nice,
196     'total': s.total,
197     'systime': s.systime,
198     }
199     )
200 tdb 1.1
201 tdb 1.12 def py_sg_get_cpu_percents():
202     cdef sg_cpu_percents *s
203     s = sg_get_cpu_percents()
204 tdb 1.3 if s == NULL:
205 tdb 1.12 raise StatgrabException, 'sg_get_cpu_percents() returned NULL'
206 tdb 1.2 return Result(
207     {'user': s.user,
208     'kernel': s.kernel,
209     'idle': s.idle,
210     'iowait': s.iowait,
211     'swap': s.swap,
212     'nice': s.nice,
213     'time_taken': s.time_taken,
214     }
215     )
216 tdb 1.1
217 tdb 1.12 def py_sg_get_mem_stats():
218     cdef sg_mem_stats *s
219     s = sg_get_mem_stats()
220 tdb 1.3 if s == NULL:
221 tdb 1.12 raise StatgrabException, 'sg_get_mem_stats() returned NULL'
222 tdb 1.2 return Result(
223     {'total': s.total,
224     'used': s.used,
225     'free': s.free,
226     'cache': s.cache,
227     }
228     )
229 tdb 1.1
230 tdb 1.12 def py_sg_get_load_stats():
231     cdef sg_load_stats *s
232     s = sg_get_load_stats()
233 tdb 1.3 if s == NULL:
234 tdb 1.12 raise StatgrabException, 'sg_get_load_stats() returned NULL'
235 tdb 1.2 return Result(
236     {'min1': s.min1,
237     'min5': s.min5,
238     'min15': s.min15,
239     }
240     )
241 tdb 1.1
242 tdb 1.12 def py_sg_get_user_stats():
243     cdef sg_user_stats *s
244     s = sg_get_user_stats()
245 tdb 1.3 if s == NULL:
246 tdb 1.12 raise StatgrabException, 'sg_get_user_stats() returned NULL'
247 tdb 1.2 return Result(
248     {'name_list': s.name_list,
249     'num_entries': s.num_entries,
250     }
251     )
252 tdb 1.1
253 tdb 1.12 def py_sg_get_swap_stats():
254     cdef sg_swap_stats *s
255     s = sg_get_swap_stats()
256 tdb 1.3 if s == NULL:
257 tdb 1.12 raise StatgrabException, 'sg_get_swap_stats() returned NULL'
258 tdb 1.2 return Result(
259     {'total': s.total,
260     'used': s.used,
261     'free': s.free,
262     }
263     )
264 tdb 1.1
265 tdb 1.12 def py_sg_get_host_info():
266     cdef sg_host_info *s
267     s = sg_get_host_info()
268 tdb 1.3 if s == NULL:
269 tdb 1.12 raise StatgrabException, 'sg_get_host_info() returned NULL'
270 tdb 1.2 return Result(
271     {'os_name': s.os_name,
272     'os_release': s.os_release,
273     'os_version': s.os_version,
274     'platform': s.platform,
275     'hostname': s.hostname,
276     'uptime': s.uptime,
277     }
278     )
279 tdb 1.1
280 tdb 1.12 def py_sg_get_fs_stats():
281     cdef sg_fs_stats *s
282 tdb 1.1 cdef int entries
283 tdb 1.12 s = sg_get_fs_stats(&entries)
284 tdb 1.3 if s == NULL:
285 tdb 1.12 raise StatgrabException, 'sg_get_fs_stats() returned NULL'
286 tdb 1.6 list = []
287 tdb 1.1 for i from 0 <= i < entries:
288 tdb 1.2 list.append(Result(
289     {'device_name': s.device_name,
290     'fs_type': s.fs_type,
291     'mnt_point': s.mnt_point,
292     'size': s.size,
293     'used': s.used,
294     'avail': s.avail,
295     'total_inodes': s.total_inodes,
296     'used_inodes': s.used_inodes,
297     'free_inodes': s.free_inodes,
298     }
299     ))
300 tdb 1.1 s = s + 1
301     return list
302    
303 tdb 1.12 def py_sg_get_disk_io_stats():
304     cdef sg_disk_io_stats *s
305 tdb 1.1 cdef int entries
306 tdb 1.12 s = sg_get_disk_io_stats(&entries)
307 tdb 1.3 if s == NULL:
308 tdb 1.12 raise StatgrabException, 'sg_get_disk_io_stats() returned NULL'
309 tdb 1.6 list = []
310 tdb 1.1 for i from 0 <= i < entries:
311 tdb 1.2 list.append(Result(
312     {'disk_name': s.disk_name,
313     'read_bytes': s.read_bytes,
314     'write_bytes': s.write_bytes,
315     'systime': s.systime,
316     }
317     ))
318 tdb 1.1 s = s + 1
319     return list
320    
321 tdb 1.12 def py_sg_get_disk_io_stats_diff():
322     cdef sg_disk_io_stats *s
323 tdb 1.1 cdef int entries
324 tdb 1.12 s = sg_get_disk_io_stats_diff(&entries)
325 tdb 1.3 if s == NULL:
326 tdb 1.12 raise StatgrabException, 'sg_get_disk_io_stats_diff() returned NULL'
327 tdb 1.6 list = []
328 tdb 1.1 for i from 0 <= i < entries:
329 tdb 1.2 list.append(Result(
330     {'disk_name': s.disk_name,
331     'read_bytes': s.read_bytes,
332     'write_bytes': s.write_bytes,
333     'systime': s.systime,
334     }
335     ))
336 tdb 1.1 s = s + 1
337     return list
338    
339 tdb 1.12 def py_sg_get_process_count():
340     cdef sg_process_count *s
341     s = sg_get_process_count()
342 tdb 1.3 if s == NULL:
343 tdb 1.12 raise StatgrabException, 'sg_get_process_count() returned NULL'
344 tdb 1.2 return Result(
345     {'total': s.total,
346     'running': s.running,
347     'sleeping': s.sleeping,
348     'stopped': s.stopped,
349     'zombie': s.zombie,
350     }
351     )
352 tdb 1.1
353 tdb 1.12 def py_sg_get_network_io_stats():
354     cdef sg_network_io_stats *s
355 tdb 1.1 cdef int entries
356 tdb 1.12 s = sg_get_network_io_stats(&entries)
357 tdb 1.3 if s == NULL:
358 tdb 1.12 raise StatgrabException, 'sg_get_network_io_stats() returned NULL'
359 tdb 1.6 list = []
360 tdb 1.1 for i from 0 <= i < entries:
361 tdb 1.2 list.append(Result(
362     {'interface_name': s.interface_name,
363     'tx': s.tx,
364     'rx': s.rx,
365 tdb 1.9 'ipackets': s.ipackets,
366     'opackets': s.opackets,
367     'ierrors': s.ierrors,
368     'oerrors': s.oerrors,
369     'collisions': s.collisions,
370 tdb 1.2 'systime': s.systime,
371     }
372     ))
373 tdb 1.1 s = s + 1
374     return list
375    
376 tdb 1.12 def py_sg_get_network_io_stats_diff():
377     cdef sg_network_io_stats *s
378 tdb 1.1 cdef int entries
379 tdb 1.12 s = sg_get_network_io_stats_diff(&entries)
380 tdb 1.3 if s == NULL:
381 tdb 1.12 raise StatgrabException, 'sg_get_network_io_stats_diff() returned NULL'
382 tdb 1.6 list = []
383 tdb 1.1 for i from 0 <= i < entries:
384 tdb 1.2 list.append(Result(
385     {'interface_name': s.interface_name,
386     'tx': s.tx,
387     'rx': s.rx,
388 tdb 1.9 'ipackets': s.ipackets,
389     'opackets': s.opackets,
390     'ierrors': s.ierrors,
391     'oerrors': s.oerrors,
392     'collisions': s.collisions,
393 tdb 1.2 'systime': s.systime,
394 tdb 1.5 }
395     ))
396     s = s + 1
397     return list
398    
399 tdb 1.12 def py_sg_get_network_iface_stats():
400     cdef sg_network_iface_stats *s
401 tdb 1.5 cdef int entries
402 tdb 1.12 s = sg_get_network_iface_stats(&entries)
403 tdb 1.5 if s == NULL:
404 tdb 1.12 raise StatgrabException, 'sg_get_network_iface_stats() returned NULL'
405 tdb 1.6 list = []
406 tdb 1.5 for i from 0 <= i < entries:
407     list.append(Result(
408     {'interface_name': s.interface_name,
409     'speed': s.speed,
410     'dup': s.dup,
411 tdb 1.8 'up' : s.up,
412 tdb 1.2 }
413     ))
414 tdb 1.1 s = s + 1
415     return list
416    
417 tdb 1.12 def py_sg_get_page_stats():
418     cdef sg_page_stats *s
419     s = sg_get_page_stats()
420 tdb 1.3 if s == NULL:
421 tdb 1.12 raise StatgrabException, 'sg_get_page_stats() returned NULL'
422 tdb 1.2 return Result(
423     {'pages_pagein': s.pages_pagein,
424     'pages_pageout': s.pages_pageout,
425     }
426     )
427 tdb 1.1
428 tdb 1.12 def py_sg_get_page_stats_diff():
429     cdef sg_page_stats *s
430     s = sg_get_page_stats_diff()
431 tdb 1.3 if s == NULL:
432 tdb 1.12 raise StatgrabException, 'sg_get_page_stats_diff() returned NULL'
433 tdb 1.2 return Result(
434     {'pages_pagein': s.pages_pagein,
435     'pages_pageout': s.pages_pageout,
436     }
437     )
438 tdb 1.1
439 tdb 1.12 def py_sg_init():
440     if sg_init() == 0:
441 tdb 1.10 return True
442     else:
443     return False
444 tdb 1.1
445 tdb 1.12 def py_sg_drop_privileges():
446     if sg_drop_privileges() == 0:
447 tdb 1.10 return True
448     else:
449     return False