summaryrefslogtreecommitdiff
path: root/src/status/netif.c
blob: e760b4c7393420944996b1dfd28d8cd2156c2d7a (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include "common.h"
#include "tools.h"
#include "config.h"

#ifndef NETIF_BASEDIR
  #define NETIF_BASEDIR "/sys/class/net/"
#endif


void status_netif(GlobalData *g, char *ifname)
{
  StatusItem s;
  char text[16] = { 0 };

  char ifpath[256];
  int ifpathlen;

  char stline[16];
  ssize_t stlen;

  double ifsum = 0.0;
  int ifsumpower;


  statusitem_init(&s);
  s.text = text;

  /* Prepare path */
  ifpathlen = sizeof(NETIF_BASEDIR) - 1 + strlen(ifname);
  if (ifpathlen + 1 + sizeof("/statistics/rx_bytes") >= sizeof(ifpath)) {
    statusError("status_netif",
                "ifpath buffer too small",
                ifname);
    return;
  }
  strcpy(ifpath, NETIF_BASEDIR);
  strcat(ifpath, ifname);


  /* Is the interface up? */
  if (access(ifpath, F_OK)) {
    //s.color = "#BEBEBE";  // grey
    return;
  }


  strcpy(&ifpath[ifpathlen], "/carrier");
  stlen = fileRead(stline, sizeof(stline), ifpath);
  if (stlen > 0) {
    if (stline[0] == '1') {
      s.color = "#FFFF00";  // yellow
    } else {
      //s.color = "#FF0000";  // red
      return;
    }
  } else {
    return;
  }

  strcpy(&ifpath[ifpathlen], "/statistics/rx_bytes");
  stlen = fileRead(stline, sizeof(stline), ifpath);
  if (stlen > 0) {
    ifsum = atof(stline);
  }

  strcpy(&ifpath[ifpathlen], "/statistics/tx_bytes");
  stlen = fileRead(stline, sizeof(stline), ifpath);
  if (stlen > 0) {
    ifsum += atof(stline);
  }


  for(ifsumpower = 0; ifsum >= 1024.0; ifsumpower++) {
    ifsum = ifsum / 1024;
  }

  snprintf(text, sizeof(text), "%s: %.*f %c",
            ifname,
            ifsumpower ? ifsumpower - 1 : ifsumpower,
            ifsum,
            powerToChar(ifsumpower));

  line_append_item(g, &s);
}