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.3 by tdb, Mon Feb 9 23:07:25 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 106 | Line 125 | cdef extern from "statgrab.h":
125      cdef extern int statgrab_drop_privileges()
126  
127  
128 + 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 + class StatgrabException:
139 +    def __init__(self, value):
140 +        self.value = value
141 +    def __str__(self):
142 +        return repr(self.value)
143 +
144 +
145   def py_get_cpu_totals():
146      cdef cpu_states_t *s
147      s = get_cpu_totals()
148 <    return {'user': s.user,
149 <            'kernel': s.kernel,
150 <            'idle': s.idle,
151 <            'iowait': s.iowait,
152 <            'swap': s.swap,
153 <            'nice': s.nice,
154 <            'total': s.total,
155 <            'systime': s.systime,
156 <           }
148 >    if s == NULL:
149 >        raise StatgrabException, 'get_cpu_totals() returned NULL'
150 >    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  
162   def py_get_cpu_diff():
163      cdef cpu_states_t *s
164      s = get_cpu_diff()
165 <    return {'user': s.user,
166 <            'kernel': s.kernel,
167 <            'idle': s.idle,
168 <            'iowait': s.iowait,
169 <            'swap': s.swap,
170 <            'nice': s.nice,
171 <            'total': s.total,
172 <            'systime': s.systime,
173 <           }
165 >    if s == NULL:
166 >        raise StatgrabException, 'get_cpu_diff() returned NULL'
167 >    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  
179   def py_cpu_percent_usage():
180      cdef cpu_percent_t *s
181      s = cpu_percent_usage()
182 <    return {'user': s.user,
183 <            'kernel': s.kernel,
184 <            'idle': s.idle,
185 <            'iowait': s.iowait,
186 <            'swap': s.swap,
187 <            'nice': s.nice,
188 <            'time_taken': s.time_taken,
189 <           }
182 >    if s == NULL:
183 >        raise StatgrabException, 'cpu_percent_usage() returned NULL'
184 >    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  
195   def py_get_memory_stats():
196      cdef mem_stat_t *s
197      s = get_memory_stats()
198 <    return {'total': s.total,
199 <            'used': s.used,
200 <            'free': s.free,
201 <            'cache': s.cache,
202 <           }
198 >    if s == NULL:
199 >        raise StatgrabException, 'get_memory_stats() returned NULL'
200 >    return Result(
201 >        {'total': s.total,
202 >         'used': s.used,
203 >         'free': s.free,
204 >         'cache': s.cache,
205 >        }
206 >    )
207  
208   def py_get_load_stats():
209      cdef load_stat_t *s
210      s = get_load_stats()
211 <    return {'min1': s.min1,
212 <            'min5': s.min5,
213 <            'min15': s.min15,
214 <           }
211 >    if s == NULL:
212 >        raise StatgrabException, 'get_load_stats() returned NULL'
213 >    return Result(
214 >        {'min1': s.min1,
215 >         'min5': s.min5,
216 >         'min15': s.min15,
217 >        }
218 >    )
219  
220   def py_get_user_stats():
221      cdef user_stat_t *s
222      s = get_user_stats()
223 <    return {'name_list': s.name_list,
224 <            'num_entries': s.num_entries,
225 <           }
223 >    if s == NULL:
224 >        raise StatgrabException, 'get_user_stats() returned NULL'
225 >    return Result(
226 >        {'name_list': s.name_list,
227 >         'num_entries': s.num_entries,
228 >        }
229 >    )
230  
231   def py_get_swap_stats():
232      cdef swap_stat_t *s
233      s = get_swap_stats()
234 <    return {'total': s.total,
235 <            'used': s.used,
236 <            'free': s.free,
237 <           }
234 >    if s == NULL:
235 >        raise StatgrabException, 'get_swap_stats() returned NULL'
236 >    return Result(
237 >        {'total': s.total,
238 >         'used': s.used,
239 >         'free': s.free,
240 >        }
241 >    )
242  
243   def py_get_general_stats():
244      cdef general_stat_t *s
245      s = get_general_stats()
246 <    return {'os_name': s.os_name,
247 <            'os_release': s.os_release,
248 <            'os_version': s.os_version,
249 <            'platform': s.platform,
250 <            'hostname': s.hostname,
251 <            'uptime': s.uptime,
252 <           }
246 >    if s == NULL:
247 >        raise StatgrabException, 'get_general_stats() returned NULL'
248 >    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  
258   def py_get_disk_stats():
259      cdef disk_stat_t *s
260      cdef int entries
261      s = get_disk_stats(&entries)
262 +    if s == NULL:
263 +        raise StatgrabException, 'get_disk_stats() returned NULL'
264      list = [entries]
265      for i from 0 <= i < entries:
266 <        list.append({'device_name': s.device_name,
267 <                     'fs_type': s.fs_type,
268 <                     'mnt_point': s.mnt_point,
269 <                     'size': s.size,
270 <                     'used': s.used,
271 <                     'avail': s.avail,
272 <                     'total_inodes': s.total_inodes,
273 <                     'used_inodes': s.used_inodes,
274 <                     'free_inodes': s.free_inodes,
275 <                    },
276 <                   )
266 >        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          s = s + 1
279      return list
280  
# Line 211 | Line 282 | def py_get_diskio_stats():
282      cdef diskio_stat_t *s
283      cdef int entries
284      s = get_diskio_stats(&entries)
285 +    if s == NULL:
286 +        raise StatgrabException, 'get_diskio_stats() returned NULL'
287      list = [entries]
288      for i from 0 <= i < entries:
289 <        list.append({'disk_name': s.disk_name,
290 <                     'read_bytes': s.read_bytes,
291 <                     'write_bytes': s.write_bytes,
292 <                     'systime': s.systime,
293 <                    },
294 <                   )
289 >        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          s = s + 1
297      return list
298  
# Line 226 | Line 300 | def py_get_diskio_stats_diff():
300      cdef diskio_stat_t *s
301      cdef int entries
302      s = get_diskio_stats_diff(&entries)
303 +    if s == NULL:
304 +        raise StatgrabException, 'get_diskio_stats_diff() returned NULL'
305      list = [entries]
306      for i from 0 <= i < entries:
307 <        list.append({'disk_name': s.disk_name,
308 <                     'read_bytes': s.read_bytes,
309 <                     'write_bytes': s.write_bytes,
310 <                     'systime': s.systime,
311 <                    },
312 <                   )
307 >        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          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 <    return {'total': s.total,
321 <            'running': s.running,
322 <            'sleeping': s.sleeping,
323 <            'stopped': s.stopped,
324 <            'zombie': s.zombie,
325 <           }
320 >    if s == NULL:
321 >        raise StatgrabException, 'get_process_stats() returned NULL'
322 >    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  
331   def py_get_network_stats():
332      cdef network_stat_t *s
333      cdef int entries
334      s = get_network_stats(&entries)
335 +    if s == NULL:
336 +        raise StatgrabException, 'get_network_stats() returned NULL'
337      list = [entries]
338      for i from 0 <= i < entries:
339 <        list.append({'interface_name': s.interface_name,
340 <                     'tx': s.tx,
341 <                     'rx': s.rx,
342 <                     'systime': s.systime,
343 <                    },
344 <                   )
339 >        list.append(Result(
340 >            {'interface_name': s.interface_name,
341 >             'tx': s.tx,
342 >             'rx': s.rx,
343 >             'systime': s.systime,
344 >            }
345 >        ))
346          s = s + 1
347      return list
348  
# Line 266 | Line 350 | def py_get_network_stats_diff():
350      cdef network_stat_t *s
351      cdef int entries
352      s = get_network_stats_diff(&entries)
353 +    if s == NULL:
354 +        raise StatgrabException, 'get_network_stats_diff() returned NULL'
355      list = [entries]
356      for i from 0 <= i < entries:
357 <        list.append({'interface_name': s.interface_name,
358 <                     'tx': s.tx,
359 <                     'rx': s.rx,
360 <                     'systime': s.systime,
361 <                    },
362 <                   )
357 >        list.append(Result(
358 >            {'interface_name': s.interface_name,
359 >             'tx': s.tx,
360 >             'rx': s.rx,
361 >             'systime': s.systime,
362 >            }
363 >        ))
364          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 <    return {'pages_pagein': s.pages_pagein,
371 <            'pages_pageout': s.pages_pageout,
372 <           }
370 >    if s == NULL:
371 >        raise StatgrabException, 'get_page_stats() returned NULL'
372 >    return Result(
373 >        {'pages_pagein': s.pages_pagein,
374 >         'pages_pageout': s.pages_pageout,
375 >        }
376 >    )
377  
378   def py_get_page_stats_diff():
379      cdef page_stat_t *s
380      s = get_page_stats_diff()
381 <    return {'pages_pagein': s.pages_pagein,
382 <            'pages_pageout': s.pages_pageout,
383 <           }
381 >    if s == NULL:
382 >        raise StatgrabException, 'get_page_stats_diff() returned NULL'
383 >    return Result(
384 >        {'pages_pagein': s.pages_pagein,
385 >         'pages_pageout': s.pages_pageout,
386 >        }
387 >    )
388  
389   def py_statgrab_init():
390      return statgrab_init()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines