ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/network_stats.c
Revision: 1.4
Committed: Fri Feb 28 22:59:35 2003 UTC (21 years, 2 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.3: +1 -3 lines
Log Message:
Tidy up of configure script, and includes.

File Contents

# Content
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 <stdlib.h>
27 #include "statgrab.h"
28 #ifdef SOLARIS
29 #include <kstat.h>
30 #include <sys/sysinfo.h>
31 #include <string.h>
32 #endif
33
34 static network_stat_t *network_stats=NULL;
35 static int interfaces=0;
36
37 void network_stat_init(int start, int end, network_stat_t *net_stats){
38
39 for(net_stats+=start; start<end; start++){
40 net_stats->interface_name=NULL;
41 net_stats->tx=0;
42 net_stats->rx=0;
43 net_stats++;
44 }
45 }
46
47 network_stat_t *network_stat_malloc(int needed_entries, int *cur_entries, network_stat_t *net_stats){
48
49 if(net_stats==NULL){
50
51 if((net_stats=malloc(needed_entries * sizeof(network_stat_t)))==NULL){
52 return NULL;
53 }
54 network_stat_init(0, needed_entries, net_stats);
55 *cur_entries=needed_entries;
56
57 return net_stats;
58 }
59
60
61 if(*cur_entries<needed_entries){
62 net_stats=realloc(net_stats, (sizeof(network_stat_t)*needed_entries));
63 if(net_stats==NULL){
64 return NULL;
65 }
66 network_stat_init(*cur_entries, needed_entries, net_stats);
67 *cur_entries=needed_entries;
68 }
69
70 return net_stats;
71 }
72
73
74 network_stat_t *get_network_stats(int *entries){
75 kstat_ctl_t *kc;
76 kstat_t *ksp;
77 kstat_named_t *knp;
78
79 static int sizeof_network_stats=0;
80
81 network_stat_t *network_stat_ptr;
82
83 if ((kc = kstat_open()) == NULL) {
84 return NULL;
85 }
86
87 interfaces=0;
88
89 for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
90 if (!strcmp(ksp->ks_class, "net")) {
91 kstat_read(kc, ksp, NULL);
92
93 if((knp=kstat_data_lookup(ksp, "rbytes64"))==NULL){
94 /* Not a network interface, so skip to the next entry */
95 continue;
96 }
97
98 network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
99 if(network_stats==NULL){
100 return NULL;
101 }
102 network_stat_ptr=network_stats+interfaces;
103 network_stat_ptr->rx=knp->value.ui64;
104
105 if((knp=kstat_data_lookup(ksp, "obytes64"))==NULL){
106 /* Not a network interface, so skip to the next entry */
107 continue;
108 }
109 network_stat_ptr->tx=knp->value.ui64;
110 if(network_stat_ptr->interface_name!=NULL){
111 free(network_stat_ptr->interface_name);
112 }
113 network_stat_ptr->interface_name=strdup(ksp->ks_name);
114
115 network_stat_ptr->systime=time(NULL);
116 interfaces++;
117 }
118 }
119
120 kstat_close(kc);
121
122 *entries=interfaces;
123
124 return network_stats;
125 }
126
127 network_stat_t *get_network_stats_diff(int *entries){
128 static network_stat_t *network_stats_diff=NULL;
129 static int sizeof_net_stats_diff=0;
130 network_stat_t *network_stats_ptr, *network_stats_diff_ptr;
131 int ifaces, x, y;
132
133 if(network_stats==NULL){
134 network_stats_ptr=get_network_stats(&ifaces);
135 *entries=ifaces;
136 return network_stats_ptr;
137 }
138
139 network_stats_diff=network_stat_malloc(interfaces, &sizeof_net_stats_diff, network_stats_diff);
140 if(network_stats_diff==NULL){
141 return NULL;
142 }
143
144 network_stats_ptr=network_stats;
145 network_stats_diff_ptr=network_stats_diff;
146
147 for(ifaces=0;ifaces<interfaces;ifaces++){
148 if(network_stats_diff_ptr->interface_name!=NULL){
149 free(network_stats_diff_ptr->interface_name);
150 }
151 network_stats_diff_ptr->interface_name=strdup(network_stats_ptr->interface_name);
152 network_stats_diff_ptr->tx=network_stats_ptr->tx;
153 network_stats_diff_ptr->rx=network_stats_ptr->rx;
154 network_stats_diff_ptr->systime=network_stats->systime;
155
156 network_stats_ptr++;
157 network_stats_diff_ptr++;
158 }
159 network_stats_ptr=get_network_stats(&ifaces);
160 network_stats_diff_ptr=network_stats_diff;
161
162 for(x=0;x<sizeof_net_stats_diff;x++){
163
164 if((strcmp(network_stats_diff_ptr->interface_name, network_stats_ptr->interface_name))==0){
165 network_stats_diff_ptr->tx = network_stats_ptr->tx - network_stats_diff_ptr->tx;
166 network_stats_diff_ptr->rx = network_stats_ptr->rx - network_stats_diff_ptr->rx;
167 network_stats_diff_ptr->systime = network_stats_ptr->systime - network_stats_diff_ptr->systime;
168 }else{
169
170 network_stats_ptr=network_stats;
171 for(y=0;y<ifaces;y++){
172 if((strcmp(network_stats_diff_ptr->interface_name, network_stats_ptr->interface_name))==0){
173 network_stats_diff_ptr->tx = network_stats_ptr->tx - network_stats_diff_ptr->tx;
174 network_stats_diff_ptr->rx = network_stats_ptr->rx - network_stats_diff_ptr->rx;
175 network_stats_diff_ptr->systime = network_stats_ptr->systime - network_stats_diff_ptr->systime;
176 break;
177 }
178
179 network_stats_ptr++;
180 }
181 }
182
183 network_stats_ptr++;
184 network_stats_diff_ptr++;
185 }
186
187 return network_stats_diff;
188 }
189