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 // Copyright (c) 1994, 1995 by Mike Romberg ( romberg@fsl.noaa.gov )
3 //
4 // NetBSD port:
5 // Copyright (c) 1995, 1996, 1997-2002 by Brian Grayson (bgrayson@netbsd.org)
6 //
7 // This file was written by Brian Grayson for the NetBSD and xosview
8 // projects.
9 // This file may be distributed under terms of the GPL or of the BSD
10 // license, whichever you choose. The full license notices are
11 // contained in the files COPYING.GPL and COPYING.BSD, which you
12 // should have received. If not, contact one of the xosview
13 // authors for a copy.
14 //
15 
16 #include "netmeter.h"
17 #include "kernel.h"
18 #include <stdlib.h>
19 #include <err.h>
20 
21 
22 NetMeter::NetMeter( XOSView *parent, double max )
23  : FieldMeterGraph( parent, 3, "NET", "IN/OUT/IDLE" ) {
24  if (!BSDNetInit()) {
25  warnx("The kernel does not seem to have the symbols needed for the NetMeter.");
26  warnx("The NetMeter has been disabled.");
27  disableMeter ();
28  }
29  else {
32  netIface_ = "False";
33  ignored_ = false;
36  }
37 }
38 
40 }
41 
44 
45  setfieldcolor( 0, parent_->getResource("netInColor") );
46  setfieldcolor( 1, parent_->getResource("netOutColor") );
47  setfieldcolor( 2, parent_->getResource("netBackground") );
48  priority_ = atoi( parent_->getResource("netPriority") );
49  dodecay_ = parent_->isResourceTrue("netDecay");
50  useGraph_ = parent_->isResourceTrue("netGraph");
51  SetUsedFormat( parent_->getResource("netUsedFormat") );
52  netIface_ = parent_->getResource("netIface");
53  if (netIface_[0] == '-') {
54  ignored_ = true;
55  netIface_.erase(0, netIface_.find_first_not_of("- "));
56  }
57 }
58 
59 void NetMeter::checkevent( void ) {
60  getstats();
61  drawfields();
62 }
63 
64 void NetMeter::getstats(void) {
65  // Reset total_ to expected maximum. If it is too low, it
66  // will be adjusted in adjust(). bgrayson
68  fields_[0] = fields_[1] = 0;
69  uint64_t nowBytesIn, nowBytesOut;
70 
72  BSDGetNetInOut(&nowBytesIn, &nowBytesOut, netIface_.c_str(), ignored_);
73  double t = (1.0) / IntervalTimeInSecs();
75 
76  fields_[0] = (double)(nowBytesIn - lastBytesIn_) * t;
77  lastBytesIn_ = nowBytesIn;
78  fields_[1] = (double)(nowBytesOut - lastBytesOut_) * t;
79  lastBytesOut_ = nowBytesOut;
80 
81  if (total_ < (fields_[0] + fields_[1]))
82  total_ = fields_[0] + fields_[1];
83  fields_[2] = total_ - fields_[0] - fields_[1];
84  /* The fields_ values have already been scaled into bytes/sec by
85  * the manipulations (* t) above. */
86  setUsed(fields_[0] + fields_[1], total_);
87 }
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 disableMeter(void)
Definition: fieldmeter.cc:36
int priority_
Definition: meter.h:54
XOSView * parent_
Definition: meter.h:52
~NetMeter(void)
Definition: netmeter.cc:39
void getstats(void)
Definition: netmeter.cc:64
void checkResources(void)
Definition: netmeter.cc:42
NetMeter(XOSView *parent, double max)
Definition: netmeter.cc:22
void checkevent(void)
Definition: netmeter.cc:59
std::string netIface_
Definition: netmeter.h:39
uint64_t lastBytesIn_
Definition: netmeter.h:37
double netBandwidth_
Definition: netmeter.h:38
bool ignored_
Definition: netmeter.h:40
uint64_t lastBytesOut_
Definition: netmeter.h:37
int isResourceTrue(const char *name)
Definition: xwin.h:89
const char * getResource(const char *name)
Definition: xwin.cc:362
void BSDGetNetInOut(uint64_t *inbytes, uint64_t *outbytes, const char *netIface, bool ignored)
Definition: kernel.cc:492
int BSDNetInit()
Definition: kernel.cc:486
static const int max
Definition: intmeter.cc:17