"SfR Fresh" - the SfR Freeware/Shareware Archive 
Member "procinfo-ng-2.0.217/linux26_netstat.cpp" of archive procinfo-ng-2.0.217.tar.gz:
As a special service "SfR Fresh" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting with prefixed line numbers.
Alternatively you can here view or download the uninterpreted source code file.
That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
1 /*
2 This file is part of procinfo-NG
3
4 procinfo-NG is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2.
7
8 procinfo-NG is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with procinfo-NG; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 // Procinfo-NG is Copyright tabris@tabris.net 2007, 2008
19
20 #include <dirent.h>
21 #include <sys/stat.h>
22
23 vector <string> findInterfaces(void) {
24 vector <string> result;
25 DIR *dirHandle = opendir("/sys/class/net/");
26 struct dirent64 *dentry;
27 const string thisDir = string("."), parentDir = string("..");
28 struct stat buf;
29 while((dentry = readdir64(dirHandle)) != NULL) {
30 if(dentry->d_name == thisDir || dentry->d_name == parentDir ) {
31 continue;
32 }
33 string path = string("/sys/class/net/") + string(dentry->d_name) + string ("/statistics/rx_bytes");
34 if( stat(path.c_str(), &buf) == 0 ) {
35 result.push_back(string(dentry->d_name));
36 }
37
38 }
39 closedir(dirHandle);
40 return result;
41 }
42
43 #include <map>
44
45 struct __netStat {
46 char iface[32];
47 uint64_t rx_bytes, tx_bytes;
48 uint64_t rx_packets, tx_packets;
49 };
50
51 struct __netStat getIfaceStats(string interface) {
52 struct __netStat ifStat;
53 bzero(&ifStat, sizeof(ifStat));
54 vector <string> lines;
55
56 lines = readFile(string("/sys/class/net/")+interface+"/statistics/rx_bytes");
57 if(lines.size()) {
58 ifStat.rx_bytes = string2uint64(lines[0]);
59 }
60
61 lines = readFile(string("/sys/class/net/")+interface+"/statistics/tx_bytes");
62 if(lines.size()) {
63 ifStat.tx_bytes = string2uint64(lines[0]);
64 }
65
66 lines = readFile(string("/sys/class/net/")+interface+"/statistics/rx_packets");
67 if(lines.size()) {
68 ifStat.rx_packets = string2uint64(lines[0]);
69 }
70
71 lines = readFile(string("/sys/class/net/")+interface+"/statistics/tx_packets");
72 if(lines.size()) {
73 ifStat.tx_packets = string2uint64(lines[0]);
74 }
75
76 return ifStat;
77 }
78
79 struct ltstr
80 {
81 bool operator()(string s1, string s2) const
82 {
83 return (s1 < s2);
84 }
85 };
86
87 vector <vector <string> > getNetStats(bool perSecond, bool showTotals, double interval) {
88 static map<string, struct __netStat, ltstr> oldInterfaceStats;
89 static map<string, struct __netStat, ltstr> interfaceStats;
90
91 vector <string> interfaces = findInterfaces();
92 vector <vector <string > > entries; entries.reserve(interfaces.size());
93 for(unsigned int i = 0; i < interfaces.size(); i++) {
94 string iface = interfaces[i];
95
96 interfaceStats[iface] = getIfaceStats(iface);
97 bool newIF = ( oldInterfaceStats.find(iface) == oldInterfaceStats.end() );
98 struct __netStat ifaceStats;
99 if( perSecond && !showTotals && !newIF ) {
100 ifaceStats.rx_bytes = uint64_t((interfaceStats[iface].rx_bytes - oldInterfaceStats[iface].rx_bytes) / interval);
101 ifaceStats.tx_bytes = uint64_t((interfaceStats[iface].tx_bytes - oldInterfaceStats[iface].tx_bytes) / interval);
102 } else if( !perSecond && !showTotals && !newIF ) {
103 ifaceStats.rx_bytes = interfaceStats[iface].rx_bytes - oldInterfaceStats[iface].rx_bytes;
104 ifaceStats.tx_bytes = interfaceStats[iface].tx_bytes - oldInterfaceStats[iface].tx_bytes;
105 } else {
106 ifaceStats.rx_bytes = interfaceStats[iface].rx_bytes;
107 ifaceStats.tx_bytes = interfaceStats[iface].tx_bytes;
108 }
109
110 vector <string> row(3);
111 row[0] = iface;
112 row[1] = "TX " + humanizeBigNums(ifaceStats.tx_bytes);
113 row[2] = "RX " + humanizeBigNums(ifaceStats.rx_bytes);
114 entries.push_back(row);
115 }
116 oldInterfaceStats = interfaceStats;
117 interfaceStats.clear();
118
119 unsigned int split = entries.size() / 2 + (entries.size() & 1); // is equiv to (entries.size() % 2)
120 vector <vector <string > > rows; rows.reserve(split);
121 for(unsigned int i = 0; i < split; i++) {
122 vector <string> row(entries[i]);
123 if(entries.size() > i+split)
124 //row.insert(row.end, entries[i+split].begin(), entries[i+split].end());
125 try {
126 // foo.at() throws an exception instead of segfaulting.
127 row.push_back(entries.at(i+split)[0]);
128 row.push_back(entries.at(i+split)[1]);
129 row.push_back(entries.at(i+split)[2]);
130 } catch(...) {
131 }
132 rows.push_back(row);
133 }
134 return rows;
135 }