class Oms_Openfalcon: '''open-falcon and oms ''' sql_db = mysql.db_operate() falcon_db = mysql_falcon.db_operate() oms_db = mysql_oms.db_operate() def __init__(self,url): self.url = url def open_falcon(self): oms_sql ="select d.abbr_name from " \ "device_server a ,business_clusterserver b,business_cluster c ,business_project d where a.id=b.device_id and" \ " b.cluster_id=c.id and c.project_id=d.id and a.hostname='%s';"%(self.url) oms_info = Oms_Openfalcon.oms_db.select_count_table(oms_sql) ip_sql = "select b.ip from device_server a,device_serverinterface b where a.id=b.device_id and b.ip not like '10.32.%%' and a.hostname='%s' limit 1;"%(self.url) ip_info = Oms_Openfalcon.oms_db.select_count_table(ip_sql) if len(oms_info) > 0: if len(oms_info) > 1: try: print(u"more cluster") cluster_join = "'"+"','".join(oms_info)+"'" oms_cluster = "select DISTINCT grp_name_id from oms_openfalcon where abbr_name in (%s);" % (cluster_join) print(oms_cluster) db = Oms_Openfalcon.sql_db.select_count_table(oms_cluster) if db[0]: print(db[0]) try: select = "select count(hostname) from host where hostname='%s';"%(self.url) count = Oms_Openfalcon.falcon_db.select_count_table(select) if count[0] < 1: sql = "insert into host(hostname,ip,agent_version,plugin_version) VALUES ('%s','%s','5.1.1','plugin not enabled');" % (self.url, ip_info[0]) try: Oms_Openfalcon.falcon_db.mysql_command(sql) hostid = "select id from host where hostname='%s';"%(self.url) host_id = Oms_Openfalcon.falcon_db.select_count_table(hostid) grp_host = "insert into grp_host(grp_id,host_id) VALUES('%s','%s')" % (db[0], host_id[0]) Oms_Openfalcon.falcon_db.mysql_command(grp_host) update = "update emp_hostname set status=1 where hostname='%s';"%(self.url) Oms_Openfalcon.sql_db.mysql_command(update) print(grp_host) print(update) print(u'Add open_falcon seccuss !!') except: print("Add Error!!!") pass elif count[0] >= 1: select ="select host_id from grp_host where host_id in(select id from host where hostname='%s');" % (self.url) host_id = Oms_Openfalcon.falcon_db.select_count_table(select) if len(host_id)>0: update = "update emp_hostname set status=1 where hostname='%s';" % (self.url) Oms_Openfalcon.sql_db.mysql_command(update) print(u"The host has been added to open_falcon_monitor.") else: hostid ="select id from host where hostname='%s';" % (self.url) h_id = Oms_Openfalcon.falcon_db.select_count_table(hostid) grp_host = "insert into grp_host(grp_id,host_id) VALUES('%s','%s');" % (db[0], h_id[0]) Oms_Openfalcon.falcon_db.mysql_command(grp_host) update = "update emp_hostname set status=1 where hostname='%s';" % (self.url) Oms_Openfalcon.sql_db.mysql_command(update) print(grp_host) print(update) print(u'table group_host update seccuss!') except pymysql.Error as e: print(e) pass elif db[0] == None: print(u'TABLE oms_openfalcon in grp_name_id is null,please manage') else: print(u"No add open_falon monitor cluster") pass except Exception as e: print(e) print('Error warning!') pass elif len(oms_info) == 1: try: print(u'One cluster') oms_cluster = "select DISTINCT grp_name_id from oms_openfalcon where abbr_name in ('%s');" % (oms_info[0]) db = Oms_Openfalcon.sql_db.select_count_table(oms_cluster) if db[0]: print(db[0]) try: select = "select count(hostname) from host where hostname='%s';"%(self.url) count = Oms_Openfalcon.falcon_db.select_count_table(select) if count[0] < 1: sql = "insert into host(hostname,ip,agent_version,plugin_version) VALUES ('%s','%s','5.1.1','plugin not enabled');" % (self.url, ip_info[0]) try: Oms_Openfalcon.falcon_db.mysql_command(sql) hostid = "select id from host where hostname='%s';"%(self.url) host_id = Oms_Openfalcon.falcon_db.select_count_table(hostid) grp_host = "insert into grp_host(grp_id,host_id) VALUES('%s','%s')" % (db[0], host_id[0]) Oms_Openfalcon.falcon_db.mysql_command(grp_host) update = "update emp_hostname set status=1 where hostname='%s';"%(self.url) print(grp_host) print(update) Oms_Openfalcon.sql_db.mysql_command(update) print(u'Add open_falcon seccuss !!') except Exception: print("Add Error!!!") pass elif count[0] >= 1: select ="select host_id from grp_host where host_id in(select id from host where hostname='%s');" % (self.url) host_id = Oms_Openfalcon.falcon_db.select_count_table(select) if len(host_id)>0: update = "update emp_hostname set status=1 where hostname='%s';" % (self.url) Oms_Openfalcon.sql_db.mysql_command(update) print(u"The host has been added to open_falcon_monitor.") else: hostid ="select id from host where hostname='%s';" % (self.url) h_id = Oms_Openfalcon.falcon_db.select_count_table(hostid) grp_host = "insert into grp_host(grp_id,host_id) VALUES('%s','%s');" % (db[0], h_id[0]) Oms_Openfalcon.falcon_db.mysql_command(grp_host) update = "update emp_hostname set status=1 where hostname='%s';" % (self.url) Oms_Openfalcon.sql_db.mysql_command(update) print grp_host print update print u'table group_host update seccuss!' except Exception as e: print(e) pass elif db[0] == None: print(u'TABLE oms_openfalcon in grp_name_id is null,please manage') else: print(u"No add open_falon monitor cluster") pass except Exception as e: print(e) print('Error warning!') pass else: print(u"oms cluster data is null") pass def open_falcon_delte(self): try: hostid = "select id from host where hostname='%s';" % (self.url) host_id = Oms_Openfalcon.falcon_db.select_count_table(hostid) grp_id = "select host_id from grp_host where host_id in (select id from host where hostname='%s');" % (self.url) grp_host_id = Oms_Openfalcon.falcon_db.select_count_table(grp_id) if len(host_id) or len(grp_host_id): grp_host = "delete from grp_host where host_id='%s'" % (host_id[0]) host = "delete from host where id='%s'" % (host_id[0]) Oms_Openfalcon.falcon_db.mysql_command(grp_host) Oms_Openfalcon.falcon_db.mysql_command(host) update = "update emp_hostname set status=1 where hostname='%s';" % (self.url) Oms_Openfalcon.sql_db.mysql_command(update) print(host) print(grp_host) print(self.url,u'Delete seccuss') else: update = "update emp_hostname set status=1 where hostname='%s';" % (self.url) Oms_Openfalcon.sql_db.mysql_command(update) print(self.url) print(u'The hostname is out in open_falcon monitor') except pymysql.Error as e: print(e) pass def oms_nocluster_delte(self): '''清理小米监控,被nocluster函数调用''' try: hostid = "select id from host where hostname='%s';" % (self.url) host_id = Oms_Openfalcon.falcon_db.select_count_table(hostid) grp_id = "select host_id from grp_host where host_id in (select id from host where hostname='%s');" % (self.url) grp_host_id = Oms_Openfalcon.falcon_db.select_count_table(grp_id) print(host_id) print(grp_host_id) if len(host_id) or len(grp_host_id): print('-----------------------------------------') grp_host = "delete from grp_host where host_id='%s'" % (host_id[0]) host = "delete from host where id='%s'" % (host_id[0]) print(grp_host,host) Oms_Openfalcon.falcon_db.mysql_command(grp_host) Oms_Openfalcon.falcon_db.mysql_command(host) print(self.url,u'Delete seccuss') else: print('------------------------------------------') print(self.url) print(u'The hostname is out in open_falcon monitor') except pymysql.Error as e: print(e) pass def nocluster(self): '''清理小米监控中,oms系统中没有业务线的主机信息''' try: no_cluster = "select hostname from device_server where id not in(select distinct a.device_id from business_clusterserver a,device_server b where a.device_id=b.id);" nodata = Oms_Openfalcon.oms_db.select_table(no_cluster) # i = 0 for reslut in nodata: # print reslut open_falcon = Oms_Openfalcon(reslut) open_falcon.oms_nocluster_delte() # i += 1 # print i except pymysql.Error as e: print(e) pass
#!/usr/bin/env python # -*- coding: UTF-8 -*- from __future__ import unicode_literals import sys import json import mysql, mysql_falcon, mysql_oms from collections import Counter import rrdgraph_quality import datetime db_falcon = mysql_falcon.db_operate() db = mysql.db_operate() db_oms = mysql_oms.db_operate() reload(sys) sys.setdefaultencoding('utf-8') sys.getdefaultencoding() def Chart_quality(): """网络服务质量""" # endpoints = "select b.hostname from idc_idc a ,device_server b where a.id = b.idc_id and a.name={0};".format(args) # print endpoints # endpoints_hostname = db_oms.select_table(endpoints) data = open('./source', 'w') data_dest = open('./source_dest', 'w') for endpoints in open('networksource', 'r').readlines(): endpoint = endpoints.strip().split()[1] room = endpoints.strip().split()[0] # print room
def selectip(self,platformAlias,serverId): sql = 'select serverIp from mds_server where platformAlias="%s" and serverId="%s"' %(platformAlias,serverId) db = db_operate() serverIp1 = db.mysql_command(settings.LOGMANGER_MYSQL,sql) for serverIp in serverIp1: return serverIp
def selectplatformAlias(self): sql = 'select distinct(platformAlias) from mds_server' db = db_operate() platformAliass = db.mysql_command(settings.LOGMANGER_MYSQL,sql) for platformAlias in platformAliass: return platformAlias
def view2(request): """check php log""" logpathlist = AddPhpLogpath.objects.all() print 'the path is %s',logpathlist sql2 = 'select distinct(platformAlias) from mds_server' db = db_operate() platformAliass = db.mysql_command(settings.LOGMANGER_MYSQL,sql2) print platformAliass try: log = logging.getLogger('test1') log.info('log content') except: print 'no log' TxtlogResult = ['ceshi','ceshi2'] if request.method=='POST': try: logTime=request.POST['date'] except: pass try: platformAlias=request.POST['agent'] except: pass try: logPath=request.POST['logdir'] except: pass try: serverId=request.POST['zone'] except: pass try: logCount=request.POST['sum'] except: pass print logTime,platformAlias,logPath,serverId,logCount A = select_ip() serverIp = A.selectip(platformAlias,serverId) print 'serverIp is %s' %(serverIp) logexist = AddPhpLogpath.objects.all() print logexist for list in logexist: print list.logpath if list.logpath == logPath: print 'the logpath is exist' else: l1 = AddPhpLogpath(logpath=logPath) l1.save() serverinfo_list = [] serverinfo = {} serverinfo["platformAlias"] = platformAlias serverinfo["serverId"] = serverId serverinfo["serverIp"] = serverIp serverinfo["logPath"] = logPath serverinfo["logTime"] = logTime serverinfo["logCount"] = logCount serverinfo_list.append(serverinfo) command = {} command["cmd"] = 'logoperate' command["checkType"] = 'phplog' command["serverinfo_list"] = serverinfo_list command = json.dumps(command) + "#zbcyh#" print command try: logResult = socket_send('14.18.204.150',command) except: print 'not connect' JsLogResult = json.loads(logResult) TxtlogResult = JsLogResult['msg'] print TxtlogResult return render_to_response('logquery3.html',{'result':logpathlist,'result2':platformAliass,'result3':TxtlogResult})
def ExecCommand(request): """Exec command""" # P = select_platformAlias() # platformAliass = P.selectplatformAlias() # for platformAlias in platformAliass: # print platformAlias TxtCommandResult = ['result'] execcommand = addCommand.objects.all().distinct() # for execcommand in execcommand1: # print execcommand # print execcommand1 # print 'the path is %s', % execcommand1 # for execcommand in execcommand1: # print 'the result is' # print execcommand # print execcommand.addcommand sql2='select distinct(platformAlias) from mds_server' db = db_operate() platformAliass = db.mysql_command(settings.LOGMANGER_MYSQL,sql2) print platformAliass if request.method == 'POST': try: platformAlias = request.POST['agent'] except: pass try: serverId = request.POST['zone'] except: pass try: execCommand1 = request.POST['execcommand'] print execCommand1 execCommand = urllib.unquote(execCommand1) print 'execCommand' print execCommand except: pass try: javapid = request.POST['javapid'] except: pass print platformAlias,serverId,execCommand,javapid A = select_ip() serverIp = A.selectip(platformAlias,serverId) print serverIp # execcommand = ExecCommand.objects.all() # print execcommand for list in execcommand: print list.addcommand if execCommand in list.addcommand: print 'the command is exist' else: l2 = addCommand(addcommand=execCommand) l2.save() serverinfo_list = [] serverinfo = {} serverinfo["platformAlias"] = platformAlias serverinfo["serverId"] = serverId serverinfo["serverIp"] = serverIp serverinfo["execcommand"] = execCommand serverinfo["javapid"] = javapid serverinfo_list.append(serverinfo) command = {} command["cmd"] = 'logoperate' command["checkType"] = 'ExecCommand' command["serverinfo_list"] = serverinfo_list command = json.dumps(command) + "#zbcyh#" print command try: commandResult = socket_send('14.18.204.150',command) except: print 'not connect' JsCommandResult = json.loads(commandResult) TxtCommandResult = JsCommandResult['msg'] print TxtCommandResult return render_to_response('CommandResult.html',{'result':platformAliass,'result2':execcommand,'result3':TxtCommandResult})
def searchjavalog(request): """check java log""" logpathlist = AddLogpath.objects.all() print 'the path is %s',logpathlist sql2 = 'select distinct(platformAlias) from mds_server' db = db_operate() platformAliass = db.mysql_command(settings.LOGMANGER_MYSQL,sql2) TxtlogResult = ['ceshi','ceshi2'] if request.method=='POST': try: logTime=request.POST['date'] except: pass try: platformAlias=request.POST['agent'] except: pass try: logPath=request.POST['logdir'] except: pass try: serverId=request.POST['zone'] except: pass try: logAccount = request.POST['sum'] except: pass # try: # key=request.POST['key'] # except: # pass # try: # hour = request.POST['hour'] # except: # pass print logTime,platformAlias,logPath,serverId,logAccount sql = 'select serverIp from mds_server where platformAlias="%s" and serverId="%s"' %(platformAlias,serverId) serverIp1 = db.mysql_command(settings.LOGMANGER_MYSQL,sql) print serverIp1 for serverIp in serverIp1: print serverIp """ logexist = AddLogpath.objects.all() print logexist for list in logexist: print list.logpath if list.logpath == logPath: print 'the logpath is exist' else: l1 = AddLogpath(logpath=logPath) l1.save() """ queryset = AddLogpath.objects.values('logtype').filter(logpath=logPath)[0]['logtype'] print queryset serverinfo_list = [] serverinfo = {} serverinfo["platformAlias"] = platformAlias serverinfo["serverId"] = serverId serverinfo["serverIp"] = serverIp serverinfo["logPath"] = logPath serverinfo["logTime"] = logTime # serverinfo["key"] = key # serverinfo["hour"] = hour serverinfo['logAccount'] = logAccount serverinfo['logtype'] = queryset # serverinfo = json.dumps(serverinfo) serverinfo_list.append(serverinfo) # serverinfo_dic = {} # serverinfo_dic['serverinfo'] = serverinfo_list command = {} command["cmd"] = 'logoperate' command["checkType"] = 'checkjavalog' command["serverinfo_list"] = serverinfo_list command = json.dumps(command) + "#zbcyh#" print command try: logResult = socket_send('14.18.204.150',command) except: print 'not connect' JsLogResult = json.loads(logResult) TxtlogResult = JsLogResult['msg'] print TxtlogResult return render_to_response('javalogquery.html',{'result':logpathlist,'result2':platformAliass,'result3':TxtlogResult})