forked from schachmat/gods
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gods.go
242 lines (214 loc) · 5.84 KB
/
gods.go
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
// This programm collects some system information, formats it nicely and sets
// the X root windows name so it can be displayed in the dwm status bar.
//
// The strange characters in the output are used by dwm to colorize the output
// ( to , needs the http://dwm.suckless.org/patches/statuscolors patch) and
// as Icons or separators (e.g. "Ý"). If you don't use the status-18 font
// (https://github.com/schachmat/status-18), you should probably exchange them
// by something else ("CPU", "MEM", "|" for separators, …).
//
// For license information see the file LICENSE
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"time"
)
const (
bpsSign = "b/s"
kibpsSign = "Kb/s"
mibpsSign = "Mb/s"
unpluggedSign = "BAT"
pluggedSign = "CHR BAT"
cpuSign = "CPU"
memSign = "MEM"
netReceivedSign = "in:"
netTransmittedSign = "out:"
floatSeparator = "."
dateSeparator = "-"
fieldSeparator = " | "
)
var (
netDevs = map[string]struct{}{
"eth0:": {},
"eth1:": {},
"wlan0:": {},
"wlp3s0:": {},
}
cores = runtime.NumCPU() // count of cores to scale cpu usage
rxOld = 0
txOld = 0
)
// fixed builds a fixed width string with given pre- and fitting suffix
func fixed(pre string, rate int) string {
if rate < 0 {
return pre + " ERR"
}
var decDigit = 0
var suf = bpsSign // default: display as B/s
switch {
case rate >= (1000 * 1024 * 1024): // > 999 MiB/s
return pre + " ERR"
case rate >= (1000 * 1024): // display as MiB/s
decDigit = (rate / 1024 / 102) % 10
rate /= (1024 * 1024)
suf = mibpsSign
case rate >= 1000: // display as KiB/s
decDigit = (rate / 102) % 10
rate /= 1024
suf = kibpsSign
}
var formated = ""
if rate >= 100 {
formated = fmt.Sprintf("%3d", rate)
} else if rate >= 10 {
formated = fmt.Sprintf("%2d.%1d", rate, decDigit)
} else {
formated = fmt.Sprintf(" %1d.%1d", rate, decDigit)
}
return pre + strings.Replace(formated, ".", floatSeparator, 1) + suf
}
// updateNetUse reads current transfer rates of certain network interfaces
func updateNetUse() string {
file, err := os.Open("/proc/net/dev")
if err != nil {
return netReceivedSign + " ERR " + netTransmittedSign + " ERR"
}
defer file.Close()
var void = 0 // target for unused values
var dev, rx, tx, rxNow, txNow = "", 0, 0, 0, 0
var scanner = bufio.NewScanner(file)
for scanner.Scan() {
_, err = fmt.Sscanf(scanner.Text(), "%s %d %d %d %d %d %d %d %d %d",
&dev, &rx, &void, &void, &void, &void, &void, &void, &void, &tx)
if _, ok := netDevs[dev]; ok {
rxNow += rx
txNow += tx
}
}
defer func() { rxOld, txOld = rxNow, txNow }()
return fmt.Sprintf("%s %s", fixed(netReceivedSign, rxNow-rxOld), fixed(netTransmittedSign, txNow-txOld))
}
// colored surrounds the percentage with color escapes if it is >= 70
func colored(icon string, percentage int) string {
return fmt.Sprintf("%s%3d", icon, percentage)
}
// updatePower reads the current battery and power plug status
func updatePower() string {
const powerSupply = "/sys/class/power_supply/"
var enFull, enNow, enPerc int = 0, 0, 0
var plugged, err = ioutil.ReadFile(powerSupply + "AC/online")
if err != nil {
return "ÏERR"
}
batts, err := ioutil.ReadDir(powerSupply)
if err != nil {
return "ÏERR"
}
readval := func(name, field string) int {
var path = powerSupply + name + "/"
var file []byte
if tmp, err := ioutil.ReadFile(path + "energy_" + field); err == nil {
file = tmp
} else if tmp, err := ioutil.ReadFile(path + "charge_" + field); err == nil {
file = tmp
} else {
return 0
}
if ret, err := strconv.Atoi(strings.TrimSpace(string(file))); err == nil {
return ret
}
return 0
}
for _, batt := range batts {
name := batt.Name()
if !strings.HasPrefix(name, "BAT") {
continue
}
enFull += readval(name, "full")
enNow += readval(name, "now")
}
if enFull == 0 { // Battery found but no readable full file.
return "ÏERR"
}
enPerc = enNow * 100 / enFull
var icon = unpluggedSign
if string(plugged) == "1\n" {
icon = pluggedSign
}
if enPerc <= 5 {
return fmt.Sprintf("%s%3d", icon, enPerc)
} else if enPerc <= 10 {
return fmt.Sprintf("%s%3d", icon, enPerc)
}
return fmt.Sprintf("%s%3d", icon, enPerc)
}
// updateCPUUse reads the last minute sysload and scales it to the core count
func updateCPUUse() string {
var load float32
var loadavg, err = ioutil.ReadFile("/proc/loadavg")
if err != nil {
return cpuSign + "ERR"
}
_, err = fmt.Sscanf(string(loadavg), "%f", &load)
if err != nil {
return cpuSign + "ERR"
}
return colored(cpuSign, int(load*100.0/float32(cores)))
}
// updateMemUse reads the memory used by applications and scales to [0, 100]
func updateMemUse() string {
var file, err = os.Open("/proc/meminfo")
if err != nil {
return memSign + "ERR"
}
defer file.Close()
// done must equal the flag combination (0001 | 0010 | 0100 | 1000) = 15
var total, used, done = 0, 0, 0
for info := bufio.NewScanner(file); done != 15 && info.Scan(); {
var prop, val = "", 0
if _, err = fmt.Sscanf(info.Text(), "%s %d", &prop, &val); err != nil {
return memSign + "ERR"
}
switch prop {
case "MemTotal:":
total = val
used += val
done |= 1
case "MemFree:":
used -= val
done |= 2
case "Buffers:":
used -= val
done |= 4
case "Cached:":
used -= val
done |= 8
}
}
return colored(memSign, used*100/total)
}
// main updates the dwm statusbar every second
func main() {
for {
var status = []string{
"",
updateNetUse(),
updateCPUUse(),
updateMemUse(),
updatePower(),
time.Now().In(time.UTC).Format("Mon 02 " + dateSeparator + " 15:04:05"),
time.Now().Local().Format("Mon 02 " + dateSeparator + " 15:04:05"),
}
exec.Command("xsetroot", "-name", strings.Join(status, fieldSeparator)).Run()
// sleep until beginning of next second
var now = time.Now()
time.Sleep(now.Truncate(time.Second).Add(time.Second).Sub(now))
}
}