dongfangho 发表于 2018-11-8 11:30:06

nginx 日志访问量统计

  1.根据访问IP统计UV
  awk '{print $1}'access.log|sort | uniq -c |wc -l
  2.统计访问URL统计PV
  awk '{print $7}' access.log|wc -l
  3.查询访问最频繁的URL
  awk '{print $7}' access.log|sort | uniq -c |sort -n -k 1 -r|more
  4.查询访问最频繁的IP
  awk '{print $1}' access.log|sort | uniq -c |sort -n -k 1 -r|more
  5.根据时间段统计查看日志
  cataccess.log| sed -n '/14\/Mar\/2015:21/,/14\/Mar\/2015:22/p'|more
  zabbix 配置文件添加
  vim /etc/zabbix/zabbix_server.conf
   view plain copy
  ### Option: AlertScriptsPath

Full path to location of custom alert scripts.

Default depends on compilation options.



Mandatory: no

Default:

AlertScriptsPath=${datadir}/zabbix/alertscripts
  AlertScriptsPath=/usr/local/zabbix/sbin
  vim /usr/local/zabbix/sbin/nginx-status.py
   view plain copy
  import urllib2,base64,sys,getopt,re
  def Usage():
  print "Usage: Nginx-Status.py -h 127.0.0.1 -p 80 -a "
  sys.exit(2)
  def main():
  if len(sys.argv) < 6:
  Usage()
  

try:  opts,args = getopt.getopt(sys.argv, "h:p:a:")
  Dict = dict(opts)
  
except getopt.GetoptError:
  Usage()
  

  
Nginx_url = "http://" + Dict['-h'] + ":" + Dict['-p'] + "/nginx_status"
  
Nginx_req = urllib2.Request(Nginx_url)
  
Nginx_res = urllib2.urlopen(Nginx_req)
  
Output_key = re.findall(r'\d{1,8}', Nginx_res.read())
  

  
if ( Dict['-a'] == "active"):
  print Output_key
  
elif ( Dict['-a'] == "accepted"):
  print Output_key
  
elif ( Dict['-a'] == "handled"):
  print Output_key
  
elif ( Dict['-a'] == "requests"):
  print Output_key
  
elif ( Dict['-a'] == "reading"):
  print Output_key
  
elif ( Dict['-a'] == "writting"):
  print Output_key
  
elif ( Dict['-a'] == "waiting"):
  print Output_key
  
else:
  print "unknown!!"
  sys.exit(1)
  

  if name == 'main':
  main()
  nginx_status_templates.xml
   view plain copy
  
  
  2.0
  2015-11-03T01:37:04Z
  
  
  nginx-status
  
  
  
  
  nginx-status
  nginx-status
  
  
  
  nginx-status
  
  
  
  
  
  nginx-accepted
  10
  
  0
  
  nginx-status.py[&quot;-h&quot;,&quot;{HOST.IP}&quot;,&quot;-p&quot;,&quot;80&quot;,&quot;-a&quot;,&quot;accepted&quot;]
  30
  90
  365
  0
  3
  
  
  0
  
  
  0
  0
  
  0
  
  1
  
  
  
  0
  0
  
  
  
  
  
  
  0
  
  
  
  
  
  nginx-active
  10
  
  0
  
  nginx-status.py[&quot;-h&quot;,&quot;{HOST.IP}&quot;,&quot;-p&quot;,&quot;80&quot;,&quot;-a&quot;,&quot;active&quot;]
  30
  90
  365
  0
  3
  
  
  0
  
  
  0
  0
  
  0
  
  1
  
  
  
  0
  0
  
  
  
  
  
  
  0
  
  
  
  
  
  nginx-handled
  10
  
  0
  
  nginx-status.py[&quot;-h&quot;,&quot;{HOST.IP}&quot;,&quot;-p&quot;,&quot;80&quot;,&quot;-a&quot;,&quot;handled&quot;]
  30
  90
  365
  0
  3
  
  
  0
  
  
  0
  0
  
  0
  
  1
  
  
  
  0
  0
  
  
  
  
  
  
  0
  
  
  
  
  
  nginx-reading
  10
  
  0
  
  nginx-status.py[&quot;-h&quot;,&quot;{HOST.IP}&quot;,&quot;-p&quot;,&quot;80&quot;,&quot;-a&quot;,&quot;reading&quot;]
  30
  90
  365
  0
  3
  
  
  0
  
  
  0
  0
  
  0
  
  1
  
  
  
  0
  0
  
  
  
  
  
  
  0
  
  
  
  
  
  nginx-requests
  10
  
  0
  
  nginx-status.py[&quot;-h&quot;,&quot;{HOST.IP}&quot;,&quot;-p&quot;,&quot;80&quot;,&quot;-a&quot;,&quot;requests&quot;]
  30
  90
  365
  0
  3
  
  
  0
  
  
  0
  0
  
  0
  
  1
  
  
  
  0
  0
  
  
  
  
  
  
  0
  
  
  
  
  
  nginx-waiting
  10
  
  0
  
  nginx-status.py[&quot;-h&quot;,&quot;{HOST.IP}&quot;,&quot;-p&quot;,&quot;80&quot;,&quot;-a&quot;,&quot;waiting&quot;]
  30
  90
  365
  0
  3
  
  
  0
  
  
  0
  0
  
  0
  
  1
  
  
  
  0
  0
  
  
  
  
  
  
  0
  
  
  
  
  
  nginx-writting
  10
  
  0
  
  nginx-status.py[&quot;-h&quot;,&quot;{HOST.IP}&quot;,&quot;-p&quot;,&quot;80&quot;,&quot;-a&quot;,&quot;writting&quot;]
  30
  90
  365
  0
  3
  
  
  0
  
  
  0
  0
  
  0
  
  1
  
  
  
  0
  0
  
  
  
  
  
  
  0
  
  
  
  
  
  
  
  
  
  
  
  
  
  nginx-clients-status
  900
  200
  0.0000
  100.0000
  1
  1
  0
  1
  0
  0.0000
  0.0000
  0
  0
  0
  0
  
  
  0
  0
  00C800
  0
  2
  0
  
  nginx-status
  nginx-status.py[&quot;-h&quot;,&quot;{HOST.IP}&quot;,&quot;-p&quot;,&quot;80&quot;,&quot;-a&quot;,&quot;active&quot;]
  
  
  
  1
  0
  C80000
  0
  2
  0
  
  nginx-status
  nginx-status.py[&quot;-h&quot;,&quot;{HOST.IP}&quot;,&quot;-p&quot;,&quot;80&quot;,&quot;-a&quot;,&quot;reading&quot;]
  
  
  
  2
  0
  0000C8
  0
  2
  0
  
  nginx-status
  nginx-status.py[&quot;-h&quot;,&quot;{HOST.IP}&quot;,&quot;-p&quot;,&quot;80&quot;,&quot;-a&quot;,&quot;waiting&quot;]
  
  
  
  3
  0
  C800C8
  0
  2
  0
  
  nginx-status
  nginx-status.py[&quot;-h&quot;,&quot;{HOST.IP}&quot;,&quot;-p&quot;,&quot;80&quot;,&quot;-a&quot;,&quot;writting&quot;]
  
  
  
  
  
  nginx-socket-status
  900
  200
  0.0000
  100.0000
  1
  1
  0
  1
  0
  0.0000
  0.0000
  0
  0
  0
  0
  
  
  0
  0
  00C800
  0
  2
  0
  
  nginx-status
  nginx-status.py[&quot;-h&quot;,&quot;{HOST.IP}&quot;,&quot;-p&quot;,&quot;80&quot;,&quot;-a&quot;,&quot;handled&quot;]
  
  
  
  1
  0
  0000C8
  0
  2
  0
  
  nginx-status
  nginx-status.py[&quot;-h&quot;,&quot;{HOST.IP}&quot;,&quot;-p&quot;,&quot;80&quot;,&quot;-a&quot;,&quot;requests&quot;]
  
  
  
  2
  0
  C800C8
  0
  2
  0
  
  nginx-status
  nginx-status.py[&quot;-h&quot;,&quot;{HOST.IP}&quot;,&quot;-p&quot;,&quot;80&quot;,&quot;-a&quot;,&quot;accepted&quot;]
  
  
  
  
  
  


页: [1]
查看完整版本: nginx 日志访问量统计