Moved report functions into osTicket object
This commit is contained in:
parent
57572c7527
commit
ad6980f82b
1 changed files with 105 additions and 97 deletions
|
|
@ -49,6 +49,105 @@ class osTicket():
|
||||||
# Connection established
|
# Connection established
|
||||||
break
|
break
|
||||||
|
|
||||||
|
def convert_report(name, test):
|
||||||
|
"""Convert report into an osTicket friendly format, returns list."""
|
||||||
|
out_report = []
|
||||||
|
source_report = test.source_report
|
||||||
|
status = strip_colors(test.status)
|
||||||
|
status = status.replace(test.label, '').strip()
|
||||||
|
|
||||||
|
# Header
|
||||||
|
index = 1
|
||||||
|
if name == 'NVMe / SMART':
|
||||||
|
out_report.append('{} ({})'.format(name, status))
|
||||||
|
if not source_report:
|
||||||
|
index = 0
|
||||||
|
source_report = test.dev.generate_attribute_source_report()
|
||||||
|
else:
|
||||||
|
out_report.append('{} ({})'.format(strip_colors(source_report[0]), status))
|
||||||
|
|
||||||
|
# Body
|
||||||
|
for line in source_report[index:]:
|
||||||
|
# Remove colors and leading spaces
|
||||||
|
line = strip_colors(line)
|
||||||
|
if line[:2] == ' ':
|
||||||
|
line = line[2:]
|
||||||
|
|
||||||
|
# Test-specific modifications
|
||||||
|
if name == 'Prime95':
|
||||||
|
r = REGEX_TEMPS.match(line)
|
||||||
|
if r:
|
||||||
|
_sensor = '{:<20}'.format(r.group(1))
|
||||||
|
_temps = r.group(2)
|
||||||
|
line = '{} {}'.format(
|
||||||
|
pad_with_dots(_sensor, pad_right=True),
|
||||||
|
_temps)
|
||||||
|
elif name == 'NVMe / SMART':
|
||||||
|
r = REGEX_NVME_SMART_ATTRIBUTES.match(line)
|
||||||
|
if r:
|
||||||
|
_dec = '{:>3}'.format(r.group(1))
|
||||||
|
_hex = r.group(2)
|
||||||
|
_atr = r.group(3).strip()
|
||||||
|
_val = '{:<20}'.format(r.group(4))
|
||||||
|
line = '{}/{}: {} {}'.format(
|
||||||
|
_hex,
|
||||||
|
pad_with_dots(_dec),
|
||||||
|
pad_with_dots(_val, pad_right=True),
|
||||||
|
_atr)
|
||||||
|
elif name == 'I/O Benchmark':
|
||||||
|
line = REGEX_BLOCK_GRAPH.sub('', line)
|
||||||
|
line = line.strip()
|
||||||
|
if not line:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Remove extra spaces
|
||||||
|
line = line.strip()
|
||||||
|
line = re.sub(r'(\s+)', ' ', line)
|
||||||
|
|
||||||
|
# Add line to report
|
||||||
|
out_report.append(line)
|
||||||
|
|
||||||
|
# Done
|
||||||
|
return out_report
|
||||||
|
|
||||||
|
def generate_report(dev):
|
||||||
|
"""Generate device report for osTicket, returns list."""
|
||||||
|
report = []
|
||||||
|
results = get_device_overall_results(dev)
|
||||||
|
|
||||||
|
# Header
|
||||||
|
if results['Full Diag']:
|
||||||
|
report.append(
|
||||||
|
'{Dev Type} hardware diagnostic tests: {Status}'.format(**results))
|
||||||
|
report.append(' ')
|
||||||
|
|
||||||
|
# Device
|
||||||
|
report.append(dev.description)
|
||||||
|
report.append(' ')
|
||||||
|
|
||||||
|
# Test reports
|
||||||
|
for name, test in dev.tests.items():
|
||||||
|
report.extend(convert_report(name, test))
|
||||||
|
if name == 'I/O Benchmark':
|
||||||
|
# TODO: Create PNG graph and upload to imgur/Nextcloud
|
||||||
|
report.append('Imgur: TODO')
|
||||||
|
report.append('Nextcloud: TODO')
|
||||||
|
report.append(' ')
|
||||||
|
|
||||||
|
# Volumes
|
||||||
|
if results['Dev Type'] == 'Disk':
|
||||||
|
# TODO: Mount all volumes and extend report
|
||||||
|
report.append('Volumes:')
|
||||||
|
report.append('TODO')
|
||||||
|
report.append(' ')
|
||||||
|
|
||||||
|
# Asterisk
|
||||||
|
if results['Asterisk']:
|
||||||
|
report.append('* NOTE: One or more tests were not run on this device')
|
||||||
|
|
||||||
|
# Done
|
||||||
|
return report
|
||||||
|
|
||||||
def disconnect(self, reset_errors=False):
|
def disconnect(self, reset_errors=False):
|
||||||
"""Close osTicket connection."""
|
"""Close osTicket connection."""
|
||||||
try:
|
try:
|
||||||
|
|
@ -114,7 +213,12 @@ class osTicket():
|
||||||
# Done
|
# Done
|
||||||
return ticket_number
|
return ticket_number
|
||||||
|
|
||||||
def post_response(self, ticket_id, response):
|
def post_device_results(self, dev, ticket_id):
|
||||||
|
"""Generate osTicket friendly report and post as response to ticket."""
|
||||||
|
response = self.generate_report(dev)
|
||||||
|
post_response(response, ticket_id)
|
||||||
|
|
||||||
|
def post_response(self, response, ticket_id):
|
||||||
"""Post a reply to a ticket in osTicket."""
|
"""Post a reply to a ticket in osTicket."""
|
||||||
self.connect()
|
self.connect()
|
||||||
|
|
||||||
|
|
@ -176,67 +280,6 @@ class osTicket():
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
|
|
||||||
# Functions
|
# Functions
|
||||||
def convert_report(name, test):
|
|
||||||
"""Convert report into an osTicket friendly format, returns list."""
|
|
||||||
out_report = []
|
|
||||||
source_report = test.source_report
|
|
||||||
status = strip_colors(test.status)
|
|
||||||
status = status.replace(test.label, '').strip()
|
|
||||||
|
|
||||||
# Header
|
|
||||||
index = 1
|
|
||||||
if name == 'NVMe / SMART':
|
|
||||||
out_report.append('{} ({})'.format(name, status))
|
|
||||||
if not source_report:
|
|
||||||
index = 0
|
|
||||||
source_report = test.dev.generate_attribute_source_report()
|
|
||||||
else:
|
|
||||||
out_report.append('{} ({})'.format(strip_colors(source_report[0]), status))
|
|
||||||
|
|
||||||
# Body
|
|
||||||
for line in source_report[index:]:
|
|
||||||
# Remove colors and leading spaces
|
|
||||||
line = strip_colors(line)
|
|
||||||
if line[:2] == ' ':
|
|
||||||
line = line[2:]
|
|
||||||
|
|
||||||
# Test-specific modifications
|
|
||||||
if name == 'Prime95':
|
|
||||||
r = REGEX_TEMPS.match(line)
|
|
||||||
if r:
|
|
||||||
_sensor = '{:<20}'.format(r.group(1))
|
|
||||||
_temps = r.group(2)
|
|
||||||
line = '{} {}'.format(
|
|
||||||
pad_with_dots(_sensor, pad_right=True),
|
|
||||||
_temps)
|
|
||||||
elif name == 'NVMe / SMART':
|
|
||||||
r = REGEX_NVME_SMART_ATTRIBUTES.match(line)
|
|
||||||
if r:
|
|
||||||
_dec = '{:>3}'.format(r.group(1))
|
|
||||||
_hex = r.group(2)
|
|
||||||
_atr = r.group(3).strip()
|
|
||||||
_val = '{:<20}'.format(r.group(4))
|
|
||||||
line = '{}/{}: {} {}'.format(
|
|
||||||
_hex,
|
|
||||||
pad_with_dots(_dec),
|
|
||||||
pad_with_dots(_val, pad_right=True),
|
|
||||||
_atr)
|
|
||||||
elif name == 'I/O Benchmark':
|
|
||||||
line = REGEX_BLOCK_GRAPH.sub('', line)
|
|
||||||
line = line.strip()
|
|
||||||
if not line:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Remove extra spaces
|
|
||||||
line = line.strip()
|
|
||||||
line = re.sub(r'(\s+)', ' ', line)
|
|
||||||
|
|
||||||
# Add line to report
|
|
||||||
out_report.append(line)
|
|
||||||
|
|
||||||
# Done
|
|
||||||
return out_report
|
|
||||||
|
|
||||||
def get_device_overall_results(dev):
|
def get_device_overall_results(dev):
|
||||||
"""Get overall results from tests for device, returns dict."""
|
"""Get overall results from tests for device, returns dict."""
|
||||||
results = {
|
results = {
|
||||||
|
|
@ -288,41 +331,6 @@ def get_device_overall_results(dev):
|
||||||
# Done
|
# Done
|
||||||
return results
|
return results
|
||||||
|
|
||||||
def generate_osticket_report(dev):
|
|
||||||
"""Generate device report for osTicket, returns list."""
|
|
||||||
report = []
|
|
||||||
results = get_device_overall_results(dev)
|
|
||||||
|
|
||||||
# Header
|
|
||||||
if results['Full Diag']:
|
|
||||||
report.append(
|
|
||||||
'{Dev Type} hardware diagnostic tests: {Status}'.format(**results))
|
|
||||||
report.append(' ')
|
|
||||||
|
|
||||||
# Device
|
|
||||||
report.append(dev.description)
|
|
||||||
report.append(' ')
|
|
||||||
|
|
||||||
# Test reports
|
|
||||||
for name, test in dev.tests.items():
|
|
||||||
report.extend(convert_report(name, test))
|
|
||||||
if name == 'I/O Benchmark':
|
|
||||||
# TODO: Create PNG graph and upload to imgur/Nextcloud
|
|
||||||
report.append('Imgur: TODO')
|
|
||||||
report.append('Nextcloud: TODO')
|
|
||||||
report.append(' ')
|
|
||||||
|
|
||||||
# Volumes
|
|
||||||
if results['Dev Type'] == 'Disk':
|
|
||||||
# TODO: Mount all volumes and extend report
|
|
||||||
report.append('Volumes:')
|
|
||||||
report.append('TODO')
|
|
||||||
report.append(' ')
|
|
||||||
|
|
||||||
# Asterisk
|
|
||||||
if results['Asterisk']:
|
|
||||||
report.append('* NOTE: One or more tests were not run on this device')
|
|
||||||
|
|
||||||
def pad_with_dots(s, pad_right=False):
|
def pad_with_dots(s, pad_right=False):
|
||||||
"""Replace space padding with dots, returns str."""
|
"""Replace space padding with dots, returns str."""
|
||||||
s = str(s).replace(' ', '..')
|
s = str(s).replace(' ', '..')
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue