From 2ba86f3f1bb90960506b693173bb2eae07f8f319 Mon Sep 17 00:00:00 2001 From: Alexey Nizhegolenko Date: Sun, 24 Jan 2021 14:44:33 +0200 Subject: [PATCH] Added the standard logging syslog support, also were added a additional tags with city.name and country.name --- geoparser.py | 32 ++++++++++++++++++++++++++++---- requirements.txt | 1 + 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/geoparser.py b/geoparser.py index 0bb4033..c32e510 100755 --- a/geoparser.py +++ b/geoparser.py @@ -1,4 +1,4 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 # Getting GEO information from Nginx access.log by IP's. # Alexey Nizhegolenko 2018 @@ -10,12 +10,28 @@ import os import re import sys import time -import geoip2.database import geohash +import logging +import logging.handlers +import geoip2.database import configparser from influxdb import InfluxDBClient from IPy import IP as ipadd + +class SyslogBOMFormatter(logging.Formatter): + def format(self, record): + result = super().format(record) + return "ufeff" + result + + +handler = logging.handlers.SysLogHandler('/dev/log') +formatter = SyslogBOMFormatter(logging.BASIC_FORMAT) +handler.setFormatter(formatter) +root = logging.getLogger(__name__) +root.setLevel(os.environ.get("LOGLEVEL", "INFO")) +root.addHandler(handler) + def logparse(LOGPATH, INFLUXHOST, INFLUXPORT, INFLUXDBDB, INFLUXUSER, INFLUXUSERPASS, MEASUREMENT, GEOIPDB, INODE): # NOQA # Preparing variables and params IPS = {} @@ -53,7 +69,7 @@ def logparse(LOGPATH, INFLUXHOST, INFLUXPORT, INFLUXDBDB, INFLUXUSER, INFLUXUSER m = re_IPV6.match(LINE) IP = m.group(1) - if ipadd(IP).iptype() == 'PUBLIC' and IP: + if ipadd(IP).iptype() == 'PUBLIC' and IP: INFO = GI.city(IP) if INFO is not None: HASH = geohash.encode(INFO.location.latitude, INFO.location.longitude) # NOQA @@ -61,13 +77,18 @@ def logparse(LOGPATH, INFLUXHOST, INFLUXPORT, INFLUXDBDB, INFLUXUSER, INFLUXUSER GEOHASH['geohash'] = HASH GEOHASH['host'] = HOSTNAME GEOHASH['country_code'] = INFO.country.iso_code + GEOHASH['country_name'] = INFO.country.country.name + GEOHASH['city_name'] = INFO.city.name IPS['tags'] = GEOHASH IPS['fields'] = COUNT IPS['measurement'] = MEASUREMENT METRICS.append(IPS) # Sending json data to InfluxDB - CLIENT.write_points(METRICS) + try: + CLIENT.write_points(METRICS) + except Exception: + logging.exception("Cannot establish connection to InfluxDB: ") def main(): @@ -100,5 +121,8 @@ def main(): if __name__ == '__main__': try: main() + except Exception: + logging.exception("Exception in main(): ") except KeyboardInterrupt: + logging.exception("Exception KeyboardInterrupt: ") sys.exit(0) diff --git a/requirements.txt b/requirements.txt index b9d0bca..b67a6a3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ configparser==3.5.0 influxdb==5.2.0 geoip2==2.9.0 IPy==1.00 +logging