天天看点

Linux集群和自动化维2.6.2 统计类脚本

<b>2.6.2 统计类脚本</b>

统计工作一直是shell和python脚本的强项,我们完全可以利用sed、awk再加上正则表达式,写出强大的统计脚本来分析我们的系统日志、安全日志及服务器应用日志等。

1. nginx负载均衡器日志汇总脚本

以下脚本是用来分析nginx负载均衡器的日志的,作为awstats的补充,它可以快速得出排名最前的网站和ip等,脚本内容如下(此脚本在centos 5.8/6.4 x86_64下均已测试通过):

#!/bin/bash

if [ $# -eq 0 ];

then

   echo "error: please specify

logfile."

   exit 0

else

   log=$1

fi

if [ ! -f $1 ];

   echo "sorry, sir, i can't find this

apache log file, pls try again!"

exit 0

####################################################

echo "most

of the ip:"

echo

"-------------------------------------------"

awk '{ print $1

}' $log | sort | uniq -c | sort -nr | head -10

of the time:"

"--------------------------------------------"

awk '{ print $4

}' $log | cut -c 14-18 | sort | uniq -c | sort -nr | head -10

of the page:"

awk '{print

$11}' $log | sed 's/^.*\(.cn*\)\"/\1/g' |

sort | uniq -c | sort -rn | head -10

of the time / most of the ip:"

}' $log | cut -c 14-18 | sort -n | uniq -c | sort -nr | head -10 &gt; timelog

for i in `awk '{

print $2 }' timelog`

do

   num=`grep $i timelog | awk '{ print $1 }'`

   echo " $i $num"

   ip=`grep $i $log | awk '{ print $1}' | sort

-n | uniq -c | sort -nr | head -10`

   echo "$ip"

   echo

done

rm -f timelog

2. 探测多节点web服务质量

工作中有时会出现网络延迟导致程序返回数据不及时的问题,这时就需要精准定位机器是在哪个时间段出现了网络延迟的情况。对此,可以通过python下的pycurl模块来实现定位,它可以通过调用pycurl提供的方法,来探测web服务质量,比如了解相应的http状态码、请求延时、http头信息、下载速度等,脚本内容如下所示(此脚本在amazon linux ami x86_64下已测试通过):

#!/usr/bin/python

#encoding:utf-8

#*/30 * * * *

/usr/bin/python /root/dnstime.py &gt;&gt; /root/myreport.txt 2&gt;&amp;1

import os

import time

import sys

import pycurl

#import commands

url="http://imp-east.example.net"

isotimeformat="%y-%m-%d

%x"

c =

pycurl.curl()

c.setopt(pycurl.url,

url)

c.setopt(pycurl.connecttimeout,

5)

c.setopt(pycurl.timeout,

c.setopt(pycurl.forbid_reuse,

1)

c.setopt(pycurl.maxredirs,

c.setopt(pycurl.noprogress,

c.setopt(pycurl.dns_cache_timeout,30)

indexfile =

open(os.path.dirname(os.path.realpath(__file__))+"/content.txt",

"wb")

c.setopt(pycurl.writeheader,

indexfile)

c.setopt(pycurl.writedata,

try:

    c.perform()

except

exception,e:

    print "connecion error:"+str(e)

    indexfile.close()

    c.close()

    sys.exit()

namelookup_time

=  c.getinfo(c.namelookup_time)

connect_time

=  c.getinfo(c.connect_time)

pretransfer_time

=   c.getinfo(c.pretransfer_time)

starttransfer_time

= c.getinfo(c.starttransfer_time)

total_time =

c.getinfo(c.total_time)

http_code =  c.getinfo(c.http_code)

size_download

=  c.getinfo(c.size_download)

header_size =

c.getinfo(c.header_size)

speed_download=c.getinfo(c.speed_download)

print "http状态码:%s" %(http_code)

print "dns解析时间:%.2f ms"%(namelookup_time*1000)

print "建立连接时间:%.2f ms" %(connect_time*1000)

print "准备传输时间:%.2f ms" %(pretransfer_time*1000)

print "传输开始时间:%.2f ms" %(starttransfer_time*1000)

print "传输结束总时间:%.2f ms" %(total_time*1000)

print "下载数据包大小:%d bytes/s" %(size_download)

print "http头部大小:%d byte" %(header_size)

print "平均下载速度:%d bytes/s" %(speed_download)

indexfile.close()

c.close()

print

time.strftime( isotimeformat, time.gmtime( time.time() ) )

"================================================================"

3.测试局域网内主机是否alive的小脚本

我们在对局域网的网络情况进行维护时,经常会遇到这样的问题,需要收集网络中存活的ip,这个时候可以写一个python脚本,自动收集某一网段的ip。现在的it技术型公司都比较大,网络工程师一般会规划几个vlan(网段),我们可以用如下这个脚本来收集某个vlan下存活的主机,(此脚本在centos

6.4 x86_64下已测试通过):

import re

import

subprocess

lifeline =

re.compile(r"(\d) received")

report =

("no response","partial response","alive")

time.ctime()

for host in

range(1,254):

   ip = "192.168.1."+str(host)

   pingaling =

subprocess.popen(["ping","-q", "-c 2",

"-r", ip], shell=false, stdin=subprocess.pipe,

stdout=subprocess.pipe)

   print "testing ",ip,

   while 1:

      pingaling.stdout.flush()

      line = pingaling.stdout.readline()

      if not line: break

      igot = re.findall(lifeline,line)

      if igot:

           print report[int(igot[0])]

python对空格的要求是非常严谨的,请大家注意下这个问题。脚本虽然短小,但非常实用、精悍,可避免到windows

下去下载局域网检测工具。平时我们在日常工作中也应该注意多收集、多写一些这样的脚本,以达到简化运维工作的目的。