def run(self): try: server_name = main_fonf.get_prop("server", "name") target_urls = main_fonf.get_prop("monitor", "urls") target_hosts = main_fonf.get_prop("monitor", "hosts") html_info = self.monitor_urls(target_urls) html_info += self.monitor_hosts(target_hosts) if html_info.strip() != '': mail_head = '<table border="0" cellspacing="0" cellpadding="0" ' \ 'style="font-family:"微软雅黑",Helvetica,Arial,sans-serif;font-size:14px " width="100%"> \ <tbody> \ <tr> \ <td style="font-family:Helvetica,Arial,sans-serif;font-size:14px;"> \ <table width="100%" border="3" cellpadding="5" cellspacing="0" > \ <tbody> \ <tr><td>监控地址</td><td>状态</td><td>描述</td></tr>' mail_foot = '</tbody> \ </table> \ </td> \ </tr> \ </tbody> \ </table>' mail_info = mail_head + html_info + mail_foot self.send_mail(server_name, mail_info) except Exception, runEx: LogUtil.error(runEx)
def _execute(command): try: config = FileUtils.get_instance('config.ini') containerId = command["serverId"] c = docker.Client(base_url='unix://var/run/docker.sock', version=config.get_prop("docker-config", "api"), timeout=10) c.remove_container(container=containerId) time.sleep(5) log = "true" output = base64.encodestring(log) state = '1' param = [ "{\"id\":\"" + command["id"] + "\",\"state\":\"" + state + "\",\"mac\":\"" + MachineUtil.get_mac_address() + "\",\"msg\":\"" + output + "\",\"version\":\"" + command["version"] + "\"}" ] AosServerService.route_service("ThriftApiService", "editState", param) except Exception, ex: LogUtil.error(ex) log = ("Error {0}".format(str(ex))) output = base64.encodestring(log) state = '1' param = [ "{\"id\":\"" + command["id"] + "\",\"state\":\"" + state + "\",\"mac\":\"" + MachineUtil.get_mac_address() + "\",\"msg\":\"" + output + "\",\"version\":\"" + command["version"] + "\"}" ] AosServerService.route_service("ThriftApiService", "editState", param)
def _docker_containers(): try: line = commands.getstatusoutput( "docker ps -a | grep -v 'CONTAINER'") r = json.dumps(line[1].replace("\n", "xiaodoufenge")) return r except Exception, ex: LogUtil.error(ex) return "[]"
def _docker_images(api): try: c = docker.Client(base_url='unix://var/run/docker.sock', version=api, timeout=10) r = json.dumps(c.images()) return r except Exception, ex: LogUtil.error(ex) return "[]"
def _docker_status(api): try: c = docker.Client(base_url='unix://var/run/docker.sock', version=api, timeout=10) r = c.version() return "1" except Exception, ex: LogUtil.error(ex) return "2"
def connect(self): try: if self.timeout: self._socket.setTimeout(self.timeout) if not self.is_open(): self._transport = self._transport_factory.getTransport( self._socket) self._transport.open() except Exception, e: LogUtil.error(e) self.close()
def nginx_run(self): try: server_response = self._nginx_conf() if server_response is None: return server_responses = server_response.split("#EDIT#") if len(server_responses) < 1: return server_response_nginx = json.loads(server_responses[0]) self._do_action(server_response_nginx) except Exception, ex: LogUtil.error(ex)
def route_service(service, method, param): try: aos_client = AosServer( AosServerService.config.get_prop("aos-config", "server_ip"), AosServerService.config.get_prop("aos-config", "server_port")) result = aos_client.route_service(service, method, json.dumps(param)) if result is None or result.model is None: return None LogUtil.in_out(service, method, param, result.model) return result.model except Exception, ex: LogUtil.error(ex)
def run(self): try: server_response = self._regist_node() if server_response is None: return server_responses = server_response.split("#ADDNODE#") if len(server_responses) < 2: return server_response_command = json.loads(server_responses[0]) server_response_config = json.loads(server_responses[1]) self._do_config(server_response_config) self._do_action(server_response_command) except Exception, ex: LogUtil.error(ex)
def _docker_restart(mac, api): try: output = os.popen("/bin/bash " + sys.path[0] + "/docker.sh ").read() c = docker.Client(base_url='unix://var/run/docker.sock', version=api, timeout=10) r = c.version() param = ["{\"mac\":\"" + mac + "\"}"] AosServerService.route_service("ThriftApiService", "startAll", param) return "1" except Exception, ex: LogUtil.error(ex) return "2"
def monitor_urls(self, target_urls): html_info = "" if target_urls is None or len(target_urls) == 0: return for target_url in target_urls.split(","): try: result = self.monitor_url(target_url) if result is not None : self.alarm("監控Url地址失敗:[%s]" % target_url) html_info += "<tr><td><a href='%s'><span style='color:#F44336;'>%s</span></a></td><td>" \ "<span style='color:#F44336;'>%s</span></td><td><span style='color:#F44336;'>" \ "%s</span></td></tr>" % (target_url, target_url, "Fail", result.message) except Exception, moUsEx: LogUtil.error(moUsEx) self.alarm("監控Url地址失敗:[%s]" % target_url) html_info += "<tr><td><a href='%s'><span style='color:#F44336;'>%s</span></a></td><td>" \ "<span style='color:#F44336;'>%s</span></td><td><span style='color:#F44336;'>" \ "%s</span></td></tr>" % (target_url, target_url, "Fail", moUsEx.message)
def monitor_hosts(self, target_hosts): html_info = "" if target_hosts is None or len(target_hosts) == 0: return for target_host in target_hosts.split(","): try: result = self.monitor_host(target_host) if result: self.alarm("監控IP地址失敗:[%s]" % target_host) html_info += "<tr><td><a href='%s'><span style='color:#F44336;'>%s</span></a></td><td>" \ "<span style='color:#F44336;'>%s</span></td><td><span style='color:#F44336;'>" \ "%s</span></td></tr>" % (target_host, target_host, "Fail", result) except Exception, moHsEx: LogUtil.error(moHsEx) self.alarm("監控IP地址失敗:[%s]" % target_host) html_info += "<tr><td><a href='%s'><span style='color:#F44336;'>%s</span></a></td><td>" \ "<span style='color:#F44336;'>%s</span></td><td><span style='color:#F44336;'>" \ "%s</span></td></tr>" % (target_host, target_host, "Fail", moHsEx.message)
def _execute(command): try: commandInfo = command["commandInfo"] line = commands.getstatusoutput(commandInfo) log = json.dumps(line[1]) output = base64.encodestring(log) state = '1' param = [ "{\"id\":\"" + command["id"] + "\",\"state\":\"" + state + "\",\"mac\":\"" + MachineUtil.get_mac_address() + "\",\"msg\":\"" + output + "\",\"version\":\"" + command["version"] + "\"}" ] AosServerService.route_service("ThriftApiService", "editState", param) except Exception, ex: LogUtil.error(ex) log = ("Error {0}".format(str(ex))) output = base64.encodestring(log) state = '1' param = [ "{\"id\":\"" + command["id"] + "\",\"state\":\"" + state + "\",\"mac\":\"" + MachineUtil.get_mac_address() + "\",\"msg\":\"" + output + "\",\"version\":\"" + command["version"] + "\"}" ] AosServerService.route_service("ThriftApiService", "editState", param)
def _execute(command): config = FileUtils.get_instance('config.ini') nginx_conf_dir = config.get_prop("nginx-config", "nginx_conf_dir") nginx_conf_dir_bak = config.get_prop("nginx-config", "nginx_conf_dir_bak") os.system('mkdir -p ' + nginx_conf_dir) os.system('cp -r ' + nginx_conf_dir + nginx_conf_dir_bak) os.system('rm -rf ' + nginx_conf_dir + '*') state = '0' try: for k1 in command: upstreamName = k1["upstreamName"] upstreamNode = json.loads(k1["upstreamNode"]) fileName = nginx_conf_dir + upstreamName + '.conf' os.system('touch ' + fileName) c = nginx.Conf() LogUtil.info("Node信息:%s" % (upstreamNode)) t = '' for k2 in range(len(upstreamNode)): if (k2 == 0): t = 'server' + ' ' + upstreamNode[k2][ "ipAdress"] + ' ' + upstreamNode[k2]["strategy"] + ';\n' if (0 < k2 < len(upstreamNode) - 1): t = t + ' ' + 'server' + ' ' + upstreamNode[k2][ "ipAdress"] + ' ' + upstreamNode[k2]["strategy"] + ';\n' if (k2 == len(upstreamNode) - 1): t = t + ' ' + 'server' + ' ' + upstreamNode[k2][ "ipAdress"] + ' ' + upstreamNode[k2]["strategy"] ipHash = k1["ipHash"] if (ipHash == '1'): u = nginx.Upstream(upstreamName, nginx.Key('', t), nginx.Key('', 'ip_hash')) c.add(u) if (ipHash == '0'): u = nginx.Upstream(upstreamName, nginx.Key('', t)) c.add(u) s = nginx.Server() s.add( nginx.Key('listen', k1["nginxListenPort"]), nginx.Key('server_name', k1["nginxServerName"]), nginx.Key('access_log', k1["accessLog"]), nginx.Location( '= /', nginx.Key('proxy_pass', 'http://' + k1["upstreamName"]), nginx.Key('proxy_redirect', 'off'), nginx.Key('proxy_set_header', 'Host $host'), nginx.Key('proxy_set_header', 'X-Real-IP $remote_addr'), nginx.Key('proxy_set_header', 'X-Forwarded-For $proxy_add_x_forwarded_for'))) c.add(s) nginx.dumpf(c, fileName) LogUtil.info("完成Nginx配置") output = os.popen('service nginx restart').read() state = '1' param = [ "{\"mac\":\"" + MachineUtil.get_mac_address() + "\",\"state\":\"" + state + "\",\"msg\":\"" + output + "\"}" ] os.system('rm -rf ' + nginx_conf_dir_bak) AosServerService.route_service("ThriftApiService", "nginxLog", param) except Exception, ex: LogUtil.error(ex) os.system('rm -rf ' + nginx_conf_dir) os.system('mv ' + nginx_conf_dir_bak + nginx_conf_dir) os.system('service nginx restart') output = ex.message state = '2' param = [ "{\"mac\":\"" + MachineUtil.get_mac_address() + "\",\"state\":\"" + state + "\",\"msg\":\"" + output + "\"}" ] AosServerService.route_service("ThriftApiService", "nginxLog", param)
def route_service(self, service_name, func_name, param): try: return self.client.routeService(service_name, func_name, param) except Exception, e: LogUtil.error(e)