def __stop_user_rackhd(self): """ Stop RackHD services from user provided RackHD code repo """ get_pid_cmd = ['ps aux | grep node| grep index.js | sed "/grep/d"| ' \ 'sed "/sudo/d" | awk \'{print $2}\' | sort -r -n'] output = utils.robust_check_output(cmd=get_pid_cmd, shell=True) process_list = output["message"].strip("\n").split("\n") for pid in process_list: pid_service_name = self.__get_pid_executing_path(pid).split("/")[-1] kill_pid_cmd = ["kill", "-9", pid] result = utils.robust_check_output(kill_pid_cmd) description = "Stop RackHD service {}".format(pid_service_name) Logger.record_command_result(description, 'error', result)
def __stop_user_rackhd(self): """ Stop RackHD services from user provided RackHD code repo """ get_pid_cmd = ['ps aux | grep node | sed "/grep/d"| ' \ 'sed "/sudo/d" | awk \'{print $2}\' | sort -r -n'] output = utils.robust_check_output(cmd=get_pid_cmd, shell=True) process_list = output["message"].strip("\n").split("\n") for pid in process_list: pid_service_name = self.__get_pid_executing_path(pid).split("/")[-1] if pid_service_name not in self.services: continue kill_pid_cmd = ["kill", "-9", pid] result = utils.robust_check_output(kill_pid_cmd) description = "Stop RackHD service {}".format(pid_service_name) Logger.record_command_result(description, 'error', result)
def __start_user_rackhd(self): """ Start RackHD services from user provided RackHD code repo """ for service in self.services: description = "Start RackHD service {}".format(service) os.chdir(os.path.join(self.source_code_path, service)) cmd = ["node index.js > /dev/null 2>&1 &"] # RackHD services need run in background result = utils.robust_check_output(cmd=cmd, shell=True) Logger.record_command_result(description, 'error', result)
def __get_version_from_dpkg(self, service): """ Check RackHD version from 'dpkg -l' output """ # dpkg -l output example: # ii on-http 2.0.0-20170316UTC-25c81ec amd64 RackHD HTTP engine service cmd = ["dpkg -l | grep {} | awk '{{print $3}}'".format(service)] result = utils.robust_check_output(cmd=cmd, shell=True) if not result["message"]: result["exit_code"] = -1 return result
def __get_pid_executing_path(self, pid): """ Get Linux pid executing path :param pid: Linux process id :return: pid executing path string, an example: "/home/onrack/src/on-http" """ # ls -l /proc/<pid> output example # lrwxrwxrwx 1 root root 0 Mar 28 14:11 cwd -> /home/onrack/src/on-http cmd = ["sudo ls -l /proc/{0} | grep cwd | awk '{{print $NF}}'".format(pid)] output = utils.robust_check_output(cmd, shell=True) return output["message"].strip("\n")
def __operate_regular_rackhd(self, operator): """ Start or stop RackHD services from regular RackHD code repo /var/renasar/ :param operator: operator for RackHD service, should be "start" or "stop" """ for service in self.services: description = "{} RackHD service {}".format( operator.capitalize(), service) cmd = ["service", service, operator] result = utils.robust_check_output(cmd) Logger.record_command_result(description, 'error', result)
def __operate_regular_rackhd(self, operator): """ Start or stop RackHD services from regular RackHD code repo /var/renasar/ :param operator: operator for RackHD service, should be "start" or "stop" """ for service in self.services: description = "{} RackHD service {}".format(operator.capitalize(), service) cmd = ["service", service, operator] result = utils.robust_check_output(cmd) if not result["exit_code"]: result["message"] = "" Logger.record_command_result(description, 'error', result)
def get_disk_capacity(self): """ Get disk capacity, fdisk -l output first line example: Disk /dev/sda: 104.9 GB, 104857600000 bytes """ result = utils.robust_check_output(self.disk_command) info_list = result["message"].strip("\n").strip(" ").split("\n") disk_info = info_list[0] pattern = re.compile("\d{0,5}\.?\d{0,3}\s+(T|G|M)B", re.I) match = pattern.search(disk_info) capacity = match.group() description = "RackHD server disk capacity: {}".format(capacity) Logger.record_log_message(description, "info", "") return capacity
def check_service_process(self): """ Check RackHD required services running status """ for service in self.services: cmd = ["service", service, "status"] result = utils.robust_check_output(cmd) description = "Check service {} running status".format(service) if result["exit_code"] == 0: output = result["message"] if output.find("running") == -1: result["exit_code"] = -1 else: result["message"] = "" # message is not necessary for successful service Logger.record_command_result(description, "error", result)
def get_disk_capacity(self): """ Get disk capacity, fdisk -l output first line example: Disk /dev/sda: 104.9 GB, 104857600000 bytes """ result = utils.robust_check_output(self.disk_command, shell=False, redirect=True) info_list = result["message"].strip("\n").strip(" ").split("\n") capacity = "" for disk_info in info_list: pattern = re.compile("Disk /dev/sd[a-z]{1,3}:\s+(\d{0,5}\.?\d{0,3}\s+(T|G|M)i?B)", re.I) match = pattern.search(disk_info) if match: capacity = match.group(1) break description = "RackHD server disk capacity: {}".format(capacity) Logger.record_log_message(description, "info", "") return capacity
def get_mem_info(self): """ Get CPU information, command output first two lines example: total used free shared buffers cached Mem: 31G 8.8G 22G 4.1M 689M 6.3G """ result = utils.robust_check_output(self.memory_command) info_list = result["message"].strip("\n").strip(" ").split("\n") title_list = info_list[0].strip(" ").split(" ") data_list = info_list[1].strip(" ").split(" ") title_list = [x for x in title_list if x] data_list = [x for x in data_list if x] del data_list[0] mem_info = {} for title, data in zip(title_list, data_list): mem_info[title] = data description = "RackHD server memory size: {}".format(mem_info["total"]) Logger.record_log_message(description, "info", "") return mem_info
def get_cpu_info(self): """ Get CPU information, command output example: CPU(s): 4 CPU MHz: 2693.509 L1d cache: 32K L1i cache: 32K L2 cache: 256K L3 cache: 20480K """ result = utils.robust_check_output(self.cpu_command) info_list = result["message"].strip("\n").strip(" ").split("\n") cpu_info = {} for info in info_list: info = info.split(":") cpu_info[info[0].strip(" ")] = info[-1].strip(" ") description = "RackHD server CPU info: {} cores, {}MHz,".format( cpu_info["CPU(s)"], cpu_info["CPU MHz"]) Logger.record_log_message(description, "info", "") return cpu_info