File: netmeter.cc

package info (click to toggle)
xosview 1.24-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,184 kB
  • sloc: cpp: 11,975; makefile: 154; ansic: 32; awk: 13; sh: 8
file content (195 lines) | stat: -rw-r--r-- 5,063 bytes parent folder | download | duplicates (2)
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
//
//  Copyright (c) 1994, 1995, 2002, 2006 by Mike Romberg ( mike.romberg@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
//

#include "netmeter.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include "stringutils.h"

static const char PROCNETDEV[] = "/proc/net/dev";
static const char SYSCLASSNET[] = "/sys/class/net";

/*
 * Parse the integer count from the given filename
 *
 * This is quite relaxed about error conditions because the file may
 * have been removed before it was opened, or truncated, in which case
 * the count is zero.
 *
 * Return: 0 if not not avilable, otherwise count (which may be zero)
 */

static unsigned long long getCount( const char *filename ){
  unsigned long long n, count = 0;
  FILE *f;

  f = fopen(filename, "r");
  if (!f)
    return 0;

  if (fscanf(f, "%llu", &n) == 1)
    count = n;

  if (fclose(f) != 0)
    abort();

  return count;
}


NetMeter::NetMeter( XOSView *parent, float max )
  : FieldMeterGraph( parent, 3, "NET", "IN/OUT/IDLE" ){
  _maxpackets = max;
  _lastBytesIn = _lastBytesOut = 0;
  _usesysfs = _ignored = false;

  struct stat buf;
  if ( stat(SYSCLASSNET, &buf) == 0 && S_ISDIR(buf.st_mode) )
    _usesysfs = true;
}

NetMeter::~NetMeter( void ){
}

void NetMeter::checkResources( void ){
  FieldMeterGraph::checkResources();

  setfieldcolor( 0, parent_->getResource( "netInColor" ) );
  setfieldcolor( 1, parent_->getResource( "netOutColor" ) );
  setfieldcolor( 2, parent_->getResource( "netBackground" ) );
  priority_ = atoi( parent_->getResource( "netPriority" ) );
  useGraph_ = parent_->isResourceTrue( "netGraph" );
  dodecay_ = parent_->isResourceTrue( "netDecay" );
  SetUsedFormat( parent_->getResource("netUsedFormat") );
  _netIface = parent_->getResource( "netIface" );
  if (_netIface[0] == '-') {
    _ignored = true;
    _netIface.erase(0, _netIface.find_first_not_of("- "));
  }
}

void NetMeter::checkevent( void ){
  unsigned long long totin = 0, totout = 0;
  fields_[2] = _maxpackets;     // assume no
  fields_[0] = fields_[1] = 0;  // network activity

  IntervalTimerStop();
  if (_usesysfs)
    getSysStats(totin, totout);
  else
    getProcStats(totin, totout);

  double t = IntervalTimeInSecs();
  IntervalTimerStart();

  if (_lastBytesIn == 0 && _lastBytesOut == 0) {  // first run
    _lastBytesIn = totin;
    _lastBytesOut = totout;
  }

  fields_[0] = (totin - _lastBytesIn) / t;
  fields_[1] = (totout - _lastBytesOut) / t;

  _lastBytesIn = totin;
  _lastBytesOut = totout;

  total_ = fields_[0] + fields_[1];
  if (total_ > _maxpackets)
    fields_[2] = 0;
  else {
    total_ = _maxpackets;
    fields_[2] = total_ - fields_[0] - fields_[1];
  }

  setUsed(fields_[0] + fields_[1], total_);
  drawfields();
}

void NetMeter::getSysStats( unsigned long long &totin, unsigned long long &totout ){
  DIR *dir;
  struct dirent *ent;
  char filename[128];
  std::ifstream ifs;

  if ( !(dir = opendir(SYSCLASSNET)) ) {
    std::cerr << "Can not open directory : " << SYSCLASSNET << std::endl;
    parent_->done(1);
    return;
  }

  // walk through /sys/class/net/*/statistics/{r,t}x_bytes
  while ( (ent = readdir(dir)) ) {
    if ( ent->d_type != DT_LNK )
      continue;
    if ( _netIface != "False" &&
         ( (!_ignored && ent->d_name != _netIface) ||
           ( _ignored && ent->d_name == _netIface) ) )
        continue;

    snprintf_or_abort(filename, 128,
        "%s/%s/statistics/rx_bytes", SYSCLASSNET, ent->d_name);
    totin += getCount(filename);

    snprintf_or_abort(filename, 128,
        "%s/%s/statistics/tx_bytes", SYSCLASSNET, ent->d_name);
    totout += getCount(filename);
  }
  closedir(dir);
}

void NetMeter::getProcStats( unsigned long long &totin, unsigned long long &totout ){
  std::ifstream ifs(PROCNETDEV);
  std::string line, ifname;

  if (!ifs) {
    std::cerr << "Can not open file : " << PROCNETDEV << std::endl;
    parent_->done(1);
    return;
  }

  ifs.ignore(1024, '\n');
  ifs.ignore(1024, '\n');

  while ( !ifs.eof() ) {
    unsigned long long vals[9];
    std::getline(ifs, line);
    if ( !ifs.good() )
      break;

    int colon = line.find_first_of(':');
    ifname = line.substr(0, colon);
    ifname.erase(0, ifname.find_first_not_of(' '));
    if (_netIface != "False") {
      if ( (!_ignored && ifname != _netIface) ||
           ( _ignored && ifname == _netIface) )
        continue;
    }

    std::string l = line.erase(0, colon + 1);
    const char *cur = l.c_str();
    if ( strncmp(cur, " No ", 4) == 0 )
      continue; // xxx: No statistics available.

    char *end = NULL;
    for (int i = 0; i < 9; i++) {
      vals[i] = strtoull(cur, &end, 10);
      cur = end;
    }
    totin += vals[0];
    totout += vals[8];
    XOSDEBUG("%s: %llu bytes received, %llu bytes sent.\n",
             ifname.c_str(), vals[0], vals[8]);
  }
}