urlwatch.py
#!/usr/bin/python
"""
testtest
"""
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'))
import optparse
import mysql_modules_
import curl_modules_
import curl_webruntime_monitor_
import curl_ngproxy_monitor_
import re
import pdb
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)
if opt.ngproxy:
ngproxy_ip_list = []
mysql_info = { 'host':'****','passwd':'***','port':****,'db':'****','user':'***'}
mysql_info['role'] = 'ngproxy'
sql = "select ip from **** where role like '%%%s%%';"%mysql_info['role']
mysql_modules_object = mysql_modules_.mysql_Info(mysql_info['host'],mysql_info['port'],mysql_info['user'],mysql_info['passwd'],mysql_info['db'],sql)
result_ip = mysql_modules_object.mysql_conection()
curl_modules_project = curl_ngproxy_monitor_.curl_ngproxy_ip(result_ip)
curl_modules_code = curl_modules_project.curl_ngproxy_function()
print curl_modules_code
if opt.monitor:
webruntime_object = curl_webruntime_monitor_.monitor_Curl()
print type(webruntime_object)
webruntime_object.webruntime_group_function()
modules/curl_modules/curl_modules_.py
#!/usr/bin/python
import pycurl
import cStringIO
import re
import pdb
class curl_Code:
def __init__(self,ip):
self.ip = ip
self.ngproxy_ip_list = []
self.ngproxy_curl_dict = {}
self.http_status = {}
def curl_ip_code(self,result_http_ip,http_header=False):
buf = cStringIO.StringIO()
url = 'http://'+result_http_ip
c=pycurl.Curl()
c.setopt(c.URL,url)
c.setopt(c.WRITEFUNCTION,buf.write)
if http_header:
c.setopt(pycurl.HEADER, True)
c.setopt(c.HTTPHEADER, list(http_header))
c.perform()
http_doc = buf.getvalue()
http_code = c.getinfo(pycurl.HTTP_CODE)
#http_code ? http_code : 'default'
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
c.close()
buf.close()
modules/curl_ngproxy_monitor/curl_ngproxy_monitor_.py
#!/usr/bin/python
import re
import pdb
import sys
import os
sys.path.append(os.path.join('../curl_modules'))
from curl_modules_ import curl_Code
class curl_ngproxy_ip:
def __init__(self,ip):
self.ip = ip
self.ngproxy_curl_dict = {}
self.ngproxy_ip_list = []
def curl_ngproxy_function(self):
for i in self.ip:
try:
ngproxy = i['ip'].split(',')
for ii in ngproxy:
re_match = re.findall('^(10|172)',ii)
if ii.find('****') >= 0 or ii.find('***') >= 0 or ii.find('***') >= 0 or ii.find('****') >= 0 or ii.find('****') >= 0 or re_match:
continue
else:
self.ngproxy_ip_list.append(ii)
except Exception ,e:
self.ngproxy_ip_list.append(i['ip'])
for iii in self.ngproxy_ip_list:
curl_ngproxy_project = curl_Code(iii)
curl_ngproxy_result = curl_ngproxy_project.curl_ip_code(iii)
self.ngproxy_curl_dict[iii] = {}
self.ngproxy_curl_dict[iii] = curl_ngproxy_result
print self.ngproxy_curl_dict
return self.ngproxy_curl_dict
modules/curl_webruntime_monitor/curl_webruntime_monitor_.py
#!/usr/bin/python
import sys
import os
sys.path.append('../mysql_modules')
sys.path.append('../curl_modules')
import mysql_modules_
import curl_modules_
from config import webruntime_Info
class monitor_Curl(webruntime_Info):
def __init__(self):
super(monitor_Curl,self).__init__()
self.mysql_info = { 'host':'****','passwd':'***','port':***,'db':'***','user':'***'}
self.webruntime_group = ['****']
self.sql = ''
def webruntime_group_function(self):
print '++++'
for i in self.webruntime_group:
self.sql = "select int_ip from nodes where role like '%%%s.webruntime%%';"%i
for ii in i:
mysql_object = mysql_modules_.mysql_Info(self.mysql_info['host'],self.mysql_info['port'],self.mysql_info['user'],self.mysql_info['passwd'],self.mysql_info['db'],self.sql)
webruntime_group_result_ip = mysql_object.mysql_conection()
for iii in webruntime_group_result_ip:
print type(iii)
curl_object = curl_modules_.curl_Code(iii['int_ip'])
curl_result = curl_object.curl_ip_code(iii['int_ip'],self.config['common'])
print curl_result
modules/curl_webruntime_monitor/config.py
#!/usr/bin/python
class webruntime_Info(object):
def __init__(self):
self.config={}
webruntime_list = ['common','bigapp','ent','java2','vipjava']
for i in webruntime_list:
self.config[i] = {}
self.config["common"] = "****", "Appname: ****", "AppVersion: ****", "AppHash: ****", "AccessKey: ****", "SecretKey: ****", "McIP: ****", "MysqlPort: ****", "AppCookie:aks=****;debug=1"
self.config["java2"] = "Host: ****", "Appname: ****", "AppVersion: ****", "AppHash: ****", "AccessKey: ****", "SecretKey: ****", "McIP: ****", "MysqlPort: ****", "AppCookie: ****;", "AppSrvc: ****"
self.config["vipjava"] = "Host: ****", "Appname: ****", "AppVersion: 1", "AppHash: ***", "AccessKey: ***", "SecretKey: ****", "McIP: ****", "MysqlPort: ***", "AppCookie: ****", "AppSrvc: ****"
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):
print self.user,self.db
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