-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstatusline-i3.py
398 lines (342 loc) · 11 KB
/
statusline-i3.py
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/usr/bin/env python
# file: statusline-i3.py
# vim:fileencoding=utf-8:ft=python
#
# Copyright © 2019 R.F. Smith <[email protected]>.
# SPDX-License-Identifier: MIT
# Created: 2019-06-30T22:23:11+0200
# Last modified: 2024-06-08T16:17:35+0200
"""
Generate a status line for i3 on FreeBSD.
"""
import argparse
import ctypes
import ctypes.util
import functools as ft
import logging
from logging.handlers import SysLogHandler
import mmap
import os
import statistics as stat
import struct
import sys
import time
import traceback
# Global data
__version__ = "2024.06.08"
libc = ctypes.CDLL(ctypes.util.find_library("c"), use_errno=True)
PAGESIZE = 0
def main():
"""
Entry point for statusline-i3.py
"""
global PAGESIZE
PAGESIZE = sysctlbyname("hw.pagesize", convert=to_int)
args = setup()
mailboxes = {name: {} for name in args.mailbox.split(":")}
cpudata = {}
netdata = {}
items = [
ft.partial(network, storage=netdata),
ft.partial(mail, mailboxes=mailboxes),
memory,
ft.partial(cpu, storage=cpudata),
date,
]
if hasbattery():
items.insert(-1, battery)
logging.info("starting")
sys.stdout.reconfigure(line_buffering=True) # Flush every line.
rv = 0
# Run
try:
while True:
start = time.monotonic()
print(" | ".join(item() for item in items))
end = time.monotonic()
delta = end - start
if delta < 1:
time.sleep(1 - delta)
except Exception:
# Occasionally, statusline-i3 dies, and I don't know why.
# This should catch what happens next time. :-)
logging.error("caught exception: " + traceback.format_exc())
rv = 2
except KeyboardInterrupt:
# This is mainly for when testing from the command-line.
logging.info("caught KeyboardInterrupt; exiting")
return rv
def setup():
"""Configure logging, process command-line arguments."""
syslog = SysLogHandler(address="/var/run/log", facility=SysLogHandler.LOG_LOCAL3)
pid = os.getpid()
syslog.ident = f"statusline-i3[{pid}]: "
logging.basicConfig(
level="INFO", format="%(levelname)s: %(message)s", handlers=(syslog,)
)
setproctitle(b"statusline-i3")
opts = argparse.ArgumentParser(prog="open", description=__doc__)
opts.add_argument("-v", "--version", action="version", version=__version__)
opts.add_argument(
"-m",
"--mailbox",
type=str,
default=os.environ["MAIL"],
help="Location of the mailboxes. One or more mailbox names separated by ‘:’",
)
return opts.parse_args(sys.argv[1:])
# Low level functions.
def to_int(value):
"""Convert binary sysctl value to integer."""
return int.from_bytes(value, byteorder="little")
def to_degC(value):
"""Convert binary sysctl value to degree Centigrade."""
return round(int.from_bytes(value, byteorder="little") / 10 - 273.15, 1)
def sysctlbyname(name, buflen=4, convert=None):
"""
Python wrapper for sysctlbyname(3) on FreeBSD.
Arguments:
name (str): Name of the sysctl to query
buflen (int): Length of the data buffer to use.
convert: Optional function to convert the data.
Returns:
The requested binary data, converted if desired.
"""
name_in = ctypes.c_char_p(bytes(name, encoding="ascii"))
oldlen = ctypes.c_size_t(buflen)
oldlenp = ctypes.byref(oldlen)
oldp = ctypes.create_string_buffer(buflen)
rv = libc.sysctlbyname(name_in, oldp, oldlenp, None, ctypes.c_size_t(0))
if rv != 0:
errno = ctypes.get_errno()
raise ValueError(f"sysctlbyname error: {errno}")
if convert:
return convert(oldp.raw[:buflen])
return oldp.raw[:buflen]
def sysctl(name, buflen=4, convert=None):
"""
Python wrapper for sysctl(3) on FreeBSD.
Arguments:
name: list or tuple of integers.
buflen (int): Length of the data buffer to use.
convert: Optional function to convert the data.
Returns:
The requested binary data, converted if desired.
"""
cnt = len(name)
mib = ctypes.c_int * cnt
name_in = mib(*name)
oldlen = ctypes.c_size_t(buflen)
oldlenp = ctypes.byref(oldlen)
oldp = ctypes.create_string_buffer(buflen)
rv = libc.sysctl(
ctypes.byref(name_in),
ctypes.c_uint(cnt),
oldp,
oldlenp,
None,
ctypes.c_size_t(0),
)
if rv != 0:
errno = ctypes.get_errno()
raise ValueError(f"sysctl error: {errno}")
if convert:
return convert(oldp.raw[:buflen])
return oldp.raw[:buflen]
def setproctitle(name):
"""
Change the name of the process
Arguments:
name (bytes): the new name for the process.
"""
fmt = ctypes.c_char_p(b"-%s")
value = ctypes.c_char_p(name)
libc.setproctitle(fmt, value)
# Helper functions.
def fmt(nbytes):
"""Format network byte amounts."""
nbytes = int(nbytes)
if nbytes >= 1000000:
nbytes /= 1000000
return f"{nbytes:.1f}MB"
if nbytes > 1000:
nbytes /= 1000
return f"{nbytes:.1f}kB"
return f"{nbytes}B"
def readmbox(mboxname, storage):
"""
Report unread mail.
Arguments:
mboxname (str): name of the mailbox to read.
storage: a dict with keys (unread, time, size) from the previous call
or an empty dict. This dict will be *modified* by this function.
Returns: The number of unread messages in this mailbox.
"""
stats = os.stat(mboxname)
# When mutt modifies the mailbox, it seems to only change the
# ctime, not the mtime! This is probably releated to how mutt saves the
# file. See also stat(2).
newtime = stats.st_ctime
newsize = stats.st_size
if stats.st_size == 0:
storage["unread"] = 0
storage["time"] = newtime
storage["size"] = 0
return 0
if not storage or newtime > storage["time"] or newsize != storage["size"]:
with open(mboxname) as mbox:
with mmap.mmap(mbox.fileno(), 0, prot=mmap.PROT_READ) as mm:
start, total = (
0,
1,
) # First mail is not found; it starts on first line...
while True:
rv = mm.find(b"\n\nFrom ", start)
if rv == -1:
break
else:
total += 1
start = rv + 7
start, read = 0, 0
while True:
rv = mm.find(b"\nStatus: R", start)
if rv == -1:
break
else:
read += 1
start = rv + 10
unread = total - read
if unread < 0:
unread = 0
# Save values for the next run.
storage["unread"], storage["time"], storage["size"] = unread, newtime, newsize
else:
unread = storage["unread"]
return unread
def hasbattery():
"""Checks if a battery is present according to ACPI."""
bat = False
try:
if sysctlbyname("hw.acpi.battery.units", convert=to_int) > 0:
bat = True
except ValueError:
pass
return bat
# Functions for generating the items.
def network(storage):
"""
Report on bytes in/out for the network interfaces.
Arguments:
storage: A dict of {interface: (inbytes, outbytes, time)} or an empty dict.
This dict will be *modified* by this function.
Returns:
A string to display.
"""
cnt = sysctlbyname("net.link.generic.system.ifcount", convert=to_int)
items = []
for n in range(1, cnt + 1):
tm = time.monotonic()
data = sysctl([4, 18, 0, 2, n, 1], buflen=208)
name = data[:16].strip(b"\x00").decode("ascii")
if name.startswith("lo"):
continue
ibytes = to_int(data[120:128])
obytes = to_int(data[128:136])
if storage and name in storage:
dt = tm - storage[name][2]
d_in = fmt((ibytes - storage[name][0]) / dt)
d_out = fmt((obytes - storage[name][1]) / dt)
items.append(f"{name}: {d_in}/{d_out}")
else:
items.append(f"{name}: 0B/0B")
# Save values for the next run.
storage[name] = (ibytes, obytes, tm)
return " ".join(items)
def mail(mailboxes):
"""
Report unread mail.
Arguments:
mailboxes: a dict of mailbox info with the paths as the keys.
Returns: A string to display.
"""
unread = 0
for k, v in mailboxes.items():
unread += readmbox(k, v)
return f"Mail: {unread}"
def memory():
"""
Report on the RAM usage on FreeBSD.
Returns: a string to display.
"""
suffixes = ("page_count", "free_count", "inactive_count", "cache_count")
stats = {
suffix: sysctlbyname(f"vm.stats.vm.v_{suffix}", convert=to_int)
for suffix in suffixes
}
try:
# For systems with ZFS, count the size of the ARC as inactive.
arcsize = (
sysctlbyname("kstat.zfs.misc.arcstats.size", buflen=8, convert=to_int)
// PAGESIZE
)
except ValueError:
arcsize = 0
memmax = stats["page_count"]
mem = (
memmax
- stats["free_count"]
- (stats["inactive_count"] + arcsize)
- stats["cache_count"]
)
usedmem = int(100 * mem / memmax)
return f"RAM: {usedmem}%"
def cpu(storage):
"""
Report the CPU usage and temperature.
Argument:
storage: A dict with keys (used, total) from the previous run or an empty dict.
This dict will be *modified* by this function.
Returns:
A string to display.
"""
temps = [
sysctlbyname(f"dev.cpu.{n}.temperature", convert=to_degC) for n in range(4)
]
T = round(stat.mean(temps))
resbuf = sysctlbyname("kern.cp_time", buflen=40)
states = struct.unpack("5L", resbuf)
# According to /usr/include/sys/resource.h, these are:
# USER, NICE, SYS, INT, IDLE
total = sum(states)
used = total - states[-1]
if storage:
prevused, prevtotal = storage["used"], storage["total"]
if total != prevtotal:
frac = int((used - prevused) / (total - prevtotal) * 100)
else: # divide by 0!
frac = "?"
else:
frac = 0
# Save values for the next run.
storage["used"], storage["total"] = used, total
return f"CPU: {frac}%, {T}°C"
def battery():
"""Return battery condition as a string."""
# Battery states acc. to /usr/src/sys/dev/acpica/acpiio.h
lookup = {
0: "on AC",
1: "discharging",
2: "charging",
3: "invalid",
4: "CRITICAL!",
7: "unknown",
}
idx = sysctlbyname("hw.acpi.battery.state", convert=to_int)
state = lookup[idx]
percent = sysctlbyname("hw.acpi.battery.life", convert=to_int)
return f"Bat: {percent}% ({state})"
def date():
"""Return the date as a string."""
return time.strftime("%a %Y-%m-%d %H:%M:%S")
if __name__ == "__main__":
main()