苹果app报告问题拒绝退款理由怎么写写

python写的一个squid访问日志分析的小程序
作者:乡村运维
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了python写的一个分析squid访问日志的小程序,本文实现的目标是统计access.log中的ip数目,需要的朋友可以参考下
这两周组里面几位想学习python,于是我们就创建了一个这样的环境和氛围来给大家学习。
昨天在群里,贴了一个需求,就是统计squid访问日志中ip 访问数和url的访问数并排序,不少同学都大体实现了相应的功能,我把我简单实现的贴出来,欢迎拍砖:
日志格式如下:
%ts.%03tu %6tr %{X-Forwarded-For}&h %Ss/%03Hs %&st %rm %ru& %un %Sh/%&A %mt "%{Referer}&h" "%{User-Agent}&h" %{Cookie}&h
.285&&&&& 0 100.64.19.225 TCP_HIT/200 8560 GET http://img1.jb51.net/games/x100.jpg& - NONE/- image/jpeg "http://www.jb51.net/" "Mozilla/4.0 ( MSIE 8.0; Windows NT 5.1; Trident/4.0; QQDownload 734; .NET4.0C; .NET CLR 2.0.50727)" pcsuv=0;%20pcuvdata=lastAccessTime=2;%20u4ad=%20uf=3
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from optparse import OptionParser
&&& 仅仅是一个关于日志文件的测试,统计处access.log 的ip数目
&&& f = open('/data/proclog/log/squid/access.log')
except IOError,e:
&&& print "can't open the file:%s" %(e)
def log_report(field):
&&&&&&& return the field of the access log
&&& if field == "ip":
&&&&&&& return& [line.split()[2] for line in f]
&&& if field == "url":
&&&&&&& return& [line.split()[6] for line in f]
def log_count(field):
&&&&&&& return a dict of like {field:number}
&&& fields2 = {}
&&& fields = log_report(field)
&&& for field_tmp in fields:
&&&&&&& if field_tmp in fields2:
&&&&&&&&&&& fields2[field_tmp] += 1
&&&&&&& else:
&&&&&&&&&&& fields2[field_tmp] = 1
&&& return fields2
def log_sort(field,number = 10 ,reverse = True):
&&&&&&& print the sorted fields to output
&&& for v in sorted(log_count(field).iteritems(),key = lambda x:x[1] , reverse = reverse )[0:int(number)]:
&&&&&&& print v[1],v[0]
if __name__ == "__main__":
&&& parser =OptionParser(usage="%prog [-i|-u] [-n num | -r]" ,version = "1.0")
&&& parser.add_option('-n','--number',dest="number",type=int,default=10,help=" print top line of the ouput")
&&& parser.add_option('-i','--ip',dest="ip",action = "store_true",help="print ip information of access log")
&&& parser.add_option('-u','--url',dest="url",action = "store_true",help="print url information of access log")
&&& parser.add_option('-r','--reverse',action = "store_true",dest="reverse",help="reverse output ")
&&& (options,args) = parser.parse_args()
&&& if len(sys.argv) & 2:
&&&&&&& parser.print_help()
&&& if options.ip and options.url:
&&&&&&&& parser.error(' -i and -u& can not be execute at the same time ')
&&& if options.ip :
&&&&&&& log_sort("ip", options.number , True and options.reverse& or False)
&&& if options.url:
&&&&&&& log_sort("url", options.number , True and& options.reverse or False)
&&& f.close()
效果如下:
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具Squid 服务器日志增长是很快的,如果不做处理的话,可以会由于系统限制单文件大小,而导致 Squid 服务停止,太大的日志文件也不适合分析。
一、日志配置
shell & grep 'log' /etc/squid/squid.conf
cache_access_log /var/log/squid/access.log
cache_log /var/log/squid/cache.log
cache_store_log /var/log/squid/store.log
logfile_rotate 7
## 这是 squid.conf 中关于 log 的配置项,分别定义了三个日志文件 access.log cache.log store.log## logfile_rotate 7 代表保留 6 个历史日志文件,跟 1 个当前日志文件
## 默认 Squid 是不作日志切割的,系统允许写多大,那么日志文件就写多大
shell & squid -k rotate
## 使用 squid -k rotate 来切割日志,切割的日志名按 .0 .1 .2 .3 .... 保存
shell & ls /var/log/squid/*.log*
/var/log/squid/access.log
/var/log/squid/cache.log
/var/log/squid/store.log
/var/log/squid/access.log.0
/var/log/squid/cache.log.0
/var/log/squid/store.log.0
shell & crontab -e
0 3 * * * /usr/sbin/squid -k rotate
## 使用任务计划来定期切割日志
shell & ls /var/log/squid/access.log*
/var/log/squid/access.log
/var/log/squid/access.log.1
/var/log/squid/access.log.3
/var/log/squid/access.log.5
/var/log/squid/access.log.0
/var/log/squid/access.log.2
/var/log/squid/access.log.4
/var/log/squid/access.log.6
shell & ls /var/log/squid/cache.log*
/var/log/squid/cache.log
/var/log/squid/cache.log.1
/var/log/squid/cache.log.3
/var/log/squid/cache.log.5
/var/log/squid/cache.log.0
/var/log/squid/cache.log.2
/var/log/squid/cache.log.4
/var/log/squid/cache.log.6
shell & ls /var/log/squid/store.log*
/var/log/squid/store.log
/var/log/squid/store.log.1
/var/log/squid/store.log.3
/var/log/squid/store.log.5
/var/log/squid/store.log.0
/var/log/squid/store.log.2
/var/log/squid/store.log.4
/var/log/squid/store.log.6
## 由于参数 logfile_rotate 7 的限定,每个日志文件最多保存 7 个( 自动删除 )
二、日志分析
/var/log/squid/access.log
默认以 UTC 1970-01-01 00:00:00 到现在的秒数表示,显示为毫秒
处理所用时间,毫秒
客户端地址
客户端 IP 地址
结果码/状态码
结果码表示 Squid 专有的编码,如 TCP_HIT 、UDP_DENIED 等,状态码是 HTTP 的响应代码,如 200、301、404、503 等
给客户端传输字节数
HTTP 或 ICP 两种,HTTP 一般为 GET ,ICP 一般为 ICP_QUERY
请求的 URL 地址
客户端身份
对端编码/对端主机
HTTP 响应的内容类型
HTTP 请求头部
HTTP 响应头部
## access.log 结果码( 部分常见的,更多请常找相关资料 )
## 在硬盘中发现有效请求资源并立即回传给用户的数据
## 没有被缓存,并回传给用户的数据
TCP_MEM_HIT
## 在内存中发现有效的请求资源并立即回传给用户的数据
TCP_REFRESH_UNMODIFIED
## 请求资源可能是旧的缓存,发送确认请求到原始服务器,原始服务器返回 304 未修改响应,指示 Squid 的缓存是最新的。并回传给用户的数据
TCP_REFRESH_MODIFIED
## 请求资源可能是旧的缓存,发送确认请求到原始服务器,原始服务器返回新的数据,指示这个缓存是旧的,缓存并回传给用户的数据
shell & cat /var/log/squid/access.log | grep TCP_HIT
## 被缓存到硬盘并回传给用户的数据
shell & cat /var/log/squid/access.log | grep TCP_MEM_HIT
## 被缓存到内存并回传给用户的数据
shell & cat /var/log/squid/access.log | grep TCP_MISS
## 没有被缓存的数据,而是从原始服务器获取并回传给用户的数据
三、缓存命中率
shell & squidclient -h 192.168.1.88 -p 80 mgr:info
HTTP/1.0 200 OK
Server: squid/3.1.10
Mime-Version: 1.0
Date: Wed, 07 Jan 2015 08:09:10 GMT
Content-Type: text/plain
Expires: Wed, 07 Jan 2015 08:09:10 GMT
Last-Modified: Wed, 07 Jan 2015 08:09:10 GMT
X-Cache: MISS from study.localhost.localdomain
X-Cache-Lookup: MISS from study.localhost.localdomain:80
Via: 1.0 study.localhost.localdomain (squid/3.1.10)
Connection: close
Squid Object Cache: Version 3.1.10
Start Time:
Wed, 07 Jan 2015 07:03:04 GMT
Current Time:
Wed, 07 Jan 2015 08:09:10 GMT
Connection information for squid:
Number of clients accessing cache:
Number of HTTP requests received:
Number of ICP messages received:
Number of ICP messages sent:
Number of queued ICP replies:
Number of HTCP messages received:
Number of HTCP messages sent:
Request failure ratio:
Average HTTP requests per minute since start:
Average ICP messages per minute since start:
Select loop called: 36984 times, 107.235 ms avg
Cache information for squid:
Hits as % of all requests:
5min: 0.0%, 60min: 86.0%
## 请求命中率 5分内平均 0.0% ,60分内平均 86.0%
Hits as % of bytes sent:
5min: 100.0%, 60min: 99.1%
## 字节命中率
Memory hits as % of hit requests:
5min: 0.0%, 60min: 0.0%
## 内存命中率
Disk hits as % of hit requests: 5min: 0.0%, 60min: 13.5%
## 磁盘命中率
Storage Swap size:
## 缓存目录大小
Storage Swap capacity:
0.7% used, 99.3% free
Storage Mem size:
## 缓存内存大小
Storage Mem capacity:
0.0% used, 100.0% free
Mean Object Size:
Requests given to unlinkd:
Median Service Times (seconds)
HTTP Requests (All):
Cache Misses:
Cache Hits:
Near Hits:
Not-Modified Replies:
DNS Lookups:
ICP Queries:
Resource usage for squid:
4.601 seconds
CPU Usage:
CPU Usage, 5 minute avg:
CPU Usage, 60 minute avg:
Process Data Segment Size via sbrk(): 3692 KB
Maximum Resident Size: 79968 KB
Page faults with physical i/o: 1
Memory accounted for:
Total accounted:
memPool accounted:
memPool unaccounted:
memPoolAlloc calls:
memPoolFree calls:
File descriptor usage for squid:
Maximum number of file descriptors:
Largest file desc currently in use:
Number of file desc currently in use:
Files queued for open:
Available number of file descriptors: 65525
Reserved number of file descriptors:
Store Disk files open:
Internal Data Structures:
149 StoreEntries
27 StoreEntries with MemObjects
26 Hot Object Cache Items
122 on-disk objects
阅读(...) 评论()新手园地& & & 硬件问题Linux系统管理Linux网络问题Linux环境编程Linux桌面系统国产LinuxBSD& & & BSD文档中心AIX& & & 新手入门& & & AIX文档中心& & & 资源下载& & & Power高级应用& & & IBM存储AS400Solaris& & & Solaris文档中心HP-UX& & & HP文档中心SCO UNIX& & & SCO文档中心互操作专区IRIXTru64 UNIXMac OS X门户网站运维集群和高可用服务器应用监控和防护虚拟化技术架构设计行业应用和管理服务器及硬件技术& & & 服务器资源下载云计算& & & 云计算文档中心& & & 云计算业界& & & 云计算资源下载存储备份& & & 存储文档中心& & & 存储业界& & & 存储资源下载& & & Symantec技术交流区安全技术网络技术& & & 网络技术文档中心C/C++& & & GUI编程& & & Functional编程内核源码& & & 内核问题移动开发& & & 移动开发技术资料ShellPerlJava& & & Java文档中心PHP& & & php文档中心Python& & & Python文档中心RubyCPU与编译器嵌入式开发驱动开发Web开发VoIP开发技术MySQL& & & MySQL文档中心SybaseOraclePostgreSQLDB2Informix数据仓库与数据挖掘NoSQL技术IT业界新闻与评论IT职业生涯& & & 猎头招聘IT图书与评论& & & CU技术图书大系& & & Linux书友会二手交易下载共享Linux文档专区IT培训与认证& & & 培训交流& & & 认证培训清茶斋投资理财运动地带快乐数码摄影& & & 摄影器材& & & 摄影比赛专区IT爱车族旅游天下站务交流版主会议室博客SNS站务交流区CU活动专区& & & Power活动专区& & & 拍卖交流区频道交流区
论坛徽章:0
提示: 作者被禁止或删除 内容自动屏蔽
&&nbsp|&&nbsp&&nbsp|&&nbsp&&nbsp|&&nbsp&&nbsp|&&nbsp
巨富豪门, 积分 27842, 距离下一级还需 12158 积分
论坛徽章:0
logrotate 是由 crontab 调用的。
rotate num 的意思不是几天,是保留几份。
squid.conf 里我没发现有 logrotate 参数,你在哪里看到的
论坛徽章:0
提示: 作者被禁止或删除 内容自动屏蔽
巨富豪门, 积分 27842, 距离下一级还需 12158 积分
论坛徽章:0
这个squid参数我也看到了,具体哪一个生效我不太清楚,但是两个都表示保存多少个日志,我是肯定的
论坛徽章:0
提示: 作者被禁止或删除 内容自动屏蔽
论坛徽章:0
提示: 作者被禁止或删除 内容自动屏蔽
论坛徽章:0
提示: 作者被禁止或删除 内容自动屏蔽
小富即安, 积分 3187, 距离下一级还需 1813 积分
论坛徽章:0
logfile_rotate 3
论坛徽章:0
提示: 作者被禁止或删除 内容自动屏蔽
稍有积蓄, 积分 270, 距离下一级还需 230 积分
论坛徽章:0
北京皓辰网域网络信息技术有限公司. 版权所有 京ICP证:060528号 北京市公安局海淀分局网监中心备案编号:
广播电视节目制作经营许可证(京) 字第1234号
中国互联网协会会员&&联系我们:
感谢所有关心和支持过ChinaUnix的朋友们
转载本站内容请注明原作者名及出处chengxcwl 的BLOG
用户名:chengxcwl
文章数:66
评论数:17
访问量:91338
注册日期:
阅读量:5863
阅读量:12276
阅读量:381903
阅读量:1073763
51CTO推荐博文
根据手册及一些文章收集整理并亲自测试了一些常用的squid命令 1、测试配置文件/usr/local/squid/sbin/squid -k parse & 2、初始化cache目录# /usr/local/squid/sbin/squid -zX也可以不加X,X可以看到初始化过程 & 3、在终端窗口里测试squid一旦你已经初始化cache目录,就可以在终端窗口里运行squid,将日志记录到标准错误。这样,你能轻易的定位任何错误或问题,并且确认squid是否成功启动。使用-N选项来保持squid在前台运行,-d1选项在标准错误里显示1级别的调试信息。# /usr/local/squid/sbin/squid -N -d1 05:52:03| Starting Squid Cache version 2.7.STABLE9 for x86_64-unknown-linux-gnu... 05:52:03| Process ID 13897 05:52:03| With 1024 file descriptors available 05:52:03| Using epoll for the IO loop 05:52:03| Performing DNS Tests... 05:52:03| Successful DNS name lookup tests... 05:52:03| helperOpenServers: Starting 5 'dnsserver' processes 05:52:03| User-Agent logging is disabled. 05:52:03| Referer logging is disabled. 05:52:03| logfileOpen: opening log /usr/local/squid/var/logs/access.log 05:52:03| Unlinkd pipe opened on FD 15 05:52:03| Swap maxSize 102400 + 8192 KB, estimated 8507 objects 05:52:03| Target number of buckets: 425 05:52:03| Using 8192 Store buckets 05:52:03| Max Mem &size: 8192 KB 05:52:03| Max Swap size: 102400 KB 05:52:03| logfileOpen: opening log /usr/local/squid/var/logs/store.log 05:52:03| Rebuilding storage in /usr/local/squid/var/cache (DIRTY) 05:52:03| Using Least Load store dir selection 05:52:03| Set Current Directory to /usr/local/squid/var/cache 05:52:03| Loaded Icons. 05:52:03| Accepting proxy HTTP connections at 0.0.0.0, port 80, FD 16. 05:52:03| Accepting ICP messages at 0.0.0.0, port 3130, FD 17. 05:52:03| Accepting SNMP messages on port 3401, FD 18. 05:52:03| WCCP Disabled. 05:52:03| Pinger socket opened on FD 19 05:52:03| Ready to serve requests. 05:52:03| Done scanning /usr/local/squid/var/cache (0 entries) 05:52:03| Finished rebuilding storage from disk. 05:52:03| & & & & 0 Entries scanned 05:52:03| & & & & 0 Invalid entries. 05:52:03| & & & & 0 With invalid flags. 05:52:03| & & & & 0 Objects loaded. 05:52:03| & & & & 0 Objects expired. 05:52:03| & & & & 0 Objects cancelled. 05:52:03| & & & & 0 Duplicate URLs purged. 05:52:03| & & & & 0 Swapfile clashes avoided. 05:52:03| & Took 0.3 seconds ( & 0.0 objects/sec). 05:52:03| Beginning Validation Procedure 05:52:03| & Completed Validation Procedure 05:52:03| & Validated 0 Entries 05:52:03| & store_swap_size = 0k 05:52:04| storeLateRelease: released 0 objects如果有到 ready to server reques,恭喜,启动成功。然后 ctrl + c,停止squid,并以后台运行的方式启动它。 &4、启动squid作为后台进程运行/usr/local/squid/sbin/squid -s &5、停止squid服务/usr/local/squid/sbin/squid -k shutdown6、重引导修改过的squid.conf配置文件/usr/local/squid/sbin/squid -k reconfigure这个估计用的时候比较多,当你发现你的配置有不尽你意的时候,可以随时修改squid.conf,然后别忘记对你的 squid.conf排错,然后再执行此指令,即可让squid重新按照你的 squid.conf 来运行。7、squid添加到系统启动项/usr/local/squid/sbin/squid -s8、查看运行信息/usr/local/squid/bin/squidclient -h 127.0.0.1 -p 80 mgr:info9、分割日志20 0 * * * /usr/local/squid/sbin/squid -k rotate10、清楚缓存/usr/local/squid/bin/squidclient -h 192.168.10.10 -p 80 -m PURGE http://192.168.10.10/code.jpeg11、查看你的日志文档more /usr/local/squid/var/logs/access.log | grep TCP_MEM_HIT该指令可以看到在squid运行过程中,有那些文件被squid缓存到内存中,并返回给访问用户more /usr/local/squid/var/logs/access.log | grep TCP_HIT该指令可以看到在squid运行过程中,有那些文件被squid缓存到cache目录中,并返回给访问用户more /usr/local/squid/var/logs/access.log | grep TCP_MISS该指令可以看到在squid运行过程中,有那些文件没有被squid缓存,而是现重原始服务器获取并返回给访问用户12、相关命令squid运行状态信息:/usr/local/squid/bin/squidclient -h 192.168.10.10 -p 80 mgr:info
/usr/local/squid/bin/squidclient -h 192.168.10.10 -p 80 mgr:5minsquid内存使用情况:/usr/local/squid/bin/squidclient -h 192.168.10.10 -p 80 mgr:memsquid缓存信息列表:/usr/local/squid/bin/squidclient -h 192.168.10.10 -p 80 mgr:objects (use it carefully,it may crash)squid磁盘使用情况:/usr/local/squid/bin/squidclient -h 192.168.10.10 -p 80 mgr:disk强制更新某个url:/usr/local/squid/bin/squidclient -h 192.168.10.10 -p 80 -m PURGE /本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:┆阅读(0)┆评论(0)

我要回帖

更多关于 ios退款理由怎么写 的文章

 

随机推荐