def metadata_backup(cfg): """ Back-up the LVM and other disk metadata, for this system """ try: na, dev, device = cfg.metadata.split('/') except ValueError as e: print("{}! cfg.metadata is {}".format(e, cfg.metadata), file=sys.stderr) return sfd_path = os.path.join(cfg.backup_path, device + '.sfd') sfdisk(d=cfg.metadata, _out=sfd_path) sfd_path_long = os.path.join(cfg.backup_path, device + '.sfd_long') sfdisk(l=cfg.metadata, _out=sfd_path_long) vgcfg = os.path.join(cfg.backup_path, 'lvm_%s.conf') vgcfgbackup(f=vgcfg) shutil.copy('/etc/fstab', cfg.backup_path) # backup 'fstab' pvs_out = os.path.join(cfg.backup_path, 'pvs_all.text') pvs(o='pv_all', _out=pvs_out) df_out = os.path.join(cfg.backup_path, 'df_h.text') df(h=True, _out=df_out)
def free_space(folder): out = sh.df('-k') for l in out.splitlines(): if folder in l: cur_free_space = int(l.split()[3]) * 1024 return cur_free_space return 0
def get_free_mb(folder): df_result = grep(df("-k", folder), "/") kb_sizes = map(int, filter(is_int, df_result.split(" "))) available_mb = kb_sizes[2] / 1024 return available_mb
def run(self): df = lambda output: sh.awk( sh.df("-h", f"--output={output}", "--total"), "END {print $1}" ).rstrip() result_free = df('avail') result_total = df('size') title_text = title('free disk space') print(f'{title_text}{result_free}/{result_total}')
def check_df(path: Path): _path = path.as_posix() df_result = sh.df("-h").splitlines() found = False for line in df_result: if _path in line: ic(line) found = True if not found: raise ValueError(f"{_path} not in df -h output")
def free_space(self): with self.losetup_context_manager() as device: fs = get_filesystem(device) with mount_context_manager(device) as mountpoint: if fs == 'btrfs': out = btrfs("fi", "usage", "--raw", mountpoint, **SH_OPTS) for line in out: line = line.strip() if line.startswith("Free (estimated):"): return int(line[line.rfind(" ") + 1:-1]) else: output = df("-B1", "--output=avail", mountpoint, **SH_OPTS) return int(output.split("\n")[1].strip())
def _command(cmd=''): if cmd == 'df': result = df('-h') elif cmd == 'ps': result = ps('-ef', _tty_out=False) elif cmd == 'ifconfig': result = ifconfig() elif cmd == 'hostinfo': result = hostinfo() elif cmd == 'sw_vers': result = sw_vers() else: redirect('/') return template(TEXTPAGE, title=cmd.upper(), text=result)
def get_device_size(device, whole_device=False): """ Pobieranie rozmiarów urządzenia/partycji :param already_formatted: Czy jest właśnie sformatowany :return: Rozmiar w bajtach """ try: if not whole_device: # Sprawdzanie systemu plików, jeśli nie jest to NTFS to wysypuje się return int(re.search("\n\/dev\/(?:\w*\s*){3}(\d*)", str(sh.df(device + "1"))).group(1)) * 1024 # Pobieranie rozmiaru całego urządzenia return int(sh.blockdev("--getsize64", device)) except: return -1
def disk_space_monitoring(perc): ''' return name of file systems where percent of usage if greater then 'perc' with bash it looks like on disk_space_monitoring.sh ''' output = awk(grep(df('-Ph'), "-vE", "'^Filesystem|tmpfs'"), '{ print $5,$1 }') for data in output: splitter = data.split('%')[0] try: perc_data = int(splitter) if perc_data > perc: print(data) except: continue
def get_device_size(device, whole_device=False): """ Pobieranie rozmiarów urządzenia/partycji :param already_formatted: Czy jest właśnie sformatowany :return: Rozmiar w bajtach """ try: if not whole_device: # Sprawdzanie systemu plików, jeśli nie jest to NTFS to wysypuje się return int( re.search("\n\/dev\/(?:\w*\s*){3}(\d*)", str(sh.df(device + "1"))).group(1)) * 1024 # Pobieranie rozmiaru całego urządzenia return int(sh.blockdev("--getsize64", device)) except: return -1
def cleanup_mount_partitions(test_env_path, testing_options): """ Make cleanup of partitions dirs @param test_env_path: str path to test environment @param testing_options: dict with special options for testing like available partitions for mounting """ if (testing_options is None or testing_options.get('mounting', None) is None): log_guest("No mounting options found") return available_partitions = testing_options['mounting'].get('partitions', None) if available_partitions is None: log_guest("No available partitions found") return if not os.path.exists(test_env_path): os.mkdir(test_env_path) tmp_mount_dir = f'{test_env_path}/tmp{uuid.uuid4().hex}' if not os.path.exists(tmp_mount_dir): log_guest(f"Creating {tmp_mount_dir} for cleanup of mount partitions") os.mkdir(tmp_mount_dir) for partition in available_partitions: # mount points can be nested and therefore we could umount all the # mounted partitions in the first iteration, we have to check each time # remaining partitions all_mounted_partitions = sh.df().stdout.decode() if not os.path.exists(partition): log_guest(f"{partition} does not exist, removing from the options") available_partitions.remove(partition) continue # if the partition is mounted somewhere in the filesystem, umount it # first and we will mount it to our specific dir if partition in all_mounted_partitions: log_guest(f"Umount of {partition}") sh.umount(partition, '-O --recursive') log_guest(f"Cleanup of device {partition}") sh.mount(partition, tmp_mount_dir, '-text4') shutil.rmtree(tmp_mount_dir, ignore_errors=True) sh.umount(partition) os.rmdir(tmp_mount_dir)
def do_umount(max_attempts=5): for attempt in range(max_attempts): log('looking for device mount %s (%d/%d)', partition, attempt + 1, max_attempts) time.sleep(0.75) try: grep(df('-h'), partition) except ErrorReturnCode: pass else: try: sudo.umount(partition) except ErrorReturnCode: time.sleep(0.5) else: return
def _populate_drive_list(self): self.storage_drives = [] for mounted_name in (next(os.walk("/media/pi"))[1]): mount_stats = sh.tail(sh.df("-h", "/media/pi/" + mounted_name), "-1") filtered_stats = filter(None, mount_stats.split(' ')) display_name = filtered_stats[1] + "\n------\n" + mounted_name source_storage = tk.Button(self.source_frame, text=display_name, command=self._create_callback_lambda( mounted_name, 'source')) dest_storage = tk.Button(self.dest_frame, text=display_name, command=self._create_callback_lambda( mounted_name, 'dest')) source_storage_obj = StorageClass(filtered_stats[1], mounted_name, source_storage, 'source') dest_storage_obj = StorageClass(filtered_stats[1], mounted_name, dest_storage, 'dest') self.storage_drives.append(source_storage_obj) self.storage_drives.append(dest_storage_obj) source_row_count = 1 dest_row_count = 1 for storage_drive in self.storage_drives: if storage_drive.locale == 'source': storage_drive.button.grid(row=source_row_count, column=0, padx=5, pady=5, sticky=tk.W + tk.E) source_row_count += 1 elif storage_drive.locale == 'dest': storage_drive.button.grid(row=dest_row_count, column=0, padx=5, pady=5, sticky=tk.W + tk.E) dest_row_count += 1
def get_system_summary_str(): SECURITY_UPDATES_COUNT_COMMAND = 'apt list --upgradable 2>/dev/null | grep "\-security" | wc -l' security_updates_count = sh.bash('-c', SECURITY_UPDATES_COUNT_COMMAND) last_week_logins = get_last_week_logins() last_week_logins_str = '' for i, login_dict in enumerate(last_week_logins): last_week_logins_str += 'User: {}\n'.format(login_dict['user']) ip = login_dict['ip'] if ip != None: last_week_logins_str += 'IP: {}\n'.format(ip) last_week_logins_str += 'Date: {}\n'.format(login_dict['date']) if i < len(last_week_logins) - 1: last_week_logins_str += '--\n' available_space_str = '' for i, line in enumerate(sh.df('-h', '--total')): line = line.strip().split() if line[0] != 'Filesystem': if line[5] == '-': line[5] = 'TOTAL' available_space_str += '{} {}\n'.format(line[4], line[5]) uptime_str = sh.uptime('-p') return \ '''\ ⏳🖥️ *UPTIME*: {} 🔔📋 *SECURITY UPDATES AVAILABLE*: {} 💻 *LAST WEEK LOGINS* `{}` 💽 *% DISK USAGE* `{}`\ '''.format(uptime_str.strip(), security_updates_count.strip(), last_week_logins_str.strip(), available_space_str.strip())
def get_top_stats(): nproc = int(sh.nproc('--all')) ctext = [l for l in sh.free().split('\n') if l.startswith('Mem:')][0] tkns = [t for t in ctext.split() if len(t)] mem_total, mem_used = map(int, tkns[1:3]) ctext = [l for l in sh.df().split('\n') if 'vg-root' in l][0] tkns = [t for t in ctext.split() if len(t)] hdd_used, hdd_avail = map(int, tkns[2:4]) ctext = sh.top('-b', '-n1').split('\n') lavg = float(ctext[0].split('load average:')[1].strip().split()[0][:-1]) ctext = ctext[7:] procs = [] for line in ctext: line = [t for t in line.split() if len(t)] if len(line) != 12: continue pid, uid, cpu, mem, cmd = line[0], line[1], float(line[8]), float(line[9]), line[11] if cpu < 50 and mem < 5: continue procs.append(dict(pid=pid, uid=uid, cpu=cpu, mem=mem, cmd=cmd)) return nproc, lavg, mem_total, mem_used, hdd_avail, hdd_used, procs
print(ls('-l', '../DATA/parrot.txt')) print('-' * 50) ll = ls.bake(l=True) print(ll('../DATA')) print('-' * 50) from sh import ls, glob print(ls('-ld', glob('/etc/pr*'))) print('-' * 50) w = sh.who() print(w) print('-' * 50) disk_usage = sh.df('-h') print(disk_usage) print('-' * 50) from sh import uname print(uname()) print(uname('-a')) print(uname(a=True)) print('-' * 50) from sh import grep, wc # grep 'sh' /etc/passwd | wc -l print(grep('sh', '/etc/passwd')) print(wc(grep('sh', '/etc/passwd'), l=True)) print('-' * 50)
#!/usr/bin/env python from sh import ls, who, df # <1> ls('-l', '../DATA') # <2> print('-' * 50) w = who() # <3> print(w) print('-' * 50) diskfull = df('-h') # <4> print(diskfull)
def cli( ctx, destination_folder: Path, zpool_size_mb: int, recordsize: str, large_dnode: bool, no_acl: bool, verbose: Union[bool, int, float], verbose_inf: bool, loopback: bool, record_count: int, ipython: bool, ): tty, verbose = tv( ctx=ctx, verbose=verbose, verbose_inf=verbose_inf, ) if os.getuid() != 0: ic("must be root") sys.exit(1) if zpool_size_mb < 64: raise ValueError("minimum zpool size is 64MB") timestamp = str(time.time()) if verbose: ic(timestamp) if loopback: free_loop = sh.losetup("--find").splitlines() loop = Path(free_loop[0]) if not path_is_block_special(loop): raise ValueError(f"loop device path {loop} is not block special") loops_in_use = sh.losetup("-l").splitlines() # ic(loops_in_use) for line in loops_in_use: if loop in loops_in_use: raise ValueError(f"loop device {loop} already in use") destination = Path(destination_folder) / Path(f"zfstester_{timestamp}") os.makedirs(destination) destination_pool_file = destination / Path(f"test_pool_{timestamp}") destination_pool_file = destination_pool_file.resolve() if verbose: ic(destination_pool_file) sh.dd( "if=/dev/zero", f"of={destination_pool_file.as_posix()}", f"bs={zpool_size_mb}M", "count=1", ) # dd if=/dev/urandom of=temp_zfs_key bs=32 count=1 || exit 1 # key_path=`readlink -f temp_zfs_key` if loopback: sh.losetup(loop, destination_pool_file, loop) atexit.register(cleanup_loop_device, loop) if verbose: ic(sh.losetup("-l")) zpool_name = destination_pool_file.name if verbose: ic(zpool_name) zpool_create_command = sh.Command("zpool") zpool_create_command = zpool_create_command.bake( "create", "-O", "atime=off", "-O", "compression=lz4", "-O", "mountpoint=none", "-O", f"recordsize={recordsize}", zpool_name, ) if loopback: zpool_create_command = zpool_create_command.bake(loop) else: zpool_create_command = zpool_create_command.bake(destination_pool_file) zpool_create_command_result = zpool_create_command().splitlines() ic(zpool_create_command_result) # run_command(zpool_create_command, verbose=True) # atexit.register(destroy_zfs_pool, zpool_name) zfs_mountpoint = Path(f"{destination_pool_file.as_posix()}_mountpoint") zfs_filesystem = f"{zpool_name}/spacetest" zfs_create_command = sh.Command("zfs") zfs_create_command = zfs_create_command.bake( "create", "-o", f"mountpoint={zfs_mountpoint.as_posix()}", "-o", f"recordsize={recordsize}", ) if large_dnode: zfs_create_command = zfs_create_command.bake("-o", "dnodesize=auto") if no_acl: zfs_create_command = zfs_create_command.bake("-o", "acl=off") zfs_create_command = zfs_create_command.bake(zfs_filesystem) zfs_create_command_result = zfs_create_command().splitlines() ic(zfs_create_command_result) atexit.register(umount_zfs_filesystem, zfs_mountpoint) # atexit.register(destroy_zfs_filesystem, zfs_filesystem) # disabled just for pure space tests # zfs create -o encryption=on -o keyformat=raw -o keylocation=file://"${key_path}" -o mountpoint=/"${destination_pool_file}"/spacetest_enc "${destination_pool_file}"/spacetest_enc || exit 1 check_df(destination_pool_file) try: make_things(root=zfs_mountpoint, count=None, thing_function=os.makedirs) except Exception as e: ic(e) ic(sh.ls("-alh", zfs_mountpoint)) check_df(destination_pool_file) sh.sync() pathstat_results = pathstat(path=zfs_mountpoint, verbose=verbose) display_results(pathstat_results, verbose=verbose) # 128K recordsize: 81266 # 512 recordsize: 80894 zfs_get_all_command_results_interesting_lines = [] zfs_get_all_command = sh.Command("zfs") zfs_get_all_command = zfs_get_all_command.bake("get", "all") zfs_get_all_command_results = zfs_get_all_command().splitlines() interesting_fields = [ "used", "available", "referenced", "compressratio", "recordsize", "checksum", "compression", "xattr", "copies", "version", "usedbysnapshots", "usedbydataset", "usedbychildren", "usedbyrefreservation", "dedup", "dnodesize", "refcompressratio", "written", "logicalused", "logicalreferenced", "acltype", "redundant_metadata", "encryption", "snapshot_count", "special_small_blocks", ] for line in zfs_get_all_command_results: if destination_pool_file.name in line: if line.split()[1] in interesting_fields: zfs_get_all_command_results_interesting_lines.append(line) print(line) print("\nInteresting lines from above:") for line in zfs_get_all_command_results_interesting_lines: print(line) df_inodes = str(sh.df("-i")) # ic(df_inodes) print() for index, line in enumerate(df_inodes.splitlines()): if index == 0: print(line) # df -i header if destination_pool_file.name in line: df_line = line print(df_line) Inodes, IUsed, IFree, IUse = df_line.split()[1:5] destination_pool_file_rzip = destination_pool_file.as_posix() + ".rz" sh.rzip("-k", "-9", "-o", destination_pool_file_rzip, destination_pool_file.as_posix()) compressed_file_size = os.stat(destination_pool_file_rzip).st_size destination_pool_file_sparse_copy = Path(destination_pool_file.as_posix() + ".sparse") sh.cp( "-v", "-i", "--sparse=always", destination_pool_file, destination_pool_file_sparse_copy, ) destination_pool_file_sparse_copy_file_size = ( os.stat(destination_pool_file_sparse_copy).st_blocks * 512) # ic(compressed_file_size) print("\nSummary:") # ic(pathstat_results) print("pool file:") os.system(" ".join(["/bin/ls", "-al", destination_pool_file.as_posix()])) bytes_in_names = pathstat_results["bytes_in_names"] objects_created = pathstat_results[4] print() print( f"The {zpool_size_mb}MB pool ran out of free inodes (there are {IFree} out of {Inodes} left) after {bytes_in_names} bytes were written by creating {objects_created} empty directories (with random uncompressable names, under the root).\nCompressed, the pool file takes {compressed_file_size} bytes." ) compression_ratio = (compressed_file_size / (zpool_size_mb * 1024 * 1024)) * 100 print("compresson ratio:", str(round(compression_ratio, 2)) + "x") print( f"A sparse copy of the pool file is {destination_pool_file_sparse_copy_file_size}B (~{int(destination_pool_file_sparse_copy_file_size/1024/1024)}MB)" ) if ipython: import IPython IPython.embed()
def ui_command_df(self): ''' df - report file system disk space usage ''' return sh.df('-h')
#!/usr/bin/env python import sh print sh.ls('-l','/tmp') print '-' * 50 w = sh.who() print w print '-' * 50 diskfull = sh.df('-h') print diskfull
print glob.glob('*.py') print math.pi #for line in urllib2.urlopen("http://baidu.com"): # print line s = "www.eddysun.com" t = zlib.compress(s) n = zlib.decompress(t) print len(s) print s print len(t) print t print len(n) print n print "-----------------------------------" print math.sin(math.pi/4) print ls("-la") print df("-h") #exit program directory sys.exit()
#!/usr/bin/env python import sh print(sh.ls('-l', '../DATA')) # <1> print('-' * 50) w = sh.who() # <2> print(w) print('-' * 50) diskfull = sh.df('-h') # <3> print(diskfull)
def to_computer(self, text): f = open( TMP_FILE, 'w' ) # write mode clears any previous content from the file if it exists if self.checks[0]: print("Saving: inxi to file") f.write( HEADER.format("Inxi -Fxzc0", "Listing computer information")) try: f.write(str(inxi('-Fxxxzc0'))) except: " 'inxi' not found, install it to get this info" f.write('\n') if self.checks[1]: print("Getting info about installed graphical driver") f.write( HEADER.format("Installed drivers", "Shows which graphic driver is installed")) try: f.write(str(mhwd('-li'))) except: print(" 'mhwd' not found, this is not Manjaro?") f.write('\n') if self.checks[2]: print("Getting list of all drivers supported on detected gpu's") f.write( HEADER.format( "Available drivers", "list of all drivers supported on detected gpu's")) try: f.write(str(mhwd('-l'))) except: print(" 'mhwd' not found, this is not Manjaro?") # f.write('\n') if self.checks[3]: print('hwinfo -graphic card') # os.system('hwinfo --gfxcard') f.write(HEADER.format("hwinfo --gfxcard", "Show Graphic Card info")) try: f.write(str(hwinfo('--gfxcard'))) except: print('hwinfo graphic card info error') f.write('hwinfo graphic card info error') f.write('\n') if self.checks[4]: print('memory info') # os.system('free -h') f.write(HEADER.format("Memory Info", "Info about Memory and Swap")) try: f.write(str(free(' -h'))) except: print('memory info error') f.write('memory info error') f.write('\n') if self.checks[5]: print('disk info') # os.system('lsblk') f.write(HEADER.format("Disk Info", "Disks and Partitions")) try: f.write(str(lsblk())) except: print('lsblk error') f.write('lsblk error') f.write('\n') if self.checks[6]: print('free disk space') # os.system('df') f.write( HEADER.format("Free Disk Space", "Free space per pertition")) try: f.write(str(df())) except: print('free disk space error') f.write('free disk space error') f.write('\n') if self.checks[7]: print('blockdev') # os.system('blockdev --getalignoff /dev/sda') f.write(HEADER.format("Disk Alignment", "0 is OK")) try: # f.write(str(os.system('blockdev --getalignoff /dev/sda'))) f.write(str(blockdev(' --getalignoff /dev/sda'))) except: print('error with blockdev') f.write('error with blockdev') f.write('\n') if self.checks[8]: print('BIOS / UEFI') # os.system('test -d /sys/firmware/efi && echo UEFI || echo BIOS') # os.system('parted -l | grep "Partition Table: "') f.write(HEADER.format("parted -l", "BIOS+msdos or UEFI+gpt")) try: f.write( str(test( ' -d /sys/firmware/efi && echo UEFI || echo BIOS'))) # f.write(str(test -d(' /sys/firmware/efi && echo UEFI || echo BIOS'))) f.write(str(parted(' -l | grep "Partition Table: "'))) # f.write(str(parted -l(' | grep "Partition Table: "'))) except: print('error with BIOS / UEFI') f.write('error with BIOS / UEFI') f.write('\n') if self.checks[9]: print("Saving: Xorg.0.log to file") f.write( HEADER.format("Xorg.0.log", "searching for: failed, error & (WW) keywords")) try: f.write( look_in_file('/var/log/Xorg.0.log', ['failed', 'error', '(WW)'])) except FileNotFoundError: print("/var/log/Xorg.0.log not found!") f.write("Xorg.0.log not found!") f.write('\n') if self.checks[10]: print("Saving: Xorg.1.log to file") f.write( HEADER.format("Xorg.1.log", "searching for: failed, error & (WW) keywords")) try: f.write( look_in_file('/var/log/Xorg.1.log', ['failed', 'error', '(WW)'])) except FileNotFoundError: print("/var/log/Xorg.1.log not found!") f.write("Xorg.1.log not found!") f.write('\n') if self.checks[11]: print("Saving: pacman.log to file") f.write( HEADER.format( "pacman.log", "searching for: pacsave, pacnew, pacorig keywords")) try: f.write( look_in_file('/var/log/pacman.log', ['pacsave', 'pacnew', 'pacorig'])) except FileNotFoundError: print( "/var/log/pacman.log not found, this is not Manjaro or Arch based Linux?" ) f.write("pacman.log not found! Not Arch based OS?") f.write('\n') if self.checks[12]: print("Saving: journalctl (emergency) to file") os.system("journalctl -b > /tmp/journalctl.txt") f.write( HEADER.format("journalctl.txt", "Searching for: Emergency keywords")) f.write( look_in_file('/tmp/journalctl.txt', ['emergency', 'Emergency', 'EMERGENCY'])) f.write('\n') if self.checks[13]: print("Saving: journalctl (alert) to file") os.system("journalctl -b > /tmp/journalctl.txt") f.write( HEADER.format("journalctl.txt", "Searching for: Alert keywords")) f.write( look_in_file('/tmp/journalctl.txt', ['alert', 'Alert', 'ALERT'])) f.write('\n') if self.checks[14]: print("Saving: journalctl (critical) to file") os.system("journalctl -b > /tmp/journalctl.txt") f.write( HEADER.format("journalctl.txt", "Searching for: Critical keywords")) f.write( look_in_file('/tmp/journalctl.txt', ['critical', 'Critical', 'CRITICAL'])) f.write('\n') if self.checks[15]: print("Saving: journalctl (failed) to file") os.system("journalctl -b > /tmp/journalctl.txt") f.write( HEADER.format("journalctl.txt", "Searching for: Failed keywords")) f.write( look_in_file('/tmp/journalctl.txt', ['failed', 'Failed', 'FAILED'])) f.write('\n') if self.checks[16]: print("Saving: rc.log to file") f.write( HEADER.format("rc.log", "OpenRc only! searching for: WARNING: keywords")) try: f.write(look_in_file('/var/log/rc.log', ['WARNING:'])) except FileNotFoundError: print("/var/log/rc.log not found! Systemd based OS?") f.write("rc.log not found! Systemd based OS?") f.write('\n') if self.checks[17]: print('openrc services status') # os.system('rc-status --all') f.write(HEADER.format("Services", "OpenRc only!")) try: f.write(str(rc - status(' --all'))) except: print("rc-status all error") f.write("rc.log not found! Systemd based OS?") f.write('\n') f.close()
def disk(self): mAvailMount = sh.awk( 'NR!=1 {print $5}', _in=sh.df("-lh")).replace("\n", " ").strip() return mAvailMount