From c4877e6b9ce185dfda8a38a0d68e4ba1f84058ff Mon Sep 17 00:00:00 2001 From: petrucci4prez Date: Fri, 12 Aug 2016 00:42:19 -0400 Subject: [PATCH] removed USB and Remote --- main.lua | 2 - module/Remote.lua | 280 ------------------------------ module/USB.lua | 422 ---------------------------------------------- 3 files changed, 704 deletions(-) delete mode 100644 module/Remote.lua delete mode 100644 module/USB.lua diff --git a/main.lua b/main.lua index 4044906..45e625b 100644 --- a/main.lua +++ b/main.lua @@ -48,8 +48,6 @@ local FileSystem = require 'FileSystem' local Pacman = require 'Pacman' local ReadWrite = require 'ReadWrite' local Memory = require 'Memory' ---~ local USB = require 'USB' ---~ local Remote = require 'Remote' local Weather = require 'Weather' local updates = -2 diff --git a/module/Remote.lua b/module/Remote.lua deleted file mode 100644 index 2489a9b..0000000 --- a/module/Remote.lua +++ /dev/null @@ -1,280 +0,0 @@ -local _CR = require 'CR' -local Widget = require 'Widget' -local Text = require 'Text' -local CriticalText = require 'CriticalText' -local Line = require 'Line' -local TextColumn = require 'TextColumn' -local util = require 'util' -local schema = require 'default_patterns' - -local _STRING_FIND = string.find -local _STRING_MATCH = string.match -local _STRING_GMATCH = string.gmatch - -local NUM_ROWS = 12 - -local USERNAME_FORMAT = '(%S+)%s+' -local START_TIME_FORMAT = '%S+%s+%S+%s+%S+%s+(%S+)%s+' - ---construction params -local SPACING = 20 -local SEPARATOR_SPACING = 17 -local MODULE_Y = 285 - -local header = Widget.Header{ - x = CONSTRUCTION_GLOBAL.RIGHT_X, - y = MODULE_Y, - width = CONSTRUCTION_GLOBAL.SIDE_WIDTH, - header = 'REMOTE CONNECTIONS' -} - -local FIREWALL_Y = header.bottom_y -local TUNNEL_Y = FIREWALL_Y + SEPARATOR_SPACING * 2 -local SOCKS_Y = TUNNEL_Y + SPACING + SEPARATOR_SPACING * 2 - -local TABLE_Y = SOCKS_Y + SPACING + SEPARATOR_SPACING * 2 -local TABLE_BODY_Y = TABLE_Y + SPACING + 6 - -local RIGHT_X = CONSTRUCTION_GLOBAL.RIGHT_X + CONSTRUCTION_GLOBAL.SIDE_WIDTH - -local firewall = { - label = Widget.Text{ - x = CONSTRUCTION_GLOBAL.RIGHT_X, - y = FIREWALL_Y, - text = 'Firewall Status', - }, - info = Widget.CriticalText{ - x = RIGHT_X, - y = FIREWALL_Y, - x_align = 'right', - text_color = schema.blue, - } -} - -local tunnel = { - labels = Widget.TextColumn{ - x = CONSTRUCTION_GLOBAL.RIGHT_X, - y = TUNNEL_Y, - spacing = SPACING, - 'Tunnel Loaded', - 'Tunnel Running', - }, - info = Widget.TextColumn{ - x = RIGHT_X, - y = TUNNEL_Y, - spacing = SPACING, - x_align = 'right', - text_color = schema.blue, - num_rows = 2, - } -} - -local socks = { - labels = Widget.TextColumn{ - x = CONSTRUCTION_GLOBAL.RIGHT_X, - y = SOCKS_Y, - spacing = SPACING, - 'Socks Loaded', - 'Socks Running', - }, - info = Widget.TextColumn{ - x = RIGHT_X, - y = SOCKS_Y, - spacing = SPACING, - x_align = 'right', - text_color = schema.blue, - num_rows = 2, - } -} - -local tbl = { - headers = { - Widget.Text{ - x = CONSTRUCTION_GLOBAL.RIGHT_X, - y = TABLE_Y, - text = 'Username', - text_color = schema.blue - }, - Widget.Text{ - x = RIGHT_X, - y = TABLE_Y, - text = 'Date / Time', - text_color = schema.blue, - x_align = 'right' - } - }, - columns = { - Widget.TextColumn{ - x = CONSTRUCTION_GLOBAL.RIGHT_X, - y = TABLE_BODY_Y, - spacing = SPACING, - num_rows = NUM_ROWS, - font_size = 10, - max_length = 9 - }, - Widget.TextColumn{ - x = RIGHT_X, - y = TABLE_BODY_Y, - spacing = SPACING, - num_rows = NUM_ROWS, - font_size = 10, - x_align = 'right', - } - } -} - -local separators = { - Widget.Line{ - p1 = { - x = CONSTRUCTION_GLOBAL.RIGHT_X, - y = TUNNEL_Y - SEPARATOR_SPACING - }, - p2 = { - x = RIGHT_X, - y = TUNNEL_Y - SEPARATOR_SPACING - } - }, - Widget.Line{ - p1 = { - x = CONSTRUCTION_GLOBAL.RIGHT_X, - y = SOCKS_Y - SEPARATOR_SPACING - }, - p2 = { - x = RIGHT_X, - y = SOCKS_Y - SEPARATOR_SPACING - } - }, - Widget.Line{ - p1 = { - x = CONSTRUCTION_GLOBAL.RIGHT_X, - y = TABLE_Y - SEPARATOR_SPACING - }, - p2 = { - x = RIGHT_X, - y = TABLE_Y - SEPARATOR_SPACING - } - } -} - -local __set_ssh_status = function(status, obj, cr) - TextColumn.set(obj, cr, 1, _STRING_MATCH(status, '/autossh') and 'Yes' or 'No') - TextColumn.set(obj, cr, 2, _STRING_MATCH(status, '/ssh') and 'Yes' or 'No') -end - -local __parse_log_line = function(log_line) - if not log_line then return end - - local username = _STRING_MATCH(log_line, USERNAME_FORMAT) - local start_date = _STRING_MATCH(log_line, START_TIME_FORMAT) - - local start_time_unix = util.execute_cmd('date --date="'..start_date..'" +"%s"', nil, '*n') - - local start_time_formatted = util.execute_cmd( - 'date --date="'..start_date..'" +"%-m-%-d-%y (%H:%M)"', '(.+)\n') - - if _STRING_FIND(log_line, 'still logged in', 1, true) then - return username, start_time_unix, '*!* '..start_time_formatted..' *!*' - else - return username, start_time_unix, start_time_formatted - end -end - -local __update = function(cr) - __set_ssh_status(util.execute_cmd('systemctl status tunnel'), tunnel.info, cr) - __set_ssh_status(util.execute_cmd('systemctl status socks'), socks.info, cr) - - if util.execute_cmd('systemctl is-active ufw') == 'active\n' then - CriticalText.set(firewall.info, cr, 'Up', 1) - else - CriticalText.set(firewall.info, cr, 'Down', 0) - end - - local next_wtmp = _STRING_GMATCH(util.execute_cmd( - "last -iw --time-format iso | grep -v '0.0.0.0' | head -n -2"), '[^\n]+') - - local next_btmp = _STRING_GMATCH(util.execute_cmd( - "lastb -iw --time-format iso | grep -v '0.0.0.0' | head -n -2"), '[^\n]+') - - local wtmp_username, wtmp_time, wtmp_str = __parse_log_line(next_wtmp()) - local btmp_username, btmp_time, btmp_str = __parse_log_line(next_btmp()) - - local column1 = tbl.columns[1] - local column2 = tbl.columns[2] - - for r = 1, NUM_ROWS do - if wtmp_time and btmp_time then - if wtmp_time > btmp_time then - TextColumn.set(column1, cr, r, wtmp_username) - TextColumn.set(column2, cr, r, wtmp_str) - wtmp_username, wtmp_time, wtmp_str = __parse_log_line(next_wtmp()) - else - TextColumn.set(column1, cr, r, '*'..btmp_username) - TextColumn.set(column2, cr, r, btmp_str) - btmp_username, btmp_time, btmp_str = __parse_log_line(next_btmp()) - end - elseif wtmp_time then - TextColumn.set(column1, cr, r, wtmp_username) - TextColumn.set(column2, cr, r, wtmp_str) - wtmp_username, wtmp_time, wtmp_str = __parse_log_line(next_wtmp()) - elseif btmp_time then - TextColumn.set(column1, cr, r, '*'..btmp_username) - TextColumn.set(column2, cr, r, btmp_str) - btmp_username, btmp_time, btmp_str = __parse_log_line(next_btmp()) - else - TextColumn.set(column1, cr, r, '--') - TextColumn.set(column2, cr, r, '--') - end - end -end - -__update(_CR) - -Widget = nil -schema = nil -SPACING = nil -SEPARATOR_SPACING = nil -MODULE_Y = nil -RIGHT_X = nil -TUNNEL_Y = nil -SOCKS_Y = nil -FIREWALL_Y = nil -TABLE_Y = nil -TABLE_BODY_Y = nil -_CR = nil - -local draw = function(cr, current_interface, trigger) - if trigger == 0 then __update(cr) end - - if current_interface == 1 then - Text.draw(header.text, cr) - Line.draw(header.underline, cr) - - TextColumn.draw(tunnel.labels, cr) - TextColumn.draw(tunnel.info, cr) - - Line.draw(separators[1], cr) - - TextColumn.draw(socks.labels, cr) - TextColumn.draw(socks.info, cr) - - Line.draw(separators[2], cr) - - Text.draw(firewall.label, cr) - CriticalText.draw(firewall.info, cr) - - Line.draw(separators[3], cr) - - local headers = tbl.headers - - Text.draw(headers[1], cr) - Text.draw(headers[2], cr) - - local column1 = tbl.columns[1] - local column2 = tbl.columns[2] - - TextColumn.draw(column1, cr) - TextColumn.draw(column2, cr) - end -end - -return draw diff --git a/module/USB.lua b/module/USB.lua deleted file mode 100644 index 000d083..0000000 --- a/module/USB.lua +++ /dev/null @@ -1,422 +0,0 @@ -local _CR = require 'CR' -local Widget = require 'Widget' -local Text = require 'Text' -local Line = require 'Line' -local TextColumn = require 'TextColumn' -local ScalePlot = require 'ScalePlot' -local util = require 'util' -local schema = require 'default_patterns' - -local _STRING_MATCH = string.match -local _STRING_GMATCH = string.gmatch -local _STRING_GSUB = string.gsub -local _TONUMBER = tonumber -local _OS_EXECUTE = os.execute -local _IO_OPEN = io.open - -local USB_IO_PATH = '/tmp/usbdump.txt' -local USBDUMP_CMD = 'timeout 2 usbdump > '..USB_IO_PATH..' &' -local RIGHT_USB_PCI = '/sys/devices/pci0000:00/0000:00:1c.4/0000:0b:00.0/' -local LEFT_USB_PCI = '/sys/devices/pci0000:00/0000:00:1d.0/' - -local FIND_RIGHT_PORTS = 'find '..RIGHT_USB_PCI..'usb[1-4]/[1-4]-[1,2] -maxdepth 0 -type d 2> /dev/null' -local FIND_PORT_3 = 'find '..LEFT_USB_PCI..'usb[1-4]/[1-4]-1/[1-4]-1.2 -maxdepth 0 -type d 2> /dev/null' -local FIND_SD_SLOT = 'find '..LEFT_USB_PCI..'usb[1-4]/[1-4]-1/[1-4]-1.6 -maxdepth 0 -type d 2> /dev/null' - -local N_COLUMNS = 3 -local N_ROWS = 4 - -local STATUS_PCI_UNLOADED = 'No PCI Module' -local STATUS_USBMON_UNLOADED = 'Usbmon Unloaded' - ---construction params -local SPACING = 20 -local PLOT_SEC_BREAK = 20 -local PLOT_HEIGHT = 56 -local SECTION_PAD = 5 -local DEVICE_HEIGHT = (PLOT_HEIGHT + PLOT_SEC_BREAK + SPACING) * 2 + SPACING + SECTION_PAD + 10 - -local __usb_label_function = function(bytes) - local new_unit = util.get_unit(bytes) - local converted = util.convert_bytes(bytes, 'B', new_unit) - - local precision = 0 - if converted < 10 then precision = 1 end - - return util.round_to_string(converted, precision)..' '..new_unit..'/s' -end - -local __create_io_plot = function(x_offset, y_offset, label) - local obj = { - label = Widget.Text{ - x = x_offset, - y = y_offset, - text = label - }, - speed = Widget.Text{ - x = x_offset + CONSTRUCTION_GLOBAL.SIDE_WIDTH, - y = y_offset, - x_align = 'right', - text_color = schema.blue - }, - plot = Widget.ScalePlot{ - x = x_offset, - y = y_offset + PLOT_SEC_BREAK, - width = CONSTRUCTION_GLOBAL.SIDE_WIDTH, - height = PLOT_HEIGHT, - y_label_func = __usb_label_function - }, - } - - return obj -end - -local __create_device_display = function(x_offset, y_offset, title) - local INPUT_Y = y_offset + SPACING + SECTION_PAD - local OUTPUT_Y = INPUT_Y + SPACING + PLOT_HEIGHT + PLOT_SEC_BREAK - - local obj = { - title = Widget.Text{ - x = x_offset, - y = y_offset, - text = title, - text_color = schema.blue - }, - link_speed = Widget.Text{ - x = x_offset + CONSTRUCTION_GLOBAL.SIDE_WIDTH, - y = y_offset, - x_align = 'right', - text_color = schema.blue - }, - idata = __create_io_plot(x_offset, INPUT_Y, 'Input'), - odata = __create_io_plot(x_offset, OUTPUT_Y, 'Output'), - } - - return obj -end - -local usb = { - header = Widget.Header{ - x = CONSTRUCTION_GLOBAL.LEFT_X, - y = CONSTRUCTION_GLOBAL.TOP_Y, - width = CONSTRUCTION_GLOBAL.SIDE_WIDTH, - header = 'USB PORTS' - } -} - -local HEADER_BOTTOM_Y = usb.header.bottom_y - -usb[1] = __create_device_display(CONSTRUCTION_GLOBAL.LEFT_X, HEADER_BOTTOM_Y, 'PORT 1') -usb[2] = __create_device_display(CONSTRUCTION_GLOBAL.LEFT_X, HEADER_BOTTOM_Y + DEVICE_HEIGHT, 'PORT 2') -usb[3] = __create_device_display(CONSTRUCTION_GLOBAL.LEFT_X, HEADER_BOTTOM_Y + DEVICE_HEIGHT * 2, 'PORT 3') - -local usbtop = { - header = Widget.Header{ - x = CONSTRUCTION_GLOBAL.CENTER_X, - y = CONSTRUCTION_GLOBAL.TOP_Y, - width = CONSTRUCTION_GLOBAL.CENTER_WIDTH, - header = 'USB DEVICES' - }, - columns = {}, - separators = {} -} - -HEADER_BOTTOM_Y = usbtop.header.bottom_y - -local HEADERS = {'Port / Slot', 'Device', 'Total I / O'} -local COLUMN_WIDTHS = {150, 323, 150} - -local current_x = CONSTRUCTION_GLOBAL.CENTER_X -local columns = usbtop.columns - -for i = 1, N_COLUMNS do - local column_x = current_x + 0.5 * COLUMN_WIDTHS[i] - - columns[i] = { - header = Widget.Text{ - x = column_x, - y = HEADER_BOTTOM_Y, - x_align = 'center', - text = HEADERS[i], - text_color = schema.blue - }, - column = Widget.TextColumn{ - x = column_x, - y = HEADER_BOTTOM_Y + SPACING + 6, - x_align = 'center', - spacing = SPACING, - num_rows = N_ROWS, - font_size = 10, - max_length = 30 - } - } - current_x = current_x + COLUMN_WIDTHS[i] -end - -current_x = CONSTRUCTION_GLOBAL.CENTER_X -local separators = usbtop.separators - -for i = 1, N_COLUMNS - 1 do - current_x = current_x + COLUMN_WIDTHS[i] - separators[i] = Widget.Line{ - p1 = { - x = current_x, - y = HEADER_BOTTOM_Y - }, - p2 = { - x = current_x, - y = HEADER_BOTTOM_Y + N_ROWS * SPACING + 6 - }, - } -end - -HEADERS = nil -COLUMN_WIDTHS = nil - -current_x = nil -columns = nil -separators = nil - -local card_slot = { - header = Widget.Header{ - x = CONSTRUCTION_GLOBAL.RIGHT_X, - y = CONSTRUCTION_GLOBAL.TOP_Y, - width = CONSTRUCTION_GLOBAL.SIDE_WIDTH, - header = "SD CARD SLOT" - } -} - -HEADER_BOTTOM_Y = card_slot.header.bottom_y - -card_slot.port = __create_device_display(CONSTRUCTION_GLOBAL.RIGHT_X, HEADER_BOTTOM_Y, 'Status') - -Widget = nil -schema = nil - -SPACING = nil -PLOT_SEC_BREAK = nil -PLOT_HEIGHT = nil -SECTION_PAD = nil -DEVICE_HEIGHT = nil -HEADER_BOTTOM_Y = nil - -local USBTOP_LIST = {} - -local __draw_device = function(device, cr) - Text.draw(device.title, cr) - Text.draw(device.link_speed, cr) - - local device_plot = device.idata - - Text.draw(device_plot.label, cr) - Text.draw(device_plot.speed, cr) - ScalePlot.draw(device_plot.plot, cr) - - local device_plot = device.odata - - Text.draw(device_plot.label, cr) - Text.draw(device_plot.speed, cr) - ScalePlot.draw(device_plot.plot, cr) -end - -local __populate_active_port = function(port, cr, data_glob, bus_num, path, name) - Text.set(port.link_speed, cr, util.read_file(path..'/speed', nil, '*n') * 0.125 ..' MiB/s') - - local idata_sum = 0 - local odata_sum = 0 - - for devnum_path in _STRING_GMATCH(util.execute_cmd('find '..path..' -name devnum'), '(.-)\n') do - - local devnum = util.read_file(devnum_path, nil, '*n') - local idata, odata = _STRING_MATCH(data_glob, devnum..':'..bus_num..':(%d+):(%d+)') - - if idata and odata then - idata_sum = idata_sum + _TONUMBER(idata) - odata_sum = odata_sum + _TONUMBER(odata) - - local io_sum = idata + odata - local io_sum_unit = util.get_unit(io_sum) - - USBTOP_LIST[#USBTOP_LIST + 1] = { - name = name, - device = util.read_file(_STRING_GSUB(devnum_path, 'devnum$', 'product'), '(.-)\n'), - io_sum_numeric = io_sum, - io_sum = util.precision_convert_bytes(io_sum, 'B', io_sum_unit, 3)..' '..io_sum_unit..'/s', - path = devnum_path - } - end - end - - local iunit = util.get_unit(idata_sum) - local ounit = util.get_unit(odata_sum) - - Text.set(port.idata.speed, cr, util.precision_convert_bytes(idata_sum, 'B', iunit, 3)..' '..iunit..'/s') - Text.set(port.odata.speed, cr, util.precision_convert_bytes(odata_sum, 'B', ounit, 3)..' '..ounit..'/s') - - ScalePlot.update(port.idata.plot, cr, idata_sum) - ScalePlot.update(port.odata.plot, cr, odata_sum) -end - -local __populate_inactive_port = function(port, cr, msg) - Text.set(port.link_speed, cr, msg) - Text.set(port.idata.speed, cr, '--') - Text.set(port.odata.speed, cr, '--') - ScalePlot.update(port.idata.plot, cr, 0) - ScalePlot.update(port.odata.plot, cr, 0) -end - -local __get_power_status = function(pci_path) - if util.read_file(util.execute_cmd('find '..pci_path.. - 'usb[1-4]/power/runtime_status -print -quit 2> /dev/null', '(.-)\n')) == 'active\n' then - return 'Disconnected' - else - return 'Suspended' - end -end - -local __update = function(cr) - _OS_EXECUTE("killall -q usbdump") - - for i = 1, #USBTOP_LIST do USBTOP_LIST[i] = nil end - - local data_glob = util.read_file(USB_IO_PATH) - - if _IO_OPEN('/sys/module/usbmon/') then - _OS_EXECUTE(USBDUMP_CMD) - if _IO_OPEN('/sys/module/xhci_pci/') then - --right ports - local port_1_bus, port_2_bus - - local right_glob = util.execute_cmd(FIND_RIGHT_PORTS) - - for path in _STRING_GMATCH(right_glob, '(.-)\n') do - local bus_num, port_num = _STRING_MATCH(path, '.-(%d)-(%d)') - - if port_num == '1' then - port_1_bus = bus_num - __populate_active_port(usb[1], cr, data_glob, bus_num, path, 'Port 1') - elseif port_num == '2' then - port_2_bus = bus_num - __populate_active_port(usb[2], cr, data_glob, bus_num, path, 'Port 2') - end - end - - if not (port_1_bus and port_2_bus) then - local power_status = __get_power_status(RIGHT_USB_PCI) - - if not port_1_bus then __populate_inactive_port(usb[1], cr, power_status) end - if not port_2_bus then __populate_inactive_port(usb[2], cr, power_status) end - end - else - __populate_inactive_port(usb[1], cr, STATUS_PCI_UNLOADED) - __populate_inactive_port(usb[2], cr, STATUS_PCI_UNLOADED) - end - - if _IO_OPEN('/sys/module/ehci_pci/') then - --left port - local left_path = util.execute_cmd(FIND_PORT_3, '(.-)\n') - - local left_hub_power_status - - if left_path == '' then - left_hub_power_status = __get_power_status(LEFT_USB_PCI) - __populate_inactive_port(usb[3], cr, left_hub_power_status) - else - __populate_active_port(usb[3], cr, data_glob, - _STRING_MATCH(left_path, 'usb(%d)'), left_path, 'Port 3') - end - - --sd port - local sd_path = util.execute_cmd(FIND_SD_SLOT, '(.-)\n') - - if sd_path == '' then - __populate_inactive_port(card_slot.port, cr, left_hub_power_status or - __get_power_status(LEFT_USB_PCI)) - else - __populate_active_port(card_slot.port, cr, data_glob, - _STRING_MATCH(sd_path, 'usb(%d)'), sd_path, 'SD Slot') - end - else - __populate_inactive_port(usb[3], cr, STATUS_PCI_UNLOADED) - __populate_inactive_port(card_slot.port, cr, STATUS_PCI_UNLOADED) - end - else - __populate_inactive_port(usb[1], cr, STATUS_USBMON_UNLOADED) - __populate_inactive_port(usb[2], cr, STATUS_USBMON_UNLOADED) - __populate_inactive_port(usb[3], cr, STATUS_USBMON_UNLOADED) - __populate_inactive_port(card_slot.port, cr, STATUS_USBMON_UNLOADED) - end - - local list_len = #USBTOP_LIST - - --sort usbtop_list (selection sort) - if list_len > 1 then - for i = 1, list_len do - local iMax = i - for j = i + 1, list_len do - if USBTOP_LIST[j].io_sum_numeric > USBTOP_LIST[iMax].io_sum_numeric then - iMax = j - end - end - if iMax ~= i then - local tmp = USBTOP_LIST[i] - USBTOP_LIST[i] = USBTOP_LIST[iMax] - USBTOP_LIST[iMax] = tmp - end - end - end - - local columns = usbtop.columns - - for i = 1, N_ROWS do - local current_entry = USBTOP_LIST[i] - - if current_entry then - TextColumn.set(columns[1].column, cr, i, current_entry.name) - TextColumn.set(columns[2].column, cr, i, current_entry.device) - TextColumn.set(columns[3].column, cr, i, current_entry.io_sum) - else - TextColumn.set(columns[1].column, cr, i, '--') - TextColumn.set(columns[2].column, cr, i, '--') - TextColumn.set(columns[3].column, cr, i, '--') - end - end -end - -_OS_EXECUTE(USBDUMP_CMD) - -local draw = function(cr, interface, trigger) - __update(cr) - - if interface == 1 then - Text.draw(usb.header.text, cr) - Line.draw(usb.header.underline, cr) - - __draw_device(usb[1], cr) - __draw_device(usb[2], cr) - __draw_device(usb[3], cr) - - Text.draw(card_slot.header.text, cr) - Line.draw(card_slot.header.underline, cr) - - __draw_device(card_slot.port, cr) - - Text.draw(usbtop.header.text, cr) - Line.draw(usbtop.header.underline, cr) - - local columns = usbtop.columns - local separators = usbtop.separators - - for i = 1, N_COLUMNS do - local column = columns[i] - Text.draw(column.header, cr) - TextColumn.draw(column.column, cr) - end - - for i = 1, N_COLUMNS - 1 do - Line.draw(separators[i], cr) - end - end -end - -return draw