diff --git a/src/opnsense/scripts/systemhealth/activity.py b/src/opnsense/scripts/systemhealth/activity.py index 924c0da54..f71d39a91 100755 --- a/src/opnsense/scripts/systemhealth/activity.py +++ b/src/opnsense/scripts/systemhealth/activity.py @@ -39,36 +39,33 @@ if __name__ == '__main__': fieldnames = None field_max_width = dict() result = {'headers': [], 'details': []} - with tempfile.NamedTemporaryFile() as output_stream: - subprocess.call(['/usr/bin/top','-aHSn','999999'], stdout=output_stream, stderr=open(os.devnull, 'wb')) - output_stream.seek(0) - is_header = True - lines = output_stream.read().decode().strip().split('\n') - for line in lines: - # end of header, start of top detection - if line.find('USERNAME') > -1 and line.find('COMMAND') > -1: - is_header = False - if is_header: - # parse headers from top command, add to result - if len(line.strip()) > 0: - result['headers'].append(line) + sp = subprocess.run(['/usr/bin/top','-aHSn','999999'], capture_output=True, text=True) + is_header = True + for line in sp.stdout.strip().split('\n'): + # end of header, start of top detection + if line.find('USERNAME') > -1 and line.find('COMMAND') > -1: + is_header = False + if is_header: + # parse headers from top command, add to result + if len(line.strip()) > 0: + result['headers'].append(line) + else: + # parse details including fieldnames (leave original) + if fieldnames is None: + fieldnames = line.split() else: - # parse details including fieldnames (leave original) - if fieldnames is None: - fieldnames = line.split() - else: - tmp = line.split() - record = dict() - for field_id in range(len(fieldnames)): - fieldname = fieldnames[field_id] - if field_id == len(fieldnames)-1: - record[fieldname] = ' '.join(tmp[field_id:]) - else: - record[fieldname] = tmp[field_id] + tmp = line.split() + record = dict() + for field_id in range(len(fieldnames)): + fieldname = fieldnames[field_id] + if field_id == len(fieldnames)-1: + record[fieldname] = ' '.join(tmp[field_id:]) + else: + record[fieldname] = tmp[field_id] - if fieldname not in field_max_width or field_max_width[fieldname] < len(record[fieldname]): - field_max_width[fieldname] = len(record[fieldname]) - result['details'].append(record) + if fieldname not in field_max_width or field_max_width[fieldname] < len(record[fieldname]): + field_max_width[fieldname] = len(record[fieldname]) + result['details'].append(record) if len(sys.argv) > 1 and sys.argv[1] == 'json': # output as json diff --git a/src/opnsense/scripts/systemhealth/fetchData.py b/src/opnsense/scripts/systemhealth/fetchData.py index 9702d6d02..1c4729da2 100755 --- a/src/opnsense/scripts/systemhealth/fetchData.py +++ b/src/opnsense/scripts/systemhealth/fetchData.py @@ -45,9 +45,5 @@ if len(sys.argv) > 1: # scan rrd directory for requested file for rrdFilename in glob.glob('%s/*.rrd' % rrd_reports_dir): if os.path.basename(rrdFilename) == filename: - with tempfile.NamedTemporaryFile() as output_stream: - subprocess.check_call(['/usr/local/bin/rrdtool', 'dump', rrdFilename], - stdout=output_stream, stderr=subprocess.STDOUT) - output_stream.seek(0) - print (output_stream.read().decode()) + subprocess.run(['/usr/local/bin/rrdtool', 'dump', rrdFilename]) break