Browse Source

New feature: stop point

master
Vladimir Smagin 2 years ago
parent
commit
6986545c59
1 changed files with 27 additions and 6 deletions
  1. +27
    -6
      nginx-analyzer.py

caltat-analyzer.py → nginx-analyzer.py View File

@@ -1,3 +1,10 @@
# ---------------------------------------
# Nginx special format analyzer
# Copyright by Vladimir Smagin, 2017
# -----
# Download new version https://bitbucket.org/21h/nginx-csv-log-analyzer/
# Any questions? 21h@blindage.org or http://blindage.org

import csv
from terminaltables import AsciiTable
import argparse
@@ -18,14 +25,16 @@ import time


cmdLineParser = argparse.ArgumentParser(description='Get statistics from nginx special format log file')
cmdLineParser.add_argument('--log', metavar='logfile', type=str, required=True, help='Of course, you need to parse something')
cmdLineParser.add_argument('--log', metavar='logfile', type=str, required=True,
help='Of course, you need to parse something')
cmdLineParser.add_argument('--code', metavar='HTTP_CODE', type=int, help='Show only response code')
cmdLineParser.add_argument('--time', metavar='DD-MM-YYYY HH:MM:SS', type=str, help='Start from timestamp DD-MM-YYYY HH:MM:SS')
cmdLineParser.add_argument('--time', metavar='', type=str, help='Start counting from timestamp DD-MM-YYYY HH:MM:SS')
cmdLineParser.add_argument('--totime', metavar='', type=str, help='Stop counting from timestamp DD-MM-YYYY HH:MM:SS')
cmdLineParser.add_argument('--summ', action='store_true', help='Display fancy summary')
args = cmdLineParser.parse_args()


def summary(userUnixTimeStamp):
def summary(userUnixTimeStamp, userUnixTimeStampStop):
with open(args.log) as nginxLogFile:
successCounter = 0
redirectCounter = 0
@@ -42,6 +51,8 @@ def summary(userUnixTimeStamp):
logUnixTimeStamp = time.strptime(logString[1], '%d/%b/%Y:%H:%M:%S %z')
if userUnixTimeStamp >= logUnixTimeStamp:
continue
if userUnixTimeStampStop < logUnixTimeStamp:
break
allRequests += 1
if '20' in logString[2]: successCounter += 1
if '30' in logString[2]: redirectCounter += 1
@@ -67,7 +78,7 @@ def summary(userUnixTimeStamp):
print(resultTable.table)


def searchCode(userUnixTimeStamp, httpCode):
def searchCode(httpCode, userUnixTimeStamp, userUnixTimeStampStop):
with open(args.log) as nginxLogFile:
codeCounter = 0
logStringsIterable = csv.reader(nginxLogFile, delimiter=",", quotechar='"', lineterminator=";")
@@ -78,19 +89,29 @@ def searchCode(userUnixTimeStamp, httpCode):
logUnixTimeStamp = time.strptime(logString[1], '%d/%b/%Y:%H:%M:%S %z')
if userUnixTimeStamp >= logUnixTimeStamp:
continue
if userUnixTimeStampStop < logUnixTimeStamp:
break
if str(httpCode) in logString[2]:
codeCounter += 1
# print result
print(codeCounter)


# Set start time point

if args.time:
unixTimeStamp = time.strptime(args.time, '%d-%m-%Y %H:%M:%S')
else:
unixTimeStamp = False

# Set stop time point
if args.totime:
unixTimeStampStop = time.strptime(args.totime, '%d-%m-%Y %H:%M:%S')
else:
unixTimeStampStop = False

if args.code:
searchCode(unixTimeStamp, args.code)
searchCode(args.code, unixTimeStamp, unixTimeStampStop)

if args.summ:
summary(unixTimeStamp)
summary(unixTimeStamp, unixTimeStampStop)

Loading…
Cancel
Save