e6d905c442a17e78244f055717df98317e05c10e
   1"""
   2Commonly used general functions.
   3
   4This module provides the following methods:
   5    - `hostname`:       get the current machine's hostname
   6    - `getlocaldomain`: get the current machine's domain name
   7    - `resolve`:        attempt to convert a local/public IP to hostname
   8    - `readlog`:        read contents of a log file from disk
   9"""
  10
  11from datetime import datetime, timedelta
  12import ipaddress
  13import logging
  14import os
  15from pkg_resources import Requirement, resource_filename
  16import re
  17import socket
  18from systemd import journal
  19
  20from logparse import config, formatting
  21from logparse.timeparse import timeparse
  22
  23
  24logger = logging.getLogger(__name__)
  25
  26
  27def hostname(path): # get the hostname of current server
  28    """
  29    Get the hostname of the current machine using the file supplied in the
  30    `hostname-path` config option.
  31    """
  32
  33    hnfile = open(path, 'r')
  34    hn = re.search('^(\w*)\n*', hnfile.read()).group(1)
  35    return hn
  36
  37
  38def getlocaldomain(): # get the parent fqdn of current server
  39    """
  40    Get parent domain name (possibly FQDN) of the current machine. Note: if
  41    `socket.fetfqdn()` returns localhost, make sure the first entry in the
  42    hostname file includes the FQDN.
  43    """
  44
  45    domain = socket.getfqdn().split('.', 1)
  46    if len(domain) != 2:
  47        logger.warning("Could not get domain of this server, only hostname. Please consider updating the hostname file at {0}".format(config.prefs.get("logparse", "hostname-path")))
  48        return 'localdomain'
  49    else:
  50        return domain[-1]
  51
  52
  53def resolve(ip, fqdn=None):        # try to resolve an ip to hostname
  54    """
  55    Attempt to resolve an IP into a hostname or FQDN.
  56    Possible values for fqdn:
  57        - fqdn            show full hostname and domain
  58        - fqdn-implicit   show hostname and domain unless local
  59        - host-only       only show hostname
  60        - ip              never resolve anything
  61    Note resolve-domains settings defined in individual sections of the config
  62    take priority over the global config (this is enforced in parser modules)
  63    """
  64    
  65    if not fqdn:
  66        fqdn = config.prefs.get("logparse", "resolve-domains")
  67
  68    if fqdn == 'ip':
  69        return(ip)
  70
  71    try:
  72        ip_obj = ipaddress.ip_address(ip)
  73    except ValueError as err:
  74        logger.debug("Invalid format: " + str(err))
  75        return ip
  76
  77    try:
  78        hn = socket.gethostbyaddr(ip)[0] # resolve ip to hostname
  79    except socket.herror:
  80        # cannot resolve ip
  81        logger.debug(ip + " cannot be found, might not exist anymore")
  82        return(ip)
  83    except Exception as err:
  84        logger.warning("Failed to resolve hostname for " + ip + ": " + str(err))
  85        return(ip)  # return ip if no hostname exists
  86
  87    if (fqdn == "host-only") or (fqdn == "fqdn-implicit" and ip_obj.is_private):
  88        return hn.split('.')[0]
  89    if fqdn == 'fqdn' or fqdn == 'fqdn-implicit':
  90        return hn
  91    return hn
  92
  93
  94
  95def readlog(path = None, mode = 'r'):
  96    """
  97    Read a logfile from disk and return string
  98    """
  99    
 100    if (path == None):
 101        logger.error("No path provided")
 102        return 1
 103    else:
 104        if (os.path.isfile(path) is False):
 105            logger.error("Log at {0} was requested but does not exist".format(path))
 106            return ''
 107        else:
 108            try:
 109                return open(path, mode).read()
 110            except IOError or OSError as e:
 111                logger.warning("Error reading log at {0}: {1}".format(path, e.strerror))
 112                return 1
 113
 114class LogPeriod:
 115
 116    def __init__(self, section):
 117        if config.prefs.get(section.split("_")[0], "period"):
 118            self.startdate = datetime.now() - timeparse(config.prefs.get(section.split("_")[0], "period"))
 119            logger.debug("Parsing logs for {0} since {1}".format(section, self.startdate.strftime(formatting.DATEFMT + " " + formatting.TIMEFMT)))
 120            self.unique = True
 121        else:
 122            self.startdate = datetime.now() - timeparse(config.prefs.get("logparse", "period"))
 123            self.unique = False