-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathquality_report.py
More file actions
140 lines (105 loc) · 4.64 KB
/
quality_report.py
File metadata and controls
140 lines (105 loc) · 4.64 KB
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
#!/usr/bin/env python3
#
# Copyright (c) 2025 Gareth Palmer <gareth.palmer3@gmail.com>
# This program is free software, distributed under the terms of
# the GNU General Public License Version 2.
import re
from urllib.parse import quote_plus
from html import escape
from datetime import datetime
import json
import requests
from lxml import etree
from flask import Blueprint, Response, request, g
import config
blueprint = Blueprint('quality_report', __name__)
REPORT_REASONS = {
'0': 'Audio had echo',
'1': 'Audio had crackling',
'2': 'I could not hear them',
'3': 'They could not hear me',
'4': 'Other issue (unspecified)',
}
@blueprint.route('/quality-report')
def quality_report():
device_name = request.args.get('name', '')
if not re.search(r'(?x) ^ SEP [0-9A-F]{12} $', device_name):
return Response('Invalid device', mimetype = 'text/plain'), 403
xml = ('<?xml version="1.0" encoding="UTF-8"?>\n'
'<CiscoIPPhoneMenu>\n'
' <Title>Quality Report</Title>\n')
for reason in REPORT_REASONS.keys():
xml += (' <MenuItem>\n'
' <Name>' + escape(REPORT_REASONS.get(reason, '')) + '</Name>\n'
' <URL>QueryStringParam:reason=' + quote_plus(reason) + '</URL>\n'
' </MenuItem>\n')
if g.is_79xx:
xml += ' <Prompt>Select reason</Prompt>'
xml += (' <SoftKeyItem>\n'
' <Name>Submit</Name>\n'
' <URL>' + request.url_root + 'quality-report/send?name=' + quote_plus(device_name) + '</URL>\n' +
' <Position>' + ('1' if g.is_79xx else '2') + '</Position>\n'
' </SoftKeyItem>\n'
' <SoftKeyItem>\n'
' <Name>Exit</Name>\n'
' <URL>Init:Services</URL>\n'
' <Position>' + ('3' if g.is_79xx else '1') + '</Position>\n'
' </SoftKeyItem>\n'
'</CiscoIPPhoneMenu>\n')
return Response(xml, mimetype = 'text/xml'), 200
@blueprint.route('/quality-report/send')
def quality_report_send():
device_name = request.args.get('name', '')
if not re.search(r'(?x) ^ SEP [0-9A-F]{12} $', device_name):
return Response('Invalid device', mimetype = 'text/plain'), 403
session = requests.Session()
response = session.get(config.manager_url, timeout = 5, params = {'Action': 'Login',
'Username': config.manager_username,
'Secret': config.manager_secret})
response.raise_for_status()
response = session.get(config.manager_url, timeout = 5, params = {'Action': 'SIPShowPeer',
'DeviceName': device_name})
response.raise_for_status()
document = etree.fromstring(response.content)
element = document.find('response/generic')
if element is not None:
ip_address = element.get('ipaddress')
status = element.get('status')
rtp_rx_stat = element.get('rtprxstat')
rtp_tx_stat = element.get('rtptxstat')
else:
ip_address = None
status = None
rtp_rx_stat = None
rtp_tx_stat = None
response = session.get(config.manager_url, timeout = 5, params = {'Action': 'Logoff'})
response.raise_for_status()
reason = request.args.get('reason', '')
with open(f'{config.reports_dir}/qrt-{device_name}.json', 'a', encoding='utf-8') as file:
file.write(json.dumps({
'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'ip_address': ip_address,
'status': status,
'reason': REPORT_REASONS.get(reason, ''),
'rtp_rx_stat': rtp_rx_stat,
'rtp_tx_stat': rtp_tx_stat,
}) + "\n")
xml = ('<?xml version="1.0" encoding="UTF-8"?>\n'
'<CiscoIPPhoneText>\n'
' <Title>Quality Report</Title>\n'
' <Text>' + escape(REPORT_REASONS.get(reason, '')) + ' has been reported.</Text>\n')
if g.is_79xx:
xml += ' <Prompt>Your current options</Prompt>\n'
xml += (' <SoftKeyItem>\n'
' <Name>Exit</Name>\n'
' <URL>Init:Services</URL>\n'
' <Position>' + ('3' if g.is_79xx else '1') + '</Position>\n'
' </SoftKeyItem>\n'
'</CiscoIPPhoneText>\n')
return Response(xml, mimetype = 'text/xml'), 200
@blueprint.before_request
def before_request():
g.is_79xx = re.search(r'(?x) ^ CP-79', request.headers.get('X-CiscoIPPhoneModelName', ''))
@blueprint.errorhandler(Exception)
def error_handler(error):
return Response(str(error), mimetype = 'text/plain'), 500