Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / ksysguard / ksysguardd / Irix / NetDev.c
blob80bc2b3105b28a94c1dc5705d1e38727b1f403c1
1 /*
2 KSysGuard, the KDE System Guard
4 Copyright (c) 2001 Tobias Koenig <tokoe@kde.org>
5 Irix Support by Carsten Kroll <ckroll@pinnaclesys.com>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of version 2 of the GNU General Public
9 License as published by the Free Software Foundation.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <fcntl.h>
23 #include <sys/socket.h>
24 #include <net/if.h>
25 #include <net/soioctl.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/sysctl.h>
29 #include <sys/time.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32 #include <invent.h>
33 #include <strings.h>
36 #include "Command.h"
37 #include "ksysguardd.h"
38 #include "NetDev.h"
40 #ifdef __GNUC__
41 #define LONGLONG long long
42 #endif
44 typedef struct {
45 char name[IFNAMSIZ];
46 u_long recBytes;
47 u_long recPacks;
48 u_long recErrs;
49 u_long recDrop;
50 u_long recMulticast;
51 u_long sentBytes;
52 u_long sentPacks;
53 u_long sentErrs;
54 u_long sentMulticast;
55 u_long sentColls;
56 } NetDevInfo;
58 #define MAXNETDEVS 32
59 static NetDevInfo NetDevs[MAXNETDEVS];
60 static NetDevInfo oNetDevs[MAXNETDEVS];
61 static int NetDevCnt = 0;
63 char **parseCommand(const char *cmd)
65 char *tmp_cmd = strdup(cmd);
66 char *begin;
67 char *retval = malloc(sizeof(char *)*2);
69 begin = rindex(tmp_cmd, '/');
70 *begin = '\0';
71 begin++;
72 retval[1] = strdup(begin); // sensor
74 begin = rindex(tmp_cmd, '/');
75 *begin = '\0';
76 begin = rindex(tmp_cmd, '/');
77 begin++;
78 retval[0] = strdup(begin); // interface
79 free(tmp_cmd);
81 return retval;
84 /* ------------------------------ public part --------------------------- */
86 void initNetDev(struct SensorModul* sm)
88 int i;
89 char monitor[1024];
91 memset(NetDevs,0,sizeof(NetDevInfo)*MAXNETDEVS);
92 memset(oNetDevs,0,sizeof(NetDevInfo)*MAXNETDEVS);
94 updateNetDev();
96 for (i = 0; i < NetDevCnt; i++) {
98 sprintf(monitor,"network/interfaces/%s/receiver/packets", NetDevs[i].name);
99 registerMonitor(monitor, "integer", printNetDevRecBytes, printNetDevRecBytesInfo, sm);
100 sprintf(monitor ,"network/interfaces/%s/receiver/errors", NetDevs[i].name);
101 registerMonitor(monitor, "integer", printNetDevRecBytes, printNetDevRecBytesInfo, sm);
103 [CK] I don't know how to get Bytes sent/received, if someone does please drop me a note.
104 sprintf(monitor,"network/interfaces/%s/receiver/data", NetDevs[i].name);
105 registerMonitor(monitor, "integer", printNetDevRecBytes, printNetDevRecBytesInfo, sm);
106 sprintf(monitor,"network/interfaces/%s/receiver/drops", NetDevs[i].name);
107 registerMonitor(monitor, "integer", printNetDevRecBytes, printNetDevRecBytesInfo, sm);
108 sprintf(monitor ,"network/interfaces/%s/receiver/multicast", NetDevs[i].name);
109 registerMonitor(monitor, "integer", printNetDevRecBytes, printNetDevRecBytesInfo, sm);
112 sprintf(monitor,"network/interfaces/%s/transmitter/packets", NetDevs[i].name);
113 registerMonitor(monitor, "integer", printNetDevSentBytes, printNetDevSentBytesInfo, sm);
114 sprintf(monitor,"network/interfaces/%s/transmitter/errors", NetDevs[i].name);
115 registerMonitor(monitor, "integer", printNetDevSentBytes, printNetDevSentBytesInfo, sm);
117 sprintf(monitor,"network/interfaces/%s/transmitter/data", NetDevs[i].name);
118 registerMonitor(monitor, "integer", printNetDevSentBytes, printNetDevSentBytesInfo, sm);
119 sprintf(monitor,"network/interfaces/%s/transmitter/multicast", NetDevs[i].name);
120 registerMonitor(monitor, "integer", printNetDevSentBytes, printNetDevSentBytesInfo, sm);
122 sprintf(monitor,"network/interfaces/%s/transmitter/collisions", NetDevs[i].name);
123 registerMonitor(monitor, "integer", printNetDevSentBytes, printNetDevSentBytesInfo, sm);
127 void exitNetDev(void)
129 int i;
130 char monitor[1024];
132 for (i = 0; i < NetDevCnt; i++) {
133 sprintf(monitor,"network/interfaces/%s/receiver/packets", NetDevs[i].name);
134 removeMonitor(monitor);
135 sprintf(monitor,"network/interfaces/%s/receiver/errors", NetDevs[i].name);
136 removeMonitor(monitor);
138 sprintf(monitor,"network/interfaces/%s/receiver/drops", NetDevs[i].name);
139 removeMonitor(monitor);
140 sprintf(monitor,"network/interfaces/%s/receiver/multicast", NetDevs[i].name);
141 removeMonitor(monitor);
142 sprintf(monitor,"network/interfaces/%s/receiver/data", NetDevs[i].name);
143 removeMonitor(monitor);
146 sprintf(monitor,"network/interfaces/%s/transmitter/packets", NetDevs[i].name);
147 removeMonitor(monitor);
148 sprintf(monitor,"network/interfaces/%s/transmitter/errors", NetDevs[i].name);
149 removeMonitor(monitor);
151 sprintf(monitor,"network/interfaces/%s/transmitter/data", NetDevs[i].name);
152 removeMonitor(monitor);
153 sprintf(monitor,"network/interfaces/%s/transmitter/multicast", NetDevs[i].name);
154 removeMonitor(monitor);
156 sprintf(monitor,"network/interfaces/%s/transmitter/collisions", NetDevs[i].name);
157 removeMonitor(monitor);
162 int updateNetDev(void)
164 int name[6];
165 int num_iface=0, i;
166 char buf[MAXNETDEVS*sizeof(struct ifreq)];
167 size_t len;
168 int s;
169 struct ifconf ifc;
170 struct ifstats *istat;
171 struct timeval tv;
172 static LONGLONG timestamp=0;
173 register LONGLONG cts,elapsed;
174 //struct ipstat ips;
176 if ((s=socket(PF_INET,SOCK_DGRAM,0)) < 0){
177 print_error("socket creation failed");
178 return(-1);
181 ifc.ifc_len = sizeof (buf);
182 ifc.ifc_buf = buf;
183 if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
184 print_error("cannot get interface configuration");
185 return(-1);
188 gettimeofday(&tv, 0);
189 cts = ((LONGLONG)tv.tv_sec * 100 + (LONGLONG) tv.tv_usec / 10000);/* in 10 ms unit*/
190 elapsed = cts - timestamp;
191 timestamp=cts;
193 NetDevCnt=0;
195 for (i = 0; i < MAXNETDEVS; i++) {
196 if ( *ifc.ifc_req[i].ifr_name == 0) break;
197 if (ioctl(s, SIOCGIFSTATS, &ifc.ifc_req[i]) < 0) {
198 print_error("cannot get interface statistics");
199 return (-1);
201 istat=&ifc.ifc_req[i].ifr_stats;
202 //if ( ifc.ifc_req[i].ifr_flags & IFF_UP) {
203 strncpy(NetDevs[i].name,ifc.ifc_req[i].ifr_name, IFNAMSIZ);
204 NetDevs[i].name[IFNAMSIZ-1]='\0';
205 NetDevs[i].recBytes = (istat->ifs_ipackets - oNetDevs[i].recBytes) * 100 / elapsed;
206 NetDevs[i].recPacks = (istat->ifs_ipackets - oNetDevs[i].recPacks) * 100 / elapsed;
207 NetDevs[i].recErrs = istat->ifs_ierrors - oNetDevs[i].recErrs;
208 //NetDevs[i].recDrop = istat - oNetDevs[i].recDrop;
209 //NetDevs[i].recMulticast = istat - oNetDevs[i].recMulticast;
210 NetDevs[i].sentBytes = istat->ifs_opackets - oNetDevs[i].sentBytes;
211 NetDevs[i].sentPacks = (istat->ifs_opackets - oNetDevs[i].sentPacks) * 100 / elapsed;
212 NetDevs[i].sentErrs = (istat->ifs_oerrors - oNetDevs[i].sentErrs) * 100 / elapsed;
213 //NetDevs[i].sentMulticast = istat - NetDevs[i].sentMulticast;
214 NetDevs[i].sentColls = (istat->ifs_collisions - oNetDevs[i].sentColls) *100/elapsed;
215 /* save it for the next round */
216 oNetDevs[i].recBytes = istat->ifs_ipackets;
217 oNetDevs[i].recPacks = istat->ifs_ipackets;
218 oNetDevs[i].recErrs = istat->ifs_ierrors;
219 //oNetDevs[i].recDrop =
220 //oNetDevs[i].recMulticast =
221 oNetDevs[i].sentBytes = istat->ifs_opackets;
222 oNetDevs[i].sentPacks = istat->ifs_opackets;
223 oNetDevs[i].sentErrs = istat->ifs_oerrors;
224 //oNetDevs[i].sentMulticast =
225 oNetDevs[i].sentColls = istat->ifs_collisions;
227 NetDevCnt++;
229 close(s);
230 return (0);
233 void printNetDevRecBytes(const char *cmd)
235 int i;
236 char **retval;
238 retval = parseCommand(cmd);
240 if (retval == NULL)
241 return;
243 for (i = 0; i < NetDevCnt; i++) {
244 if (!strcmp(NetDevs[i].name, retval[0])) {
245 if (!strncmp(retval[1], "data", 4))
246 fprintf(CurrentClient, "%lu", NetDevs[i].recBytes);
247 if (!strncmp(retval[1], "packets", 7))
248 fprintf(CurrentClient, "%lu", NetDevs[i].recPacks);
249 if (!strncmp(retval[1], "errors", 6))
250 fprintf(CurrentClient, "%lu", NetDevs[i].recErrs);
251 if (!strncmp(retval[1], "drops", 5))
252 fprintf(CurrentClient, "%lu", NetDevs[i].recDrop);
253 if (!strncmp(retval[1], "multicast", 9))
254 fprintf(CurrentClient, "%lu", NetDevs[i].recMulticast);
257 free(retval[0]);
258 free(retval[1]);
259 free(retval);
261 fprintf(CurrentClient, "\n");
264 void printNetDevRecBytesInfo(const char *cmd)
266 char **retval;
268 retval = parseCommand(cmd);
270 if (retval == NULL)
271 return;
273 if (!strncmp(retval[1], "data", 4))
274 fprintf(CurrentClient, "Received Data\t0\t0\tkBytes/s\n");
275 if (!strncmp(retval[1], "packets", 7))
276 fprintf(CurrentClient, "Received Packets\t0\t0\t1/s\n");
277 if (!strncmp(retval[1], "errors", 6))
278 fprintf(CurrentClient, "Receiver Errors\t0\t0\t1/s\n");
279 if (!strncmp(retval[1], "drops", 5))
280 fprintf(CurrentClient, "Receiver Drops\t0\t0\t1/s\n");
281 if (!strncmp(retval[1], "multicast", 9))
282 fprintf(CurrentClient, "Received Multicast Packets\t0\t0\t1/s\n");
284 free(retval[0]);
285 free(retval[1]);
286 free(retval);
289 void printNetDevSentBytes(const char *cmd)
291 int i;
292 char **retval;
294 retval = parseCommand(cmd);
296 if (retval == NULL)
297 return;
299 for (i = 0; i < NetDevCnt; i++) {
300 if (!strcmp(NetDevs[i].name, retval[0])) {
301 if (!strncmp(retval[1], "data", 4))
302 fprintf(CurrentClient, "%lu", NetDevs[i].sentBytes);
303 if (!strncmp(retval[1], "packets", 7))
304 fprintf(CurrentClient, "%lu", NetDevs[i].sentPacks);
305 if (!strncmp(retval[1], "errors", 6))
306 fprintf(CurrentClient, "%lu", NetDevs[i].sentErrs);
307 if (!strncmp(retval[1], "multicast", 9))
308 fprintf(CurrentClient, "%lu", NetDevs[i].sentMulticast);
309 if (!strncmp(retval[1], "collisions", 10))
310 fprintf(CurrentClient, "%lu", NetDevs[i].sentColls);
313 free(retval[0]);
314 free(retval[1]);
315 free(retval);
317 fprintf(CurrentClient, "\n");
320 void printNetDevSentBytesInfo(const char *cmd)
322 char **retval;
324 retval = parseCommand(cmd);
326 if (retval == NULL)
327 return;
329 if (!strncmp(retval[1], "data", 4))
330 fprintf(CurrentClient, "Sent Data\t0\t0\tkBytes/s\n");
331 if (!strncmp(retval[1], "packets", 7))
332 fprintf(CurrentClient, "Sent Packets\t0\t0\t1/s\n");
333 if (!strncmp(retval[1], "errors", 6))
334 fprintf(CurrentClient, "Transmitter Errors\t0\t0\t1/s\n");
335 if (!strncmp(retval[1], "multicast", 9))
336 fprintf(CurrentClient, "Sent Multicast Packets\t0\t0\t1/s\n");
337 if (!strncmp(retval[1], "collisions", 10))
338 fprintf(CurrentClient, "Transmitter Collisions\t0\t0\t1/s\n");
340 free(retval[0]);
341 free(retval[1]);
342 free(retval);