From d948998ac3bada9dcceb70e879b0ca1106459760 Mon Sep 17 00:00:00 2001 From: Alexey Date: Wed, 10 Oct 2018 10:56:13 +0300 Subject: [PATCH] change some things in install.sh again --- install.sh | 14 +++++---- parser.py | 84 ------------------------------------------------------ 2 files changed, 8 insertions(+), 90 deletions(-) delete mode 100755 parser.py diff --git a/install.sh b/install.sh index 826c05e..9e3b495 100755 --- a/install.sh +++ b/install.sh @@ -4,29 +4,31 @@ ## Installetion script for GeoStat ## Alexey Nizhegolenko 2018 ## - +echo "" echo "Creating virtual ENV and installing requirements..." -virtualenv venv && source venv/bin/activate sleep 1 +virtualenv venv && source venv/bin/activate pip install -r requirements.txt && deactivate -sleep 1 +echo "" echo "Please edit settings.ini file and set right parameters..." +sleep 1 cp settings.ini.back settings.ini "${VISUAL:-"${EDITOR:-vi}"}" "settings.ini" - +echo "" echo "Installing SystemD service..." +sleep 1 while read line do eval echo "$line" done < "./geostat.service.template" > /lib/systemd/system/geostat.service systemctl enable geostat.service -sleep 1 +echo "" echo "All done, now you can start getting GEO data from your log" -echo "run 'systemd start geostat.service' for this" +echo "run 'systemctl start geostat.service' for this" echo "Good Luck !" diff --git a/parser.py b/parser.py deleted file mode 100755 index f9dbac3..0000000 --- a/parser.py +++ /dev/null @@ -1,84 +0,0 @@ -#! /usr/bin/env python - - -# Getting GEO information for Nginx access.log IP's. -# Alexey Nizhegolenko 2018 - - -import os -import re -# import sys -import json -import pygeoip -# import subprocess -import Geohash -import configparser -from collections import Counter -# from datetime import datetime - - -def logparse(logpath): - GI = pygeoip.GeoIP('GeoLiteCity.dat', pygeoip.const.MEMORY_CACHE) - GETIP = r"^(?P[0-9]{,3}\.[0-9]{,3}\.[0-9]{,3}\.[0-9]{,3})" - IPS = Counter() - with open(logpath, "r") as file: - for line in file: - IP = re.search(GETIP, line).group(1) - if IP: - IPS[IP] += 1 - OUTPUT = [] - for KEYIP, VALUE in IPS.items(): - OUTIPS = {} - INFO = GI.record_by_addr(KEYIP) - if INFO is not None: - HASH = Geohash.encode(INFO['latitude'], INFO['longitude']) - OUTIPS['ip'] = KEYIP - OUTIPS['geohash'] = HASH - OUTIPS['count'] = VALUE - OUTPUT.append(OUTIPS) - - return OUTPUT - - -''' -def logparse(logpath): - GI = pygeoip.GeoIP('GeoLiteCity.dat', pygeoip.const.MEMORY_CACHE) - GETIP = r"^(?P[0-9]{,3}\.[0-9]{,3}\.[0-9]{,3}\.[0-9]{,3})" - OUTPUT = [] - IPS = {} - with open(logpath, "r") as file: - for line in file: - IP = re.search(GETIP, line) - if IP: - INFO = GI.record_by_addr(IP.group(1)) - if INFO is not None: - HASH = Geohash.encode(INFO['latitude'], INFO['longitude']) - # IPS['key'] = INFO['country_code'] - # IPS['name'] = INFO['country_name'] - IPS['ip'] = IP.group(1) - IPS['geohash'] = HASH - # IPS['data'] = {'latitude': INFO['latitude'], 'longitude': INFO['longitude']} # NOQA - # IPS['latitude'] = INFO['latitude'] - # IPS['longitude'] = INFO['longitude'] - OUTPUT.append(IPS) - return OUTPUT -''' - - -def main(): - # Set LANG ENV - os.environ["LC_ALL"] = "C" - # Getting params from config - pwd = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) - config = configparser.ConfigParser() - config.read('%s/settings.ini' % pwd) - logpath = config.get('NGINX_LOG', 'logpath') - # Parse given log file - IPS_IN = logparse(logpath) - # Send result to fle - with open('/tmp/metrics.json', 'w') as outputfile: - json.dump(IPS_IN, outputfile, indent=4, sort_keys=True) - - -if __name__ == '__main__': - main()