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.
23 #include <sys/socket.h>
25 #include <net/soioctl.h>
28 #include <sys/sysctl.h>
30 #include <sys/types.h>
37 #include "ksysguardd.h"
41 #define LONGLONG long long
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
);
67 char *retval
= malloc(sizeof(char *)*2);
69 begin
= rindex(tmp_cmd
, '/');
72 retval
[1] = strdup(begin
); // sensor
74 begin
= rindex(tmp_cmd
, '/');
76 begin
= rindex(tmp_cmd
, '/');
78 retval
[0] = strdup(begin
); // interface
84 /* ------------------------------ public part --------------------------- */
86 void initNetDev(struct SensorModul
* sm
)
91 memset(NetDevs
,0,sizeof(NetDevInfo
)*MAXNETDEVS
);
92 memset(oNetDevs
,0,sizeof(NetDevInfo
)*MAXNETDEVS
);
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)
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)
166 char buf
[MAXNETDEVS
*sizeof(struct ifreq
)];
170 struct ifstats
*istat
;
172 static LONGLONG timestamp
=0;
173 register LONGLONG cts
,elapsed
;
176 if ((s
=socket(PF_INET
,SOCK_DGRAM
,0)) < 0){
177 print_error("socket creation failed");
181 ifc
.ifc_len
= sizeof (buf
);
183 if (ioctl(s
, SIOCGIFCONF
, (char *)&ifc
) < 0) {
184 print_error("cannot get interface configuration");
188 gettimeofday(&tv
, 0);
189 cts
= ((LONGLONG
)tv
.tv_sec
* 100 + (LONGLONG
) tv
.tv_usec
/ 10000);/* in 10 ms unit*/
190 elapsed
= cts
- timestamp
;
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");
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
;
233 void printNetDevRecBytes(const char *cmd
)
238 retval
= parseCommand(cmd
);
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
);
261 fprintf(CurrentClient
, "\n");
264 void printNetDevRecBytesInfo(const char *cmd
)
268 retval
= parseCommand(cmd
);
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");
289 void printNetDevSentBytes(const char *cmd
)
294 retval
= parseCommand(cmd
);
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
);
317 fprintf(CurrentClient
, "\n");
320 void printNetDevSentBytesInfo(const char *cmd
)
324 retval
= parseCommand(cmd
);
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");