"""
from datetime import datetime, timedelta
-import inspect
+import ipaddress
import logging
import os
from pkg_resources import Requirement, resource_filename
import socket
from systemd import journal
-from logparse import config
+from logparse import config, formatting
+from logparse.timeparse import timeparse
logger = logging.getLogger(__name__)
return(ip)
try:
- socket.inet_aton(ip) # succeeds if text contains ip
+ ip_obj = ipaddress.ip_address(ip)
+ except ValueError as err:
+ logger.debug("Invalid format: " + str(err))
+ return ip
+
+ try:
hn = socket.gethostbyaddr(ip)[0] # resolve ip to hostname
- if fqdn == 'fqdn-implicit' and hn.split('.', 1)[1] == getlocaldomain():
- return(hn.split('.')[0])
- elif fqdn == 'fqdn' or fqdn == 'fqdn-implicit':
- return(hn)
- elif fqdn == 'host-only':
- return(hn.split('.')[0])
- else:
- logger.warning("Invalid value for FQDN config")
- return(hn)
except socket.herror:
# cannot resolve ip
logger.debug(ip + " cannot be found, might not exist anymore")
return(ip)
- except (OSError, socket.error): # socket.error for Python 2 compatibility
- # already a hostname
- logger.debug(ip + " is already a hostname")
- return(ip)
except Exception as err:
- logger.warning("failed to resolve hostname for " + ip + ": " + str(err))
+ logger.warning("Failed to resolve hostname for " + ip + ": " + str(err))
return(ip) # return ip if no hostname exists
+ if (fqdn == "host-only") or (fqdn == "fqdn-implicit" and ip_obj.is_private):
+ return hn.split('.')[0]
+ if fqdn == 'fqdn' or fqdn == 'fqdn-implicit':
+ return hn
+ return hn
+
+
def readlog(path = None, mode = 'r'):
"""
except IOError or OSError as e:
logger.warning("Error reading log at {0}: {1}".format(path, e.strerror))
return 1
+
+class LogPeriod:
+
+ def __init__(self, section):
+ if config.prefs.get(section.split("_")[0], "period"):
+ self.startdate = datetime.now() - timeparse(config.prefs.get(section.split("_")[0], "period"))
+ logger.debug("Parsing logs for {0} since {1}".format(section, self.startdate.strftime(formatting.DATEFMT + " " + formatting.TIMEFMT)))
+ self.unique = True
+ else:
+ self.startdate = datetime.now() - timeparse(config.prefs.get("logparse", "period"))
+ self.unique = False