本文共 4976 字,大约阅读时间需要 16 分钟。
日志分析脚本:
#!/usr/local/python27/bin/python2.7# coding=utf-8import pygalimport reimport osimport sysimport datetimeBASE_PATH = "./"def extract(path): ip = dict() time = dict() status = dict() version = dict() method = dict() with open(path) as f: for line in f: matched = reobj.search(line) if not matched: continue log_obj = matched.groupdict() if log_obj.get("ip") not in ip.keys(): ip[log_obj.get("ip")] = 0 ip[log_obj.get("ip")] += 1 origin_time = log_obj.get("time") if not origin_time: continue dt = datetime.datetime.strptime(origin_time,'%d/%b/%Y:%H:%M:%S') dt_s = dt.strftime('%Y-%m-%d %H') if dt_s not in time.keys(): time[dt_s] = 0 time[dt_s] += 1 if log_obj.get('status') not in status.keys(): status[log_obj.get('status')] = 0 status[log_obj.get('status')] += 1 if log_obj.get('version') not in version.keys(): version[log_obj.get('version')] = 0 version[log_obj.get('version')] += 1 if log_obj.get("method") not in method.keys(): method[log_obj.get("method")] = 0 method[log_obj.get("method")] += 1 return ip,time,status,version,methoddef make_path(dt): path = os.path.join(BASE_PATH,dt) if not os.path.exists(path): os.makedirs(path) return pathdef time_graph(time,dt): chart = pygal.Bar() chart.title = "Access of %s" % dt keys = ['%s %2d' % (dt,x) for x in range(24)] values = [time.get(key) for key in keys] chart.x_labels = [x.split()[1] for x in keys] chart.add("Access",values) path = make_path(dt) chart.render_to_file(os.path.join(path,"time.svg"))def status_graph(status,dt): chart = pygal.Pie() chart.title = "Status of %s" % dt for k,v in status.items(): chart.add(k,v) path = make_path(dt) chart.render_to_file(os.path.join(path,"status.svg"))def version_graph(vsersion,dt): chart = pygal.Pie() chart.title = "versions of %s" % dt for k,v in version.items(): chart.add(k,v) path = make_path(dt) chart.render_to_file(os.path.join(path,"version.svg"))def method_graph(method,dt): chart = pygal.Pie() chart.title = "methods of %s" % dt for k,v in method.items(): chart.add(k,v) path = make_path(dt) chart.render_to_file(os.path.join(path,"method.svg"))if __name__ == '__main__': log_file = sys.argv[1] dt = sys.argv[2] ip,time,status,version,method = extract(log_file) time_graph(time,dt) status_graph(status,dt) version_graph(version,dt) method_graph(method,dt)
文件差异对比:
#!/usr/local/python27/bin/python2.7import difflibimport systry: textfile1=sys.argv[1] textfile2=sys.argv[2]except e: print("Error:" + str(e)) sys.exit()def readfile(filename): try: fileHandle = open(filename,'rb') text=fileHandle.read().splitlines() fileHandle.close() return text except IOError as a: print("Error:" + str(a)) sys.exit()if textfile1 =="" or textfile2=="": print('Usage:filediif.py filename1 filename2') sys.exit()text1_lines = readfile(textfile1)text2_lines = readfile(textfile2)d = difflib.HtmlDiff()a = d.make_file(text1_lines,text2_lines)print(a)
输出结果:
./filediff.py aa1.txt aa2.txt >diff.html
HTTP访问脚本
#!/usr/local/python27/bin/python2.7import urllib2req = urllib2.urlopen('http://weather.yahooapis.com/forecastrss?w=2151849&u=c')print(req.read())
HTTP状态检测—适用于单域名多主机DNS负载均衡场景
#!/usr/local/python27/bin/python2.7import dns.resolverimport osimport httplibiplist = []appdomain='www.hello.com'def get_iplist(domain=""): try: A = dns.resolver.query(domain,'A') except e: print("dns resolver error:" + str(e)) return for i in A.response.answer: for j in i.items: iplist.append(j.address) return Truedef checkip(ip): checkurl=ip+":80" getcontent="" httplib.socket.setdefaulttimeout(5) conn=httplib.HTTPConnection(checkurl) try: conn.request("GET","/",headers = {"Host": appdomain}) r = conn.getresponse() getcontent = r.read(15) finally: if getcontent == "": print (ip+" [OK]") else: print (ip+" [Error]")if __name__ == "__main__": if get_iplist(appdomain) and len(iplist)>0: for ip in iplist: checkip(ip) else: print("dns resolver error")
DNS解析脚本
#!/usr/local/python3/bin/python3import dns.resolverdomain = input('please input an domain: ')A = dns.resolver.query(domain,'A')for i in A.response.answer: for j in i.items: print(j.address)
MX记录解析
#!/usr/local/python3/bin/python3import dns.resolverdomain = input('please input an domain: ')#这里接收两个参数的传入,需要解析的域名,解析的记录类型;MX = dns.resolver.query(domain,'MX')for i in MX: print('MX preference =', i.preference, 'mail exchanger = ', i.exchange)
转载地址:http://ahmna.baihongyu.com/