#!/usr/bin/python
# asrexacheck - 4.0
# for more information or feedback please refer to My Oracle Support doc ID 1450112.1

import commands
import datetime
import getopt
import os
import pprint
import re
import shutil
import socket
import sys
import time
import zipfile


class Asr:
  """ ASR checks """

  def __init__(self, logger, env, aSet):
    self.l = logger
    self.e = env
    self.aSet = aSet
    self.irule_regex = re.compile(r'.*rules/(\d+)$')
    self.ilomip = self.e["ilomip"]
    self.ilomhn = self.e["ilomhn"]
    self.hostip = self.e["hostip"]
    self.hosthn = self.e["hosthn"]
    self.asrIF = self.e["asrif"]
    self.u = Utils(self.l)
    self.snmpSubscribers = self._get_snmp_subscribers()
    self.alertmgmtRules = self._get_alert_mgt_rules()
    self.osRules = self._get_os_asr_hosts()
    self.ilomRules = self._get_ilom_asr_hosts()
    self.fromIps = self._get_from_ips()
    self.ipRouteGetIf = None
    self.asrM = None
    self.asrOutput = []

  def _get_snmp_subscribers(self):
    if self.asrIF == "cellcli":
      cmd = "cellcli -e list cell attributes snmpsubscriber"
    elif self.asrIF == "dbmcli":
      cmd = "dbmcli -e list dbserver attributes snmpsubscriber"
    else:
      cmd = "%s -get_snmp_subscribers -type asr" % self.asrIF
    return self.u.run_shell_cmd(cmd) or []

  def _get_alert_mgt_rules(self):
    cmd = "%s sunoem cli 'show -l all /SP/alertmgmt/rules'" % self.e["ipmitool"]
    out =  self.u.run_shell_cmd(cmd) or []
    r = None
    rules = {}
    for o in out:
      m = re.search(r'\s*(/SP/alertmgmt/rules/\d+).*', o)
      if m:
        r = m.groups()[0]
        rules.setdefault(r, {})
      if "=" in o and r:
        fields = o.split("=")
        rules[r][fields[0].strip()] = fields[1].strip()
    return rules

  def _ip_route_get_nw_interface(self, asrm):
    nif = None
    cmd = "ip route get %s" % asrm
    out = self.u.run_shell_cmd(cmd) or []
    for o in out:
      if asrm in o:
        nif = re.sub(r'.*dev\s+(\S+)\s+src\s.*', r'\1', o)
    return nif

  def _os_validation(self):
    if self.asrIF == "cellcli":
      cmd = "cellcli -e alter cell validate snmp type=asr"
    elif self.asrIF == "dbmcli":
      cmd = "dbmcli -e alter dbserver validate snmp type=asr"
    else:
      cmd = "%s -validate_snmp_subscriber -type asr" % self.asrIF
    return self.u.run_shell_cmd(cmd) or []

  def _ilom_validation(self, rule):
    cmd = "%s sunoem cli 'set %s testrule=true'" % (self.e["ipmitool"], rule)
    return self.u.run_shell_cmd(cmd) or []

  def _get_rule_template(self, host, ip, interface):
    """ provides generic rule syntax to standardize handling of snmpsubscriber and ilom alertmgmt rules """
    return {
      "interface": interface,
      "community": "",
      "fromip"   : "",
      "host"     : host,
      "ip"       : ip,
      "level"    : "",
      "port"     : "",
      "ilomrule" : "",
      "testrule" : "",
      "type"     : "",
      "version"  : "",
    }

  def _get_os_asr_hosts(self):
    hosts = []
    for o in self.snmpSubscribers[0].split("("):
      if not o.startswith("host"):
        continue
      fields = o.split(",")
      host = fields[0].replace("host=", "")
      (ip, host) = self.u.ip_host_translation(host)
      if not ip:
        ip = "Unresolveable"
      if not host:
        host = "Unresolveable"
      dt = self._get_rule_template(host, ip, "os")
      for f in fields:
        if "=" in f:
          (k, v) = f.split("=")
          # already key in new dict
          if k == "host":
            continue
          dt[k] = v.strip(")")
      hosts.append(dt)
    return hosts

  def _get_ilom_asr_hosts(self):
    hosts = []
    for rule,d in self.alertmgmtRules.items():
      if not "destination" in d or d["destination"] == "0.0.0.0":
        continue
      host = d["destination"]
      (ip, host) = self.u.ip_host_translation(host)
      if not ip:
        ip = "Unresolveable"
      if not host:
        host = "Unresolveable"
      dt = self._get_rule_template(host, ip, "ilom")
      dt["ilomrule"] = rule
      for k,v in d.items():
        # already key of new return dict
        if k == "destination":
          continue
        # renaming to match dict of os rules
        if k == "destination_port":
          k = "port"
        elif k == "snmp_version":
          k = "version"
        elif k == "community_or_username":
          k = "community"
        elif k == "destination_port":
          k = "port"
        dt[k] = v
      hosts.append(dt)
    return hosts

  def _match_asr_managers(self):
    ASRManagers = []
    non2cRules = []
    for orule in self.osRules:
      # 1. OS rule type = asr ?
      if not "asr" in orule["type"].lower():
        continue
      for irule in self.ilomRules:
        # 2. os ip == ilom ip ?
        if orule["ip"] != irule["ip"]:
          continue
        # 3. ilom rule == minor ?
        if irule["level"].lower() != "minor":
          continue
        # 4. ilom version == 2c ?
        if irule["version"].lower() != "2c" and not irule["ilomrule"] in non2cRules:
          self.asrOutput.append(self.l.build_out("ILOM property snmp_version is set to use SNMP protocol version %s (not 2c) for IP Address %s in rule %s" % (irule["version"], irule["ip"], self._shorten_ilom_rule(irule["ilomrule"])), "info"))
          non2cRules.append(irule["ilomrule"])
          continue
        # 5. then, you are an ASRM
        ASRManagers.append({
          "ip"          : irule["ip"],
          "ilomrule"    : irule["ilomrule"],
          "osport"      : orule["port"],
          "ilomport"    : irule["port"],
        })
    return ASRManagers

  def _validation_success_msg(self, typeValidation, toIp, toPort=None):
    if toPort:
      port = ":" + toPort
    else:
      port = ""
    self.asrOutput.append(self.l.build_out("%s Test event sent to %s%s" % (typeValidation, toIp, port), "ok"))

  def _get_from_ips(self):
    ips = {}
    for rule in self.osRules:
      if rule["fromip"].strip():
        ips[rule["ip"]] = rule["fromip"]
    return ips

  def _get_notification_method(self):
    if self.asrIF not in ("cellcli", "dbmcli"):
      return False
    cmd = "cellcli -e list cell attributes notificationMethod"
    if self.asrIF == "dbmcli":
      cmd = "dbmcli -e list dbserver attributes notificationMethod"
    return self.u.run_shell_cmd(cmd) or []

  def _shorten_ilom_rule(self, rule):
    return self.irule_regex.sub(r'\1', rule)

  def asr_checks(self):
    asrIfShort = self.asrIF.upper()
    if "MON" in asrIfShort:
      asrIfShort = "MON.PL"
    if not self.osRules or not self.ilomRules:
      self.asrOutput.append(self.l.build_out("No ASR Manager was found configured on both server and ILOM, exiting", "info"))
      return
    self.asrOutput.append("%-15s %-15s %-4s %-4s %7s %-4s %-7s %-9s %-7s" % \
      ("Destination", "Hostname", "Rule", "Type", asrIfShort, "Port", "Level", "Community", "Version"))
    self.asrOutput.append(self.l.get_divider_line())
    rows = []
    for rule in self.osRules + self.ilomRules:
      asrIfShort = ""
      if rule["interface"] == "os":
        asrIfShort = "YES"
      ruleType = ""
      if "asr" in rule["type"]:
        ruleType = "ASR"
      hostName = str(rule["host"])
      if len(hostName) > 15:
        hostName = hostName[0:15]
      rows.append("%-15s %-15s  %2s  %4s %7s %4s %-7s %8s  %6s " % \
        (rule["ip"], hostName, self._shorten_ilom_rule(rule["ilomrule"]), ruleType, asrIfShort, rule["port"], rule["level"], rule["community"], rule["version"])
      )
    for r in sorted(rows):
      self.asrOutput.append(r)
    self.asrOutput.append("")

    ASRManagers = self._match_asr_managers()

    if len(ASRManagers) == 0:
      self.asrOutput.append(self.l.build_out("None of the OS and ILOM IP addresses coincide, exiting", "info"))
      return
    elif len(ASRManagers) > 1:
      self.asrOutput.append(self.l.build_out("There is more than one instance of 'type=asr' (%s), exiting" % ", ".join(list(set([am["ip"] for am in ASRManagers]))), "warn"))
      return
    else:
      self.asrM = ASRManagers[0]
      self.asrOutput.append(self.l.build_out("Exactly one OS and ILOM IP coincide (%s)" % self.asrM["ip"], "ok"))

    if not self.u.ping(self.asrM["ip"]):
      self.asrOutput.append(self.l.build_out("ASRM IP %s is not pingable" % self.asrM["ip"], "info"))

    # cannot do earlier because we need a single ASRM IP as input to this command
    self.ipRouteGetIf = self._ip_route_get_nw_interface(self.asrM["ip"])

    notificationMethod = self._get_notification_method()
    if notificationMethod and not self.u.filter_output_regex(notificationMethod, "snmp|Snmp|SNMP"):
      self.asrOutput.append(self.l.build_out("Notification method does not contain snmp", "info"))

    self.asrOutput.append("* Validation: ")
    # 1. OS validation
    osValOut = self._os_validation()
    if not osValOut or not self.u.filter_output_regex(osValOut, "(Sending test trap.*to destination|successfully altered)"):
      self.asrOutput.append(self.l.build_out("Trap not sent, output: %s" % "".join(osValOut), "err"))
      self.asrOutput.append("Exiting")
      return
    self._validation_success_msg("OS", self.asrM["ip"], toPort=self.asrM["osport"])

    # 2. ILOM validation
    ilomValOut = self._ilom_validation(self.asrM["ilomrule"])
    if not ilomValOut or self.u.filter_output(ilomValOut, "To send a test alert"):
      self.asrOutput.append(self.l.build_out("Unable to send Test Event due to ILOM Configuration issue", "err"))
      if ilomValOut:
        for ivo in ilomValOut:
          if "set" in ivo:
            self.asrOutput.append(ivo)
      self.asrOutput.append("Exiting")
      return
    self._validation_success_msg("ILOM", self.asrM["ip"], toPort=self.asrM["ilomport"])

  def print_asr_checks(self):
    self.l.print_header(self.__class__.__name__)
    for line in self.asrOutput:
      self.l.print_out(line)


class AsrSetting:

  def __init__(self, logger):
    # Not yet fully implemented
    self.l = logger
    self.f = self.l.outputs["setting"] # created in logging -> setup
    self._create_header()

  def _create_header(self):
    header = "Hostname,Serial,Host_IP,ILOM_IP,Host_destination_IP,ILOM_destination_IP"
    if os.stat(self.f).st_size == 0:
      self.l.write_to_file(self.f, header)

  def write_record(self, env, destinations):
    record = [
      env["hosthn"], env["nserial"],
      env["hostip"], env["ilomip"],
      destinations["host"], destinations["ilom"],
    ]
    record = [str(r) for r in record]
    self.l.write_to_file(self.f, ",".join(record))

  def grep_records(self, grep="ALL"):
    return self.l.read_from_file(self.f, grep)


class Cli:
  """ Handle command line parameters """

  def __init__(self, version, baseDir):
    self.version = version
    self.defaultBasedir = baseDir
    try:
      self.opts, self.args = getopt.getopt(sys.argv[1:],"L:h",["Logdir=", "help", ])
    except getopt.GetoptError, err:
      print err
      self.usage()

  def get_args(self):
    logDir = self.defaultBasedir
    for opt, arg in self.opts:
      if opt in ("-L", "--Log base directory"):
        if os.path.isfile(arg):
          print "[WARN] Base directory is an existing file, please specify another value"
          sys.exit(1)
        logDir = arg
        if logDir == "/":
          print "[WARN] Cannot specify / (root) as destination, please choose another directory"
          sys.exit(1)
      elif opt in ("-h", "--help"):
        self.usage()
    return logDir

  def usage(self):
    print """
    %(scriptname)s, version: %(version)s

    Automated Service Request (ASR) Engineered System validation script.

    DESCRIPTION
    ASREXACHECK validates the ASR configuration of the ILOM and the host OS of the server in which it is executed.
    It will report any concerns found and can send a minimal set of test events resulting in emails used to confirm that the installation has been verified.
    Engineered Systems should run this as a sub component of EXACHK which will run this on all the nodes in their cluster.
    For more information or feedback please refer to My Oracle Support doc ID 1450112.1

    USAGE
    %(scriptname)s [OPTIONS]

      -L, --Logdir           specify a base log directory (default %(defaultBasedir)s)
      -h, --help             print help message
    """ % {
      "scriptname"       : sys.argv[0].replace('./', ''),
      "version"          : self.version,
      "defaultBasedir"    : self.defaultBasedir,
    }
    sys.exit()


class Env:
  """ ES configuration and setting of environment variables """

  def __init__(self, logging):
    self.l = logging
    self.u = Utils(self.l)
    self.exaTypes = ["Exadata", "Exalogic", "Exalytics", "BDA", "SSC", ]
    self.uname = os.uname()
    self.arch = self.uname[0]
    self.hostname = self.uname[1]
    self.osVersion = self.uname[2]
    self.ipmitool = self._locate_ipmitool()
    self._check_requirements()
    self._get_system_info()
    self._get_other_config_info()

  def _locate_ipmitool(self):
    if "sunos" in " ".join(self.uname).lower():
      path = self._get_first_matching_path(["/opt/ipmitool/bin/ipmitool", "/usr/sbin/ipmitool", "/usr/sfw/bin/ipmitool", ])
      if path:
        return path
    else:
      # linux should be standard in path
      path = "ipmitool"
      if self.u.which(path): # checks in $PATH
        return path
    self.l.print_out("required ipmitool command not found.", "err")
    sys.exit(1)

  def _get_system_info(self):
    self.dmidecodeT1 = self._get_dmidecode_t1()
    self.dmidecodeT129 = self._get_dmidecode_t129()
    self.esProductName = False
    # dmidecode
    if self.dmidecodeT1 and len(self.dmidecodeT129) > 5:
      self.productName = self._get_product_name_dmidecode()
      self.nodeSerial = self._get_node_serial_dmidecode()
      self.rackSerial = self._get_rack_serial_dmidecode()
      self.esProductName = self._get_es_flavor_string_dmidecode()
      self.sysIdent = False
    # if we know what we are return, otherwise verify ILOM data
    if self._get_es_flavor(self.esProductName):
      return
    else:
      # not done yet, get show /System
      self.ilomSystemOutput = self._get_show_system()
      if self.ilomSystemOutput:
        self.productName = self.ilomSystemOutput.get("model", False)
        self.nodeSerial = self.ilomSystemOutput.get("component_serial_number", False)
        self.rackSerial = self.ilomSystemOutput.get("serial_number", False)
        if not self.nodeSerial:
          self.nodeSerial = self.ilomSystemOutput.get("serial_number", False)
          self.rackSerial = False # if not component_serial_number used we have old format, get rack serial from system_identifier
        self.esProductName = self.ilomSystemOutput.get("part_number", False)
        # sometimes don't get ES string in this field (e.g. 4707542-1) so then use the system_identifier
        if not self._get_es_flavor(self.esProductName) or not self.rackSerial:
          self.sysIdent = self.ilomSystemOutput.get("system_identifier", False)
          self._set_rackserial_esprodname_from_sysidentifier()
      else:
        # should not get here as /System should have this info
        self.ilomSysOutput = self._get_show_sys()
        self.ilomSpOutput = self._get_show_sp()
        if self.ilomSysOutput and self.ilomSpOutput:
          self.productName = self.ilomSysOutput.get("product_name", False)
          self.nodeSerial = self.ilomSysOutput.get("product_serial_number", False)
          self.sysIdent = self.ilomSpOutput.get("system_identifier", False)
          self._set_rackserial_esprodname_from_sysidentifier()
        else:
          self.l.print_out("Cannot get all system information, tried dmidecode -t1 and -t129, ILOM /System /SYS and /SP properties (including legacy system_identifier)", "err")
          sys.exit()

  def _set_rackserial_esprodname_from_sysidentifier(self):
    try:
      self.rackSerial = self.sysIdent.split()[-1]
      self.esProductName = " ".join(self.sysIdent.split()[:-1])
    except:
      self.rackSerial = False
      self.esProductName = False

  def _get_other_config_info(self):
    self.esFlavor = self._get_es_flavor(self.esProductName)
    if not self.esFlavor:
      self.l.print_out("Identified ES type '%s' not in known ES types: %s" % (str(self.esProductName), ", ".join(self.exaTypes)), "err")
      sys.exit(1)
    self.serverType = self._get_server_type()
    self.imageVersion = self._get_image_version()
    self.hostIp = self.get_host_ip()
    self.ilomIp = self.get_ilom_ip()
    self.ilomVersion = self.get_ilom_version()
    self.hostHn = self.u.convert_ip_to_host(self.hostIp)
    self.ilomHn = self.u.convert_ip_to_host(self.ilomIp)
    self.esPaths = self._get_es_paths()
    self.asrIF = self._get_asr_interface()
    self.ibhosts = self._get_ib_hosts()
    self.networkIfs = self._get_network_interfaces()
    self.snmp = self._get_snmp()
    self.dns = self._get_dns()
    self.uname = self._get_uname()

  def _get_uname(self):
    return self.u.run_shell_cmd("uname -a") or []

  def _get_ib_hosts(self):
    ibhostsAll = self.u.run_shell_cmd("ibhosts")
    return self.u.run_shell_cmd("ibhosts | grep HCA | cut -f2 -d'\"' | awk '{print $2, $1}' | sort")

  def _get_network_interfaces(self):
    if self.serverType == "COMPUTE":
      nifs = { "eth0" : {}, "bondeth0" : {}, }
    else:
      nifs = { "eth0" : {}, }
    for i in nifs.keys():
      ifOut = self.u.run_shell_cmd("ifconfig " + i)
      if not ifOut:
        nifs[i]["ip"] = "no_interface"
        nifs[i]["hn"] = "no_interface"
      else:
        nifs[i]["ip"] = self._parse_ip(ifOut)
        if not nifs[i]["ip"]:
          nifs[i]["ip"] = "no_ip"
          nifs[i]["hn"] = "no_ip"
        else:
          nifs[i]["hn"] = self.u.convert_ip_to_host(nifs[i]["ip"])
          if not nifs[i]["hn"]:
            nifs[i]["hn"] = "no_hostname"
    return nifs

  def _props_to_dict(self, out):
    d = {}
    for o in out:
      if "=" in o:
        (k, v) = o.split("=")
        d[k.strip()] = v.strip()
    return d

  def _get_snmp(self):
    out = self.u.run_shell_cmd(self.ipmitool + " sunoem cli 'show /SP/services/snmp'")
    return self._props_to_dict(out)

  def _get_dns(self):
    out = self.u.run_shell_cmd(self.ipmitool + " sunoem cli 'show /SP/clients/dns'")
    return self._props_to_dict(out)

  def _parse_ip(self, out):
    for o in out:
      if "inet addr:" in o:
        return re.sub(r'inet addr:(\S+)\s+Bcast.*', r'\1', o).strip()
      elif "inet6 addr" in o:
        return re.sub(r'inet6 addr:\s?(\S+)\s+.*', r'\1', o).strip()
    return False

  def _check_if_root(self):
    if os.geteuid() != 0:
      self.l.print_out("This script must be run as the root user.", "err")
      sys.exit(1)

  def _bmc_responds(self):
    bmcResponse = self.u.run_shell_cmd("%s bmc info 2>&1 >/dev/null; echo $?" % self.ipmitool)
    if bmcResponse and bmcResponse[0] != "0":
      self.l.print_out("Ipmitool bmc info command did not respond, a working BMC interface is required for this script.", "err")
      sys.exit(1)

  def _check_requirements(self):
    self._check_if_root()
    self._bmc_responds()

  # dmidecode
  def _get_dmidecode_t1(self):
    return self.u.run_shell_cmd("dmidecode -t1") or []

  def _get_dmidecode_t129(self):
    return self.u.run_shell_cmd("dmidecode -t129") or []

  def _sunoem_cli_cmd(self, param):
    return '%s sunoem cli "show %s"' % (self.ipmitool, param)

  def _get_show_system(self):
    return self.u.output_to_dict(self.u.run_shell_cmd(self._sunoem_cli_cmd("/SYSTEM")))

  def _get_show_sys(self):
    return self.u.output_to_dict(self.u.run_shell_cmd(self._sunoem_cli_cmd("/SYS")))

  def _get_show_sp(self):
    return self.u.output_to_dict(self.u.run_shell_cmd(self._sunoem_cli_cmd("/SP")))

  def _query_dmidecode_t1(self, strVal):
    val = [line for line in self.dmidecodeT1 if strVal in line]
    if val:
      return val[0].strip().replace(strVal, "")

  def _get_product_name_dmidecode(self):
    return self._query_dmidecode_t1("Product Name: ")

  def _get_dmidecode_t129_strings_pos(self):
    try:
      return [i for i,x in enumerate(self.dmidecodeT129) if "Strings:" in x][0]
    except IndexError:
      return False

  def _query_dmidecode_t129(self, offset):
    startPos = self._get_dmidecode_t129_strings_pos()
    if startPos:
      offsetPos = startPos + offset
      if len(self.dmidecodeT129) > offsetPos:
        return self.dmidecodeT129[offsetPos].strip()

  def _get_node_serial_dmidecode(self):
    return self._query_dmidecode_t1("Serial Number: ")

  def _get_rack_serial_dmidecode(self):
    return self._query_dmidecode_t129(4)

  def _get_es_flavor_string_dmidecode(self):
    return self._query_dmidecode_t129(2)

  def _get_es_flavor(self, esProductName):
    if esProductName:
      esProductName = esProductName.lower()
      if "exalogic" in esProductName:
        return "Exalogic"
      elif "exalytics" in esProductName:
        return "Exalytics"
      elif "big data" in esProductName:
        return "BDA"
      elif "supercluster" in esProductName:
        return "SSC"
      elif "exa" in esProductName or "appliance" in esProductName or "zdlra" in esProductName or "oracle database machine" in esProductName:
        return "Exadata"
    return False

  def _get_server_type(self):
    imageInfoNode = self.u.run_shell_cmd("imageinfo -node") or []
    if self.u.filter_output(imageInfoNode, "STORAGE"):
      return "STORAGE"
    elif self.u.filter_output(imageInfoNode, "COMPUTE"):
      return "COMPUTE"
    else:
      return "not_found"

  def _get_image_version(self):
    imageVersion = self.u.run_shell_cmd("imageinfo") or []
    matches = self.u.filter_output_regex(imageVersion, "(Active )?[Ii]mage version.*:.*\d+\.\d+\.\d+\.\d+\.\d+")
    try:
      return matches[0].split(":")[1].strip()
    except:
      return "not_found"

  def get_host_ip(self):
    return socket.gethostbyname(socket.gethostname())

  def get_ilom_ip(self):
    out = self.u.run_shell_cmd(self.ipmitool + " sunoem cli 'show /SP/network ipaddress'")
    try:
      return [o for o in out if "ipaddress = " in o][0].replace("ipaddress = ", "").strip()
    except:
      return False

  def get_ilom_version(self):
    out = self.u.run_shell_cmd(self.ipmitool + " sunoem cli 'version'")
    try:
      return [o for o in out if re.search(r'^SP firmware \d+\.', o.strip())][0].strip().replace("SP firmware ", "")
    except:
      return False

  def _get_first_matching_path(self, paths):
    for p in paths:
      if os.path.isfile(p):
        return p
    return False

  def _get_es_paths(self):
    paths = {
      "Exadata" : {
        "ToolsDir" : "/opt/oracle.SupportTools/",
        "MonHwAsr"  : "/opt/oracle.cellos/compmon/exadata_mon_hw_asr.pl",
      },
      "Exalogic" : {
        "ToolsDir" : "/opt/exalogic.tools/tools/",
        "MonHwAsr"  : False,
      },
      "Exalytics" : {
        "ToolsDir" : "/opt/exalytics/asr",
        "MonHwAsr"  : "/opt/exalytics/asr/bda_mon_hw_asr.pl",
      },
      "BDA" : {
        "ToolsDir" : "/opt/oracle/bda/compmon",
        "MonHwAsr"  : "/opt/oracle/bda/compmon/bda_mon_hw_asr.pl",
        "InstallInfo"  : self._get_first_matching_path(["/opt/oracle/bda/install/state/config.json", "/opt/oracle/bda/install/state/mammoth-saved.params"]),
      },
      "SSC" : {
        "ToolsDir" : "/opt/oracle.SupportTools/",
        "MonHwAsr"  : False,
      },
    }
    if self.esFlavor in paths:
      return paths[self.esFlavor]

  def _get_asr_interface(self):
    if self.u.which("cellcli"): return "cellcli"
    if self.u.which("dbmcli"):  return "dbmcli"
    if self.esPaths["MonHwAsr"] and os.path.isfile(self.esPaths["MonHwAsr"]):
      return self.esPaths["MonHwAsr"]
    else:
      self.l.print_out("No ASR interface found, tried cellcli, dbmcli, and could not locate the mon_hw_asr script", "err")
      sys.exit()

  def print_environment(self):
    self.l.print_header("System configuration")
    self.l.print_out("Product name           : %s" % self.esProductName)
    self.l.print_out("Product serial         : %s" % self.rackSerial)
    self.l.print_out("Component name         : %s" % self.productName)
    self.l.print_out("Component serial       : %s" % self.nodeSerial)
    if self.sysIdent:
      self.l.print_out("System identifier      : %s" % self.sysIdent)
    self.l.print_out("Engineered System type : %s" % self.esFlavor)
    self.l.print_out("Server type            : %s" % self.serverType)
    self.l.print_out("Image version          : %s" % self.imageVersion)
    self.l.print_out("OS IP Address          : %s" % self.hostIp)
    self.l.print_out("OS Hostname            : %s" % self.hostHn)
    self.l.print_out("OS version             : %s" % self.osVersion)
    self.l.print_out("ILOM IP Address        : %s" % self.ilomIp)
    self.l.print_out("ILOM Hostname          : %s" % self.ilomHn)
    self.l.print_out("ILOM version           : %s" % self.ilomVersion)

  def print_network(self, fromIps, ipRouteGetIf):
    self.l.print_header("Network")
    self.l.print_out("%-9s  %-31s %-15s %-5s %-15s" % ("Interface", "IP Address", "Hostname", "Route", "  fromIP"))
    self.l.print_divider_line()
    for nif, d in sorted(self.networkIfs.items()):
      hostName = str(d["hn"])
      if len(hostName) > 15:
        hostName = hostName[0:15]
      route = "NO"
      if ipRouteGetIf and nif == ipRouteGetIf:
        route = "YES"
      fromIp = ""
      if fromIps:
        for ip, fip in fromIps.items():
          if d["ip"] == ip:
            fromIp = fip
      self.l.print_out("%-9s  %-31s %-15s %-5s %-15s" % (nif, d["ip"], hostName, route, fromIp))
    if "servicestate" in self.snmp and self.snmp["servicestate"] == "disabled":
      self.l.print_out("ILOM SNMP servicestate is disabled",  "warn")

  def get_config(self):
    return {
      "lprod"    : self.esProductName,
      "sprod"    : self.productName,
      "nserial"  : self.nodeSerial,
      "rserial"  : self.rackSerial,
      "estype"   : self.esFlavor,
      "stype"    : self.serverType,
      "image"    : self.imageVersion,
      "ostype"   : self.arch,
      "hostname" : self.hostname,
      "hostip"   : self.hostIp,
      "ilomip"   : self.ilomIp,
      "hosthn"   : self.hostHn,
      "ilomhn"   : self.ilomHn,
      "ipmitool" : self.ipmitool,
      "paths"    : self.esPaths,
      "asrif"    : self.asrIF,
    }


class Logging:
  """ Class for printing and writing outputs """

  def __init__(self, outputs, verbose):
    self.verbose = verbose
    self.outputs = outputs
    self.outputWidth = 80
    self.states = {
      "ok" : "[OK]",
      "err" : "[FAIL]",
      "warn" : "[WARN]",
      "info" : "[INFO]",
    }
    self.alphaPattern = re.compile(r'[^-_a-zA-Z0-9]')
    self.multiUnderscore = re.compile(r'_+')

  def setUp(self, initInfo):
    if os.path.isdir(self.outputs["hostdir"]):
      if self.verbose:
        print "Hostdir %s present, removing it" % self.outputs["hostdir"]
      shutil.rmtree(self.outputs["hostdir"])
    if self.verbose:
      print "Creating command outputs sub directory %s" % self.outputs["cmds"]
    os.makedirs(self.outputs["cmds"], 0755)
    for fname in (self.outputs["setting"], self.outputs["version"], self.outputs["summary"]):
      # if setting file, don't delete it
      if "asr_setting" in fname.lower() and os.path.isfile(fname):
        continue
      if self.verbose:
        print "Create file %s" % fname
      f = open(fname, 'w')
      # if version file, write config details
      if "version" in fname.lower():
        for k,v in initInfo.items():
          f.write("%s: %s\n" % (k,v))
      f.close()

  def print_header(self, title):
    self.print_out("\n" + "="*self.outputWidth)
    self.print_out(title.upper())
    self.print_out("="*self.outputWidth)

  def print_divider_line(self):
    self.print_out("-"*self.outputWidth)

  def get_divider_line(self):
    return "-"*self.outputWidth

  def print_out(self, line, state=None):
    if state and state in self.states:
      line = self.states[state] + " " + line
    print line
    self.capture_out(line)

  def build_out(self, line, state=None):
    if state and state in self.states:
      line = self.states[state] + " " + line
    return line

  def write_to_file(self, fname, content):
    try:
      f = open(fname, 'a') # setting file needs appending
      if type(content) == list:
        content = "\n".join(content)
      f.write(content + "\n")
      f.close()
    except:
      self.print_out("Cannot write to file %s" % fname, "err")
      sys.exit()

  def read_from_file(self, fname, grep="All"):
    try:
      f = open(fname, 'r')
      lines = f.readlines()
      f.close()
      return [li.strip() for li in lines if grep == "All" or grep in li]
    except:
      self.print_out("Cannot read from file %s" % fname, "err")
      sys.exit()

  def capture_out(self, content):
    fname = self.outputs["summary"]
    self.write_to_file(fname, content)

  def _clean_fname(self, fname):
    fname = fname.replace("2>&1", "").strip()
    fname = self.alphaPattern.sub(r'_', fname)
    fname = self.multiUnderscore.sub(r'_', fname)
    fname += ".out"
    return fname

  def capture_out_cmd(self, cmd, content, numRetries, returnCode, timeElapsed):
    fname = os.path.join(self.outputs["cmds"], self._clean_fname(cmd))
    self.write_to_file(fname, "* cmd: \n%s" % cmd)
    self.write_to_file(fname, "\n===\n* output:")
    self.write_to_file(fname, content)
    self.write_to_file(fname, "\n\n* stats:")
    self.write_to_file(fname, "- exit code: %s" % returnCode)
    self.write_to_file(fname, "- attempts: %d" % numRetries)
    self.write_to_file(fname, "- duration: %s seconds" % timeElapsed)


class Utils:
  """ Helper methods to support the main classes """

  def __init__(self, logger):
    self.l = logger
    self.success_exit_code = 0
    self.max_num_retries = 3
    self.sleep_between_retries = 5

  def _strip_domain_name(self, host):
    """ hide domain name from output """
    try:
      return host.split(".")[0]
    except:
      return False

  def run_shell_cmd(self, cmd):
    startTime = int(time.time())
    returnCode, output = commands.getstatusoutput(cmd)
    numAttempts = 1
    # retry only ipmitool cmds that can give Failed to connect: Busy
    if "ipmitool" in cmd:
      while True:
        if returnCode == self.success_exit_code:
          break
        if numAttempts == self.max_num_retries:
          break
        time.sleep(self.sleep_between_retries)
        returnCode, output = commands.getstatusoutput(cmd)
        numAttempts += 1
    timeElapsed = str(int(time.time()) - startTime)
    self.l.capture_out_cmd(cmd, output, numAttempts, returnCode, timeElapsed)
    return output.split("\n")

  def ping(self, ip):
    out = self.run_shell_cmd("ping -c 1 " + ip) or []
    successValues = [ "%s is alive" % ip, "1 packets transmitted, 1 received", ", 0% packet loss", ]
    for o in out:
      for sv in successValues:
        if sv in o:
          return True
    return False

  def ip_host_translation(self, iphost):
    ip = host = False
    if self.valid_ip(iphost):
      ip = iphost
      host = self.convert_ip_to_host(ip)
    else:
      host = iphost
      ip = self.convert_host_to_ip(host)
    host = self._strip_domain_name(host)
    return (ip, host)

  def match_ips(self, osIps, spIps):
    matches = []
    for oi in osIps:
      for si in spIps:
        if oi == si:
          matches.append(oi)
    return list(set(matches))

  def filter_output(self, output, strVal):
    return [line for line in output if strVal in line]

  def filter_output_regex(self, output, regexVal):
    return [line for line in output if re.search(r'%s' % regexVal, line)]

  def output_to_dict(self, output):
    d = {}
    for op in output:
      if "=" in op:
        k = op.split("=", 1)[0].strip()
        v = op.split("=", 1)[1].strip()
        d[k] = v
    return d

  def valid_ip(self, ip):
    try:
      socket.inet_aton(ip)
      return True
    except socket.error:
      return False

  def convert_host_to_ip(self, host):
    try:
      return socket.gethostbyname(host)
    except:
      return False

  def convert_ip_to_host(self, ip):
    try:
      host = socket.gethostbyaddr(ip)[0]
      return self._strip_domain_name(host)
    except:
      return False

  def which(self, fname):
    for path in os.environ["PATH"].split(":"):
      if os.path.exists(path + "/" + fname):
        return path + "/" + fname

  def zip_output(self, zfName, logdir):
    zf = zipfile.ZipFile(zfName, "w", zipfile.ZIP_DEFLATED)
    abs_src = os.path.abspath(logdir)
    for dirname, subdirs, files in os.walk(logdir):
      for filename in files:
        absname = os.path.abspath(os.path.join(dirname, filename))
        arcname = absname[len(abs_src) + 1:]
        zf.write(absname, arcname)
    filesInZip = zf.namelist()
    zf.close()
    # check if zipfile was generated correctly
    if len(filesInZip) > 3 and "VERSION" in filesInZip:
      return True
    return False

  def get_readable_time_from_utstamp(self, t):
    try:
      return ( datetime.datetime.fromtimestamp(int(t)).strftime('%Y-%m-%d %H:%M:%S'))
    except:
      return "Could not print readable timestamp"


if __name__ == "__main__":
  
  script = "asrexacheck"
  version = "4.0"
  baseDir = "/var/log/asrexacheck"
  
  if os.path.isfile(baseDir):
    print "Destination dir %s is a file, rename or remove it" % baseDir
    sys.exit(1)
  if not os.path.isdir(baseDir):
    print "Base dir %s not present, creating it" % baseDir
    try:
      os.makedirs(baseDir)
    except:
      print "Cannot create base dir %s" % baseDir
      sys.exit(1)
  
  cli = Cli(version, baseDir)
  baseDir = cli.get_args()
  verbose = False
  
  today = time.strftime("%Y-%m-%d", time.gmtime())
  tstamp = time.strftime("%Y%m%dT%H%M%S", time.gmtime())
  logDir = os.path.join(baseDir, today)
  startTime = int(time.time())
  
  hostName = os.uname()[1].split(".")[0]
  outputs = {
    # dirs
    "basedir"  : baseDir,
    "timedir"  : logDir,
    "hostdir"  : os.path.join(logDir, hostName),
    "summary"  : os.path.join(logDir, hostName, "summary"),
    "cmds"     : os.path.join(logDir, hostName, "cmd_outs"),
    # files
    "version"  : os.path.join(logDir, "VERSION"),
    "setting"  : os.path.join(logDir, "ASR_setting"),
  }
  l = Logging(outputs, verbose=verbose)
  l.setUp({"version" : version, "verbose" : verbose, })
  u = Utils(l)
  l.print_out("%s version: %s" % (script, version))
  l.print_out("Current time: %s" % u.get_readable_time_from_utstamp(startTime))
  
  e = Env(l)
  env = e.get_config()
  e.print_environment()
  
  aSet = AsrSetting(l)
  a = Asr(l, env, aSet)
  
  # need asr checks first to get the fromIps and route interface
  a.asr_checks()
  e.print_network(a.fromIps, a.ipRouteGetIf)
  a.print_asr_checks()
  
  endTime = int(time.time())
  timeElapsed = str(endTime - startTime)
  l.print_out("\nTime elapsed script execution: %s" % time.strftime("%H:%M:%S", time.gmtime(float(timeElapsed))) )
  zfName = os.path.join(baseDir, "%s_%s_%s.zip" % (script, hostName, tstamp))
  zfCreated = u.zip_output(zfName, logDir)
  if zfCreated:
    l.print_out("Zipfile %s of the outputs created" % zfName)
    if os.path.isdir(logDir):
      shutil.rmtree(logDir)
  else:
    l.print_out("Zipfile %s not created, please get outputs from %s" % (zfName, logDir), "warn")

