#!/usr/bin/python # -*- coding: utf-8 -*- from shutil import copytree, ignore_patterns, copy, make_archive from onl.platform.current import OnlPlatform import errno import subprocess import os import logging BASE_ROOT = "/tmp/" DIR_NAME = "onl-support-" + OnlPlatform().PLATFORM DIR_ROOT = BASE_ROOT + DIR_NAME COMMAND_ROOT = DIR_ROOT + "/support/" TARFILE_NAME = DIR_NAME + ".tar.gz" TARFILE = BASE_ROOT + TARFILE_NAME # Set logger logger = logging.getLogger('onl-support') logger.setLevel(logging.DEBUG) ch = logging.StreamHandler() ch.setLevel(logging.INFO) logger.addHandler(ch) def copy_dir(src_dir): dest = DIR_ROOT + src_dir try: copytree(src_dir, dest, symlinks=True, ignore=ignore_patterns('alternatives', '*.deb', '*.swi')) except OSError as e: if e.errno == errno.ENOTDIR: copy(src_dir, dest) else: logger.debug('Directory not copied. Error: {}'.format(e)) def dump_command(command, output_filename=None): # Create directory if not exists if not os.path.exists(COMMAND_ROOT): os.makedirs(COMMAND_ROOT) # Defined filename if output_filename: dest = COMMAND_ROOT + output_filename # Create new filename else: dest = COMMAND_ROOT + command # Filename using command name # Output result try: log_file = open(dest, "w+") command = command.split() # str() split to `list` proc = subprocess.Popen(command, stdout=log_file, stderr=log_file) proc.wait() log_file.close() except Exception as e: logger.debug('Failed to open file: {}'.format(e)) def main(): copy_dir_list = ['/etc', '/var/log', '/mnt/onl'] dump_command_list = ['onl-sysconfig', 'lsmod', 'ifconfig', 'dmesg', 'uptime', 'df', 'blkid', 'onlpdump', 'onl-platform-show', ['ntpdc -p', 'ntpdc.result'], ['ps auxww', 'ps.result'], ] logger.info("Dump Information...") for dir_name in copy_dir_list: copy_dir(dir_name) logger.debug('Dump directory {} '.format(dir_name)) for command in dump_command_list: if isinstance(command, str): dump_command(command) elif isinstance(command, list): dump_command(command[0], command[1]) logger.debug('Dump command {} '.format(command)) # Tarfile logger.info("Processing...") archive_path = make_archive(DIR_NAME, 'gztar', DIR_ROOT) logger.info("Support tarball created: {}".format(archive_path)) if __name__ == "__main__": main()