def install_fb(cls): installer = cls.download_fb() LogHelper.info("start to install") if PlatformHelper.is_Linux() and PlatformHelper.get_arch() == "deb-64": install_cmd = "dpkg -i %s" % installer cls.__set_fb_path("/etc/filebeat/") if PlatformHelper.is_Linux() and PlatformHelper.get_arch() == "deb-32": install_cmd = "rpm -vi %s" % installer cls.__set_fb_path("/etc/filebeat/") if PlatformHelper.is_win(): import zipfile win_fb_dir = "C:\\filebeat" target = os.path.join(win_fb_dir, "filebeat-5.2.0-windows-x86_64") installcommnd = os.path.join(target, "install-service-filebeat.ps1") zip_ref = zipfile.ZipFile(installer, 'r') zip_ref.extractall(win_fb_dir) zip_ref.close() # FileHelper.rename() install_cmd = "powershell.exe %s" % (installcommnd) cls.__set_fb_path(target) cmd_output = CmdHelper.runas_admin(install_cmd) return cmd_output # # TODO: Unzip to start" service # install_cmd = "to be implemented" # win_fb_dir = "C:\\filebeat" # if not FileHelper.dir_exist(win_fb_dir): # FileHelper.create_directory(win_fb_dir) # cls.__set_fb_path(win_fb_dir) # install_cmd = "unzip %s -C %s" % (installer, win_fb_dir) if PlatformHelper.is_mac(): mac_fb_dir = "/filebeat" if not FileHelper.dir_exist(mac_fb_dir): FileHelper.create_directory(mac_fb_dir) cls.__set_fb_path(mac_fb_dir) install_cmd = "tar xzvf %s -C %s" % (installer, mac_fb_dir) cmd_output = CmdHelper.run(install_cmd) LogHelper.info(cmd_output)
def start_fb(cls): if PlatformHelper.is_mac(): fb_binary_dir = os.path.join(cls.config_path, cls.package_name.split(".tar.gz")[0]) os.chdir(fb_binary_dir) cmd = "./filebeat -c filebeat.yml &" output = os.system(cmd) if PlatformHelper.is_Linux(): cmd = "service filebeat start" output = CmdHelper.run(cmd) if PlatformHelper.is_win(): cmd = "sc start filebeat" # This is to make sure filebeat service can be started after install time.sleep(1) output = CmdHelper.run(cmd) # service = "Get-WmiObject -ClassWin32_Service-Filter name='filebeat'" # service.StartService() # service.StopService() LogHelper.info("start filebeat result %s" % output) return output
def is_fb_running(cls): result = False if PlatformHelper.is_Linux(): cmd = "service filebeat status" cmd_output = CmdHelper.run(cmd) LogHelper.debug(cmd_output) if cmd_output.find("running") >= 0: result = True if PlatformHelper.is_mac(): cmd = "ps aux | grep 'filebeat -c' | grep -v 'grep'" output = CmdHelper.run(cmd) if output.find("filebeat") >= 0: result = True if PlatformHelper.is_win(): cmd = "sc query filebeat" output = CmdHelper.run(cmd) if output.find("RUNNING") >= 0: result = True if result: LogHelper.debug("filebeat is running") else: LogHelper.info("filebeat is not running") return result
def kill_fb(cls): """ kill file beat process :return: """ result = False if PlatformHelper.is_mac() or PlatformHelper.is_Linux(): cmd = "ps aux | grep 'filebeat -c' | grep -v 'grep'" output = CmdHelper.run(cmd) process_id = output.split()[1] kill_fb_cmd = "kill -9 %s" % process_id output = CmdHelper.run(kill_fb_cmd) if PlatformHelper.is_win(): cmd = "sc stop filebeat" output = CmdHelper.run(cmd) LogHelper.debug(output) if cls.is_fb_running(): LogHelper.error( "filebeat service CAN NOT be stopped successfully.") else: LogHelper.info("filebeat service is stopped") result = True return result
def get_log_path(cls): if PlatformHelper.is_Linux(): logger_path = ConfigAdapter.log_path("LINUX") elif PlatformHelper.is_win(): logger_path = ConfigAdapter.log_path("WINDOWS") elif PlatformHelper.is_mac(): logger_path = ConfigAdapter.log_path("MAC") return logger_path
def __create_default_logger(cls): if PlatformHelper.is_Linux(): logger_path = os.path.join("/", "tmp") elif PlatformHelper.is_win(): logger_path = os.path.join("c:", "tmp") elif PlatformHelper.is_mac(): logger_path = os.path.join("/", "tmp") log_filepath = os.path.join(logger_path, "automation.log") return cls.create_logger(log_filepath)
def get_started(file, start): """ Get the started line number from log. :param file: file :param start: time after start :return: int(line number) """ if PlatformHelper.is_win(): search_pattern = re.compile("^(.*\s.*)\s.*\.exe.*$", re.IGNORECASE) elif PlatformHelper.is_Linux(): search_pattern = re.compile("^(.*)\.\d{6}.*$", re.IGNORECASE) indices = FileHelper.find_text_indices(file, search_pattern) return indices[0]
def get_backup_start(file): search_pattern = "start backup" if PlatformHelper.is_win(): search_pattern = "Starting backup:" elif PlatformHelper.is_Linux(): search_pattern = "Executing : start" elif PlatformHelper.is_mac(): search_pattern = "Starting backup:" else: pass indices = FileHelper.find_text_indices(file, search_pattern) if indices: return indices[-1]
def create_file(path, overwrite=True, size=1024, content="random"): """ method to create test files """ basedir = os.path.dirname(path) FileHelper.create_directory(basedir) if PlatformHelper.is_Linux(): # or PlatformHelper.is_mac(): cmd = 'sudo chmod -R 777 {path}'.format(path=basedir) CmdHelper.run(cmd) if content == "random": FileHelper._create_file_random(path, overwrite, size, 1024) else: FileHelper._create_file_pattern(path, overwrite, size, content)
def remove_tree(dir, topdown=True): for root, dirs, files in os.walk(dir, topdown=False): for name in files: filename = os.path.join(root, name) os.chmod(filename, stat.S_IWUSR) os.remove(filename) for name in dirs: try: os.removedirs(os.path.join(root, name)) except OSError as e: if PlatformHelper.is_Linux( ): # or PlatformHelper.is_mac(): CmdHelper.run('sudo rm -rf {path}'.format( path=os.path.join(root, name)))
def create_directory(path, recursive=True): """ :param path: directory path :return: path """ path = os.path.abspath(path) sub_path = os.path.dirname(path) if not os.path.exists(sub_path) and recursive: FileHelper.create_directory(sub_path) if not os.path.exists(path): try: os.mkdir(path) except OSError: if PlatformHelper.is_Linux(): # or PlatformHelper.is_mac(): cmd = 'sudo mkdir -p {path}'.format(path=path) CmdHelper.run(cmd) return path
def config_fb(cls, new_config={}): if PlatformHelper.is_mac(): cls.filebeat_config = os.path.join( cls.config_path, cls.package_name.split(".tar.gz")[0], "filebeat.yml") if PlatformHelper.is_Linux() or PlatformHelper.is_win(): cls.filebeat_config = os.path.join(cls.config_path, 'filebeat.yml') # yaml_file = open(cls.filebeat_config, 'r') # config = yaml.load(yaml_file) # print config with open(cls.filebeat_config, 'w') as f: config_dict = yaml.dump(new_config, f) return config_dict
def get_kpis(file, starttime=0): """ Get service KPIs :param file: Mozy backup log :param starttime: Only analyse latest log :return: Key Service KPI. e.g.["/client/sync": response time, "/batch": response time] """ kpi = None with open(file, "r") as f: for line_no, line in enumerate(f.readlines()): if PlatformHelper.is_win(): KPIHelper.get_windows_kpi(kpi, line, starttime) #datetime.datetime.strptime(starttime, '%d%b%Y %H:%M:%S').strftime('%Y-%m-%dT%H:%M:%SZ')) elif PlatformHelper.is_Linux(): KPIHelper.get_linux_kpi(kpi, line.strip(), starttime) elif PlatformHelper.is_mac(): KPIHelper.get_mac_kpis(file, line.strip())
def is_fb_installed(cls): result = False if PlatformHelper.is_Linux(): cmd = "service filebeat status" if PlatformHelper.is_mac(): cmd = "ps aux | grep 'filebeat -c' | grep -v 'grep'" if PlatformHelper.is_win(): cmd = "sc query filebeat" output = CmdHelper.run(cmd) LogHelper.debug(output) match = re.search(r"(FAILED)", output, re.IGNORECASE) if match: LogHelper.info("filebeat is not installed") else: result = True LogHelper.debug("filebeat is installed") return result