コード例 #1
0
    def __call__(self):
        """
        Method to call the plugin directly.
        """

        try:

            self.parse_args()
            self.init_root_logger()

            self._read_config()
            self.parse_args_second()

            log.debug("Creating REST API client object ...")
            self.api = RestApi.from_config(
                extra_config_file=self.argparser.args.extra_config_file,
                api_url=self.argparser.args.api_url,
                timeout=self.timeout,
            )

            if self.verbose > 2:
                log.debug("Current object:\n%s", pp(self.as_dict()))

            self.pre_run()
            self.run()

        except Exception as e:
            state = nagios.state.unknown
            ename = e.__class__.__name__
            appname = os.path.basename(sys.argv[0])
            out = "%s on executing %r: %s" % (ename, appname,  e)
            if self.verbose:
                self.handle_error(str(e), ename, do_traceback=True)

            self.exit(state, out)
コード例 #2
0
    state = {"OK": 0, "WARNING": 1, "CRITICAL": 2, "UNKNOWN": 3}

    fqdn = socket.getfqdn(socket.gethostname())
    (hostname, domain) = split(fqdn, '.', 1)
    del fqdn

    url = args.url
    efile = args.auth_token
    timeout = args.timeout

    pattern = re.compile('^(pserver|gw|ps)(\d+[a-z]-)?\d+$')
    if not pattern.match(hostname):
        print("UNKNOWN: only pservers and gateways are supported")
        sys.exit(state['UNKNOWN'])

    api = RestApi.from_config(extra_config_file=efile, api_url=url, timeout=timeout)

    # fetch the cluster name of current hostname
    response = dict()
    try:
        response = api.pservers(name=hostname)
    except Exception as e:
        print("UNKNOWN: failed to fetch cluster name from dcmanager api for host %s: %s" %
              (hostname, str(e)))
        sys.exit(state['UNKNOWN'])

    if len(response) == 1 and 'cluster' in response[0]:
        cluster = response[0]['cluster']
    else:
        print("UNKNOWN: dcmanager api response for host %s does not contain "
              "any 'cluster' attribute" % hostname)