ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/network_stats.c
Revision: 1.2
Committed: Tue Feb 18 23:23:36 2003 UTC (21 years, 3 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.1: +2 -1 lines
Log Message:
Changed the kstat_close to not return NULL in event of a failure. If we
cant close it, well there is nothing i can do about that, so i may as well
at least return something useful since its done all the hardwork by that
point. And anywan, it should never fail to close :)

File Contents

# User Rev Content
1 pajs 1.1 /*
2     * i-scream central monitoring system
3     * http://www.i-scream.org.uk
4     * Copyright (C) 2000-2002 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    
21     #ifdef HAVE_CONFIG_H
22     #include "config.h"
23     #endif
24    
25     #include <stdio.h>
26     #include "statgrab.h"
27     #include <stdlib.h>
28     #ifdef SOLARIS
29     #include <kstat.h>
30     #include <sys/sysinfo.h>
31     #include <string.h>
32     #endif
33    
34     #define START_VAL 1
35    
36     network_stat_t *network_stat_init(int num_iface, int *wmark, network_stat_t *net_stats){
37     int x;
38     network_stat_t *net_stats_ptr;
39    
40     if(*wmark==-1){
41     printf("new malloc\n");
42     if((net_stats=malloc(START_VAL * sizeof(network_stat_t)))==NULL){
43     return NULL;
44     }
45     *wmark=START_VAL;
46     net_stats_ptr=net_stats;
47     for(x=0;x<(*wmark);x++){
48     net_stats_ptr->interface_name=NULL;
49     net_stats_ptr++;
50     }
51     return net_stats;
52     }
53     if(num_iface>(*wmark-1)){
54     if((net_stats=realloc(net_stats, (*wmark)*2 * sizeof(network_stat_t)))==NULL){
55     return NULL;
56     }
57    
58     *wmark=(*wmark)*2;
59     net_stats_ptr=net_stats+num_iface;
60     for(x=num_iface;x<(*wmark);x++){
61     net_stats_ptr->interface_name=NULL;
62     net_stats_ptr++;
63     }
64     return net_stats;
65     }
66    
67     return net_stats;
68     }
69    
70     network_stat_t *get_network_stats(int *entries){
71     kstat_ctl_t *kc;
72     kstat_t *ksp;
73     kstat_named_t *knp;
74    
75     int interfaces=0;
76     network_stat_t *network_stat_ptr;
77     static network_stat_t *network_stats=NULL;
78     static int watermark=-1;
79    
80     if ((kc = kstat_open()) == NULL) {
81     return NULL;
82     }
83    
84     for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
85     if (!strcmp(ksp->ks_class, "net")) {
86     kstat_read(kc, ksp, NULL);
87    
88     if((knp=kstat_data_lookup(ksp, "rbytes64"))==NULL){
89     /* Not a network interface, so skip to the next entry */
90     continue;
91     }
92     network_stats=network_stat_init(interfaces, &watermark, network_stats);
93     if(network_stats==NULL){
94     return NULL;
95     }
96     network_stat_ptr=network_stats+interfaces;
97     network_stat_ptr->rx=knp->value.ui64;
98    
99     if((knp=kstat_data_lookup(ksp, "obytes64"))==NULL){
100     /* Not a network interface, so skip to the next entry */
101     continue;
102     }
103     network_stat_ptr->tx=knp->value.ui64;
104     if(network_stat_ptr->interface_name!=NULL){
105     free(network_stat_ptr->interface_name);
106     }
107     network_stat_ptr->interface_name=strdup(ksp->ks_name);
108     interfaces++;
109     }
110     }
111 pajs 1.2
112     kstat_close(kc);
113 pajs 1.1
114     *entries=interfaces;
115    
116     return network_stats;
117    
118     }
119