最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實(shí)時(shí)分析日志的一個(gè)小腳本分享

 更新時(shí)間:2017年05月07日 15:56:56   作者:不懂真人  
這篇文章主要給大家分享了一個(gè)實(shí)時(shí)分析日志的python小腳本,文中給出了詳細(xì)的介紹和示例代碼供大家參考學(xué)習(xí),對(duì)大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。

前言

大家都知道Web運(yùn)維總要關(guān)注相關(guān)域名的實(shí)時(shí)2xx/s、4xx/s、5xx/s、響應(yīng)時(shí)間、帶寬等這些指標(biāo),之前的日志是五分鐘一分割,簡(jiǎn)單的用awk就可以了,現(xiàn)在由于要推送日志到ELK,繼續(xù)之前五分鐘一分割會(huì)有問題,就改為一天分割一次。改成一天一分割后,顯然再繼續(xù)用Shell就不合適了,于是就用Python寫了下。

方法如下:

腳本主要運(yùn)用了文件的seek和tell函數(shù),原理如下:

       1.加入crontab,每5分鐘執(zhí)行一次

       2.只分析從上次讀取日志文件的結(jié)束位置到這次讀取文件時(shí)的末尾位置之間的日志,出結(jié)果
可以使用zabbix_sender把結(jié)果發(fā)送到zabbix server或者直接使用zabbix agent來讀取這個(gè)文件取數(shù)據(jù),配合zabbix出圖、做報(bào)警,代碼如下:

#!/usr/bin/env python
#coding: utf-8

from __future__ import division
import os

LOG_FILE = '/data0/logs/nginx/xxxx-access_log'
POSITION_FILE = '/tmp/position.log'
STATUS_FILE = '/tmp/http_status'
#crontab 執(zhí)行時(shí)間
CRON_TIME = 300

def get_position():
 #第一次讀取日志文件,POSITION_FILE為空
 if not os.path.exists(POSITION_FILE):
  start_position = str(0)
  end_position = str(os.path.getsize(LOG_FILE))
  fh = open(POSITION_FILE,'w')
  fh.write('start_position: %s\n' % start_position)
  fh.write('end_position: %s\n' % end_position)
  fh.close()
  os._exit(1)
 else:
  fh = open(POSITION_FILE)
  se = fh.readlines()
  fh.close()
  #其他意外情況導(dǎo)致POSITION_FILE內(nèi)容不是兩行
  if len(se) != 2:
   os.remove(POSITION_FILE)
   os._exit(1)
  last_start_position,last_end_position = [item.split(':')[1].strip() for item in se]
  start_position = last_end_position
  end_position = str(os.path.getsize(LOG_FILE))
  #日志輪轉(zhuǎn)導(dǎo)致start_position > end_position
  #print start_position,end_position
  if start_position > end_position:
   start_position = 0
  #日志停止?jié)L動(dòng)時(shí)
  elif start_position == end_position:
   os._exit(1)
  #print start_position,end_position
  fh = open(POSITION_FILE,'w')
  fh.write('start_position: %s\n' % start_position)
  fh.write('end_position: %s\n' % end_position)
  fh.close()
  return map(int,[start_position,end_position])

def write_status(content):
 fh = open(STATUS_FILE,'w')
 fh.write(content)
 fh.close()

def handle_log(start_position,end_position):
 log = open(LOG_FILE)
 log.seek(start_position,0)
 status_2xx,status_403,status_404,status_500,status_502,status_503,status_504,status_all,rt,bandwidth = 0,0,0,0,0,0,0,0,0,0
 while True:
  current_position = log.tell()
  if current_position >= end_position:
   break
  line = log.readline()
  line = line.split(' ')
  host,request_time,time_local,status,bytes_sent = line[1],line[3],line[5],line[10],line[11]
  #print host,request_time,time_local,status,bytes_sent
  status_all += 1
  try:
   rt += float(request_time.strip('s'))
   bandwidth += int(bytes_sent)
  except:
   pass
  if status == '200' or status == '206':
   status_2xx += 1
  elif status == '403':
   status_403 += 1
  elif status == '404':
   status_404 += 1
  elif status == '500':
   status_500 += 1
  elif status == '502':
   status_502 += 1
  elif status == '503':
   status_503 += 1
  elif status == '504':
   status_504 += 1
 log.close()
 #print "status_2xx: %s\nstatus_403: %s\nstatus_404: %s\nstatus_500: %s\nstatus_502: %s\nstatus_503: %s\nstatus_504: %s\nstatus_all: %s\nrt: %s\nbandwidth: %s\n" % (status_2xx/CRON_TIME,status_403/CRON_TIME,status_404/CRON_TIME,status_500/CRON_TIME,status_502/CRON_TIME,status_503/CRON_TIME,status_504/CRON_TIME,status_all/CRON_TIME,rt/status_all,bandwidth/CRON_TIME)

 write_status("status_2xx: %s\nstatus_403: %s\nstatus_404: %s\nstatus_500: %s\nstatus_502: %s\nstatus_503: %s\nstatus_504: %s\nstatus_all: %s\nrt: %s\nbandwidth: %s\n" % (status_2xx/CRON_TIME,status_403/CRON_TIME,status_404/CRON_TIME,status_500/CRON_TIME,status_502/CRON_TIME,status_503/CRON_TIME,status_504/CRON_TIME,status_all/CRON_TIME,rt/status_all,bandwidth/CRON_TIME))

if __name__ == '__main__':
 start_position,end_position = get_position()
 handle_log(start_position,end_position)

看下分析的結(jié)果:

cat /tmp/http_status
status_2xx: 17.3333333333
status_403: 0.0
status_404: 1.0
status_500: 0.0
status_502: 0.0
status_503: 0.0
status_504: 0.0
status_all: 20.0
rt: 0.0782833333333
bandwidth: 204032.0

后來發(fā)現(xiàn)有點(diǎn)問題,start_position、end_position 使用字符串比較會(huì)有問題,如下:

In [5]: '99772400' > '100227572'
Out[5]: True

In [6]: int('99772400') > int('100227572')
Out[6]: False

因此,更正為:

#日志輪轉(zhuǎn)導(dǎo)致start_position > end_position
#print start_position,end_position
if int(start_position) > int(end_position):
 start_position = 0
#日志停止?jié)L動(dòng)時(shí)
elif int(start_position) == int(end_position):
 os._exit(1)

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • Python 線程池模塊之多線程操作代碼

    Python 線程池模塊之多線程操作代碼

    最近在做一個(gè)爬蟲相關(guān)的項(xiàng)目,單線程的整站爬蟲,耗時(shí)真的不是一般的巨大,運(yùn)行一次也是心累,所以,要想實(shí)現(xiàn)整站爬蟲,多線程是不可避免的,那么python多線程又應(yīng)該怎樣實(shí)現(xiàn)呢?今天小編給大家分享下實(shí)現(xiàn)代碼,感興趣的朋友一起看看吧
    2021-05-05
  • 用python打印1~20的整數(shù)實(shí)例講解

    用python打印1~20的整數(shù)實(shí)例講解

    在本篇內(nèi)容中小編給大家分享了關(guān)于python打印1~20的整數(shù)的具體步驟以及實(shí)例方法,需要的朋友們參考下。
    2019-07-07
  • 10個(gè)簡(jiǎn)單但很有用的Python裝飾器分享

    10個(gè)簡(jiǎn)單但很有用的Python裝飾器分享

    裝飾器(Decorators)是Python中一種強(qiáng)大而靈活的功能,用于修改或增強(qiáng)函數(shù)或類的行為,本文為大家整理了10個(gè)簡(jiǎn)單但很有用的Python裝飾器,希望對(duì)大家有所幫助
    2023-08-08
  • python中求兩個(gè)向量的夾角方式

    python中求兩個(gè)向量的夾角方式

    這篇文章主要介紹了python中求兩個(gè)向量的夾角方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Python filter()及reduce()函數(shù)使用方法解析

    Python filter()及reduce()函數(shù)使用方法解析

    這篇文章主要介紹了Python filter()及reduce()函數(shù)使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Python max函數(shù)中key的用法及原理解析

    Python max函數(shù)中key的用法及原理解析

    最近有童鞋向小編求助怎么樣找到字符串中出現(xiàn)字?jǐn)?shù)最多的字符呢,其實(shí)最簡(jiǎn)單的處理方法是使用max函數(shù),max()函數(shù)用于獲得給定的可迭代對(duì)象中的最大值,關(guān)于Python max函數(shù)key用法跟隨小編一起通過本文學(xué)習(xí)下吧
    2021-06-06
  • Python Miniforge3 環(huán)境配置的實(shí)現(xiàn)

    Python Miniforge3 環(huán)境配置的實(shí)現(xiàn)

    這篇文章主要介紹了Python Miniforge3 環(huán)境配置的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考,一起跟隨小編過來看看吧
    2017-11-11
  • Pytorch如何指定device(cuda or cpu)

    Pytorch如何指定device(cuda or cpu)

    這篇文章主要介紹了Pytorch如何指定device(cuda or cpu)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • 解決Pycharm 運(yùn)行后沒有輸出的問題

    解決Pycharm 運(yùn)行后沒有輸出的問題

    這篇文章主要介紹了解決Pycharm 運(yùn)行后沒有輸出的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • 使用pycharm運(yùn)行flask應(yīng)用程序的詳細(xì)教程

    使用pycharm運(yùn)行flask應(yīng)用程序的詳細(xì)教程

    這篇文章主要介紹了使用pycharm運(yùn)行flask應(yīng)用程序,首先大家需要使用pycharm創(chuàng)建你的第一個(gè)app,接下來就開始配置pycharm,需要的朋友可以參考下
    2021-06-06

最新評(píng)論

孟连| 尖扎县| 桂林市| 天柱县| 福建省| 来安县| 望都县| 增城市| 普宁市| 梁山县| 石景山区| 玛沁县| 搜索| 鹤峰县| 绥芬河市| 武城县| 铜鼓县| 莱西市| 苏尼特左旗| 孟连| 镇平县| 萝北县| 杭锦后旗| 东光县| 乌什县| 微博| 阳信县| 商水县| 奉节县| 饶阳县| 呼和浩特市| 大同市| 岳池县| 西林县| 嵊泗县| 洛川县| 莱阳市| 南宁市| 达孜县| 嘉荫县| 靖远县|