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.9 by tdb, Sat Mar 6 22:11:22 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 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  
23   ctypedef long time_t
24  
# Line 79 | Line 98 | cdef extern from "statgrab.h":
98          char *interface_name
99          long long tx
100          long long rx
101 +        long long ipackets
102 +        long long opackets
103 +        long long ierrors
104 +        long long oerrors
105 +        long long collisions
106          time_t systime
107  
108 +    ctypedef enum statgrab_duplex:
109 +        FULL_DUPLEX
110 +        HALF_DUPLEX
111 +        UNKNOWN_DUPLEX
112 +
113 +    ctypedef struct network_iface_stat_t:
114 +        char *interface_name
115 +        int speed
116 +        statgrab_duplex dup
117 +        int up
118 +
119      ctypedef struct page_stat_t:
120          long long pages_pagein
121          long long pages_pageout
# Line 100 | Line 135 | cdef extern from "statgrab.h":
135      cdef extern process_stat_t *get_process_stats()
136      cdef extern network_stat_t *get_network_stats(int *entries)
137      cdef extern network_stat_t *get_network_stats_diff(int *entries)
138 +    cdef extern network_iface_stat_t *get_network_iface_stats(int *entries)
139      cdef extern page_stat_t *get_page_stats()
140      cdef extern page_stat_t *get_page_stats_diff()
141      cdef extern int statgrab_init()
142      cdef extern int statgrab_drop_privileges()
143  
144  
145 + py_FULL_DUPLEX = FULL_DUPLEX
146 + py_HALF_DUPLEX = HALF_DUPLEX
147 + py_UNKNOWN_DUPLEX = UNKNOWN_DUPLEX
148 +
149 +
150 + 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 + class StatgrabException(Exception):
161 +    def __init__(self, value):
162 +        self.value = value
163 +    def __str__(self):
164 +        return repr(self.value)
165 +
166 +
167   def py_get_cpu_totals():
168      cdef cpu_states_t *s
169      s = get_cpu_totals()
170 <    return {'user': s.user,
171 <            'kernel': s.kernel,
172 <            'idle': s.idle,
173 <            'iowait': s.iowait,
174 <            'swap': s.swap,
175 <            'nice': s.nice,
176 <            'total': s.total,
177 <            'systime': s.systime,
178 <           }
170 >    if s == NULL:
171 >        raise StatgrabException, 'get_cpu_totals() returned NULL'
172 >    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  
184   def py_get_cpu_diff():
185      cdef cpu_states_t *s
186      s = get_cpu_diff()
187 <    return {'user': s.user,
188 <            'kernel': s.kernel,
189 <            'idle': s.idle,
190 <            'iowait': s.iowait,
191 <            'swap': s.swap,
192 <            'nice': s.nice,
193 <            'total': s.total,
194 <            'systime': s.systime,
195 <           }
187 >    if s == NULL:
188 >        raise StatgrabException, 'get_cpu_diff() returned NULL'
189 >    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  
201   def py_cpu_percent_usage():
202      cdef cpu_percent_t *s
203      s = cpu_percent_usage()
204 <    return {'user': s.user,
205 <            'kernel': s.kernel,
206 <            'idle': s.idle,
207 <            'iowait': s.iowait,
208 <            'swap': s.swap,
209 <            'nice': s.nice,
210 <            'time_taken': s.time_taken,
211 <           }
204 >    if s == NULL:
205 >        raise StatgrabException, 'cpu_percent_usage() returned NULL'
206 >    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  
217   def py_get_memory_stats():
218      cdef mem_stat_t *s
219      s = get_memory_stats()
220 <    return {'total': s.total,
221 <            'used': s.used,
222 <            'free': s.free,
223 <            'cache': s.cache,
224 <           }
220 >    if s == NULL:
221 >        raise StatgrabException, 'get_memory_stats() returned NULL'
222 >    return Result(
223 >        {'total': s.total,
224 >         'used': s.used,
225 >         'free': s.free,
226 >         'cache': s.cache,
227 >        }
228 >    )
229  
230   def py_get_load_stats():
231      cdef load_stat_t *s
232      s = get_load_stats()
233 <    return {'min1': s.min1,
234 <            'min5': s.min5,
235 <            'min15': s.min15,
236 <           }
233 >    if s == NULL:
234 >        raise StatgrabException, 'get_load_stats() returned NULL'
235 >    return Result(
236 >        {'min1': s.min1,
237 >         'min5': s.min5,
238 >         'min15': s.min15,
239 >        }
240 >    )
241  
242   def py_get_user_stats():
243      cdef user_stat_t *s
244      s = get_user_stats()
245 <    return {'name_list': s.name_list,
246 <            'num_entries': s.num_entries,
247 <           }
245 >    if s == NULL:
246 >        raise StatgrabException, 'get_user_stats() returned NULL'
247 >    return Result(
248 >        {'name_list': s.name_list,
249 >         'num_entries': s.num_entries,
250 >        }
251 >    )
252  
253   def py_get_swap_stats():
254      cdef swap_stat_t *s
255      s = get_swap_stats()
256 <    return {'total': s.total,
257 <            'used': s.used,
258 <            'free': s.free,
259 <           }
256 >    if s == NULL:
257 >        raise StatgrabException, 'get_swap_stats() returned NULL'
258 >    return Result(
259 >        {'total': s.total,
260 >         'used': s.used,
261 >         'free': s.free,
262 >        }
263 >    )
264  
265   def py_get_general_stats():
266      cdef general_stat_t *s
267      s = get_general_stats()
268 <    return {'os_name': s.os_name,
269 <            'os_release': s.os_release,
270 <            'os_version': s.os_version,
271 <            'platform': s.platform,
272 <            'hostname': s.hostname,
273 <            'uptime': s.uptime,
274 <           }
268 >    if s == NULL:
269 >        raise StatgrabException, 'get_general_stats() returned NULL'
270 >    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  
280   def py_get_disk_stats():
281      cdef disk_stat_t *s
282      cdef int entries
283      s = get_disk_stats(&entries)
284 <    list = [entries]
284 >    if s == NULL:
285 >        raise StatgrabException, 'get_disk_stats() returned NULL'
286 >    list = []
287      for i from 0 <= i < entries:
288 <        list.append({'device_name': s.device_name,
289 <                     'fs_type': s.fs_type,
290 <                     'mnt_point': s.mnt_point,
291 <                     'size': s.size,
292 <                     'used': s.used,
293 <                     'avail': s.avail,
294 <                     'total_inodes': s.total_inodes,
295 <                     'used_inodes': s.used_inodes,
296 <                     'free_inodes': s.free_inodes,
297 <                    },
298 <                   )
288 >        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          s = s + 1
301      return list
302  
# Line 211 | Line 304 | def py_get_diskio_stats():
304      cdef diskio_stat_t *s
305      cdef int entries
306      s = get_diskio_stats(&entries)
307 <    list = [entries]
307 >    if s == NULL:
308 >        raise StatgrabException, 'get_diskio_stats() returned NULL'
309 >    list = []
310      for i from 0 <= i < entries:
311 <        list.append({'disk_name': s.disk_name,
312 <                     'read_bytes': s.read_bytes,
313 <                     'write_bytes': s.write_bytes,
314 <                     'systime': s.systime,
315 <                    },
316 <                   )
311 >        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          s = s + 1
319      return list
320  
# Line 226 | Line 322 | def py_get_diskio_stats_diff():
322      cdef diskio_stat_t *s
323      cdef int entries
324      s = get_diskio_stats_diff(&entries)
325 <    list = [entries]
325 >    if s == NULL:
326 >        raise StatgrabException, 'get_diskio_stats_diff() returned NULL'
327 >    list = []
328      for i from 0 <= i < entries:
329 <        list.append({'disk_name': s.disk_name,
330 <                     'read_bytes': s.read_bytes,
331 <                     'write_bytes': s.write_bytes,
332 <                     'systime': s.systime,
333 <                    },
334 <                   )
329 >        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          s = s + 1
337      return list
338  
339   def py_get_process_stats():
340      cdef process_stat_t *s
341      s = get_process_stats()
342 <    return {'total': s.total,
343 <            'running': s.running,
344 <            'sleeping': s.sleeping,
345 <            'stopped': s.stopped,
346 <            'zombie': s.zombie,
347 <           }
342 >    if s == NULL:
343 >        raise StatgrabException, 'get_process_stats() returned NULL'
344 >    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  
353   def py_get_network_stats():
354      cdef network_stat_t *s
355      cdef int entries
356      s = get_network_stats(&entries)
357 <    list = [entries]
357 >    if s == NULL:
358 >        raise StatgrabException, 'get_network_stats() returned NULL'
359 >    list = []
360      for i from 0 <= i < entries:
361 <        list.append({'interface_name': s.interface_name,
362 <                     'tx': s.tx,
363 <                     'rx': s.rx,
364 <                     'systime': s.systime,
365 <                    },
366 <                   )
361 >        list.append(Result(
362 >            {'interface_name': s.interface_name,
363 >             'tx': s.tx,
364 >             'rx': s.rx,
365 >             'ipackets': s.ipackets,
366 >             'opackets': s.opackets,
367 >             'ierrors': s.ierrors,
368 >             'oerrors': s.oerrors,
369 >             'collisions': s.collisions,
370 >             'systime': s.systime,
371 >            }
372 >        ))
373          s = s + 1
374      return list
375  
# Line 266 | Line 377 | def py_get_network_stats_diff():
377      cdef network_stat_t *s
378      cdef int entries
379      s = get_network_stats_diff(&entries)
380 <    list = [entries]
380 >    if s == NULL:
381 >        raise StatgrabException, 'get_network_stats_diff() returned NULL'
382 >    list = []
383      for i from 0 <= i < entries:
384 <        list.append({'interface_name': s.interface_name,
385 <                     'tx': s.tx,
386 <                     'rx': s.rx,
387 <                     'systime': s.systime,
388 <                    },
389 <                   )
384 >        list.append(Result(
385 >            {'interface_name': s.interface_name,
386 >             'tx': s.tx,
387 >             'rx': s.rx,
388 >             'ipackets': s.ipackets,
389 >             'opackets': s.opackets,
390 >             'ierrors': s.ierrors,
391 >             'oerrors': s.oerrors,
392 >             'collisions': s.collisions,
393 >             'systime': s.systime,
394 >            }
395 >        ))
396          s = s + 1
397      return list
398  
399 + def py_get_network_iface_stats():
400 +    cdef network_iface_stat_t *s
401 +    cdef int entries
402 +    s = get_network_iface_stats(&entries)
403 +    if s == NULL:
404 +        raise StatgrabException, 'get_network_iface_stats() returned NULL'
405 +    list = []
406 +    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 +             'up' : s.up,
412 +            }
413 +        ))
414 +        s = s + 1
415 +    return list
416 +
417   def py_get_page_stats():
418      cdef page_stat_t *s
419      s = get_page_stats()
420 <    return {'pages_pagein': s.pages_pagein,
421 <            'pages_pageout': s.pages_pageout,
422 <           }
420 >    if s == NULL:
421 >        raise StatgrabException, 'get_page_stats() returned NULL'
422 >    return Result(
423 >        {'pages_pagein': s.pages_pagein,
424 >         'pages_pageout': s.pages_pageout,
425 >        }
426 >    )
427  
428   def py_get_page_stats_diff():
429      cdef page_stat_t *s
430      s = get_page_stats_diff()
431 <    return {'pages_pagein': s.pages_pagein,
432 <            'pages_pageout': s.pages_pageout,
433 <           }
431 >    if s == NULL:
432 >        raise StatgrabException, 'get_page_stats_diff() returned NULL'
433 >    return Result(
434 >        {'pages_pagein': s.pages_pagein,
435 >         'pages_pageout': s.pages_pageout,
436 >        }
437 >    )
438  
439   def py_statgrab_init():
440      return statgrab_init()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines