def _get_values(self): # example line # 1539513512 02:42:a0:15:a2:c3 192.168.0.111 opi1 * data = get_file_data('/var/lib/misc/dnsmasq.leases', as_lines=True) leases = [] for line in data: fields = line.split() # invalid lines are silently ignored if len(fields) < 4: continue lease = { 'lifetime': int(fields[0]) - int(datetime.now().timestamp()), 'hostname': fields[3], 'ip': fields[2], 'mac': fields[1], } leases.append(lease) return {'leases': leases}
def _get_dev_dataflow(self, dev): rx = int( get_file_data( "/sys/class/net/{dev}/statistics/rx_bytes".format(dev=dev), firstline=True).strip()) tx = int( get_file_data( "/sys/class/net/{dev}/statistics/tx_bytes".format(dev=dev), firstline=True).strip()) return { 'rx_bytes': rx, 'tx_bytes': tx, 'rx_rate': self._get_rate(dev + '_rx', rx), 'tx_rate': self._get_rate(dev + '_tx', tx), }
def _get_meminfo(self): lines = get_file_data( filename='/proc/meminfo').split('\n') meminfo = {} for line in lines: m = re.match('^([^:]+):\s*(\d+)( kB)?$', line) if m: meminfo[m.group(1)] = int(m.group(2)) return meminfo
def _get_temperatures(self): pattern = '/sys/class/thermal/thermal_zone*/temp' files = glob.iglob(pattern) data = [] for filename in sorted(files): # on OrangePi, there is a thermal_zone1 file, which cannot be read, # so as workaround, we silently ignore exceptions here. try: temp = int(get_file_data(filename, firstline=True)) / 1000.0 data.append(temp) except: pass return data
def get_dev_info(self, dev): '''Get network device info. :param dev: Network device name, e.g. eth0. :return: Dict with device info.''' devinfo = {'device': dev} # Example: # 2: ens160 inet 10.180.2.190/24 brd 10.180.2.255 scope global ens160 ... data = get_cmd_data(['ip', '-o', '-4', 'address', 'show', dev], firstline=True) data = data.strip() m = re.search(r'inet ([.0-9]+)\/([0-9]+)', data) if m: devinfo['ipaddress'] = m.group(1) devinfo['netsize'] = int(m.group(2)) # Example: # wlp3s0 inet6 fe80::6267:20ff:fe3c:14e8/64 scope link ... data = get_cmd_data(['ip', '-o', '-6', 'address', 'show', dev], firstline=True) data = data.strip() m = re.search(r'inet6 ([0-9a-f:]+)\/([0-9]+)', data) if m: devinfo['ipv6address'] = m.group(1) devinfo['ipv6prefix'] = int(m.group(2)) devinfo['hwaddress'] = get_file_data( "/sys/class/net/{dev}/address".format(dev=dev), firstline=True).strip() devinfo.update(self._get_dev_dataflow(dev)) return devinfo
def _get_uptime(self): data = get_file_data( filename='/proc/uptime', firstline=True) return float(data.split(' ')[0])
def check(self): data = get_file_data('/var/lib/misc/dnsmasq.leases', as_lines=True)
def _get_loadavg(self): loaddata = get_file_data(filename='/proc/loadavg', firstline=True) values = [float(v) for v in loaddata.split(' ')[0:3]] return values