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
|
"""
Sopel plugin: server stats
Commands (owner only) - use !server or !srv:
!server - show this usage
!server uptime - uptime and load averages
!server disk - disk usage for /
!server mem - RAM and swap usage
!server load - 1/5/15 min load + process count
!server status [service] - rc-service status (all or one)
!server net [iface] - RX/TX for iface (default: first non-lo)
!server top [n] - top n procs by CPU (default 5)
!server topmem [n] - top n procs by memory (default 5)
!server io - disk read/write stats for main block device
!server conns - TCP connection summary
!server who - logged-in users
!server logs <service> [n] - last n lines of service log (default 10)
"""
import os
import subprocess
from sopel import plugin
import os as _os, sys as _sys
_alfred_dir = _os.path.dirname(_os.path.abspath(__file__))
if _alfred_dir not in _sys.path:
_sys.path.insert(0, _alfred_dir)
import helpstrings as h
SERVICES = [
"nginx", "postfix", "dovecot", "ngircd", "soju", "websocat",
"opendkim", "fcgiwrap", "alfred", "dcron", "0x0",
"xandikos", "wg-quick.wg0",
]
LOG_PATHS = {
"nginx": ["/var/log/nginx/error.log", "/var/log/nginx/access.log"],
"postfix": ["/var/log/mail.log", "/var/log/messages"],
"dovecot": ["/var/log/dovecot.log", "/var/log/messages"],
"ngircd": ["/var/log/ngircd.log", "/var/log/messages"],
"soju": ["/var/log/soju.log"],
"websocat": ["/var/log/websocat/daemon.log"],
"0x0": ["/var/log/0x0/daemon.log", "/var/log/0x0/prune.log"],
"xandikos": ["/var/log/xandikos/daemon.log"],
"alfred": ["/var/log/alfred/sopel.log", "/var/log/alfred/daemon.log"],
"herald": ["/var/log/herald/sopel.log", "/var/log/herald/daemon.log"],
"bikabot": ["/var/log/bikabot/sopel.log", "/var/log/bikabot/daemon.log"],
"daft": ["/var/log/daft/sopel.log", "/var/log/daft/daemon.log"],
"daffy": ["/var/log/daffy/sopel.log", "/var/log/daffy/daemon.log"],
"contessa": ["/var/log/contessa/sopel.log", "/var/log/contessa/daemon.log"],
"osterman": ["/var/log/osterman/sopel.log", "/var/log/osterman/daemon.log"],
"salvador": ["/var/log/salvador/sopel.log", "/var/log/salvador/daemon.log"],
"shireen": ["/var/log/shireen/sopel.log", "/var/log/shireen/daemon.log"],
"jeeves": ["/var/log/jeeves/sopel.log", "/var/log/jeeves/daemon.log"],
}
def _is_owner(bot, trigger):
return trigger.nick == bot.settings.core.owner
def _run(cmd, timeout=10):
try:
r = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout)
out = (r.stdout + r.stderr).strip()
return out[:400] if out else "(no output)"
except subprocess.TimeoutExpired:
return "(timed out)"
except Exception as e:
return f"(error: {e})"
def _uptime(bot, _args):
bot.say(_run(["uptime"]))
def _disk(bot, _args):
for line in _run(["df", "-h", "/"]).splitlines():
bot.say(line)
def _mem(bot, _args):
for line in _run(["free", "-h"]).splitlines():
bot.say(line)
def _load(bot, _args):
try:
with open("/proc/loadavg") as f:
fields = f.read().split()
load = f"{fields[0]} {fields[1]} {fields[2]}"
procs = fields[3]
bot.say(f"load: {load} | procs: {procs}")
except OSError as e:
bot.say(f"error: {e}")
def _status(bot, args):
targets = [args[0]] if args else SERVICES
results = []
for svc in targets:
r = subprocess.run(
["rc-service", svc, "status"],
capture_output=True, text=True, timeout=5
)
out = (r.stdout + r.stderr).strip()
state = "UP" if "started" in out else "DOWN" if "stopped" in out else out[:20]
results.append(f"{svc}:{state}")
bot.say(" | ".join(results))
def _net(bot, args):
iface = args[0] if args else None
try:
with open("/proc/net/dev") as f:
lines = f.readlines()[2:]
devs = {}
for line in lines:
parts = line.split()
name = parts[0].rstrip(":")
if name == "lo":
continue
devs[name] = {"rx": int(parts[1]), "tx": int(parts[9])}
if not devs:
bot.say("net: no interfaces found")
return
target = iface if iface in devs else next(iter(devs))
if iface and iface not in devs:
bot.say(f"net: unknown iface '{iface}', using {target}")
def fmt(n):
for unit in ("B", "KB", "MB", "GB"):
if n < 1024:
return f"{n:.1f}{unit}"
n /= 1024
return f"{n:.1f}TB"
d = devs[target]
bot.say(f"{target}: RX {fmt(d['rx'])} | TX {fmt(d['tx'])}")
except OSError as e:
bot.say(f"error: {e}")
def _top_procs(bot, args, sort_col, label):
try:
n = int(args[0]) if args else 5
n = max(1, min(n, 15))
except ValueError:
bot.say("usage: !srv top [n]")
return
r = subprocess.run(["top", "-b", "-n", "1"], capture_output=True, text=True)
lines = r.stdout.strip().splitlines()
proc_lines = []
in_procs = False
for line in lines:
if not in_procs:
if "PID" in line and "PPID" in line:
in_procs = True
continue
if line.strip():
proc_lines.append(line)
if not proc_lines:
bot.say("no processes found")
return
# columns: PID PPID USER STAT VSZ %VSZ CPU %CPU COMMAND
procs = []
for line in proc_lines:
parts = line.split(None, 8)
if len(parts) < 9:
continue
try:
val = float(parts[sort_col].rstrip("%m"))
user = parts[2]
cmd = parts[8].split()[0].split("/")[-1][:25]
procs.append((val, user, cmd))
except (ValueError, IndexError):
continue
procs.sort(reverse=True)
out = " | ".join(f"{cmd}({user},{v:.1f}%)" for v, user, cmd in procs[:n])
bot.say(f"{label}: {out}" if out else f"{label}: (no data)")
def _top(bot, args):
_top_procs(bot, args, sort_col=7, label="cpu")
def _topmem(bot, args):
_top_procs(bot, args, sort_col=5, label="mem")
def _io(bot, _args):
try:
with open("/proc/diskstats") as f:
lines = f.readlines()
for line in lines:
parts = line.split()
if len(parts) < 14:
continue
name = parts[2]
if name.startswith(("sd", "vd", "nvme", "hd", "mmcblk")) and not name[-1].isdigit():
reads = int(parts[5])
writes = int(parts[9])
bot.say(f"{name}: reads {reads} | writes {writes}")
return
bot.say("io: no block device found")
except OSError as e:
bot.say(f"error: {e}")
def _conns(bot, _args):
for line in _run(["ss", "-s"]).splitlines():
bot.say(line)
def _who(bot, _args):
out = _run(["who"])
if not out.strip() or out == "(no output)":
bot.say("who: no users logged in")
return
for line in out.splitlines():
bot.say(line)
def _logs(bot, args):
if not args:
bot.say("usage: !srv logs <service> [n]")
return
svc = args[0]
try:
n = int(args[1]) if len(args) > 1 else 10
n = max(1, min(n, 30))
except ValueError:
bot.say("usage: !srv logs <service> [n]")
return
candidates = LOG_PATHS.get(svc, [f"/var/log/{svc}.log", f"/var/log/{svc}/error.log"])
for path in candidates:
if os.path.exists(path):
out = _run(["tail", "-n", str(n), path])
for line in out.splitlines()[:n]:
bot.say(line)
return
bot.say(f"logs: no log file found for '{svc}'")
_SUBCOMMANDS = {
"uptime": _uptime,
"disk": _disk,
"mem": _mem,
"load": _load,
"status": _status,
"net": _net,
"top": _top,
"topmem": _topmem,
"io": _io,
"conns": _conns,
"who": _who,
"logs": _logs,
}
@plugin.commands("server", "srv")
def cmd_server(bot, trigger):
if not _is_owner(bot, trigger):
return
args = (trigger.group(2) or "").split()
sub = args[0].lower() if args else ""
if not sub:
bot.say(h.SRV_TOPIC)
return
fn = _SUBCOMMANDS.get(sub)
if fn:
fn(bot, args[1:])
else:
bot.say(f"server: unknown subcommand '{sub}' - type !srv for usage")
|