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
|
//
// Copyright (c) 1994, 1995 by Mike Romberg ( romberg@fsl.noaa.gov )
//
// Modifications to support dynamic addresses by:
// Michael N. Lipp (mnl@dtro.e-technik.th-darmstadt.de)
//
// This file may be distributed under terms of the GPL
//
//
// $Id: netmeter.cc,v 1.11 1998/05/19 05:42:12 mromberg Exp $
//
//-----------------------------------------------------------------------
//
// To use this meter, ipaccounting needs to be configured in the kernel and
// accounting needs to be set up. Here are a couple of lines from my
// rc.local which add ip accounting on all packets to and from my ip
// address (192.168.0.3):
//
// /sbin/ipfw add accounting all iface 192.168.0.3 from 192.168.0.3 to 0/0
// /sbin/ipfw add accounting all iface 192.168.0.3 from 0/0 to 192.168.0.3
//
// If you have more than one ip address you can add lines similar to the
// ones above for the other addresses and this class will combine them in
// its display.
//-----------------------------------------------------------------------
#include "netmeter.h"
#include "xosview.h"
#include <unistd.h>
#include <fstream.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#if defined(GNULIBC) || defined(__GLIBC__)
#include <net/if.h>
#else
#include <linux/if.h>
#endif
#include <netinet/in.h>
#include <errno.h>
NetMeter::NetMeter( XOSView *parent, float max )
: FieldMeterDecay( parent, 3, "NET", "IN/OUT/IDLE" ){
_timer.start();
maxpackets_ = max;
_lastBytesIn = _lastBytesOut = 0;
checkOSVersion();
}
NetMeter::~NetMeter( void ){
close (_ipsock);
}
void NetMeter::checkOSVersion(void)
{
ifstream ifs("/proc/sys/kernel/osrelease");
if (!ifs)
{
cerr <<"Can not open file : " << "/proc/sys/kernel/osrelease" <<endl;
exit(1);
}
int major, minor;
_bytesInDev = 0;
ifs >> major;
ifs.ignore(1);
ifs >> minor;
ifs.ignore(1);
if (major > 2 || (major == 2 && minor >= 1))
{
_netfilename = "/proc/net/dev";
_bytesInDev = 1;
}
else
_netfilename = "/proc/net/ip_acct";
}
void NetMeter::checkResources( void ){
FieldMeterDecay::checkResources();
setfieldcolor( 0, parent_->getResource( "netInColor" ) );
setfieldcolor( 1, parent_->getResource( "netOutColor" ) );
setfieldcolor( 2, parent_->getResource( "netBackground" ) );
priority_ = atoi (parent_->getResource( "netPriority" ) );
dodecay_ = !strcmp (parent_->getResource( "netDecay" ), "True" );
SetUsedFormat (parent_->getResource("netUsedFormat"));
_ipsock = socket(AF_INET, SOCK_DGRAM, 0);
if (_ipsock == -1) {
cerr <<"Can not open socket : " <<strerror( errno ) <<endl;
parent_->done(1);
return;
}
}
void NetMeter::checkevent(void)
{
if (_bytesInDev)
checkeventNew();
else
checkeventOld();
}
void NetMeter::checkeventNew(void)
{
ifstream ifs(_netfilename);
if (!ifs)
{
cerr <<"Can not open file : " <<_netfilename <<endl;
parent_->done(1);
return;
}
unsigned long in, out, ig;
unsigned long totin = 0, totout = 0;
fields_[2] = maxpackets_; // assume no
fields_[0] = fields_[1] = 0; // network activity
_timer.stop();
ifs.ignore(1024, '\n');
ifs.ignore(1024, '\n');
while (!ifs.eof())
{
ifs.ignore(1024, ':');
ifs >> in >> ig >> ig >> ig >> ig >> ig >> ig >> ig >> out;
if (!ifs.eof())
{
totin += in;
totout += out;
}
ifs.ignore(1024, '\n');
}
float t = 1000000.0 / _timer.report();
if (t < 0)
t = 0.1;
fields_[0] = (totin - _lastBytesIn) * t;
fields_[1] = (totout - _lastBytesOut) * t;
_lastBytesIn = totin;
_lastBytesOut = totout;
adjust();
if (total_)
setUsed(fields_[0] + fields_[1], total_);
_timer.start();
drawfields();
}
void NetMeter::checkeventOld(void)
{
_timer.stop();
fields_[2] = maxpackets_; // assume no
fields_[0] = fields_[1] = 0; // network activity
ifstream ifs(_netfilename);
if (!ifs)
{
cerr <<"Can not open file : " << _netfilename <<endl;
parent_->done(1);
return;
}
struct ifconf ifc;
char buff[1024];
ifc.ifc_len = sizeof(buff);
ifc.ifc_buf = buff;
if (ioctl(_ipsock, SIOCGIFCONF, &ifc) < 0)
{
cerr <<"Can not get interface list : " <<strerror( errno ) <<endl;
parent_->done(1);
return;
}
char c;
unsigned long sa, da, sm, dm, bytes;
unsigned long tot_in = 0, tot_out = 0;
ifs.ignore(1024, '\n');
while (!ifs.eof())
{
ifs >> hex >> sa >> c >> sm >> c >> c >> da >> c >> dm;
for (int index = 0 ; index < 7 ; index++)
ifs.ignore(9999, ' ');
ifs >> dec >> bytes;
ifs.ignore(9999, '\n');
if (!ifs.eof())
{
struct ifreq *ifr = ifc.ifc_req;
for (register i = ifc.ifc_len / sizeof(struct ifreq);
--i >= 0; ifr++)
{
unsigned long adr = ntohl(
((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr);
if (sm == 0 && da == adr)
{
tot_in += bytes;
break;
}
if (dm == 0 && sa == adr)
{
tot_out += bytes;
break;
}
}
}
}
// This will happen when a dynamic connection (SLIP/PPP) goes down.
if ((tot_in < _lastBytesIn) || (tot_out < _lastBytesOut))
{
fields_[0] = fields_[1] = 0;
_lastBytesIn = tot_in;
_lastBytesOut = tot_out;
}
else
{
float t = 1000000.0 / _timer.report();
if (t < 0) // can happen when system clock is reset. (ntp, timed, etc)
t = 0.1;
fields_[0] = (tot_in - _lastBytesIn) * t;
fields_[1] = (tot_out - _lastBytesOut) * t;
_lastBytesIn = tot_in;
_lastBytesOut = tot_out;
}
adjust();
if (total_)
setUsed(fields_[0] + fields_[1], total_);
_timer.start();
drawfields();
}
void NetMeter::adjust(void)
{
total_ = fields_[0] + fields_[1];
if (total_ > maxpackets_)
fields_[2] = 0;
else
{
total_ = maxpackets_;
fields_[2] = total_ - fields_[0] - fields_[1];
}
}
|