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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines