def verify_nodes(self, sedna_config_value): """ Verify the status of the given nodes :param sedna_config_value:the SednaConfig Object containing the info of port and nodes :return: the verification result of nodes """ node_results = [] node_services_result = [] logger = LOG() log_root = logger.root() for node in sedna_config_value.nodes: raw_node_result = self._rpc_client.connect( ip=node.ip, group=node.group, port=sedna_config_value.port, node_services=node.services) for service_result in raw_node_result: service = Service(**service_result["service"]) result =\ Result(service=service, status=service_result["status"]) node_services_result.append(result) node_result =\ NodeResult(group=node.group, ip=node.ip, result=node_services_result) node_results.append(node_result) log_root.debug("Master return:%s" % node_results) return node_results
def sample(self, services): """ The entrance to start the logic to check the status of the given services on the current node. :param services: the list of services Object whose status to check :return: the verification results which is a list of Result Object """ results = [] logger = LOG() logger_root = logger.root() logger_tester = logger.tester() logger_root.debug( "NodeSampler:sample has been called by sampler server.") logger_tester.debug( "NodeSampler:sample has been called by sampler server.") for service in services: checker_class = self._checker_map[service.check_methods] logger_root.debug( "service.name:%s service.check_method: %s service.checker:%s" % (service.name, service.check_methods, checker_class)) if checker_class: logger_tester.info("%s", checker_class) checker = checker_class() service_status = checker.check(service.name) service_result = Result(service, service_status) results.append(service_result) else: raise Exception( "The '%s' checker used to check '%s' cannot be find in sampler node." % (service.check_methods, service.name)) logger_root.debug("NodeSampler return :%s" % results) return results
def start(self): """ Start the server. """ log = LOG() log_root = log.root() log_root.info("Listening in port %s............" % self._port) self._server.serve_forever()
def register_checker(self, name, klass): self._checker_map[name] = klass logger = LOG() log_root = logger.root() log_root.debug( "Register Checker: {Check method: %s , Checker registered: %s}" % (name, klass)) log_tester = logger.tester() log_tester.info( "Register Checker: {Check method: %s , Checker registered: %s}" % (name, klass))
def connect(self, ip, group, port, node_services): log = LOG() log_root = log.root() url = "http://" + ip + ":" + port log_root.debug( "Sent from Master-node_services: %s -url: %s -group: %s " % (node_services, url, group)) proxy = ServerProxy(url) raw_node_result = proxy.sample(node_services) log_root.info( "Raw Info received from Sampler: %s" % raw_node_result) return raw_node_result
def format_output_log(self, node_results): log = LOG() log_root = log.root() for node_result in node_results: for result in node_result.result: if result.status != "active": log_root.info( "[ip:%s, group:%s][service[name:%s, ip:%s, method:%s], status:%s]]" % (node_result.ip, node_result.group, result.service.name, result.service.ip, result.service.check_methods, result.status)) log_root.error( "[ip:%s, group:%s][service[name:%s, ip:%s, method:%s], status:%s]]" % (node_result.ip, node_result.group, result.service.name, result.service.ip, result.service.check_methods, result.status))
def sample(self, services): """ The wrapper of sample method to convert service dictionaries to service object :param services: the list of Service dicts which is transformed from Service Obj to dict form :return: services sample result """ list_services = [] log = LOG() log_root = log.root() log_tester = log.tester() log_root.info("Receive raw info from Master: %s" % services) for service in services: list_services.append(Service(**service)) log_root.info("Receive format info from Master: %s" % list_services) log_tester.info("Receive format info from Master: %s" % list_services) return self._sampler.sample(list_services)
def check(self, service_name): log = LOG() log_root = log.root() command = shlex.split("systemctl is-active ") command.append(service_name) try: output = subprocess.Popen(command, stdout=subprocess.PIPE) except: log_root.error("service: %s, status: %s" % (service_name, "no checker")) return "no checker" str_status = str(output.stdout.readlines()) if "active" in str_status: status = "active" if "inactive" in str_status: status = "inactive" log_root.error("service: %s, status: %s" % (service_name, "inactive")) return status elif 'unknown' in str_status: status = "unknown" log_root.error("service: %s, status: %s" % (service_name, "unknown")) return status