File: netloadmeter.cc

package info (click to toggle)
sinfo 0.0.48-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,332 kB
  • sloc: sh: 11,213; cpp: 6,722; makefile: 271; xml: 151; perl: 149
file content (336 lines) | stat: -rw-r--r-- 8,031 bytes parent folder | download | duplicates (3)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <iostream>

#ifdef __sun__
#include <time.h>
#include <limits.h>
#include <errno.h>
#include <kstat.h>
#include <string.h>
#include <stdlib.h>
#include <sys/sysinfo.h>
#include <sys/stat.h>
#include <sys/swap.h>
#endif

#ifdef __FreeBSD__
#include <sys/types.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <net/if.h>
#endif

#include "netloadmeter.h"
using namespace std;


NetloadMeter::NetloadMeter(const std::string & _iface)
{
  if (0 == _iface.size())
  {
    iface[0] = 0;
    // cout << "iface not given" << endl;

  }
  else
  {
    strncpy(iface, _iface.c_str(), IFACESIZE);
    iface[IFACESIZE - 1] = 0;
    // cout << "iface given by constructor as " << _iface << endl;
  }
  selectNetIface();
  std::cout << "iface=" << iface << std::endl;
}


#ifdef __linux__
const char * NetloadMeter::selectNetIface()
{
  if (0 == iface[0])
  {
    FILE * f = fopen("/proc/net/dev", "r");
    if (f != 0)
    {
      fscanf(f, "%*[^\n]\n");
      fscanf(f, "%*[^\n]\n");

      unsigned long iface_sum = 0;
      while (1)
      {
        char thisiface[IFACESIZE] = "";
        unsigned long rxbytes, txbytes;
        int num = fscanf(f, "%[^:]:%lu %*u %*u %*u %*u %*u %*u %*u"
                         "%lu %*u %*u %*u %*u %*u %*u %*u\n",
                         thisiface, &rxbytes, &txbytes);
        if (num != 3)
          break;


        unsigned long thisiface_sum = rxbytes + txbytes;
        if (( thisiface_sum > iface_sum)
            && (strcmp(thisiface, "lo") != 0))
        {
          iface_sum = thisiface_sum;
          strncpy(iface, thisiface, IFACESIZE);
          iface[IFACESIZE - 1] = 0;
        }
      }
      fclose (f);
    }
  }
  return iface;
}


#define BUFSIZE 4096
bool NetloadMeter::getNetload(Netload & netload)
{
  // Try to open /proc/net/dev for reading.
  FILE * f = fopen("/proc/net/dev", "r");
  if (f != 0)
  {
    char buf[BUFSIZE];
    /* Read the file into a buffer. */
    int num = fread(buf, 1, BUFSIZE - 1, f);
    fclose(f);

    if (num > 0)
    {
      buf[num] = 0;

      char *pch = strstr(buf, iface);
      if (pch != 0)
      {
        // Skip the interface name.
        pch += strlen(iface);
        // Skip the ":" after the interface name.
        pch++;

        unsigned long rxbytes, rxpkt, txbytes, txpkt;
        num = sscanf(pch, "%lu %lu %*u %*u %*u %*u %*u %*u"
                     "%lu %lu %*u %*u %*u %*u %*u %*u",
                     &rxbytes, &rxpkt, &txbytes, &txpkt);


        if (4 == num)
        {
          netload.rxbytes=rxBytesDeriver.setCurrentValueAndGetDerivation(rxbytes);
          netload.rxpkt=rxPktDeriver.setCurrentValueAndGetDerivation(rxpkt);
          netload.txbytes=txBytesDeriver.setCurrentValueAndGetDerivation(txbytes);
          netload.txpkt=txPktDeriver.setCurrentValueAndGetDerivation(txpkt);
          /*
                    cout << "netload.rxbytes " << netload.rxbytes << endl;
                    cout << "netload.txbytes " << netload.txbytes << endl;
                    cout << "netload.rxpkt " << netload.rxpkt << endl;
                    cout << "netload.txpkt " << netload.txpkt << endl;
          */
          netload.iface=string(iface);
          return true;
        }
      }

    }
  }
  return false;
}
#endif



#ifdef __sun__

const char * NetloadMeter::selectNetIface()
{
  if (0 == iface[0])
  {

    struct kstat_ctl *kc;
    if ((kc = kstat_open()) == 0)
    {
      fprintf(stderr, "Sorry, I cannot initialise the `kstat' library.\n"
              "This library is used for accessing kernel information.\n"
              "The diagnostics are: `%s'.\n\n", strerror(errno));
    }
    else
    {
      kstat_t *ks = kstat_lookup(kc, "hme", 0, "hme0");
      if ((ks) && (kstat_read(kc, ks, 0) != -1))
      {
        strncpy(iface, "hme0", IFACESIZE);
      }


      ks = kstat_lookup(kc, "le", 0, "le0");
      if ((ks) && (kstat_read(kc, ks, 0) != -1))
      {
        strncpy(iface, "le0", IFACESIZE);
      }

      kstat_close(kc);
    }

  }
  return iface;
}


bool NetloadMeter::getNetload(Netload & netload, const char * iface)
{
  struct kstat_ctl *kc;
  if ((kc = kstat_open()) == 0)
  {
    fprintf(stderr, "Sorry, I cannot initialise the `kstat' library.\n"
            "This library is used for accessing kernel information.\n"
            "The diagnostics are: `%s'.\n\n", strerror(errno));
  }
  else
  {

    char shortiface[IFACESIZE];
    strncpy(shortiface, iface, IFACESIZE);
    shortiface[IFACESIZE - 1] = 0;

    shortiface[strlen(shortiface) - 1] = 0;

    kstat_t *ks = kstat_lookup(kc, shortiface, 0, (char*)iface);
    if (!ks || kstat_read(kc, ks, 0) == -1)
    {
      perror("kstat_lookup/read");
      kstat_close(kc);
    }
    else
    {

      kstat_named_t *kn;

      unsigned long rxbytes=0;
      unsigned long rxpkt=0;
      unsigned long txbytes=0;
      unsigned long txpkt=0;
      kn = (kstat_named_t *)kstat_data_lookup(ks, "rbytes");
      if (kn)
        rxbytes = kn->value.ui32;

      kn = (kstat_named_t *)kstat_data_lookup(ks, "obytes");
      if (kn)
        txbytes = kn->value.ui32;

      kn = (kstat_named_t *)kstat_data_lookup(ks, "ipackets");
      if (kn)
        rxpkt = kn->value.ui32;

      kn = (kstat_named_t *)kstat_data_lookup(ks, "opackets");
      if (kn)
        txpkt = kn->value.ui32;

      kstat_close(kc);

      float elapsed = nlget_elapsed_time();

      netload.rxbytes = (rxbytes - lastrxbytes) / elapsed;
      netload.txbytes = (txbytes - lasttxbytes) / elapsed;
      netload.rxpkt = (rxpkt - lastrxpkt) / elapsed;
      netload.txpkt = (txpkt - lasttxpkt) / elapsed;
      /*
        cout << "netload.rxbytes " << netload.rxbytes << endl;
        cout << "netload.txbytes " << netload.txbytes << endl;
        cout << "netload.rxpkt " << netload.rxpkt << endl;
        cout << "netload.txpkt " << netload.txpkt << endl;
      */
      lastrxbytes = rxbytes;
      lasttxbytes = txbytes;
      lastrxpkt = rxpkt;
      lasttxpkt = txpkt;

      strncpy(netload.iface, iface, IFACESIZE);
      netload.iface[IFACESIZE - 1] = 0;

      return true;
    }
  }

  return false;
}
#endif


#ifdef __FreeBSD__
const char * NetloadMeter::selectNetIface()
{
  struct ifaddrs *ifap, *ifa;
  if (getifaddrs(&ifap))
  {
    std::cerr << "error using getifaddrs" << std::endl;
    return iface;
  }

  unsigned long iface_sum = 0;
  for (ifa = ifap; ifa; ifa = ifa->ifa_next)
  {
    if (ifa->ifa_addr->sa_family != AF_LINK)
      continue;

    struct if_data * if_dataptr = (struct if_data *) ifa->ifa_data;


    unsigned long rxbytes, txbytes;
    rxbytes = if_dataptr->ifi_ibytes;
    txbytes = if_dataptr->ifi_obytes;

    unsigned long thisiface_sum = rxbytes + txbytes;
    if (( thisiface_sum > iface_sum)
        && (strcmp(ifa->ifa_name, "lo0") != 0))
    {
      iface_sum = thisiface_sum;
      strncpy(iface, ifa->ifa_name, IFACESIZE);
      iface[IFACESIZE - 1] = 0;
    }
  }
  freeifaddrs(ifap);


  return iface;
}

bool NetloadMeter::getNetload(Netload & netload)
{
  struct ifaddrs *ifap, *ifa;
  if (getifaddrs(&ifap))
  {
    std::cerr << "error using getifaddrs" << std::endl;
    return false;
  }

  unsigned long rxbytes, rxpkt, txbytes, txpkt;

  for (ifa = ifap; ifa; ifa = ifa->ifa_next)
  {
    if (ifa->ifa_addr->sa_family != AF_LINK)
      continue;

    if (0 == strcmp(iface, ifa->ifa_name))
    {
      struct if_data * if_dataptr = (struct if_data *) ifa->ifa_data;

      rxbytes = if_dataptr->ifi_ibytes;
      txbytes = if_dataptr->ifi_obytes;
      rxpkt = if_dataptr->ifi_ipackets;
      txpkt = if_dataptr->ifi_opackets;
    }
  }
  freeifaddrs(ifap);


  netload.rxbytes=rxBytesDeriver.setCurrentValueAndGetDerivation(rxbytes);
  netload.rxpkt=rxPktDeriver.setCurrentValueAndGetDerivation(rxpkt);
  netload.txbytes=txBytesDeriver.setCurrentValueAndGetDerivation(txbytes);
  netload.txpkt=txPktDeriver.setCurrentValueAndGetDerivation(txpkt);


  return true;
}
#endif