ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/pystatgrab/_statgrab.pyx
(Generate patch)

Comparing projects/pystatgrab/_statgrab.pyx (file contents):
Revision 1.1 by tdb, Fri Feb 6 14:10:08 2004 UTC vs.
Revision 1.13 by tdb, Sun May 30 15:32:28 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines