0
|
1 from starlette.applications import Starlette
|
|
2 from starlette.exceptions import HTTPException
|
|
3 from starlette.requests import Request
|
|
4 from starlette.responses import JSONResponse, HTMLResponse
|
|
5 from starlette.staticfiles import StaticFiles
|
|
6 from starlette.routing import Route, Mount
|
|
7 from starlette.templating import Jinja2Templates
|
|
8 import uvicorn
|
|
9 import psutil
|
|
10 from starlette_exporter import PrometheusMiddleware, handle_metrics
|
|
11 from prometheus_client import Summary, Gauge
|
|
12 import background_loop
|
|
13 from typing import List, Optional
|
|
14 import logging
|
|
15 import socket
|
|
16 import sys
|
2
|
17 if psutil.OSX:
|
0
|
18 import idle_osx as idle
|
1
|
19 import volume_osx as volume
|
2
|
20 import power_osx as power
|
|
21 elif psutil.LINUX:
|
0
|
22 import idle_linux as idle
|
1
|
23 import volume_linux as volume
|
2
|
24 import power_linux as power
|
0
|
25 else:
|
|
26 raise NotImplementedError(repr(sys.implementation))
|
|
27
|
|
28 hostname = socket.gethostname().split('.')[0]
|
|
29 logging.basicConfig(level=logging.INFO)
|
|
30 log = logging.getLogger()
|
|
31
|
|
32
|
|
33 def progname(cmdline: List[str]) -> Optional[str]:
|
|
34 if len(cmdline) < 1:
|
|
35 return None
|
|
36 if cmdline[-1].endswith('/steam'):
|
|
37 return 'steam'
|
|
38 if cmdline[0].endswith('/minecraft-launcher'):
|
|
39 return 'minecraft-launcher'
|
|
40 if cmdline[0].endswith('/java') and '--versionType' in cmdline:
|
|
41 return 'minecraft'
|
|
42
|
|
43
|
|
44 RACC_RUNNING = Gauge("racc_running", "program is running", ['host', 'prog'])
|
|
45 RACC_IDLE = Gauge("racc_idle", "desktop mouse/kb idle seconds", ['host'])
|
2
|
46 RACC_SCREEN = Gauge("racc_screen", "screen in unlocked/on mode", ['host'])
|
0
|
47
|
|
48
|
|
49 def update_progs(first_run):
|
|
50 out = []
|
|
51 progs = set()
|
|
52 for proc in psutil.process_iter(['pid', 'name']):
|
|
53 try:
|
|
54 prog = progname(proc.cmdline())
|
|
55 if prog:
|
|
56 progs.add(prog)
|
|
57 except (psutil.AccessDenied, psutil.NoSuchProcess):
|
|
58 pass
|
|
59
|
|
60 for p in [
|
|
61 'minecraft',
|
|
62 'minecraft-launcher',
|
|
63 'steam',
|
|
64 ]:
|
|
65 RACC_RUNNING.labels(host=hostname, prog=p).set(p in progs)
|
|
66 RACC_IDLE.labels(host=hostname).set(idle.get_idle_seconds())
|
2
|
67 RACC_SCREEN.labels(host=hostname).set(power.is_screen_on())
|
0
|
68
|
1
|
69 async def root(req: Request) -> HTMLResponse:
|
|
70 vol = await volume.get_volume()
|
|
71 return HTMLResponse(f'''controls for {hostname} whose volume is {vol}''')
|
0
|
72
|
|
73
|
|
74 def main():
|
|
75 app = Starlette(debug=True,
|
|
76 routes=[
|
|
77 Route('/', root),
|
|
78 ],
|
|
79 on_startup=[
|
|
80 lambda: background_loop.loop_forever(update_progs, 3),
|
|
81 ])
|
|
82
|
|
83 app.add_middleware(PrometheusMiddleware, app_name='racc')
|
|
84 app.add_route("/metrics", handle_metrics)
|
|
85 return app
|
|
86
|
|
87
|
|
88 if __name__ == "__main__":
|
|
89 uvicorn.run(main(), host='0.0.0.0', port=5150)
|