--- projects/pystatgrab/_statgrab.pyx 2004/02/06 15:12:14 1.2 +++ projects/pystatgrab/_statgrab.pyx 2004/04/06 14:53:01 1.11 @@ -1,4 +1,24 @@ -# TODO: deal with failures better (return nothing, or raise error?) +# +# i-scream pystatgrab +# http://www.i-scream.org +# Copyright (C) 2000-2004 i-scream +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# $Id: _statgrab.pyx,v 1.11 2004/04/06 14:53:01 tdb Exp $ +# ctypedef long time_t @@ -78,8 +98,24 @@ cdef extern from "statgrab.h": char *interface_name long long tx long long rx + long long ipackets + long long opackets + long long ierrors + long long oerrors + long long collisions time_t systime + ctypedef enum statgrab_duplex: + FULL_DUPLEX + HALF_DUPLEX + UNKNOWN_DUPLEX + + ctypedef struct network_iface_stat_t: + char *interface_name + int speed + statgrab_duplex dup + int up + ctypedef struct page_stat_t: long long pages_pagein long long pages_pageout @@ -99,12 +135,18 @@ cdef extern from "statgrab.h": cdef extern process_stat_t *get_process_stats() cdef extern network_stat_t *get_network_stats(int *entries) cdef extern network_stat_t *get_network_stats_diff(int *entries) + cdef extern network_iface_stat_t *get_network_iface_stats(int *entries) cdef extern page_stat_t *get_page_stats() cdef extern page_stat_t *get_page_stats_diff() cdef extern int statgrab_init() cdef extern int statgrab_drop_privileges() +py_FULL_DUPLEX = FULL_DUPLEX +py_HALF_DUPLEX = HALF_DUPLEX +py_UNKNOWN_DUPLEX = UNKNOWN_DUPLEX + + class Result: def __init__(self, attrs): self.attrs = attrs @@ -115,10 +157,18 @@ class Result: def __repr__(self): return str(self.attrs) +class StatgrabException(Exception): + def __init__(self, value): + self.value = value + def __str__(self): + return repr(self.value) + def py_get_cpu_totals(): cdef cpu_states_t *s s = get_cpu_totals() + if s == NULL: + raise StatgrabException, 'get_cpu_totals() returned NULL' return Result( {'user': s.user, 'kernel': s.kernel, @@ -134,6 +184,8 @@ def py_get_cpu_totals(): def py_get_cpu_diff(): cdef cpu_states_t *s s = get_cpu_diff() + if s == NULL: + raise StatgrabException, 'get_cpu_diff() returned NULL' return Result( {'user': s.user, 'kernel': s.kernel, @@ -149,6 +201,8 @@ def py_get_cpu_diff(): def py_cpu_percent_usage(): cdef cpu_percent_t *s s = cpu_percent_usage() + if s == NULL: + raise StatgrabException, 'cpu_percent_usage() returned NULL' return Result( {'user': s.user, 'kernel': s.kernel, @@ -163,6 +217,8 @@ def py_cpu_percent_usage(): def py_get_memory_stats(): cdef mem_stat_t *s s = get_memory_stats() + if s == NULL: + raise StatgrabException, 'get_memory_stats() returned NULL' return Result( {'total': s.total, 'used': s.used, @@ -174,6 +230,8 @@ def py_get_memory_stats(): def py_get_load_stats(): cdef load_stat_t *s s = get_load_stats() + if s == NULL: + raise StatgrabException, 'get_load_stats() returned NULL' return Result( {'min1': s.min1, 'min5': s.min5, @@ -184,6 +242,8 @@ def py_get_load_stats(): def py_get_user_stats(): cdef user_stat_t *s s = get_user_stats() + if s == NULL: + raise StatgrabException, 'get_user_stats() returned NULL' return Result( {'name_list': s.name_list, 'num_entries': s.num_entries, @@ -193,6 +253,8 @@ def py_get_user_stats(): def py_get_swap_stats(): cdef swap_stat_t *s s = get_swap_stats() + if s == NULL: + raise StatgrabException, 'get_swap_stats() returned NULL' return Result( {'total': s.total, 'used': s.used, @@ -203,6 +265,8 @@ def py_get_swap_stats(): def py_get_general_stats(): cdef general_stat_t *s s = get_general_stats() + if s == NULL: + raise StatgrabException, 'get_general_stats() returned NULL' return Result( {'os_name': s.os_name, 'os_release': s.os_release, @@ -217,7 +281,9 @@ def py_get_disk_stats(): cdef disk_stat_t *s cdef int entries s = get_disk_stats(&entries) - list = [entries] + if s == NULL: + raise StatgrabException, 'get_disk_stats() returned NULL' + list = [] for i from 0 <= i < entries: list.append(Result( {'device_name': s.device_name, @@ -238,7 +304,9 @@ def py_get_diskio_stats(): cdef diskio_stat_t *s cdef int entries s = get_diskio_stats(&entries) - list = [entries] + if s == NULL: + raise StatgrabException, 'get_diskio_stats() returned NULL' + list = [] for i from 0 <= i < entries: list.append(Result( {'disk_name': s.disk_name, @@ -254,7 +322,9 @@ def py_get_diskio_stats_diff(): cdef diskio_stat_t *s cdef int entries s = get_diskio_stats_diff(&entries) - list = [entries] + if s == NULL: + raise StatgrabException, 'get_diskio_stats_diff() returned NULL' + list = [] for i from 0 <= i < entries: list.append(Result( {'disk_name': s.disk_name, @@ -269,6 +339,8 @@ def py_get_diskio_stats_diff(): def py_get_process_stats(): cdef process_stat_t *s s = get_process_stats() + if s == NULL: + raise StatgrabException, 'get_process_stats() returned NULL' return Result( {'total': s.total, 'running': s.running, @@ -282,12 +354,19 @@ def py_get_network_stats(): cdef network_stat_t *s cdef int entries s = get_network_stats(&entries) - list = [entries] + if s == NULL: + raise StatgrabException, 'get_network_stats() returned NULL' + list = [] for i from 0 <= i < entries: list.append(Result( {'interface_name': s.interface_name, 'tx': s.tx, 'rx': s.rx, + 'ipackets': s.ipackets, + 'opackets': s.opackets, + 'ierrors': s.ierrors, + 'oerrors': s.oerrors, + 'collisions': s.collisions, 'systime': s.systime, } )) @@ -298,21 +377,48 @@ def py_get_network_stats_diff(): cdef network_stat_t *s cdef int entries s = get_network_stats_diff(&entries) - list = [entries] + if s == NULL: + raise StatgrabException, 'get_network_stats_diff() returned NULL' + list = [] for i from 0 <= i < entries: list.append(Result( {'interface_name': s.interface_name, 'tx': s.tx, 'rx': s.rx, + 'ipackets': s.ipackets, + 'opackets': s.opackets, + 'ierrors': s.ierrors, + 'oerrors': s.oerrors, + 'collisions': s.collisions, 'systime': s.systime, } )) s = s + 1 return list +def py_get_network_iface_stats(): + cdef network_iface_stat_t *s + cdef int entries + s = get_network_iface_stats(&entries) + if s == NULL: + raise StatgrabException, 'get_network_iface_stats() returned NULL' + list = [] + for i from 0 <= i < entries: + list.append(Result( + {'interface_name': s.interface_name, + 'speed': s.speed, + 'dup': s.dup, + 'up' : s.up, + } + )) + s = s + 1 + return list + def py_get_page_stats(): cdef page_stat_t *s s = get_page_stats() + if s == NULL: + raise StatgrabException, 'get_page_stats() returned NULL' return Result( {'pages_pagein': s.pages_pagein, 'pages_pageout': s.pages_pageout, @@ -322,6 +428,8 @@ def py_get_page_stats(): def py_get_page_stats_diff(): cdef page_stat_t *s s = get_page_stats_diff() + if s == NULL: + raise StatgrabException, 'get_page_stats_diff() returned NULL' return Result( {'pages_pagein': s.pages_pagein, 'pages_pageout': s.pages_pageout, @@ -329,7 +437,13 @@ def py_get_page_stats_diff(): ) def py_statgrab_init(): - return statgrab_init() + if statgrab_init() == 0: + return True + else: + return False def py_statgrab_drop_privileges(): - return statgrab_drop_privileges() + if statgrab_drop_privileges() == 0: + return True + else: + return False