From 735b50b67f54ccf45130f200392d1b2a6e168b43 Mon Sep 17 00:00:00 2001 From: Alexey Date: Mon, 15 Oct 2018 14:27:41 +0300 Subject: [PATCH] Fixed after logrotate file access problem --- geoparser.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/geoparser.py b/geoparser.py index 005fab1..d216356 100755 --- a/geoparser.py +++ b/geoparser.py @@ -13,7 +13,7 @@ import configparser from influxdb import InfluxDBClient -def logparse(LOGPATH, INFLUXHOST, INFLUXPORT, INFLUXDBDB, INFLUXUSER, INFLUXUSERPASS, MEASUREMENT): # NOQA +def logparse(LOGPATH, INFLUXHOST, INFLUXPORT, INFLUXDBDB, INFLUXUSER, INFLUXUSERPASS, MEASUREMENT, INODE): # NOQA # Preparing variables and params IPS = {} COUNT = {} @@ -29,10 +29,13 @@ def logparse(LOGPATH, INFLUXHOST, INFLUXPORT, INFLUXDBDB, INFLUXUSER, INFLUXUSER STR_RESULTS = os.stat(LOGPATH) ST_SIZE = STR_RESULTS[6] FILE.seek(ST_SIZE) - while 1: + while True: METRICS = [] WHERE = FILE.tell() LINE = FILE.readline() + INODENEW = os.stat(LOGPATH).st_ino + if INODE != INODENEW: + break if not LINE: time.sleep(1) FILE.seek(WHERE) @@ -71,7 +74,14 @@ def main(): INFLUXUSERPASS = CONFIG.get('INFLUXDB', 'password') # Parsing log file and sending metrics to Influxdb - logparse(LOGPATH, INFLUXHOST, INFLUXPORT, INFLUXDBDB, INFLUXUSER, INFLUXUSERPASS, MEASUREMENT) # NOQA + while True: + # Get inode from log file + INODE = os.stat(LOGPATH).st_ino + # Run main loop and grep a log file + if os.path.exists(LOGPATH): + logparse(LOGPATH, INFLUXHOST, INFLUXPORT, INFLUXDBDB, INFLUXUSER, INFLUXUSERPASS, MEASUREMENT, INODE) # NOQA + else: + print('File %s not found' % LOGPATH) if __name__ == '__main__':