From 50700ce12ec192570464baf21fa153a79ffc420f Mon Sep 17 00:00:00 2001 From: Alexey Date: Mon, 8 Oct 2018 23:25:40 +0300 Subject: [PATCH] Cleared a code a bit --- geoparser.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/geoparser.py b/geoparser.py index a7373b4..96ac75a 100755 --- a/geoparser.py +++ b/geoparser.py @@ -1,30 +1,29 @@ #! /usr/bin/env python -# Getting GEO information for Nginx access.log IP's. +# Getting GEO information from Nginx access.log by IP's. # Alexey Nizhegolenko 2018 import os import re -# import sys +import sys import time -import json import pygeoip -# import subprocess import Geohash import configparser from influxdb import InfluxDBClient -# from collections import Counter -# from datetime import datetime def logparse(LOGPATH, INFLUXHOST, INFLUXPORT, INFLUXDBDB, INFLUXUSER, INFLUXUSERPASS, MEASUREMENT): # NOQA + # Preparing variables and params + IPS = {} + COUNT = {} + GEOHASH = {} CLIENT = InfluxDBClient(host=INFLUXHOST, port=INFLUXPORT, username=INFLUXUSER, password=INFLUXUSERPASS, database=INFLUXDBDB) # NOQA GETIP = r"^(?P[0-9]{,3}\.[0-9]{,3}\.[0-9]{,3}\.[0-9]{,3})" GI = pygeoip.GeoIP('GeoLiteCity.dat', pygeoip.const.MEMORY_CACHE) - GEOHASH = {} - COUNT = {} - IPS = {} + + # Main loop to parse access.log file in tailf style with sending metrcs with open(LOGPATH, "r") as FILE: STR_RESULTS = os.stat(LOGPATH) ST_SIZE = STR_RESULTS[6] @@ -42,19 +41,19 @@ def logparse(LOGPATH, INFLUXHOST, INFLUXPORT, INFLUXDBDB, INFLUXUSER, INFLUXUSER INFO = GI.record_by_addr(IP) if INFO is not None: HASH = Geohash.encode(INFO['latitude'], INFO['longitude']) # NOQA - GEOHASH['geohash'] = HASH COUNT['count'] = 1 - IPS['measurement'] = MEASUREMENT + GEOHASH['geohash'] = HASH IPS['tags'] = GEOHASH IPS['fields'] = COUNT + IPS['measurement'] = MEASUREMENT METRICS.append(IPS) - RESULT = json.dumps(METRICS) - CLIENT.write_points(RESULT) + + # Sending json data to InfluxDB + CLIENT.write_points(METRICS) def main(): - - # Preparing of config reading + # Preparing for reading config file PWD = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) CONFIG = configparser.ConfigParser() CONFIG.read('%s/settings.ini' % PWD) @@ -73,4 +72,8 @@ def main(): if __name__ == '__main__': - main() + try: + main() + except KeyboardInterrupt: + os.system('clear') + sys.exit()