urlwatch.py 主脚本 #!/usr/bin/python """ This is a monitor program,it integrates curl ngproxy extranet ip and curl runtime serivce """ import os import sys dir_ = os.path.dirname(os.path.realpath(__file__)) modules_dir = os.path.join(dir_,'modules') sys.path.append(os.path.join(modules_dir,'mysql_modules')) sys.path.append(os.path.join(modules_dir,'curl_modules')) sys.path.append(os.path.join(modules_dir,'curl_webruntime_monitor')) sys.path.append(os.path.join(modules_dir,'curl_ngproxy_monitor')) mysql_config = os.path.join(dir_+'/','mysql.ini') import optparse import mysql_modules_ import curl_modules_ import curl_webruntime_monitor_ import curl_ngproxy_monitor_ import re import pdb import ConfigParser if __name__ == '__main__': parser = optparse.OptionParser(usage="%s" % __doc__.strip()) parser.add_option("-n","--ngproxy",action="store_true", dest="ngproxy", default=False, help="curl ngproxy ip") parser.add_option("-m","--monitor",action="store_true", dest="monitor", default=False, help="curl runtime monitor") (opt,args) = parser.parse_args(sys.argv) config = ConfigParser.ConfigParser() print mysql_config config.read(mysql_config) host = config.get('mysql','host') port = (config.getint('mysql','port')) user = config.get('mysql','user') passwd = config.get('mysql','passwd') db = config.get('mysql','db') if opt.ngproxy: ngproxy_ip_list = [] curl_modules_project = curl_ngproxy_monitor_.curl_ngproxy_ip() curl_modules_code = curl_modules_project.ngproxy_result_ip() curl_modules_project.thread_ngproxy(curl_modules_code) if opt.monitor: webruntime_object = curl_webruntime_monitor_.monitor_Curl(user,passwd,port,host,db) webruntime_object.webruntime_group_function() mysql.ini 数据库的登陆 [mysql] host = *** port = *** user = *** db = *** passwd = *** [mysql_monitor] host = *** port = *** user = *** db = *** passwd = *** ngproxy_black_list.conf ngproxy的黑名单 ***** black_list.conf monitor的黑名单 ***** modules/alert_monitor/alert_monitor_.py 报警用户的模块 #!/usr/bin/python import urllib class alert_Monitor: def __init__(self,monitor_message): self.monitor_message=monitor_message def send_monitor(self,monitor_message): params = urllib.urlencode(self.monitor_message) print urllib.urlopen('******',params).read() modules/curl_modules/curl_modules_.py #!/usr/bin/python import pycurl import cStringIO import re import pdb import sys import os dir_ = os.path.dirname(os.path.realpath(__file__)) sys.path.append(dir_ + '/../alert_monitor') import alert_monitor_ class curl_Code: def __init__(self,ip): self.ip = ip self.ngproxy_ip_list = [] self.ngproxy_curl_dict = {} self.http_status = {} self.conection_error = '' def curl_ip_code(self,result_http_ip,http_header=False,http_command=False): buf = cStringIO.StringIO() if http_command: post_request = http_command url = 'http://'+result_http_ip+post_request else: url = 'http://'+result_http_ip try: c=pycurl.Curl() c.setopt(c.URL,url) c.setopt(c.WRITEFUNCTION,buf.write) if http_header: c.setopt(c.HTTPHEADER, list(http_header)) c.perform() except: self.conection_error = 1 return self.conection_error http_doc = buf.getvalue() http_code = c.getinfo(pycurl.HTTP_CODE) http_time = c.getinfo(pycurl.CONNECT_TIME) self.http_status['http_code'] = http_code self.http_status['http_time'] = http_time self.http_status['http_doc'] = http_doc return self.http_status
modules/curl_ngproxy_monitor/curl_ngproxy_monitor_.py curl ngproxy外网ip的模块 #!/usr/bin/python import re import pdb import sys import os import ConfigParser import threading import time dir_ = os.path.dirname(os.path.realpath(__file__)) sys.path.append(dir_ + '/../curl_modules') sys.path.append(dir_ + '/../alert_monitor') mysql_config = os.path.join(dir_+'/../../','mysql.ini') sys.path.append(dir_ + '/../mysql_modules') from curl_modules_ import curl_Code import alert_monitor_ import mysql_modules_ class curl_ngproxy_ip: def __init__(self): self.ngproxy_curl_dict = {} self.ngproxy_ip_list = [] self.sms = 1 self.config = ConfigParser.ConfigParser() self.config.read(mysql_config) self.host = self.config.get('mysql','host') self.port = self.config.get('mysql','port') self.user = self.config.get('mysql','user') self.db = self.config.get('mysql','db') self.passwd = self.config.get('mysql','passwd') self.open_black_file = [] self.thread_list = [] self.get_black_list() def get_black_list(self): black_ip = os.path.join(dir_ + '/../../', 'ngproxy_black_list.conf') self.open_black_file = [x.split('\n')[0] for x in open(black_ip,'r').readlines()] def ngproxy_result_ip(self): sql = "select ip from nodes where role like '%ngproxy%';" mysql_modules_object = mysql_modules_.mysql_Info(self.host,self.port,self.user,self.passwd,self.db,sql) result_ip = mysql_modules_object.mysql_conection() return result_ip def curl_ngproxy_function(self,iii): curl_ngproxy_project = curl_Code(iii) curl_ngproxy_result = curl_ngproxy_project.curl_ip_code(iii) if curl_ngproxy_result == 1: error_msg_content = 'new_watchurl_url:' + iii +';error:couldn\'t connect to host' send_error_message = {'service':'proxy','checkpoint':'urlwatch','title':error_msg_content,\ 'content':error_msg_content,'cluster':'public','grade':self.sms} monitor_project = alert_monitor_.alert_Monitor(send_error_message) monitor_project.send_monitor(send_error_message) return self.ngproxy_curl_dict[iii] = curl_ngproxy_result if self.ngproxy_curl_dict[iii]['http_time'] >= 10: error_msg_content = 'new_watch_url:' + iii +';error:http_time_too_long_is_'+str(self.ngproxy_curl_dict[iii]['http_time']) send_error_message = {'service':'proxy','checkpoint':'urlwatch','title':error_msg_content,\ 'content':error_msg_content,'cluster':'public','grade':self.sms} monitor_project = alert_monitor_.alert_Monitor(send_error_message) monitor_project.send_monitor(send_error_message) if self.ngproxy_curl_dict[iii]['http_code'] != 404: error_msg_content = 'new_watchurl_url:' + iii +';error:http_code_is_'+self.ngproxy_curl_dict[iii]['http_code'] send_error_message = {'service':'proxy','checkpoint':'urlwatch','title':error_msg_content,\ 'content':error_msg_content,'cluster':'public','grade':self.sms} monitor_project = alert_monitor_.alert_Monitor(send_error_message) monitor_project.send_monitor(send_error_message) def thread_ngproxy(self,result_ip): for i in result_ip: ngproxy = i['ip'].split(',') for ii in ngproxy: re_match = re.findall('^(10|172)',ii) if ii in self.open_black_file or re_match: continue else: self.ngproxy_ip_list.append(ii) for dict_ii in self.ngproxy_ip_list: aa = threading.Thread(target = self.curl_ngproxy_function, args = (dict_ii,)) self.thread_list.append(aa) aa.start() for threads in self.thread_list: threads.join() modules/curl_webruntime_monitor/curl_webruntime_monitor_.py curl monitor的模块 #!/usr/bin/python import sys import os import xml.etree.cElementTree as ET import threading import time import pdb dir_ = os.path.dirname(os.path.realpath(__file__)) sys.path.append(dir_ + '/../mysql_modules') sys.path.append(dir_ + '/../curl_modules') sys.path.append(dir_ + '/../alert_monitor') sys.path.append(dir_ + '/../monitor_record') import mysql_modules_ import curl_modules_ import alert_monitor_ import monitor_record_ from config import webruntime_Info class monitor_Curl(webruntime_Info): def __init__(self,user,passwd,port,host,db): super(monitor_Curl,self).__init__() self.webruntime_group = self.config self.group = self.config.keys() self.sum = -1 self.sql = '' self.thread_list = [] self.service = ['fts','environ','fetchurl','mail','socket','fs','segment','vcode','cron','counter','memcache','mysql','taskqueue','image','kvdb','session','channel','deferredjob','cdn','storage'] self.sms = 1 self.error_num = 0 self.mutex = '' self.user = user self.passwd = passwd self.port = port self.host = host self.db = db self.open_black_file = [] self.get_black_list() def get_black_list(self): black_ip = os.path.join(dir_ + '/../../', 'black_list.conf') self.open_black_file = [x.split('\n')[0] for x in open(black_ip,'r').readlines()] def service_thread(self,ip,group,object_name,url_ip,services): post_request = "/?format=xml&service=%s"%services curl_result = object_name.curl_ip_code(url_ip,self.config[group]['header'],post_request) if curl_result == 1: title = 'curl monitor failed:Failed connect to' + url_ip + ';No route to host' + '[' + group + '::' + url_ip + ']' conection_error_msg = {'service':'webruntime','checkpoint':'monitor','title':'new_monitor_test_'+title,'content':title,'cluster':'public','grade':self.sms} self.mutex.acquire() if self.error_num == 1: pass else: self.error_num = 1 print conection_error_msg monitor_project = alert_monitor_.alert_Monitor(conection_error_msg) monitor_project.send_monitor(conection_error_msg) msg_monitor_project = monitor_record_.monitor_Msg_record() msg_monitor_project.mysql_conection('webruntime','monitor',title,title,'critical',group) self.mutex.release() return else: msg_monitor_project = monitor_record_.monitor_Msg_record() msg_monitor_project.mysql_conection('webruntime','monitor','','','ok',group) xml_result = curl_result['http_doc'] try: root = ET.fromstring(xml_result) except: return time1 = time.time() for resource in root.findall('resource'): if resource.find('error'): monitor_service = resource.find('service').text monitor_error_source = resource.find('error').find('assert').find('source').text + '[' + group + '::' + url_ip + '::' + monitor_service + ']' monitor_error_info = resource.find('error').find('assert').find('info').text + '[' + group + '::' + url_ip + '::' + monitor_service + ']' send_error_message = {'service':monitor_service,'checkpoint':'monitor','title':'new_monitor_test_'+monitor_error_source,'content':'new_monitor_test_'+monitor_error_info,'cluster':'public','grade':self.sms} print send_error_message monitor_project = alert_monitor_.alert_Monitor(send_error_message) monitor_project.send_monitor(send_error_message) msg_monitor_project = monitor_record_.monitor_Msg_record() msg_monitor_project.mysql_conection(monitor_service,'monitor',monitor_error_source,monitor_error_info,'critical',group) print ip['int_ip'] + services + resource.find('item').text + ' is error' else: monitor_service = resource.find('service').text msg_monitor_project = monitor_record_.monitor_Msg_record() msg_monitor_project.mysql_conection(monitor_service,'monitor','','','ok',group) def webruntime_xml_result(self,ip,group): #try: url_ip = ip['int_ip']+":"+str(self.config[group]['port']) object_name = curl_modules_.curl_Code(url_ip) self.mutex = threading.RLock() for services in self.service: aa = threading.Thread(target = self.service_thread, args = (ip,group,object_name,url_ip,services)) self.thread_list.append(aa) aa.start() for threads in self.thread_list: threads.join() def webruntime_group_function(self): for i in self.webruntime_group: self.sum += 1 pid = os.fork() if pid == 0: self.fobidden_pid_function() elif pid > 0: continue elif pid < 0: print "fork failed" def fobidden_pid_function(self): name_i = self.group[self.sum] self.sql = "select int_ip from nodes where role like '%%%sruntime%%';"%name_i mysql_object = mysql_modules_.mysql_Info(self.host,self.port,self.user,self.passwd,self.db,self.sql) webruntime_group_result_ip = mysql_object.mysql_conection() for iii in webruntime_group_result_ip: if iii['int_ip'] in self.open_black_file: continue self.webruntime_xml_result(iii,name_i) sys.exit(0) urlwatch/modules/curl_webruntime_monitor/config.py 各个webruntime的header信息 #!/usr/bin/python class webruntime_Info(object): def __init__(self): self.config={} webruntime_list = ['com.web','bigapp.web','ent.web','ent.java','com.java'] for i in webruntime_list: self.config[i] = {} self.config[i]['header'] = {} self.config[i]['port'] = {} self.config['com.web']['port'] = '80' self.config['bigapp.web']['port'] = '80' self.config['ent.web']['port'] = '80' self.config['ent.java']['port'] = '19999' self.config['com.java']['port'] = '19999' self.config["com.web"]['header'] = "***** self.config["bigapp.web"]['header'] = "****" self.config["ent.web"]['header'] = "****" self.config["com.java"]['header'] = "****" self.config['ent.java']['header'] = "****" modules/monitor_record/monitor_record_.py 把报警信息已经正常的信息,写入到数据库,已做sla统计 #!/usr/bin/python import MySQLdb import ConfigParser import time import os dir_ = os.path.dirname(os.path.realpath(__file__)) mysql_config = os.path.join(dir_+'/../../','mysql.ini') class monitor_Msg_record: def __init__(self): self.config = ConfigParser.ConfigParser() self.config.read(mysql_config) self.host = self.config.get('mysql_monitor','host') self.port = self.config.get('mysql_monitor','port') self.user = self.config.get('mysql_monitor','user') self.db = self.config.get('mysql_monitor','db') self.passwd = self.config.get('mysql_monitor','passwd') self.timestamp = time.time() def mysql_conection(self,service,check_point,title,content,status,platform): sql = "insert into **** (service,checkpoint,title,content,status,timestamp,platform)\ values('%s','%s','%s','%s','%s','%s','%s')"%(service,check_point\ ,MySQLdb.escape_string(title),MySQLdb.escape_string(content),status,self.timestamp,platform) conn=MySQLdb.connect(host=str(self.host),user=self.user,passwd=self.passwd,port=int(self.port),db=self.db) cur=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cur.execute(sql) modules/mysql_modules/mysql_modules_.py 普通数据库查询使用的模块 #!/usr/bin/python import MySQLdb class mysql_Info: def __init__(self,host,port,user,passwd,db,sql): self.host = host self.port = port self.passwd = passwd self.db = db self.user = user self.sql = sql def mysql_conection(self): conn=MySQLdb.connect(host=str(self.host),user=self.user,passwd=self.passwd,port=int(self.port),db=self.db) cur=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cur.execute(self.sql) result=cur.fetchall() return result
本文出自 “expect批量同步数据” 博客,谢绝转载!