1 /******************************************************************************
2  *  bwm-ng parsing and retrieve stuff                                         *
3  *                                                                            *
4  *  Copyright (C) 2004-2007 Volker Gropp (bwmng@gropp.org)                    *
5  *                                                                            *
6  *  for more info read README.                                                *
7  *                                                                            *
8  *  This program is free software; you can redistribute it and/or modify      *
9  *  it under the terms of the GNU General Public License as published by      *
10  *  the Free Software Foundation; either version 2 of the License, or         *
11  *  (at your option) any later version.                                       *
12  *                                                                            *
13  *  This program is distributed in the hope that it will be useful,           *
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
16  *  GNU General Public License for more details.                              *
17  *                                                                            *
18  *  You should have received a copy of the GNU General Public License         *
19  *  along with this program; if not, write to the Free Software               *
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *
21  *                                                                            *
22  *****************************************************************************/
23 
24 #include "proc_net_dev.h"
25 
26 #ifdef PROC_NET_DEV
27 /* do the actual work, get and print stats if verbose */
get_iface_stats_proc(char verbose)28 void get_iface_stats_proc (char verbose) {
29 	char *ptr;
30 
31 	FILE *f=NULL;
32 	char *buffer=NULL,*name=NULL;
33 
34 	int hidden_if=0,current_if_num=0;
35 	t_iface_speed_stats stats; /* local struct, used to calc total values */
36 	t_iface_speed_stats tmp_if_stats;
37 
38 	memset(&stats,0,(size_t)sizeof(t_iface_speed_stats)); /* init it */
39 	/* dont open proc_net_dev if netstat_i is requested, else try to open and if it fails fallback */
40 	if (!(f=fopen(PROC_FILE,"r"))) {
41 		deinit(1, "open of procfile failed: %s\n",strerror(errno));
42 	}
43 	buffer=(char *)malloc(MAX_LINE_BUFFER);
44 
45 	/* we skip first 2 lines if not bsd at any mode */
46 	if ((fgets(buffer,MAX_LINE_BUFFER,f) == NULL ) ||
47 			(fgets(buffer,MAX_LINE_BUFFER,f) == NULL ))
48 		deinit(1, "read of proc failed: %s\n",strerror(errno));
49 
50 	name=(char *)malloc(MAX_LINE_BUFFER);
51 	while ( (fgets(buffer,MAX_LINE_BUFFER,f) != NULL) ) {
52         /* get the name */
53         ptr=strchr(buffer,':');
54         /* wrong format */
55         if (ptr==NULL) { deinit(1, "wrong format of input stream\n"); }
56 		/* set : to end_of_string and move to first char of "next" string (to first data) */
57         *ptr++ = 0;
58         sscanf(ptr,"%llu%llu%llu%*i%*i%*i%*i%*i%llu%llu%llu",&tmp_if_stats.bytes.in,&tmp_if_stats.packets.in,&tmp_if_stats.errors.in,&tmp_if_stats.bytes.out,&tmp_if_stats.packets.out,&tmp_if_stats.errors.out);
59         sscanf(buffer,"%s",name);
60 		/* init new interfaces and add fetched data to old or new one */
61 		hidden_if = process_if_data (hidden_if, tmp_if_stats, &stats, name, current_if_num, verbose
62 #ifdef IOCTL
63                 ,check_if_up(name)
64 #else
65                 ,1
66 #endif
67 				);
68 		current_if_num++;
69     } /* fgets done (while) */
70 	/* add to total stats and output current stats if verbose */
71 	finish_iface_stats (verbose, stats, hidden_if,current_if_num);
72     /* clean buffers */
73 	free(buffer);
74 	free(name);
75 	/* close input stream */
76 	fclose(f);
77     return;
78 }
79 #endif
80 
81