孤独是一种态度
Code
python 日志分析统计脚本
三 19th
脚本类似shell的tail功能,用来统计每分钟的4xx、5xx的状态码数量,统计php执行时间小于1秒、1-5秒、5秒以上的数量,和每秒的并发请求。将结果放到/tmp目录下, 也可以用cacti将结果画图。
#!/usr/bin/env python
import time, os
#-----------------------------
log_file = '/var/log/httpd/cmi_access_log'
#-----------------------------
log_name = log_file.split('/')[-1]
file = open(log_file, 'r')
lt1,to1_5,gt5,status4,status5,concurrent = 0,0,0,0,0,0
Time2 = '0'
st_results = os.stat(log_file)
st_size = st_results[6]
file.seek(st_size)
size = os.path.getsize(log_file)
while 1:
Time = time.strftime("%Y_%m_%d",time.localtime(time.time() - 60))
where = file.tell()
line = file.readline()
L = line.split('"')
Time1 = time.strftime("%Y_%m_%d_%H:%M",time.localtime(time.time() - 60))
if not line:
size = os.path.getsize(log_file)
if size < where:
file = open(log_file, 'r')
else:
time.sleep(1)
file.seek(where)
else:
try:
Phptime = float(L[-2])
url = L[1]
status = L[2][1:4]
if '4' in status[0]:
status4 += 1
elif '5' in status[0]:
status5 += 1
if Time1 in Time2:
if 'f5.php' not in url:
concurrent += 1
if Phptime < 1:
lt1 += 1
elif 1 <= Phptime <= 5:
to1_5 += 1
else:
gt5 += 1
else:
concurrent = concurrent / 60
out = Time1 + "," + str(lt1) + "," + str(to1_5) + "," + str(gt5) + "," + str(status4) + "," + str(status5) + "," + str(concurrent)
F = open('/tmp/' + log_name + '_' + Time + '.data', 'a')
print >> F, out
F.close()
Time2 = Time1
lt1,to1_5,gt5,status4,status5,concurrent = 0,0,0,0,0,0
except:
pass
原创文章,转载请注明: 转载自gjw_apparitor 博客
本文链接地址: python 日志分析统计脚本
SHELL 中几个特殊变量
四 6th
$0 shell脚本的名字
$* shell的参数
$@ shell的参数
$# shell的参数个数
$_ shell的最后一个参数
$$ shell脚本自身进程的ID
$! shell在后台运行的最后的工作的PID(进程ID).
$? 上一条命令的返回值
原创文章,转载请注明: 转载自gjw_apparitor 博客
本文链接地址: SHELL 中几个特殊变量
SSH的自动登录脚本
四 6th
#!/usr/bin/expect -f
set ipaddress [lindex $argv 0]
set passwd [lindex $argv 1]
set passwd1 [lindex $argv 2]
set timeout 30
spawn ssh -p 22 -c arcfour -o ConnectTimeout=30 root@$ipaddress "iptables-save && service iptables restart"
#spawn ssh -p 22 -c arcfour -o ConnectTimeout=30 root@$ipaddress "/etc/init.d/dnsmasq status"
#spawn rsync -avpur --progress -e "ssh -p 22 -c arcfour" /tmp/dnsmasq root@${ipaddress}:~/
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "$passwd\r" }
}
#expect "]*"
expect "password:"
send "$passwd1\r"
expect "]*"
原创文章,转载请注明: 转载自gjw_apparitor 博客
本文链接地址: SSH的自动登录脚本
dig 详解 (转载)
四 6th
转自:http://docutek.asia/faq/infoblox/296.htm
dig 是 UNIX/BSD 系统都自带的 DNS 诊断工具,使用十分灵活,被很多 DNS 管理员用来做 DNS 诊断的工具。但在 Windows 系统中并没有提供 dig 程序,需要用户自行下载安装。
用法
dig [@server] [-b address] [-c class] [-f filename] [-k filename] [-m] [-p port#] [-q name] [-t type] [-x addr] [-y [hmac:]name:key] [-4] [-6] [name] [type] [class] [queryopt...]
符号说明:”[]” 表明该选项是可选项。
dig 是完全命令行的工具,使用者需要记得各个选项的用法。如果忘记可以使用 -h 选项查看所有的参数。以下就各个选项进行说明:
server
指定 DNS Server 服务器。
更多 >
原创文章,转载请注明: 转载自gjw_apparitor 博客
本文链接地址: dig 详解 (转载)
shell 条件判断语句
四 6th
1 字符串判断
str1 = str2 当两个串有相同内容、长度时为真
str1 != str2 当串str1和str2不等时为真
-n str1 当串的长度大于0时为真(串非空)
-z str1 当串的长度为0时为真(空串)
str1 当串str1为非空时为真
2 数字的判断
int1 -eq int2 两数相等为真
int1 -ne int2 两数不等为真
int1 -gt int2 int1大于int2为真
int1 -ge int2 int1大于等于int2为真
int1 -lt int2 int1小于int2为真
int1 -le int2 int1小于等于int2为真
3 文件的判断
-r file 用户可读为真
-w file 用户可写为真
-x file 用户可执行为真
-f file 文件为正规文件为真
-d file 文件为目录为真
-c file 文件为字符特殊文件为真
-b file 文件为块特殊文件为真
-s file 文件大小非0时为真
-t file 当文件描述符(默认为1)指定的设备为终端时为真
3 复杂逻辑判断
-a 与
-o 或
! 非
原创文章,转载请注明: 转载自gjw_apparitor 博客
本文链接地址: shell 条件判断语句
IP段掩码换算脚本
四 6th
由于公司的DNS IP库比较陈旧,打算升级一下。跟朋友要了个最新的全国的IP库(他们是做CDN的)
打开一看都是32掩码的。貌似这个bind的不支持呀
221.14.122.0 221.14.127.255 河南省网通 221.13.128.0 221.15.255.255 河南省网通 221.192.168.0 221.192.171.255 河北省网通 61.237.195.0 61.237.195.255 黑龙江省铁通 219.147.36.0 219.147.36.255 山西省电信 202.98.247.0 202.98.248.255 西藏电信 218.21.128.0 218.21.255.255 内蒙古联通 61.171.112.0 61.171.127.255 上海市电信 61.171.160.0 61.171.179.255 上海市电信 218.21.0.0 218.21.47.255 宁夏电信
原创文章,转载请注明: 转载自gjw_apparitor 博客
本文链接地址: IP段掩码换算脚本
