-
Notifications
You must be signed in to change notification settings - Fork 0
/
raspotify-monitor.py
75 lines (58 loc) · 1.8 KB
/
raspotify-monitor.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
from flask import Flask, redirect
from jinja2 import Template
from subprocess import getoutput
import re
app = Flask(__name__)
status_template = Template(open('templates/status.html', 'r').read())
def refresh_status():
stdout = getoutput("sudo netstat -anpt | awk '((/LISTEN/||/ESTABLISHED/)&&/librespot/)||/Program/{;print $0}'")
stati = {'run': False, 'listen': False, 'established': False, 'statuslines': []}
if re.search(r"(LISTEN\s+\d+\/librespot)", stdout, re.MULTILINE):
stati['listen'] = True
if re.search(r"(ESTABLISHED\s+\d+\/librespot)", stdout, re.MULTILINE):
stati['established'] = True
stdout2 = getoutput("sudo service raspotify status")
stati['statuslines'] = stdout2.split('\n')
if re.search(r"(Active:\sactive\s\(running\))", stdout2, re.MULTILINE):
stati['run'] = True
return stati
def control_service(task):
stdout = getoutput("sudo service raspotify " + task)
return stdout
@app.route('/')
def home():
return redirect("/status", code=301)
@app.route('/status')
def status():
print("STATUS CALLED")
stati = refresh_status()
return status_template.render(
listenstatus=stati['listen'],
establishedstatus=stati['established'],
runstatus=stati['run'],
statuslines=stati['statuslines'][:5],
refreshtime=7
)
@app.route('/restart')
def restart():
output = control_service('restart')
if output == '':
return 'OK'
else:
return 'ERROR'
@app.route('/start')
def start():
output = control_service('start')
if output == '':
return 'OK'
else:
return 'ERROR'
@app.route('/stop')
def stop():
output = control_service('stop')
if output == '':
return 'OK'
else:
return 'ERROR'
if __name__ == '__main__':
app.run()