0
|
1 import logging
|
3
|
2 import os
|
0
|
3 import socket
|
|
4 import sys
|
3
|
5
|
|
6 import background_loop
|
|
7 import daemonocle
|
|
8 import psutil
|
|
9 import uvicorn
|
|
10 from prometheus_client import Gauge
|
|
11 from starlette.applications import Starlette
|
|
12 from starlette.requests import Request
|
|
13 from starlette.responses import HTMLResponse
|
|
14 from starlette.routing import Route
|
|
15 from starlette.staticfiles import StaticFiles
|
|
16 from starlette_exporter import PrometheusMiddleware, handle_metrics
|
|
17 ''
|
|
18 import progs_all as progs
|
|
19
|
2
|
20 if psutil.OSX:
|
3
|
21 import current_window_title_osx as current_window_title
|
0
|
22 import idle_osx as idle
|
2
|
23 import power_osx as power
|
3
|
24 import volume_osx as volume
|
2
|
25 elif psutil.LINUX:
|
3
|
26 import current_window_title_linux as current_window_title
|
0
|
27 import idle_linux as idle
|
3
|
28 import power_linux as power
|
1
|
29 import volume_linux as volume
|
0
|
30 else:
|
|
31 raise NotImplementedError(repr(sys.implementation))
|
|
32
|
3
|
33 DEBUG = os.environ.get('RACC_DEBUG', False)
|
0
|
34 logging.basicConfig(level=logging.INFO)
|
|
35 log = logging.getLogger()
|
3
|
36 hostname = socket.gethostname().split('.')[0]
|
0
|
37 RACC_RUNNING = Gauge("racc_running", "program is running", ['host', 'prog'])
|
|
38 RACC_IDLE = Gauge("racc_idle", "desktop mouse/kb idle seconds", ['host'])
|
3
|
39 RACC_SCREEN = Gauge("racc_screen_on", "screen in unlocked/on mode", ['host'])
|
|
40 RACC_CURRENT_WINDOW = Gauge("racc_current_window",
|
|
41 "label carries title; site is last part",
|
|
42 ['host', 'title', 'site'])
|
0
|
43
|
|
44
|
3
|
45 def update(first_run):
|
|
46 try:
|
|
47 for p, val in progs.get_running_progs().items():
|
|
48 RACC_RUNNING.labels(host=hostname, prog=p).set(val)
|
|
49 except Exception:
|
|
50 if DEBUG: raise
|
|
51
|
|
52 try:
|
|
53 RACC_IDLE.labels(host=hostname).set(idle.get_idle_seconds())
|
|
54 except Exception:
|
|
55 if DEBUG: raise
|
|
56
|
|
57 try:
|
|
58 RACC_SCREEN.labels(host=hostname).set(power.is_screen_on())
|
|
59 except Exception:
|
|
60 if DEBUG: raise
|
0
|
61
|
3
|
62 try:
|
|
63 title = current_window_title.get_current_window_title()
|
|
64 # chrome, at least on osx, adds icon to window title when it's playing audio
|
|
65 title = title.rstrip('🔊').strip()
|
|
66 # some websites choose to title like '<something> - <site name>'
|
|
67 last_section = title.split(' - ')[-1].strip()
|
|
68 if last_section in {'YouTube', 'Google Search', 'Google Chrome', 'Visual Studio Code', 'Roblox'}:
|
|
69 site = last_section.lower().replace(' ', '_')
|
|
70 else:
|
|
71 site = ''
|
|
72 RACC_CURRENT_WINDOW.clear()
|
|
73 RACC_CURRENT_WINDOW.labels(host=hostname, title=title,
|
|
74 site=site).set(1)
|
|
75 except Exception:
|
|
76 if DEBUG: raise
|
|
77
|
0
|
78
|
1
|
79 async def root(req: Request) -> HTMLResponse:
|
|
80 vol = await volume.get_volume()
|
|
81 return HTMLResponse(f'''controls for {hostname} whose volume is {vol}''')
|
0
|
82
|
|
83
|
|
84 def main():
|
|
85 app = Starlette(debug=True,
|
|
86 routes=[
|
|
87 Route('/', root),
|
|
88 ],
|
|
89 on_startup=[
|
3
|
90 lambda: background_loop.loop_forever(update, 5),
|
0
|
91 ])
|
|
92
|
|
93 app.add_middleware(PrometheusMiddleware, app_name='racc')
|
|
94 app.add_route("/metrics", handle_metrics)
|
3
|
95 uvicorn.run(app, host='0.0.0.0', port=5150)
|
0
|
96
|
|
97
|
|
98 if __name__ == "__main__":
|
3
|
99 d = daemonocle.Daemon(
|
|
100 worker=main,
|
|
101 work_dir='.',
|
|
102 pid_file='/tmp/racc.pid',
|
|
103 detach=True,
|
|
104 )
|
|
105 d.stop(timeout=1, force=True)
|
|
106 if DEBUG:
|
|
107 main()
|
|
108 else:
|
|
109 d.cli()
|