def misc(): """ Display Miscellaneous usage data """ g = core.Get() text = 'Miscellaneous Usage:\n' print(text) text = 'OS: {} \t Uptime: {} seconds\n'.format(g.os(), g.uptime()) print(text) text = 'Users:-' print(text) users = g.users() for user_dict in users: print() text = 'Name: {} \t Session started: {} \t Host: {}'.format( user_dict['name'], user_dict['sess_started'], user_dict['host']) print(text) text = '\nSwap:-\n' print(text) swap_dict = g.swap() text = 'Total: {:,} Bytes \t Used: {:,} Bytes ({}%) \t Free: {:,} Bytes'.format( swap_dict['total'], swap_dict['used'], swap_dict['percent'], swap_dict['free']) print(text) text = 'Swapped in: {:,} Bytes \t Swapped out: {:,} Bytes'.format( swap_dict['sin'], swap_dict['sout']) print(text)
def start(): try: warnings.filterwarnings('ignore') while True: g = core.Get() settings_file = open(os.path.join( os.path.expanduser('~'), '.sms/settings.json')) limit = json.load(settings_file)['limit'] settings_file.close() if 'cpu' in limit: cpu_usage = g.cpu_overall() if cpu_usage >= limit['cpu']: message = 'CPU limit reached\nCurrent CPU usage is {} %\nMaximum allowed limit is set at {} %'.format( cpu_usage, limit['cpu']) notification.notify( title='System Monitoring System', message=message) if 'memory' in limit: memory_usage = g.memory()['percent'] if memory_usage >= limit['memory']: message = 'Memory limit reached\nCurrent Memory usage is {} %\nMaximum allowed limit is set at {} %'.format( memory_usage, limit['memory']) notification.notify( title='System Monitoring System', message=message) if 'storage' in limit: storages = g.storage() for storage in storages: storage_usage = storage['percent'] if storage_usage >= limit['storage']: message = 'Storage limit reached for device {}\nCurrent Storage usage is {} %\nMaximum allowed limit is set at {} %'.format( storage['device'], storage_usage, limit['storage']) notification.notify( title='System Monitoring System', message=message) if 'swap' in limit: swap_usage = g.swap()['percent'] if swap_usage >= limit['swap']: message = 'Swap limit reached\nCurrent Swap usage is {} %\nMaximum allowed limit is set at {} %'.format( swap_usage, limit['swap']) notification.notify( title='System Monitoring System', message=message) time.sleep(300) except KeyboardInterrupt: print('\nExiting')
def memory(): """ Display detailed Memory usage data """ g = core.Get() text = 'Memory Usage:\n' print(text) memory_dict = g.memory() text = 'Total: {:,} Bytes \t Available: {:,} Bytes \t Used(excl. Cache & buffer): {:,} Bytes ({}%)'.format( memory_dict['total'], memory_dict['available'], memory_dict['used_excl'], memory_dict['percent']) print(text) text = 'Used (incl, Cache & Buffer): {:,} Bytes \t Free: {:,} Bytes'.format( memory_dict['used_incl'], memory_dict['free']) print(text)
def network(): """ Display detailed Network usage data """ g = core.Get() text = 'Network Usage:' print(text) networks = g.network() if os.name == 'nt': for network_dict in networks: print() text = 'Interface: {} \t Bytes sent: {} \t Bytes received: {}'.format( network_dict['interface'], network_dict['bytes_sent'], network_dict['bytes_recv']) print(text) text = 'Packets sent: {} \t Packets received: {} \t Errors in: {}'.format( network_dict['packets_sent'], network_dict['packets_recv'], network_dict['errin']) print(text) text = 'Errors out: {} \t Dropped in: {} \t Dropped out: {}'.format( network_dict['errout'], network_dict['dropin'], network_dict['dropout']) print(text) else: for network_dict in networks: print() text = 'Interface: {} \t IP: {} \t Bytes sent: {}'.format( network_dict['interface'], network_dict['ip'], network_dict['bytes_sent']) print(text) text = 'Bytes received: {} \t Packets sent: {} \t Packets received: {}'.format( network_dict['bytes_recv'], network_dict['packets_sent'], network_dict['packets_recv']) print(text) text = 'Errors in: {} \t Errors out: {} \t Dropped in: {}'.format( network_dict['errin'], network_dict['errout'], network_dict['dropin']) print(text) text = 'Dropped out: {}'.format(network_dict['dropout']) print(text) speed_dict = core.test_speed() text = '\nDownload speed: {} \t Upload speed: {}'.format( speed_dict['down_speed'], speed_dict['up_speed']) print(text)
def process(): """ Display detailed Process usage data """ g = core.Get() text = 'Process Usage:' print(text) processes = g.process() for process_dict in processes: print() text = 'PID: {} \t Name: {} \t User: {}'.format( process_dict['pid'], process_dict['name'], process_dict['user']) print(text) text = 'Status: {} \t Created: {} seconds since the epoch \t Memory: {} %'.format( process_dict['status'], process_dict['created'], process_dict['memory']) print(text) text = 'CPU: {} %'.format(process_dict['cpu']) print(text)
def cpu(): """ Display detailed CPU usage data """ g = core.Get() text = 'CPU Usage:\n' print(text) cpu_dict = g.cpu() if os.name == 'nt': text = 'User: {} % \t System: {} % \t Idle: {} %'.format( cpu_dict['idle'], cpu_dict['user'], cpu_dict['system']) print(text) counter = 1 for cpu_core in cpu_dict['cores']: print() text = 'Core {}:-'.format(counter) print(text) text = 'User: {} % \t System: {} % \t Idle: {} %'.format( cpu_core['user'], cpu_core['system'], cpu_core['idle']) print(text) counter += 1 else: text = 'Load Average: {} {} {} \t User: {} % \t System: {} %'.format( cpu_dict['load_avg'][0], cpu_dict['load_avg'][1], cpu_dict['load_avg'][2], cpu_dict['user'], cpu_dict['system']) print(text) text = 'Idle: {} % \t I/O Wait: {} %'.format(cpu_dict['idle'], cpu_dict['iowait']) print(text) counter = 1 for cpu_core in cpu_dict['cores']: print() text = 'Core {}:-'.format(counter) print(text) text = 'User: {} % \t System: {} % \t Idle: {} %'.format( cpu_core['user'], cpu_core['system'], cpu_core['idle']) print(text) text = 'I/O Wait: {} %'.format(cpu_core['iowait']) print(text) counter += 1
def storage(): """ Display detailed Storage usage data """ g = core.Get() text = 'Storage Usage:' print(text) storages = g.storage() for storage_dict in storages: print() text = 'Device: {} \t Mounted: {} \t Type: {}'.format( storage_dict['device'], storage_dict['mountpoint'], storage_dict['fstype']) print(text) text = 'Options: {} \t Total: {:,} Bytes \t Used: {:,} Bytes ({}%)'.format( storage_dict['options'], storage_dict['total'], storage_dict['used'], storage_dict['percent']) print(text) text = 'Free: {:,} Bytes'.format(storage_dict['free']) print(text)
def main(): """ Opens GUI application """ auth = False g = core.Get() sg.theme("SandyBeach") sg.set_options(font=("Montserrat", 10)) settings = sg.UserSettings( filename=os.path.join(os.path.expanduser("~"), ".sms/settings.json")) window = layout(g, settings) email = {"id": None, "password": None} report_opt = { "-report-main-": "Summary", "-report-cpu-": "CPU", "-report-mem-": "Memory", "-report-proc-": "Process", "-report-storage-": "Storage", "-report-net-": "Network", "-report-misc-": "Miscellaneous", } email_opt = { "-email-main-": "Summary", "-email-cpu-": "CPU", "-email-mem-": "Memory", "-email-proc-": "Process", "-email-storage-": "Storage", "-email-net-": "Network", "-email-misc-": "Miscellaneous", } while True: event, values = window.read() name_email = settings.get("email", None) try: if values[0] == "Settings Menu": if not auth: auth = authenticate(settings) if not auth: window.Element("Main Menu").select() except TypeError: break if event == sg.WIN_CLOSED: break if event == "Change password": change_password(settings) if event == "Add record": add_record(settings) name_email = settings.get("email", None) n_e = [n + " - " + e for e, n in name_email.items()] window["-email-"].update(n_e) window.refresh() if event == "Change record" and not values["-email-"]: sg.popup( "Select an email from the list and try again!", title="No email selected", ) elif event == "Change record" and values["-email-"]: if values["-email-"][0] == "File doesn't exist - No record found": sg.popup("Add record and try again!", title="No record found") else: change_record(values["-email-"], settings) n_e = [n + " - " + e for e, n in name_email.items()] window["-email-"].update(n_e) window.refresh() if event == "Delete record" and not values["-email-"]: sg.popup( "Select an email from the list and try again!", title="No email selected", ) elif event == "Delete record" and values["-email-"]: if values["-email-"][0] == "File doesn't exist - No record found": sg.popup("Add record and try again!", title="No record found") else: delete_record(values["-email-"], settings) n_e = [n + " - " + e for e, n in name_email.items()] window["-email-"].update(n_e) window.refresh() if event == "Set notification limit": set_notification_limit(settings) if event in report_opt: file = report.down_report(report_opt[event]) sg.popup( report_opt[event] + " report saved to file " + file, title="Report generation successful!", ) elif event in email_opt: if not name_email: sg.popup( "Add an email in the Settings menu and try again!", title="No email ids present", ) else: email_report(email, email_opt[event]) window.close()
def create_report(resource): pdf = FPDF(orientation='P', format='A4') try: pdf.add_font('Montserrat', '', os.path.join(os.path.expanduser('~'), '.sms/Montserrat-Regular.ttf'), uni=True) pdf.add_font('Montserrat', 'B', os.path.join(os.path.expanduser('~'), '.sms/Montserrat-Bold.ttf'), uni=True) font_style = 'Montserrat' except: font_style = 'Arial' g = core.Get() if resource == 'Summary': pdf.add_page() pdf.set_author('SMS') pdf.set_font(font_style, 'B', 10) pdf.cell(0, txt='System Monitoring System', ln=1, align='C') pdf.set_font(font_style, 'B', 32) pdf.cell(0, h=25, txt='Report', ln=1, align='C') pdf.set_font(font_style, 'B', 20) pdf.cell(0, h=15, txt='Overall Usage Summary - ', ln=1) pdf.set_font(font_style, '', 12) text = 'OS: {}'.format(g.os()) pdf.cell(0, h=5, txt=text, ln=1) text = 'Uptime: {} seconds'.format(g.uptime()) pdf.cell(0, h=5, txt=text, ln=1) pdf.ln() # CPU data pdf.set_font(font_style, 'B', 16) pdf.cell(0, h=15, txt='CPU:-', ln=1) pdf.set_font(font_style, '', 12) cpu_dict = g.cpu() if os.name != 'nt': text = 'Load Average: {} {} {}'.format(cpu_dict['load_avg'][0], cpu_dict['load_avg'][1], cpu_dict['load_avg'][2]) pdf.cell(0, h=5, txt=text, ln=1) text = 'User: {} %'.format(cpu_dict['user']) pdf.cell(0, h=5, txt=text, ln=1) text = 'System: {} %'.format(cpu_dict['system']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Idle: {} %'.format(cpu_dict['idle']) pdf.cell(0, h=5, txt=text, ln=1) if os.name != 'nt': text = 'I/O Wait: {} %'.format(cpu_dict['iowait']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Cores: {}'.format(cpu_dict['num_cores']) pdf.cell(0, h=5, txt=text, ln=1) pdf.ln() # Memory data pdf.set_font(font_style, 'B', 16) pdf.cell(0, h=15, txt='Memory:-', ln=1) pdf.set_font(font_style, '', 12) memory_dict = g.memory() text = 'Total: {:,} Bytes'.format(memory_dict['total']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Used: {:,} Bytes'.format(memory_dict['used_incl']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Free: {:,} Bytes'.format(memory_dict['free']) pdf.cell(0, h=5, txt=text, ln=1) pdf.ln() # Network data pdf.set_font(font_style, 'B', 16) pdf.cell(0, h=15, txt='Network:-', ln=1) pdf.set_font(font_style, '', 12) networks = g.network() for network_dict in networks: text = 'Interface: {}'.format(network_dict['interface']) pdf.cell(0, h=5, txt=text, ln=1) if os.name != 'nt': text = 'IP: {}'.format(network_dict['ip']) pdf.cell(0, h=5, txt=text, ln=1) pdf.ln() # Storage data pdf.set_font(font_style, 'B', 16) pdf.cell(0, h=15, txt='Storage:-', ln=1) pdf.set_font(font_style, '', 12) storages = g.storage() for storage_dict in storages: text = 'Device: {}'.format(storage_dict['device']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Mounted: {}'.format(storage_dict['mountpoint']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Total: {:,} Bytes'.format(storage_dict['total']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Used: {:,} Bytes ({}%)'.format(storage_dict['used'], storage_dict['percent']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Free: {:,} Bytes'.format(storage_dict['free']) pdf.cell(0, h=5, txt=text, ln=1) pdf.ln() # Swap data pdf.set_font(font_style, 'B', 16) pdf.cell(0, h=15, txt='Swap:-', ln=1) pdf.set_font(font_style, '', 12) swap_dict = g.swap() text = 'Total: {:,} Bytes'.format(swap_dict['total']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Used: {:,} Bytes ({}%)'.format(swap_dict['used'], swap_dict['percent']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Free: {:,} Bytes'.format(swap_dict['free']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Swapped in: {:,} Bytes'.format(swap_dict['sin']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Swapped out: {:,} Bytes'.format(swap_dict['sout']) pdf.cell(0, h=5, txt=text, ln=1) pdf.ln() # Users data pdf.set_font(font_style, 'B', 16) pdf.cell(0, h=15, txt='Users:-', ln=1) pdf.set_font(font_style, '', 12) users = g.users() for user_dict in users: text = 'Name: {}'.format(user_dict['name']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Session started: {}'.format(user_dict['sess_started']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Host: {}'.format(user_dict['host']) pdf.cell(0, h=5, txt=text, ln=1) pdf.ln() elif resource == 'CPU': pdf.add_page() pdf.set_author('SMS') pdf.set_font(font_style, 'B', 10) pdf.cell(0, txt='System Monitoring System', ln=1, align='C') pdf.set_font(font_style, 'B', 32) pdf.cell(0, h=25, txt='Report', ln=1, align='C') pdf.set_font(font_style, 'B', 20) pdf.cell(0, h=15, txt='CPU Usage Summary - ', ln=1) pdf.set_font(font_style, 'B', 16) pdf.cell(0, h=15, txt='Overall:-', ln=1) pdf.set_font(font_style, '', 12) cpu_dict = g.cpu() if os.name != 'nt': text = 'Load Average: {} {} {}'.format(cpu_dict['load_avg'][0], cpu_dict['load_avg'][1], cpu_dict['load_avg'][2]) pdf.cell(0, h=5, txt=text, ln=1) text = 'User: {} %'.format(cpu_dict['user']) pdf.cell(0, h=5, txt=text, ln=1) text = 'System: {} %'.format(cpu_dict['system']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Idle: {} %'.format(cpu_dict['idle']) pdf.cell(0, h=5, txt=text, ln=1) text = 'I/O Wait: {} %'.format(cpu_dict['iowait']) pdf.cell(0, h=5, txt=text, ln=1) counter = 1 for cpu_core in cpu_dict['cores']: pdf.ln() pdf.set_font(font_style, 'B', 16) text = 'Core {}:-'.format(counter) pdf.cell(0, h=15, txt=text, ln=1) pdf.set_font(font_style, '', 12) text = 'User: {} %'.format(cpu_core['user']) pdf.cell(0, h=5, txt=text, ln=1) text = 'System: {} %'.format(cpu_core['system']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Idle: {} %'.format(cpu_core['idle']) pdf.cell(0, h=5, txt=text, ln=1) if os.name != 'nt': text = 'I/O Wait: {} %'.format(cpu_core['iowait']) pdf.cell(0, h=5, txt=text, ln=1) counter += 1 elif resource == 'Memory': pdf.add_page() pdf.set_author('SMS') pdf.set_font(font_style, 'B', 10) pdf.cell(0, txt='System Monitoring System', ln=1, align='C') pdf.set_font(font_style, 'B', 32) pdf.cell(0, h=25, txt='Report', ln=1, align='C') pdf.set_font(font_style, 'B', 20) pdf.cell(0, h=15, txt='Memory Usage Summary - ', ln=1) pdf.set_font(font_style, '', 12) memory_dict = g.memory() text = 'Total: {:,} Bytes'.format(memory_dict['total']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Available: {:,} Bytes'.format(memory_dict['available']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Used (excl. Cache & buffer): {:,} Bytes ({}%)'.format( memory_dict['used_excl'], memory_dict['percent']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Used (incl. Cache & buffer): {:,} Bytes'.format( memory_dict['used_incl']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Free: {:,} Bytes'.format(memory_dict['free']) pdf.cell(0, h=5, txt=text, ln=1) elif resource == 'Network': pdf.add_page() pdf.set_author('SMS') pdf.set_font(font_style, 'B', 10) pdf.cell(0, txt='System Monitoring System', ln=1, align='C') pdf.set_font(font_style, 'B', 32) pdf.cell(0, h=25, txt='Report', ln=1, align='C') pdf.set_font(font_style, 'B', 20) pdf.cell(0, h=15, txt='Network Usage Summary - ', ln=1) pdf.set_font(font_style, '', 12) networks = g.network() for network_dict in networks: text = 'Interface: {}'.format(network_dict['interface']) pdf.cell(0, h=5, txt=text, ln=1) if os.name != 'nt': text = 'IP: {}'.format(network_dict['ip']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Bytes sent: {}'.format(network_dict['bytes_sent']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Bytes received: {}'.format(network_dict['bytes_recv']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Packets sent: {}'.format(network_dict['packets_sent']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Packets received: {}'.format(network_dict['packets_recv']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Errors in: {}'.format(network_dict['errin']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Errors out: {}'.format(network_dict['errout']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Dropped in: {}'.format(network_dict['dropin']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Dropped out: {}'.format(network_dict['dropout']) pdf.cell(0, h=5, txt=text, ln=1) pdf.ln() speed_dict = core.test_speed() text = 'Download speed: {}'.format(speed_dict['down_speed']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Upload speed: {}'.format(speed_dict['up_speed']) pdf.cell(0, h=5, txt=text, ln=1) elif resource == 'Storage': pdf.add_page() pdf.set_author('SMS') pdf.set_font(font_style, 'B', 10) pdf.cell(0, txt='System Monitoring System', ln=1, align='C') pdf.set_font(font_style, 'B', 32) pdf.cell(0, h=25, txt='Report', ln=1, align='C') pdf.set_font(font_style, 'B', 20) pdf.cell(0, h=15, txt='Storage Usage Summary - ', ln=1) pdf.set_font(font_style, '', 12) storages = g.storage() for storage_dict in storages: text = 'Device: {}'.format(storage_dict['device']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Mounted: {}'.format(storage_dict['mountpoint']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Type: {}'.format(storage_dict['fstype']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Options: {}'.format(storage_dict['options']) pdf.multi_cell(0, h=5, txt=text) text = 'Total: {:,} Bytes'.format(storage_dict['total']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Used: {:,} Bytes ({}%)'.format(storage_dict['used'], storage_dict['percent']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Free: {:,} Bytes'.format(storage_dict['free']) pdf.cell(0, h=5, txt=text, ln=1) pdf.ln() elif resource == 'Process': pdf.add_page() pdf.set_author('SMS') pdf.set_font(font_style, 'B', 10) pdf.cell(0, txt='System Monitoring System', ln=1, align='C') pdf.set_font(font_style, 'B', 32) pdf.cell(0, h=25, txt='Report', ln=1, align='C') pdf.set_font(font_style, 'B', 20) pdf.cell(0, h=15, txt='Process Usage Summary - ', ln=1) pdf.set_font(font_style, '', 12) processes = g.process() for process_dict in processes: text = 'PID: {}'.format(process_dict['pid']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Name: {}'.format(process_dict['name']) pdf.cell(0, h=5, txt=text, ln=1) text = 'User: {}'.format(process_dict['user']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Status: {}'.format(process_dict['status']) pdf.multi_cell(0, h=5, txt=text) text = 'Created: {} seconds since the epoch'.format( process_dict['created']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Memory: {} %'.format(process_dict['memory']) pdf.cell(0, h=5, txt=text, ln=1) text = 'CPU: {} %'.format(process_dict['cpu']) pdf.cell(0, h=5, txt=text, ln=1) pdf.ln() elif resource == 'Miscellaneous': pdf.add_page() pdf.set_author('SMS') pdf.set_font(font_style, 'B', 10) pdf.cell(0, txt='System Monitoring System', ln=1, align='C') pdf.set_font(font_style, 'B', 32) pdf.cell(0, h=25, txt='Report', ln=1, align='C') pdf.set_font(font_style, 'B', 20) pdf.cell(0, h=15, txt='Miscellaneous Usage Summary - ', ln=1) pdf.set_font(font_style, '', 12) text = 'OS: {}'.format(g.os()) pdf.cell(0, h=5, txt=text, ln=1) text = 'Uptime: {} seconds'.format(g.uptime()) pdf.cell(0, h=5, txt=text, ln=1) pdf.ln() pdf.set_font(font_style, 'B', 16) pdf.cell(0, h=15, txt='Users:-', ln=1) pdf.set_font(font_style, '', 12) users = g.users() for user_dict in users: text = 'Name: {}'.format(user_dict['name']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Session started: {}'.format(user_dict['sess_started']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Host: {}'.format(user_dict['host']) pdf.cell(0, h=5, txt=text, ln=1) pdf.ln() pdf.set_font(font_style, 'B', 16) pdf.cell(0, h=15, txt='Swap:-', ln=1) pdf.set_font(font_style, '', 12) swap_dict = g.swap() text = 'Total: {:,} Bytes'.format(swap_dict['total']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Used: {:,} Bytes ({}%)'.format(swap_dict['used'], swap_dict['percent']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Free: {:,} Bytes'.format(swap_dict['free']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Swapped in: {:,} Bytes'.format(swap_dict['sin']) pdf.cell(0, h=5, txt=text, ln=1) text = 'Swapped out: {:,} Bytes'.format(swap_dict['sout']) pdf.cell(0, h=5, txt=text, ln=1) else: msg = """ Invalid Resource Name provided. The following options are available: - Summary - CPU - Memory - Network - Storage - Process - Miscellaneous """ raise ArgumentError(msg) return pdf
import core g = core.Get() def test_cpu(): assert type(g.cpu()) is dict def test_cpu_load_avg(): assert type(g.cpu()['load_avg']) is tuple def test_cpu_user(): assert type(g.cpu()['user']) is float def test_cpu_system(): assert type(g.cpu()['system']) is float def test_cpu_idle(): assert type(g.cpu()['idle']) is float def test_cpu_iowait(): assert type(g.cpu()['iowait']) is float def test_cpu_num_cores(): assert type(g.cpu()['num_cores']) is int
def summary(): """ Display a summary of System usage data """ g = core.Get() text = 'OS: {} \t Uptime: {}'.format(g.os(), g.uptime()) print(text) # CPU data text = '\nCPU:-' print(text) cpu_dict = g.cpu() if os.name == 'nt': text = 'User: {} % \t System: {} % \t Idle: {} %'.format( cpu_dict['idle'], cpu_dict['user'], cpu_dict['system']) print(text) text = 'Cores: {}'.format(cpu_dict['num_cores']) print(text) else: text = 'Load Average: {} {} {} \t User: {} % \t System: {} %'.format( cpu_dict['load_avg'][0], cpu_dict['load_avg'][1], cpu_dict['load_avg'][2], cpu_dict['user'], cpu_dict['system']) print(text) text = 'Idle: {} % \t I/O Wait: {} % \t Cores: {}'.format( cpu_dict['idle'], cpu_dict['iowait'], cpu_dict['num_cores']) print(text) # Memory data text = '\nMemory:-' print(text) memory_dict = g.memory() text = 'Total: {:,} Bytes \t Used: {:,} Bytes \t Free: {:,} Bytes'.format( memory_dict['total'], memory_dict['used_incl'], memory_dict['free']) print(text) # Network data text = '\nNetwork:-' print(text) networks = g.network() for network_dict in networks: if os.name == 'nt': text = 'Interface: {}'.format(network_dict['interface']) print(text) else: text = 'Interface: {} \t IP: {}'.format(network_dict['interface'], network_dict['ip']) print(text) # Storage data text = '\nStorage:-' print(text) storages = g.storage() for storage_dict in storages: text = 'Device: {} \t Mounted: {} \t Total: {:,} Bytes'.format( storage_dict['device'], storage_dict['mountpoint'], storage_dict['total']) print(text) text = 'Used: {:,} Bytes ({}%) \t Free: {:,} Bytes'.format( storage_dict['used'], storage_dict['percent'], storage_dict['free']) print(text) # Swap data text = '\nSwap:-' print(text) swap_dict = g.swap() text = 'Total: {:,} Bytes \t Used: {:,} Bytes ({}%) \t Free: {:,} Bytes'.format( swap_dict['total'], swap_dict['used'], swap_dict['percent'], swap_dict['free']) print(text) text = 'Swapped in: {:,} Bytes \t Swapped out: {:,} Bytes'.format( swap_dict['sin'], swap_dict['sout']) print(text) # Users data text = '\nUsers:-' print(text) users = g.users() for user_dict in users: text = 'Name: {} \t Session started: {} \t Host: {}'.format( user_dict['name'], user_dict['sess_started'], user_dict['host']) print(text)