xosview  1.23
About: xosview is an X Windows based system monitor (cpu, memory, swap and network usage; interrupt and serial activities; load average).
  Fossies Dox: xosview-1.23.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

netmeter.cc
Go to the documentation of this file.
1 //
2 // Rewritten for Solaris by Arno Augustin 1999
3 // augustin@informatik.uni-erlangen.de
4 //
5 
6 #include "netmeter.h"
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <stropts.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <sys/sockio.h>
13 #include <string.h>
14 #include <iostream>
15 #ifdef DEBUG
16 #include <sstream>
17 #endif
18 
19 
20 NetMeter::NetMeter( XOSView *parent, kstat_ctl_t *kc, float max )
21  : FieldMeterGraph( parent, 3, "NET", "IN/OUT/IDLE" ){
22  _kc = kc;
23  _ignored = false;
24  _maxpackets = max;
27  if ( (_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
28  std::cerr << "Opening socket failed." << std::endl;
29  parent_->done(1);
30  return;
31  }
32 }
33 
34 NetMeter::~NetMeter( void ){
35  close(_socket);
36 }
37 
38 void NetMeter::checkResources( void ){
40 
41  setfieldcolor( 0, parent_->getResource("netInColor") );
42  setfieldcolor( 1, parent_->getResource("netOutColor") );
43  setfieldcolor( 2, parent_->getResource("netBackground") );
44  priority_ = atoi( parent_->getResource("netPriority") );
45  dodecay_ = parent_->isResourceTrue("netDecay");
46  useGraph_ = parent_->isResourceTrue("netGraph");
47  SetUsedFormat( parent_->getResource("netUsedFormat") );
48  _netIface = parent_->getResource("netIface");
49  if (_netIface[0] == '-') {
50  _ignored = true;
51  _netIface.erase(0, _netIface.find_first_not_of("- "));
52  }
53 }
54 
55 void NetMeter::checkevent( void ){
56  getnetstats();
57  drawfields();
58 }
59 
60 void NetMeter::getnetstats( void ){
61  uint64_t nowBytesIn = 0, nowBytesOut = 0;
62  kstat_named_t *k;
63  kstat_t *ksp;
65  _nets->update(_kc);
66 
68  for (unsigned int i = 0; i < _nets->count(); i++) {
69 #ifdef DEBUG
70  std::stringstream msg;
71 #endif
72  ksp = (*_nets)[i];
73  if ( _netIface != "False" &&
74  ( (!_ignored && ksp->ks_name != _netIface) ||
75  ( _ignored && ksp->ks_name == _netIface) ) )
76  continue;
77  if ( kstat_read(_kc, ksp, NULL) == -1 )
78  continue;
79 
80 #ifdef DEBUG
81  msg << ksp->ks_name << ": ";
82 #endif
83  // try 64-bit byte counter first, then 32-bit one, then packet counter
84  if ( (k = (kstat_named_t *)kstat_data_lookup(ksp, "rbytes64")) == NULL ) {
85  if ( (k = (kstat_named_t *)kstat_data_lookup(ksp, "rbytes")) == NULL ) {
86  if ( (k = (kstat_named_t *)kstat_data_lookup(ksp, "ipackets")) == NULL )
87  continue;
88  // for packet counter, mtu is needed
89  strncpy(_lfr.lifr_name, ksp->ks_name, sizeof(_lfr.lifr_name));
90  if ( ioctl(_socket, SIOCGLIFMTU, (caddr_t)&_lfr) < 0 )
91  continue;
92  nowBytesIn += kstat_to_ui64(k) * _lfr.lifr_mtu; // not exactly, but must do
93 #ifdef DEBUG
94  msg << kstat_to_ui64(k) << " packets received ";
95 #endif
96  }
97  else {
98  nowBytesIn += kstat_to_ui64(k);
99 #ifdef DEBUG
100  msg << kstat_to_ui64(k) << " bytes received ";
101 #endif
102  }
103  }
104  else {
105  nowBytesIn += kstat_to_ui64(k);
106 #ifdef DEBUG
107  msg << kstat_to_ui64(k) << " bytes received ";
108 #endif
109  }
110 
111  if ( (k = (kstat_named_t *)kstat_data_lookup(ksp, "obytes64")) == NULL ) {
112  if ( (k = (kstat_named_t *)kstat_data_lookup(ksp, "obytes")) == NULL ) {
113  if ( (k = (kstat_named_t *)kstat_data_lookup(ksp, "opackets")) == NULL )
114  continue;
115  strncpy(_lfr.lifr_name, ksp->ks_name, sizeof(_lfr.lifr_name));
116  if ( ioctl(_socket, SIOCGLIFMTU, (caddr_t)&_lfr) < 0 )
117  continue;
118  nowBytesOut += kstat_to_ui64(k) * _lfr.lifr_mtu;
119 #ifdef DEBUG
120  msg << kstat_to_ui64(k) << " packets sent ";
121 #endif
122  }
123  else {
124  nowBytesOut += kstat_to_ui64(k);
125 #ifdef DEBUG
126  msg << kstat_to_ui64(k) << " bytes sent ";
127 #endif
128  }
129  }
130  else {
131  nowBytesOut += kstat_to_ui64(k);
132 #ifdef DEBUG
133  msg << kstat_to_ui64(k) << " bytes sent ";
134 #endif
135  }
136 #ifdef DEBUG
137  msg << std::ends;
138  XOSDEBUG("%s\n", msg.str().c_str())
139 #endif
140  }
141 
142  uint64_t correction = 0x10000000;
143  correction *= 0x10;
144  /* Deal with 32-bit wrap by making last value 2^32 less. Yes,
145  * this is a better idea than adding to nowBytesIn -- the
146  * latter would only work for the first wrap (1+2^32 vs. 1)
147  * but not for the second (1+2*2^32 vs. 1) -- 1+2^32 -
148  * (1+2^32) is still too big. */
149  if (nowBytesIn < _lastBytesIn)
150  _lastBytesIn -= correction;
151  if (nowBytesOut < _lastBytesOut)
152  _lastBytesOut -= correction;
153  if(_lastBytesIn == 0)
154  _lastBytesIn = nowBytesIn;
155  if(_lastBytesOut == 0)
156  _lastBytesOut = nowBytesOut;
157 
158  double t = IntervalTimeInSecs();
159  fields_[0] = (double)(nowBytesIn - _lastBytesIn) / t;
160  fields_[1] = (double)(nowBytesOut - _lastBytesOut) / t;
161 
163  _lastBytesIn = nowBytesIn;
164  _lastBytesOut = nowBytesOut;
165 
166  if (total_ < fields_[0] + fields_[1])
167  total_ = fields_[0] + fields_[1];
168  fields_[2] = total_ - fields_[0] - fields_[1];
169  setUsed(fields_[0] + fields_[1], total_);
170 }
virtual void checkResources(void)
virtual void drawfields(int mandatory=0)
double total_
Definition: fieldmeter.h:41
void setfieldcolor(int field, const char *color)
Definition: fieldmeter.cc:108
void setUsed(double val, double total)
Definition: fieldmeter.cc:76
void IntervalTimerStart()
Definition: fieldmeter.h:62
double * fields_
Definition: fieldmeter.h:40
void IntervalTimerStop()
Definition: fieldmeter.h:63
void SetUsedFormat(const char *const str)
Definition: fieldmeter.cc:60
double IntervalTimeInSecs()
Definition: fieldmeter.h:68
void update(kstat_ctl_t *kcp)
Definition: kstats.h:48
@ NETS
Definition: kstats.h:21
size_t count(void)
Definition: kstats.h:47
static KStatList * getList(kstat_ctl_t *kcp, module m)
Definition: kstats.h:23
int priority_
Definition: meter.h:54
XOSView * parent_
Definition: meter.h:52
float _maxpackets
Definition: netmeter.h:27
~NetMeter(void)
Definition: netmeter.cc:39
struct lifreq _lfr
Definition: netmeter.h:37
kstat_ctl_t * _kc
Definition: netmeter.h:33
void checkResources(void)
Definition: netmeter.cc:42
int _socket
Definition: netmeter.h:38
KStatList * _nets
Definition: netmeter.h:34
void getnetstats(void)
Definition: netmeter.cc:60
NetMeter(XOSView *parent, double max)
Definition: netmeter.cc:22
std::string _netIface
Definition: netmeter.h:28
unsigned long long _lastBytesOut
Definition: netmeter.h:30
void checkevent(void)
Definition: netmeter.cc:59
unsigned long long _lastBytesIn
Definition: netmeter.h:30
bool _ignored
Definition: netmeter.h:29
int done(void)
Definition: xwin.h:29
int isResourceTrue(const char *name)
Definition: xwin.h:89
const char * getResource(const char *name)
Definition: xwin.cc:362
unsigned long long kstat_to_ui64(kstat_named_t *k)
Definition: kstats.h:118
static const int max
Definition: intmeter.cc:17
#define XOSDEBUG(...)
Definition: xosview.h:84