Skip to content

Commit 1973feb

Browse files
committed
bandwidth2: use injected properties
See: vivien#132
1 parent 413159d commit 1973feb

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

bandwidth2/README.md

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,32 @@ It comes with some other features though:
1616
make
1717
```
1818

19-
### Example Blocklet
19+
### Config
2020
```ini
2121
[bandwidth]
2222
label=
23-
command=$SCRIPT_DIR/bandwidth2 -w 307200:30720 -c 512000:51200
23+
command=$SCRIPT_DIR/bandwidth2
2424
interval=persist
2525
markup=pango
26+
#INTERFACE=(checks all interfaces)
27+
#USE_BITS=0
28+
#USE_BYES=1
29+
#WARN_RX=0
30+
#WARN_TX=0
31+
#CRIT_RX=0
32+
#CRIT_TX=0
2633
```
2734

28-
## Options
29-
30-
```
31-
Usage: ./bandwidth2 [-b|B] [-t seconds] [-i interface] [-w Bytes:Bytes] [-c Bytes:Bytes] [-h]
32-
33-
-b use bits/s
34-
-B use Bytes/s (default)
35-
-t seconds refresh time (default is 1)
36-
-i interface network interface to monitor. If not specified, check all interfaces.
37-
-w Bytes:Bytes Set warning (color orange) for Rx:Tx bandwidth. (default: none)
38-
-c Bytes:Bytes Set critical (color red) for Rx:Tx bandwidth. (default: none)
39-
-h this help
35+
E.g.
4036

37+
```ini
38+
[bandwidth]
39+
label=
40+
command=$SCRIPT_DIR/bandwidth2
41+
interval=persist
42+
markup=pango
43+
WARN_RX=307200
44+
WARN_TX=30720
45+
CRIT_RX=512000
46+
CRIT_TX=51200
4147
```

bandwidth2/bandwidth2.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,35 @@ void display(int const unit, double b, int const warning, int const critical)
9898
int main(int argc, char *argv[])
9999
{
100100
int c, unit = 'B', t = 1;
101-
char *iface = NULL;
101+
char iface[BUFSIZ] = {0};
102102
int warningrx = 0, warningtx = 0, criticalrx = 0, criticaltx = 0;
103+
char *envvar = NULL;
104+
105+
envvar = getenv("USE_BITS");
106+
if (envvar && *envvar == '1')
107+
unit = 'b';
108+
envvar = getenv("USE_BYTES");
109+
if (envvar && *envvar == '1')
110+
unit = 'B';
111+
envvar = getenv("REFRESH_TIME");
112+
if (envvar)
113+
t = atoi(envvar);
114+
envvar = getenv("INTERFACE");
115+
if (envvar)
116+
snprintf(iface, BUFSIZ, "%s:", envvar);
117+
envvar = getenv("WARN_RX");
118+
if (envvar)
119+
warningrx = atoi(envvar);
120+
envvar = getenv("WARN_TX");
121+
if (envvar)
122+
warningtx = atoi(envvar);
123+
envvar = getenv("CRIT_RX");
124+
if (envvar)
125+
criticalrx = atoi(envvar);
126+
envvar = getenv("CRIT_TX");
127+
if (envvar)
128+
criticaltx = atoi(envvar);
129+
103130
while (c = getopt(argc, argv, "bBht:i:w:c:"), c != -1) {
104131
switch (c) {
105132
case 'b':
@@ -110,7 +137,7 @@ int main(int argc, char *argv[])
110137
t = atoi(optarg);
111138
break;
112139
case 'i':
113-
iface = strcat(optarg, ":");
140+
snprintf(iface, BUFSIZ, "%s:", optarg);
114141
break;
115142
case 'w':
116143
sscanf(optarg, "%d:%d", &warningrx, &warningtx);
@@ -132,7 +159,7 @@ int main(int argc, char *argv[])
132159

133160
while (1) {
134161
sleep(t);
135-
get_values(iface, &s, &received, &sent);
162+
get_values(iface[0] ? iface : NULL, &s, &received, &sent);
136163

137164
rx = (received - received_old) / (float)(s - s_old);
138165
tx = (sent - sent_old) / (float)(s - s_old);

0 commit comments

Comments
 (0)