summaryrefslogtreecommitdiffstats
path: root/ksysguard/ksysguardd/Irix/NetDev.c
blob: 403fea6cf8a8c25d701c73ebe8291ae9f88fe807 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
    KSysGuard, the KDE System Guard

	Copyright (c) 2001 Tobias Koenig <tokoe@kde.org>
	Irix Support by Carsten Kroll <ckroll@pinnaclesys.com>

    This program is free software; you can redistribute it and/or
    modify it under the terms of version 2 of the GNU General Public
    License as published by the Free Software Foundation.

    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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

#include <fcntl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/soioctl.h>
#include <stdio.h>
#include <string.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <invent.h>
#include <strings.h>


#include "Command.h"
#include "ksysguardd.h"
#include "NetDev.h"

#ifdef __GNUC__
#define LONGLONG long long
#endif

typedef struct {
	char name[IFNAMSIZ];
	u_long recBytes;
	u_long recPacks;
	u_long recErrs;
	u_long recDrop;
	u_long recMulticast;
	u_long sentBytes;
	u_long sentPacks;
	u_long sentErrs;
	u_long sentMulticast;
	u_long sentColls;
} NetDevInfo;

#define MAXNETDEVS 32
static NetDevInfo  NetDevs[MAXNETDEVS];
static NetDevInfo oNetDevs[MAXNETDEVS];
static int NetDevCnt = 0;

char **parseCommand(const char *cmd)
{
	char *tmp_cmd = strdup(cmd);
	char *begin;
	char *retval = malloc(sizeof(char *)*2);

	begin = rindex(tmp_cmd, '/');
	*begin = '\0';
	begin++;
	retval[1] = strdup(begin); // sensor

	begin = rindex(tmp_cmd, '/');
	*begin = '\0';
	begin = rindex(tmp_cmd, '/');
	begin++;
	retval[0] = strdup(begin); // interface
	free(tmp_cmd);

	return retval;
}

/* ------------------------------ public part --------------------------- */

void initNetDev(struct SensorModul* sm)
{
	int i;
	char monitor[1024];

	memset(NetDevs,0,sizeof(NetDevInfo)*MAXNETDEVS);
	memset(oNetDevs,0,sizeof(NetDevInfo)*MAXNETDEVS);

	updateNetDev();

	for (i = 0; i < NetDevCnt; i++) {

		sprintf(monitor,"network/interfaces/%s/receiver/packets", NetDevs[i].name);
		registerMonitor(monitor, "integer", printNetDevRecBytes, printNetDevRecBytesInfo, sm);
		sprintf(monitor ,"network/interfaces/%s/receiver/errors", NetDevs[i].name);
		registerMonitor(monitor, "integer", printNetDevRecBytes, printNetDevRecBytesInfo, sm);
		/*
		[CK] I don't know how to get Bytes sent/received, if someone does please drop me a note.
		sprintf(monitor,"network/interfaces/%s/receiver/data", NetDevs[i].name);
		registerMonitor(monitor, "integer", printNetDevRecBytes, printNetDevRecBytesInfo, sm);
		sprintf(monitor,"network/interfaces/%s/receiver/drops", NetDevs[i].name);
		registerMonitor(monitor, "integer", printNetDevRecBytes, printNetDevRecBytesInfo, sm);
		sprintf(monitor ,"network/interfaces/%s/receiver/multicast", NetDevs[i].name);
		registerMonitor(monitor, "integer", printNetDevRecBytes, printNetDevRecBytesInfo, sm);
		*/

		sprintf(monitor,"network/interfaces/%s/transmitter/packets", NetDevs[i].name);
		registerMonitor(monitor, "integer", printNetDevSentBytes, printNetDevSentBytesInfo, sm);
		sprintf(monitor,"network/interfaces/%s/transmitter/errors", NetDevs[i].name);
		registerMonitor(monitor, "integer", printNetDevSentBytes, printNetDevSentBytesInfo, sm);
		/*
		sprintf(monitor,"network/interfaces/%s/transmitter/data", NetDevs[i].name);
		registerMonitor(monitor, "integer", printNetDevSentBytes, printNetDevSentBytesInfo, sm);
		sprintf(monitor,"network/interfaces/%s/transmitter/multicast", NetDevs[i].name);
		registerMonitor(monitor, "integer", printNetDevSentBytes, printNetDevSentBytesInfo, sm);
		*/
		sprintf(monitor,"network/interfaces/%s/transmitter/collisions", NetDevs[i].name);
		registerMonitor(monitor, "integer", printNetDevSentBytes, printNetDevSentBytesInfo, sm);
	}
}

void exitNetDev(void)
{
	int i;
	char monitor[1024];

	for (i = 0; i < NetDevCnt; i++) {
		sprintf(monitor,"network/interfaces/%s/receiver/packets", NetDevs[i].name);
		removeMonitor(monitor);
		sprintf(monitor,"network/interfaces/%s/receiver/errors", NetDevs[i].name);
		removeMonitor(monitor);
/*
		sprintf(monitor,"network/interfaces/%s/receiver/drops", NetDevs[i].name);
		removeMonitor(monitor);
		sprintf(monitor,"network/interfaces/%s/receiver/multicast", NetDevs[i].name);
		removeMonitor(monitor);
		sprintf(monitor,"network/interfaces/%s/receiver/data", NetDevs[i].name);
		removeMonitor(monitor);
*/

		sprintf(monitor,"network/interfaces/%s/transmitter/packets", NetDevs[i].name);
		removeMonitor(monitor);
		sprintf(monitor,"network/interfaces/%s/transmitter/errors", NetDevs[i].name);
		removeMonitor(monitor);
/*
		sprintf(monitor,"network/interfaces/%s/transmitter/data", NetDevs[i].name);
		removeMonitor(monitor);
		sprintf(monitor,"network/interfaces/%s/transmitter/multicast", NetDevs[i].name);
		removeMonitor(monitor);
*/
		sprintf(monitor,"network/interfaces/%s/transmitter/collisions", NetDevs[i].name);
		removeMonitor(monitor);

	}
}

int updateNetDev(void)
{
	int name[6];
	int num_iface=0, i;
	char buf[MAXNETDEVS*sizeof(struct ifreq)];
	size_t len;
	int s;
	struct ifconf ifc;
	struct ifstats *istat;
	struct timeval tv;
	static LONGLONG timestamp=0;
	LONGLONG cts,elapsed;
	//struct ipstat ips;

	if ((s=socket(PF_INET,SOCK_DGRAM,0)) < 0){
		print_error("socket creation failed");
		return(-1);
	}

	ifc.ifc_len = sizeof (buf);
	ifc.ifc_buf = buf;
	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
		print_error("cannot get interface configuration");
		return(-1);
	}

	gettimeofday(&tv, 0);
	cts = ((LONGLONG)tv.tv_sec * 100 + (LONGLONG) tv.tv_usec / 10000);/* in 10 ms unit*/
	elapsed = cts - timestamp;
	timestamp=cts;

	NetDevCnt=0;

	for (i = 0; i < MAXNETDEVS; i++) {
		if ( *ifc.ifc_req[i].ifr_name == 0) break;
		if (ioctl(s, SIOCGIFSTATS, &ifc.ifc_req[i]) < 0) {
			print_error("cannot get interface statistics");
			return (-1);
		}
		istat=&ifc.ifc_req[i].ifr_stats;
		//if ( ifc.ifc_req[i].ifr_flags & IFF_UP) {
			strncpy(NetDevs[i].name,ifc.ifc_req[i].ifr_name, IFNAMSIZ);
                        NetDevs[i].name[IFNAMSIZ-1]='\0';
			NetDevs[i].recBytes = (istat->ifs_ipackets - oNetDevs[i].recBytes) * 100 / elapsed;
			NetDevs[i].recPacks = (istat->ifs_ipackets - oNetDevs[i].recPacks) * 100 / elapsed;
			NetDevs[i].recErrs = istat->ifs_ierrors - oNetDevs[i].recErrs;
			//NetDevs[i].recDrop = istat - oNetDevs[i].recDrop;
			//NetDevs[i].recMulticast = istat - oNetDevs[i].recMulticast;
			NetDevs[i].sentBytes = istat->ifs_opackets - oNetDevs[i].sentBytes;
			NetDevs[i].sentPacks = (istat->ifs_opackets - oNetDevs[i].sentPacks) * 100 / elapsed;
			NetDevs[i].sentErrs  = (istat->ifs_oerrors - oNetDevs[i].sentErrs) * 100 / elapsed;
			//NetDevs[i].sentMulticast = istat - NetDevs[i].sentMulticast;
			NetDevs[i].sentColls = (istat->ifs_collisions - oNetDevs[i].sentColls) *100/elapsed;
			/* save it for the next round */
			oNetDevs[i].recBytes = istat->ifs_ipackets;
			oNetDevs[i].recPacks = istat->ifs_ipackets;
			oNetDevs[i].recErrs  = istat->ifs_ierrors;
			//oNetDevs[i].recDrop =
			//oNetDevs[i].recMulticast =
			oNetDevs[i].sentBytes = istat->ifs_opackets;
			oNetDevs[i].sentPacks = istat->ifs_opackets;
			oNetDevs[i].sentErrs  = istat->ifs_oerrors;
			//oNetDevs[i].sentMulticast =
			oNetDevs[i].sentColls = istat->ifs_collisions;
		//}
		NetDevCnt++;
	}
	close(s);
	return (0);
}

void printNetDevRecBytes(const char *cmd)
{
	int i;
	char **retval;

	retval = parseCommand(cmd);

	if (retval == NULL)
		return;

	for (i = 0; i < NetDevCnt; i++) {
		if (!strcmp(NetDevs[i].name, retval[0])) {
			if (!strncmp(retval[1], "data", 4))
				fprintf(CurrentClient, "%lu", NetDevs[i].recBytes);
			if (!strncmp(retval[1], "packets", 7))
				fprintf(CurrentClient, "%lu", NetDevs[i].recPacks);
			if (!strncmp(retval[1], "errors", 6))
				fprintf(CurrentClient, "%lu", NetDevs[i].recErrs);
			if (!strncmp(retval[1], "drops", 5))
				fprintf(CurrentClient, "%lu", NetDevs[i].recDrop);
			if (!strncmp(retval[1], "multicast", 9))
				fprintf(CurrentClient, "%lu", NetDevs[i].recMulticast);
		}
	}
	free(retval[0]);
	free(retval[1]);
	free(retval);

	fprintf(CurrentClient, "\n");
}

void printNetDevRecBytesInfo(const char *cmd)
{
	char **retval;
	
	retval = parseCommand(cmd);
	
	if (retval == NULL)
		return;

	if (!strncmp(retval[1], "data", 4))
		fprintf(CurrentClient, "Received Data\t0\t0\tkBytes/s\n");
	if (!strncmp(retval[1], "packets", 7))
		fprintf(CurrentClient, "Received Packets\t0\t0\t1/s\n");
	if (!strncmp(retval[1], "errors", 6))
		fprintf(CurrentClient, "Receiver Errors\t0\t0\t1/s\n");
	if (!strncmp(retval[1], "drops", 5))
		fprintf(CurrentClient, "Receiver Drops\t0\t0\t1/s\n");
	if (!strncmp(retval[1], "multicast", 9))
		fprintf(CurrentClient, "Received Multicast Packets\t0\t0\t1/s\n");
	
	free(retval[0]);
	free(retval[1]);
	free(retval);
}

void printNetDevSentBytes(const char *cmd)
{
	int i;
	char **retval;
	
	retval = parseCommand(cmd);
	
	if (retval == NULL)
		return;

	for (i = 0; i < NetDevCnt; i++) {
		if (!strcmp(NetDevs[i].name, retval[0])) {
			if (!strncmp(retval[1], "data", 4))
				fprintf(CurrentClient, "%lu", NetDevs[i].sentBytes);
			if (!strncmp(retval[1], "packets", 7))
				fprintf(CurrentClient, "%lu", NetDevs[i].sentPacks);
			if (!strncmp(retval[1], "errors", 6))
				fprintf(CurrentClient, "%lu", NetDevs[i].sentErrs);
			if (!strncmp(retval[1], "multicast", 9))
				fprintf(CurrentClient, "%lu", NetDevs[i].sentMulticast);
			if (!strncmp(retval[1], "collisions", 10))
				fprintf(CurrentClient, "%lu", NetDevs[i].sentColls);
		}
	}
	free(retval[0]);
	free(retval[1]);
	free(retval);

	fprintf(CurrentClient, "\n");
}

void printNetDevSentBytesInfo(const char *cmd)
{
	char **retval;
	
	retval = parseCommand(cmd);
	
	if (retval == NULL)
		return;

	if (!strncmp(retval[1], "data", 4))
		fprintf(CurrentClient, "Sent Data\t0\t0\tkBytes/s\n");
	if (!strncmp(retval[1], "packets", 7))
		fprintf(CurrentClient, "Sent Packets\t0\t0\t1/s\n");
	if (!strncmp(retval[1], "errors", 6))
		fprintf(CurrentClient, "Transmitter Errors\t0\t0\t1/s\n");
	if (!strncmp(retval[1], "multicast", 9))
		fprintf(CurrentClient, "Sent Multicast Packets\t0\t0\t1/s\n");
	if (!strncmp(retval[1], "collisions", 10))
		fprintf(CurrentClient, "Transmitter Collisions\t0\t0\t1/s\n");

	free(retval[0]);
	free(retval[1]);
	free(retval);
}