def _init_probe(self):
     if self.options.host == 'localhost':
         self.options.host = socket.getfqdn()
     self.hostname = self.options.host
     (username, password) = open(self.options.config,
                                 'r').readline().rstrip('\n').split(':')
     self.cdh_api = get_root_resource(self.options.host, self.options.port,
                                      username, password,
                                      self.options.use_tls,
                                      self.CM_API_VERSION)
     return {'username': username, 'password': password}
    def run(self):
        (self.options, args) = self._parse_args()
        if self.options.host == 'localhost':
            hostname = socket.getfqdn()
        else:
            hostname = self.options.host

        # Step 1: init container
        try:
            zbx_container = self._init_container()
        except:
            return 1

        # Step 2: get data
        try:
            (self.options.username, self.options.password) = \
                open(self.options.passfile, 'r').readline().rstrip('\n').split(':')
            cdh_api = get_root_resource(
                self.options.host,
                self.options.port,
                self.options.username,
                self.options.password,
                self.options.use_tls,
                self.CM_API_VERSION
            )


            data = {}
            if self.options.mode == "update_items":
                zbx_container.set_type("items")
                data = self._get_metrics(cdh_api, hostname)
            elif self.options.mode == "discovery":
                zbx_container.set_type("lld")
                data = self._get_discovery(cdh_api, hostname)
        except:
            return 2

        # Step 3: format & load data into container
        try:
            zbx_container.add(data)
            zbx_container.add_item(hostname, "hadoop.cm.zbx_version", self.__version__)
        except:
            return 3

        # Step 4: send container data to Zabbix server
        try:
            zbx_container.send(zbx_container)
        except protobix.SenderException as zbx_e:
            if self.options.debug:
                print self.ZBX_CONN_ERR % zbx_e.err_text
            return 4
        # Everything went fine. Let's return 0 and exit
        return 0
Exemple #3
0
def main():
    (options, args) = parse_args()

    try:
        (options.username,
         options.password) = open(options.passfile,
                                  'r').readline().rstrip('\n').split(':')
    except:
        print >> sys.stderr, "Unable to read username and password from file '%s'. "
        "Make sure the file is readable and contains a single line of "
        "the form \"<username>:<password>\"" % options.passfile

    if options.host == 'localhost':
        hostname = platform.node()
    else:
        hostname = options.host

    try:
        cdh_api = get_root_resource(options.host, options.port,
                                    options.username, options.password,
                                    options.use_tls, CM_API_VERSION)
    except:
        return 1

    zbx_container = protobix.DataContainer()
    if options.mode == "update_items":
        zbx_container.set_type("items")
        data = get_metrics(cdh_api, hostname)
    elif options.mode == "discovery":
        zbx_container.set_type("lld")
        data = get_discovery(cdh_api, hostname)

    zbx_container.add(data)
    zbx_container.add_item(hostname, "hadoop.cm.zbx_version", __version__)

    zbx_container.set_host(options.zabbix_server)
    zbx_container.set_port(int(options.zabbix_port))
    zbx_container.set_debug(options.debug)
    zbx_container.set_verbosity(options.verbose)
    zbx_container.set_dryrun(options.dry)

    try:
        zbxret = zbx_container.send(zbx_container)
    except protobix.SenderException as zbx_e:
        if options.debug:
            print ZBX_CONN_ERR % zbx_e.err_text
        return 2
    else:
        return 0
def main():
    (options, args) = parse_args()

    try:
        (options.username, options.password) = open(options.passfile, "r").readline().rstrip("\n").split(":")
    except:
        print >> sys.stderr, "Unable to read username and password from file '%s'. "
        "Make sure the file is readable and contains a single line of "
        'the form "<username>:<password>"' % options.passfile

    if options.host == "localhost":
        hostname = socket.getfqdn()
    else:
        hostname = options.host

    try:
        cdh_api = get_root_resource(
            options.host, options.port, options.username, options.password, options.use_tls, CM_API_VERSION
        )
    except:
        return 1

    zbx_container = protobix.DataContainer()
    if options.mode == "update_items":
        zbx_container.set_type("items")
        data = get_metrics(cdh_api, hostname)
    elif options.mode == "discovery":
        zbx_container.set_type("lld")
        data = get_discovery(cdh_api, hostname)

    zbx_container.add(data)
    zbx_container.add_item(hostname, "hadoop.cm.zbx_version", __version__)

    zbx_container.set_host(options.zabbix_server)
    zbx_container.set_port(int(options.zabbix_port))
    zbx_container.set_debug(options.debug)
    zbx_container.set_verbosity(options.verbose)
    zbx_container.set_dryrun(options.dry)

    try:
        zbxret = zbx_container.send(zbx_container)
    except protobix.SenderException as zbx_e:
        if options.debug:
            print ZBX_CONN_ERR % zbx_e.err_text
        return 2
    else:
        return 0
Exemple #5
0
def main():
  (options, args) = parse_args()

  try:
    (username, password) = open(options.passfile, 'r').readline().rstrip('\n').split(':')
  except:
    print >> sys.stderr, "Unable to read username and password from file '%s'. "
    "Make sure the file is readable and contains a single line of "
    "the form \"<username>:<password>\"" % options.passfile

  root = get_root_resource(options.host, options.port, username,
                           password, options.use_tls, CM_API_VERSION)

  hosts_map = get_host_map(root)

  if options.mode == "update_status":

    ''' The way in which the status is reported depends on whether or not the
        -n/--use-send-nsca option was set.  By default, the status updates are
        simply appended to the external command file.  If nsca is used, the
        status updates are piped via stdin to send_nsca.
    '''
    # <host>\t<service>\t<return_code>\t<message>\n
    NSCA_STATUS_FORMAT = "{0}\t{1}\t{2}\t{3}\n"
    # [<timestamp>] PROCESS_SERVICE_CHECK_RESULT;<host>;<service>;<return_code>;<message>\n
    CMD_STATUS_FORMAT = "[{0}] PROCESS_SERVICE_CHECK_RESULT;{1};{2};{3};{4}\n"

    try:
      services_list = get_services(root, hosts_map, "full")
      timestamp = long(time()) # approximate time the services were queried

      tmp_status_file = tempfile.TemporaryFile()
      try:
        for service in services_list:
          health = service["health_summary"]
          if not health is None:
            code = CM_STATE_CODES[health]
            if options.use_send_nsca:
              status_line = NSCA_STATUS_FORMAT.format(
                service["hostname"], service["name"], code, service["status"])
            else:
              status_line = CMD_STATUS_FORMAT.format(
                timestamp, service["hostname"], service["name"], code, service["status"])

            tmp_status_file.write(status_line)

        tmp_status_file.flush()
        tmp_status_file.seek(0)

        if options.use_send_nsca:
          submit_status_send_nsca(options.send_nsca_path, options.nsca_host,
                                  options.nsca_port, options.send_nsca_config,
                                  tmp_status_file, options.verbose)
        else:
          submit_status_external_cmd(options.cmd_file, tmp_status_file)
      finally:
        tmp_status_file.close()
    except SystemExit as e:
      sys.exit(e)
    except Exception as e:
      print >> sys.stderr, "An unknown error occurred: %s" % str(e)
      raise

  elif options.mode == "generate_cfg":

    host_cfg_file = open(join(options.cfg_dir, CM_HOST_CFG_FILENAME), 'w+')
    host_cfg_file.write(PASSIVE_HOST_TEMPLATE)

    for host in hosts_map.values():
      host_cfg_file.write(CM_HOST_TEMPLATE %
                          (host["hostname"], host["hostname"], host["address"],))

    host_cfg_file.close()

    services_cfg_file = open(join(options.cfg_dir, CM_SERVICE_CFG_FILENAME), 'w+')
    services_cfg_file.write(PASSIVE_SERVICE_TEMPLATE)

    services_list = get_services(root, hosts_map)
    for service in services_list:
      services_cfg_file.write(CM_SERVICE_TEMPLATE %
                              (service["hostname"],
                              service["name"],
                              service["display_name"],
                              service["url"]))

    services_cfg_file.close()

  else:
    print >> sys.stderr, "Fatal error: Unrecognized mode"
Exemple #6
0
def main():
  (options, args) = parse_args()

  try:
    (username, password) = open(options.passfile, 'r').readline().rstrip('\n').split(':')
  except:
    print >> sys.stderr, "Unable to read username and password from file '%s'. "
    "Make sure the file is readable and contains a single line of "
    "the form \"<username>:<password>\"" % options.passfile

  root = get_root_resource(options.host, options.port, username,
                           password, options.use_tls, CM_API_VERSION)
  clusters = root.get_all_clusters()
  for cluster in clusters:
      host_refs = cluster.list_hosts()
      services  = cluster.get_all_services()

      print "# Cluster Summary #"
      print "Name: %s" % cluster.displayName
      print "Number of hosts: %d" % len(host_refs)
      print "Number of services: %d" % len(services)

      print "## Host Summary"
      for ref in host_refs:
          host = root.get_host(ref.hostId)
          print "Host: %(host)s, Health Summary: %(health)s" % {
             'host': host.hostname
           , 'health': host.healthSummary
          }

      print "## Host Health Checks"
      for ref in host_refs:
          host = root.get_host(ref.hostId)
          checks = host.healthChecks
          for check in checks:
              print "Host: %(host)s, Health Check: %(health)s, Status: %(status)s" % {
                'host': host.hostname
              , 'health': check['name']
              , 'status': check['summary']
              }

      print "## Service Summary"
      for service in services:
        print "Service: %(name)s, Health Summary: %(health)s" % {
           'name': service.displayName
         , 'health': service.healthSummary
        }
        for check in service.healthChecks:
            print "Service: %(name)s, Health Check: %(health)s, Status: %(status)s" % {
                'name': service.displayName
              , 'health': check['name']
              , 'status': check['summary']
            }
        for role in service.get_all_roles():
            print "Service: %(service_name)s, Role: %(role_name)s, Health Summary: %(health)s" % {
                'service_name': service.displayName
              , 'role_name': role.name
              , 'health': role.healthSummary
            }
            for check in role.healthChecks:
                print "Service: %(service_name)s, Role: %(role_name)s, Health Check: %(health)s, Status: %(status)s" % {
                    'service_name': service.displayName
                  , 'role_name': role.name
                  , 'health': check['name']
                  , 'status': check['summary']
                }

  sys.exit(0)
Exemple #7
0
def main():
    (options, args) = parse_args()

    try:
        (username, password) = open(options.passfile,
                                    'r').readline().rstrip('\n').split(':')
    except:
        print >> sys.stderr, "Unable to read username and password from file '%s'. "
        "Make sure the file is readable and contains a single line of "
        "the form \"<username>:<password>\"" % options.passfile

    root = get_root_resource(options.host, options.port, username, password,
                             options.use_tls, CM_API_VERSION)
    clusters = root.get_all_clusters()
    for cluster in clusters:
        host_refs = cluster.list_hosts()
        services = cluster.get_all_services()

        print "# Cluster Summary #"
        print "Name: %s" % cluster.displayName
        print "Number of hosts: %d" % len(host_refs)
        print "Number of services: %d" % len(services)

        print "## Host Summary"
        for ref in host_refs:
            host = root.get_host(ref.hostId)
            print "Host: %(host)s, Health Summary: %(health)s" % {
                'host': host.hostname,
                'health': host.healthSummary
            }

        print "## Host Health Checks"
        for ref in host_refs:
            host = root.get_host(ref.hostId)
            checks = host.healthChecks
            for check in checks:
                print "Host: %(host)s, Health Check: %(health)s, Status: %(status)s" % {
                    'host': host.hostname,
                    'health': check['name'],
                    'status': check['summary']
                }

        print "## Service Summary"
        for service in services:
            print "Service: %(name)s, Health Summary: %(health)s" % {
                'name': service.displayName,
                'health': service.healthSummary
            }
            for check in service.healthChecks:
                print "Service: %(name)s, Health Check: %(health)s, Status: %(status)s" % {
                    'name': service.displayName,
                    'health': check['name'],
                    'status': check['summary']
                }
            for role in service.get_all_roles():
                print "Service: %(service_name)s, Role: %(role_name)s, Health Summary: %(health)s" % {
                    'service_name': service.displayName,
                    'role_name': role.name,
                    'health': role.healthSummary
                }
                for check in role.healthChecks:
                    print "Service: %(service_name)s, Role: %(role_name)s, Health Check: %(health)s, Status: %(status)s" % {
                        'service_name': service.displayName,
                        'role_name': role.name,
                        'health': check['name'],
                        'status': check['summary']
                    }

    sys.exit(0)
Exemple #8
0
def main():
    (options, args) = parse_args()

    try:
        (username, password) = open(options.passfile,
                                    'r').readline().rstrip('\n').split(':')
    except:
        print >> sys.stderr, "Unable to read username and password from file '%s'. "
        "Make sure the file is readable and contains a single line of "
        "the form \"<username>:<password>\"" % options.passfile

    root = get_root_resource(options.host, options.port, username, password,
                             options.use_tls, CM_API_VERSION)

    hosts_map = get_host_map(root)

    if options.mode == "update_status":
        ''' The way in which the status is reported depends on whether or not the
        -n/--use-send-nsca option was set.  By default, the status updates are
        simply appended to the external command file.  If nsca is used, the
        status updates are piped via stdin to send_nsca.
    '''
        # <host>\t<service>\t<return_code>\t<message>\n
        NSCA_STATUS_FORMAT = "{0}\t{1}\t{2}\t{3}\n"
        # [<timestamp>] PROCESS_SERVICE_CHECK_RESULT;<host>;<service>;<return_code>;<message>\n
        CMD_STATUS_FORMAT = "[{0}] PROCESS_SERVICE_CHECK_RESULT;{1};{2};{3};{4}\n"

        try:
            services_list = get_services(root, hosts_map, "full")
            timestamp = long(
                time())  # approximate time the services were queried

            tmp_status_file = tempfile.TemporaryFile()
            try:
                for service in services_list:
                    health = service["health_summary"]
                    if not health is None:
                        code = CM_STATE_CODES[health]
                        if options.use_send_nsca:
                            status_line = NSCA_STATUS_FORMAT.format(
                                service["hostname"], service["name"], code,
                                service["status"])
                        else:
                            status_line = CMD_STATUS_FORMAT.format(
                                timestamp, service["hostname"],
                                service["name"], code, service["status"])

                        tmp_status_file.write(status_line)

                tmp_status_file.flush()
                tmp_status_file.seek(0)

                if options.use_send_nsca:
                    submit_status_send_nsca(options.send_nsca_path,
                                            options.nsca_host,
                                            options.nsca_port,
                                            options.send_nsca_config,
                                            tmp_status_file, options.verbose)
                else:
                    submit_status_external_cmd(options.cmd_file,
                                               tmp_status_file)
            finally:
                tmp_status_file.close()
        except SystemExit as e:
            sys.exit(e)
        except Exception as e:
            print >> sys.stderr, "An unknown error occurred: %s" % str(e)
            raise

    elif options.mode == "generate_cfg":

        host_cfg_file = open(join(options.cfg_dir, CM_HOST_CFG_FILENAME), 'w+')
        host_cfg_file.write(PASSIVE_HOST_TEMPLATE)

        for host in hosts_map.values():
            host_cfg_file.write(CM_HOST_TEMPLATE % (
                host["hostname"],
                host["hostname"],
                host["address"],
            ))

        host_cfg_file.close()

        services_cfg_file = open(
            join(options.cfg_dir, CM_SERVICE_CFG_FILENAME), 'w+')
        services_cfg_file.write(PASSIVE_SERVICE_TEMPLATE)

        services_list = get_services(root, hosts_map)
        for service in services_list:
            services_cfg_file.write(CM_SERVICE_TEMPLATE %
                                    (service["hostname"], service["name"],
                                     service["display_name"], service["url"]))

        services_cfg_file.close()

    else:
        print >> sys.stderr, "Fatal error: Unrecognized mode"