Cleared a code a bit
This commit is contained in:
parent
ddd2d58649
commit
50700ce12e
1 changed files with 19 additions and 16 deletions
35
geoparser.py
35
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<remote_host>[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()
|
||||
|
|
Loading…
Reference in a new issue