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 27defhostname(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) 35return hn 36 37 38defgetlocaldomain():# 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) 46iflen(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"))) 48return'localdomain' 49else: 50return domain[-1] 51 52 53defresolve(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 65if not fqdn: 66 fqdn = config.prefs.get("logparse","resolve-domains") 67 68if fqdn =='ip': 69return(ip) 70 71try: 72 ip_obj = ipaddress.ip_address(ip) 73exceptValueErroras err: 74 logger.debug("Invalid format: "+str(err)) 75return ip 76 77try: 78 hn = socket.gethostbyaddr(ip)[0]# resolve ip to hostname 79except socket.herror: 80# cannot resolve ip 81 logger.debug(ip +" cannot be found, might not exist anymore") 82return(ip) 83exceptExceptionas err: 84 logger.warning("Failed to resolve hostname for "+ ip +": "+str(err)) 85return(ip)# return ip if no hostname exists 86 87if(fqdn =="host-only")or(fqdn =="fqdn-implicit"and ip_obj.is_private): 88return hn.split('.')[0] 89if fqdn =='fqdn'or fqdn =='fqdn-implicit': 90return hn 91return hn 92 93 94 95defreadlog(path =None, mode ='r'): 96""" 97 Read a logfile from disk and return string 98 """ 99 100if(path ==None): 101 logger.error("No path provided") 102return1 103else: 104if(os.path.isfile(path)is False): 105 logger.error("Log at{0}was requested but does not exist".format(path)) 106return'' 107else: 108try: 109returnopen(path, mode).read() 110exceptIOErrororOSErroras e: 111 logger.warning("Error reading log at{0}:{1}".format(path, e.strerror)) 112return1 113 114class LogPeriod: 115 116def__init__(self, section): 117if 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 121else: 122 self.startdate = datetime.now() -timeparse(config.prefs.get("logparse","period")) 123 self.unique =False