Change named colors to hex values
[sysstatus.git] / src / status / netif.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5
6 #include "common.h"
7 #include "tools.h"
8 #include "config.h"
9
10 #ifndef NETIF_BASEDIR
11   #define NETIF_BASEDIR "/sys/class/net/"
12 #endif
13
14
15 void status_netif(GlobalData *g, char *ifname)
16 {
17   StatusItem s;
18   char text[16] = { 0 };
19
20   char ifpath[256];
21   int ifpathlen;
22
23   char stline[16];
24   ssize_t stlen;
25
26   double ifsum = 0.0;
27   int ifsumpower;
28
29
30   statusitem_init(&s);
31   s.text = text;
32
33   /* Prepare path */
34   ifpathlen = sizeof(NETIF_BASEDIR) - 1 + strlen(ifname);
35   if (ifpathlen + 1 + sizeof("/statistics/rx_bytes") >= sizeof(ifpath)) {
36     statusError("status_netif",
37                 "ifpath buffer too small",
38                 ifname);
39     return;
40   }
41   strcpy(ifpath, NETIF_BASEDIR);
42   strcat(ifpath, ifname);
43
44
45   /* Is the interface up? */
46   if (access(ifpath, F_OK)) {
47     //s.color = "#BEBEBE";  // grey
48     return;
49   }
50
51
52   strcpy(&ifpath[ifpathlen], "/carrier");
53   stlen = fileRead(stline, sizeof(stline), ifpath);
54   if (stlen > 0) {
55     if (stline[0] == '1') {
56       s.color = "#FFFF00";  // yellow
57     } else {
58       //s.color = "#FF0000";  // red
59       return;
60     }
61   } else {
62     return;
63   }
64
65   strcpy(&ifpath[ifpathlen], "/statistics/rx_bytes");
66   stlen = fileRead(stline, sizeof(stline), ifpath);
67   if (stlen > 0) {
68     ifsum = atof(stline);
69   }
70
71   strcpy(&ifpath[ifpathlen], "/statistics/tx_bytes");
72   stlen = fileRead(stline, sizeof(stline), ifpath);
73   if (stlen > 0) {
74     ifsum += atof(stline);
75   }
76
77
78   for(ifsumpower = 0; ifsum >= 1024.0; ifsumpower++) {
79     ifsum = ifsum / 1024;
80   }
81
82   snprintf(text, sizeof(text), "%s: %.*f %c",
83             ifname,
84             ifsumpower ? ifsumpower - 1 : ifsumpower,
85             ifsum,
86             powerToChar(ifsumpower));
87
88   line_append_item(g, &s);
89 }