def probe(self):
        """Query system state and return metrics.

        :return: generator that emits
          :class:`~nagiosplugin.metric.Metric` objects
        """
        # Execute `systemctl is-active <service>` and get output
        # - active
        # - inactive (by unkown unit file)
        # - failed
        try:
            p = subprocess.Popen(['systemctl', 'is-active', self.unit],
                                 stderr=subprocess.PIPE,
                                 stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE)
            stdout, stderr = p.communicate()
        except OSError as e:
            raise nagiosplugin.CheckError(e)

        if stderr:
            raise nagiosplugin.CheckError(stderr)
        if stdout:
            for line in io.StringIO(stdout.decode('utf-8')):
                active = line.strip()
                yield Metric(name=self.unit, value=active, context='unit')
Exemple #2
0
    def probe(self):
        # Execute systemctl is-active and get output
        try:
            p = subprocess.Popen(['systemctl', 'is-active', self.service],
                                 stderr=subprocess.PIPE,
                                 stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE)
            pres, err = p.communicate()
        except OSError as e:
            raise nagiosplugin.CheckError(e)

        if err:
            raise nagiosplugin.CheckError(err)
        if pres:
            result = ""
            for line in io.StringIO(pres.decode('utf-8')):
                result = "%s %s" % (result, line.split(' ')[0])
            result = result.strip()
            if result == "active":
                return [
                    nagiosplugin.Metric('systemd', (True, None),
                                        context='systemd')
                ]
            else:
                return [
                    nagiosplugin.Metric('systemd', (False, self.service),
                                        context='systemd')
                ]

        return [
            nagiosplugin.Metric('systemd', (False, "No Service given"),
                                context='systemd')
        ]
def execute_cli(args: typing.Union[str, typing.Iterator[str]]) -> str:
    """Execute a command on the command line (cli = command line interface))
    and capture the stdout. This is a wrapper around ``subprocess.Popen``.

    :param args: A list of programm arguments.

    :raises nagiosplugin.CheckError: If the command produces some stderr output
      or if an OSError exception occurs.

    :return: The stdout of the command.
    """
    try:
        p = subprocess.Popen(args,
                             stderr=subprocess.PIPE,
                             stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE)
        stdout, stderr = p.communicate()
    except OSError as e:
        raise nagiosplugin.CheckError(e)

    if p.returncode != 0:
        raise nagiosplugin.CheckError('The command exits with a none-zero'
                                      'return code ({})'.format(p.returncode))

    if stderr:
        raise nagiosplugin.CheckError(stderr)

    if stdout:
        stdout = stdout.decode('utf-8')
        return stdout
Exemple #4
0
    def probe(self):
        log.debug("Graylog2Throughput.probe started")
        try:
            # /system/throughput sometimes returns 0 (which is normal),
            # retry max_retry times before returns 0
            for _ in xrange(self._max_retry):
                log.debug("try #%d", _)
                r = requests.get(
                    self._api_url, auth=(self._username, self._password))
                log.debug("response: %s", r.content)
                throughput = r.json()['throughput']
                log.debug("throughput : %s", str(throughput))
                if throughput != 0:
                    break
                time.sleep(1)  # take a break before retry
        except requests.ConnectionError:
            log.warn("Could not conect to server: %s", self._api_url)
            raise nagiosplugin.CheckError(
                'Could not connect to graylog2 server: {}'.format(
                    self._api_url))
        except ValueError:
            log.warn("Could not parse response")
            raise nagiosplugin.CheckError(
                'Invalid response from graylog2 server: {}'.format(
                    self._api_url))

        log.debug("Graylog2Throughput finished")
        log.debug("returning %d", int(throughput))
        return [
            nagiosplugin.Metric('throughput', int(throughput), min=0)
        ]
    def probe(self):
        """Query system state and return metrics.

        :return: generator that emits
          :class:`~nagiosplugin.metric.Metric` objects
        """
        try:
            p = subprocess.Popen(['systemd-analyze'],
                                 stderr=subprocess.PIPE,
                                 stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE)
            stdout, stderr = p.communicate()
        except OSError as e:
            raise nagiosplugin.CheckError(e)

        if stderr:
            raise nagiosplugin.CheckError(stderr)

        if stdout:
            stdout = stdout.decode('utf-8')
            # First line:
            # Startup finished in 1.672s (kernel) + 21.378s (userspace) =
            # 23.050s

            # On raspian no second line
            # Second line:
            # graphical.target reached after 1min 2.154s in userspace
            match = re.search(r'reached after (.+) in userspace', stdout)

            if not match:
                match = re.search(r' = (.+)\n', stdout)

        yield Metric(name='startup_time',
                     value=format_timespan_to_seconds(match.group(1)),
                     context='startup_time')
Exemple #6
0
    def probe(self):
        # Execute systemctl --failed --no-legend and get output
        try:
            p = subprocess.Popen(['systemctl', '--failed', '--no-legend'],
                                 stderr=subprocess.PIPE,
                                 stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE)
            pres, err = p.communicate()
        except OSError as e:
            raise nagiosplugin.CheckError(e)

        if err:
            raise nagiosplugin.CheckError(err)

        if pres:
            result = ""
            for line in io.StringIO(pres.decode('utf-8')):
                result = "%s %s" % (result, line.split(' ')[0])

            return [
                nagiosplugin.Metric('systemd', (False, result),
                                    context='systemd')
            ]

        return [
            nagiosplugin.Metric('systemd', (True, None), context='systemd')
        ]
    def probe(self):
        """Query system state and return metrics.

        :return: generator that emits
          :class:`~nagiosplugin.metric.Metric` objects
        """
        # We don’t use `systemctl --failed --no-legend`, because we want to
        # collect performance data of all units.
        try:
            p = subprocess.Popen(
                ['systemctl', 'list-units', '--all', '--no-legend'],
                stderr=subprocess.PIPE,
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE)
            stdout, stderr = p.communicate()
        except OSError as e:
            raise nagiosplugin.CheckError(e)

        if stderr:
            raise nagiosplugin.CheckError(stderr)

        # Dictionary to store all units according their active state.
        units = {
            'failed': [],
            'active': [],
            'activating': [],
            'inactive': [],
        }
        if stdout:
            # Output of `systemctl list-units --all --no-legend`:
            # UNIT           LOAD   ACTIVE SUB     JOB   DESCRIPTION
            # foobar.service loaded active waiting       Description text
            count_units = 0
            for line in io.StringIO(stdout.decode('utf-8')):
                split_line = line.split()
                # foobar.service
                unit = split_line[0]
                # failed
                active = split_line[2]
                # Only count not excludes units.
                if unit not in self.excludes:
                    units[active].append(unit)
                    count_units += 1

            for unit in units['failed']:
                if unit not in self.excludes:
                    yield Metric(name=unit, value='failed', context='unit')

            for active, unit_names in units.items():
                yield Metric(name='units_{}'.format(active),
                             value=len(units[active]),
                             context='performance_data')

            yield Metric(name='count_units',
                         value=count_units,
                         context='performance_data')

        if len(units['failed']) == 0:
            yield Metric(name='all', value=None, context='unit')
    def probe(self):
        """
        :return: generator that emits
          :class:`~nagiosplugin.metric.Metric` objects
        """
        try:
            p = subprocess.Popen(['systemctl', 'list-timers', '--all'],
                                 stderr=subprocess.PIPE,
                                 stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE)
            stdout, stderr = p.communicate()
        except OSError as e:
            raise nagiosplugin.CheckError(e)

        if stderr:
            raise nagiosplugin.CheckError(stderr)

        # NEXT                          LEFT
        # Sat 2020-05-16 15:11:15 CEST  34min left

        # LAST                          PASSED
        # Sat 2020-05-16 14:31:56 CEST  4min 20s ago

        # UNIT             ACTIVATES
        # apt-daily.timer  apt-daily.service
        if stdout:
            lines = stdout.decode('utf-8').splitlines()
            table_heading = lines[0]
            self.column_boundaries = self.detect_column_boundaries(
                table_heading)
            # Remove the first line because it is the header.
            # Remove the two last lines: empty line + "XX timers listed."
            table_body = lines[1:-2]

            state = nagiosplugin.Ok  # ok

            for row in table_body:
                unit = self.get_column_text(row, 'UNIT')
                if self.re_match(unit):
                    continue
                next_date_time = self.get_column_text(row, 'NEXT')

                if next_date_time == 'n/a':
                    passed_text = self.get_column_text(row, 'PASSED')

                    if passed_text == 'n/a':
                        state = nagiosplugin.Critical
                    else:
                        passed = format_timespan_to_seconds(passed_text)

                        if passed_text == 'n/a' or passed >= self.critical:
                            state = nagiosplugin.Critical
                        elif passed >= self.warning:
                            state = nagiosplugin.Warn

                yield Metric(name=unit, value=state, context='dead_timers')
    def probe(self):
        """
        Querys the REST-API and create throughput metrics.

        :return: a throughput metric.
        """

        _log.info('Reading XML from: %s', self.xml_obj.build_request_url())

        api_outbytes, api_inbytes = 0, 0

        current_time = get_time()
        soup = self.xml_obj.read()
        ifnet = soup.find('ifnet')

        for item in ifnet.find_all('entry'):
            api_inbytes = Finder.find_item(item, 'ibytes')
            api_outbytes = Finder.find_item(item, 'obytes')

        _log.debug('Path to statefile: %r' % get_statefile_path())
        with np.Cookie(get_statefile_path()) as cookie:

            old_inbytes = cookie.get(self.host + self.interface_name + 'i', api_inbytes)
            old_outbytes = cookie.get(self.host + self.interface_name + 'o', api_outbytes)
            old_time = cookie.get(self.host + self.interface_name + 't', current_time)

            if not api_inbytes or not api_outbytes or float(api_inbytes) < 0 or float(api_outbytes) < 0:
                raise np.CheckError('Couldn\'t get a valid value!')

            cookie[self.host + self.interface_name + 'i'] = api_inbytes
            cookie[self.host + self.interface_name + 'o'] = api_outbytes
            cookie[self.host + self.interface_name + 't'] = current_time

        if float(api_inbytes) < float(old_inbytes) or float(api_outbytes) < float(old_outbytes):
            raise np.CheckError('Couldn\'t get a valid value: Found throughput less then old!')

        diff_time = int(current_time) - int(old_time)
        if diff_time > 0:
            in_bits_per_second = round(
                ((float(api_inbytes) - float(old_inbytes)) / diff_time) * 8, 2)
            out_bits_per_second = round(
                ((float(api_outbytes) - float(old_outbytes)) / diff_time) * 8,
                2)
        else:
            raise np.CheckError(
                'Difference between old timestamp and new timestamp is less '
                'or equal 0: If it is the first time you run the script, '
                'please execute it again!')

        return [
            np.Metric('in_bps_' + str(self.interface_name), in_bits_per_second,
                      min=0),
            np.Metric('out_bps_' + str(self.interface_name), out_bits_per_second,
                      min=0)]
    def probe(self):
        oid_role = '1.3.6.1.4.1.2636.3.1.8.1.12.1.1.0.0'
        raw_role = str(self.argoss_snmp.fetch_oid(oid_role))
        if ("Single" in raw_role):
            role = "single"
        elif ("node" in raw_role):
            role = "node"
        else:
            raise nagiosplugin.CheckError("Error: this host is a slave.")

        data = {}
        self.argoss_snmp.gather_data(data, '1.3.6.1.4.1.2636.3.1.13.1', {
            5: 'operatings',
            6: 'status',
            11: 'mem'
        })

        operatings = {}
        for index, opDesc in data['operatings'].items():
            if (role == "single"):
                m = re.search('^Routing\ Engine (\d+)$', str(opDesc))
                if m:
                    descr = 'mem_re' + m.group(1)
                    operatings[index] = [descr]
                m = re.search('^FPC:.*SPC.*\ (.*)$', str(opDesc))
                if m:
                    descr = 'mem_spc@' + m.group(1)
                    operatings[index] = [descr]
            elif ("node" in role):
                m = re.search('^' + role + '\ Routing\ Engine (\d+)$',
                              str(opDesc))
                if m:
                    descr = 'mem_re' + m.group(1)
                    operatings[index] = [descr]
                m = re.search('^' + role + '\ FPC:.*SPC.*\ (.*)$', str(opDesc))
                if m:
                    descr = 'mem_spc@' + m.group(1)
                    operatings[index] = [descr]

        total_load = 0
        for index, descr in operatings.items():
            if int(data['status'][index]) == 2:
                total_load = total_load + int(data['mem'][index])

        if not len(operatings):
            raise nagiosplugin.CheckError(
                "Impossible de trouver une carte MEM")

        alert_mem_percent = total_load / len(operatings)
        yield nagiosplugin.Metric('alert_mem_percent',
                                  alert_mem_percent,
                                  None,
                                  context='alert_mem_percent')
Exemple #11
0
 def get_containers(self):
     try:
         result = rq.get(self.url, headers=self.headers, verify=self.verify)
     except Exception as e:
         raise np.CheckError(
             'Failed to query API: {}'.format(e)
         )
     _log.debug('API Response: {}'.format(result.content))
     if result.status_code != 200:
         raise np.CheckError(
             'http status code was {}'.format(result.status_code)
         )
     return result
Exemple #12
0
    def probe(self):
        log.debug("PgSQLQuery.probe started")
        try:
            log.debug("connecting with postgresql")
            c = psycopg2.connect(host=self.host,
                                 port=self.port,
                                 user=self.user,
                                 password=self.passwd,
                                 database=self.database)
            cursor = c.cursor()
            log.debug("about to execute query: %s", self.query)
            cursor.execute(self.query)
            records = cursor.rowcount
            log.debug("resulted in %d records", records)
            log.debug(records)
            log.debug(cursor.fetchall())
        except psycopg2.Error as err:
            log.critical(err)
            raise nap.CheckError(
                'Something went wrong with '
                'PostgreSQL query operation, Error: {}'.format(err))

        log.debug("PgSQLQuery.probe finished")
        log.debug("returning %d", records)
        return [nap.Metric('record', records, context='records')]
Exemple #13
0
    def load_filesystem(self):
        data = {}
        self.argoss_snmp.gather_data(data,
                                     '1.3.6.1.2.1.25.2.3.1',
                                     {3: 'label',
                                      4: 'alloc',
                                      5: 'size',
                                      6: 'used'})
        id_fs = ''
        for l in data['label']:
            data['label'][l] = str(data['label'][l])
            if(data['label'][l].split(':\\')[0].lower() ==
               self.label_fs.lower()):
                id_fs = l
        if not id_fs:
            raise nagiosplugin.CheckError('Filesystem not found')

        alloc = int(data['alloc'][id_fs])
        size = int(data['size'][id_fs])
        used = int(data['used'][id_fs])

        fs_used = used * alloc
        fs_total = size * alloc
        if fs_total == 0:
            return fs_used, fs_total, 0
        alert_fs_percent = round(100 * (used / size), 2)
        return fs_used, fs_total, alert_fs_percent
Exemple #14
0
    def probe(self):
        c = Controller(self._ctrl_addr,
                       self._ctrl_user,
                       self._ctrl_passwd,
                       ssl_verify=False)
        for ap in c.get_aps():
            if ap.get('name') == self._hostname:
                if self._metric == Metrics.STATUS:
                    return [nagiosplugin.Metric('status', self._get_state(ap), context='null')]
                elif self._metric == Metrics.CPU:
                    return [nagiosplugin.Metric('cpu', self._get_cpu(ap), context='cpu')]
                elif self._metric == Metrics.MEMORY:
                    return [nagiosplugin.Metric('memory', self._get_mem(ap), context='memory')]
                elif self._metric == Metrics.SPEED:
                    return [nagiosplugin.Metric('speed', self._get_speed(ap), context='speed')]
                elif self._metric == Metrics.SATISFACTION:
                    return [nagiosplugin.Metric('satisfaction', self._get_satisfaction(ap), context='satisfaction')]
                elif self._metric == Metrics.TEMPERATURE:
                    return [nagiosplugin.Metric('temperature', self._get_temperature(ap), context='temperature')]
                elif self._metric == Metrics.OVERHEATING:
                    return [nagiosplugin.Metric('overheating', self._get_overheating(ap), context='null')]
                elif self._metric == Metrics.POWER_LEVEL:
                    return [nagiosplugin.Metric('power_level', self._get_power_level(ap), context='power_level')]

        raise nagiosplugin.CheckError('unable to find specified device: {}'.format(self._hostname))
Exemple #15
0
    def probe(self):
        oid_desc = '1.3.6.1.2.1.47.1.1.1.1.2'
        oid_indice = '1.3.6.1.4.1.9.9.109.1.1.1.1.2'
        oid_value = '1.3.6.1.4.1.9.9.109.1.1.1.1.7'

        fo = 0.0
        ro = 0.0
        i = 0

        request = self.argoss_snmp.snmp_table(oid_desc)
        oid_desc_found = {}
        for response in request:
            if 'CPU 0 of module R0' in str(response.value):
                oid_desc_found["ro"] = ((str(response.oid)).split('.'))[-1]
            if 'CPU 0 of module F0' in str(response.value):
                oid_desc_found["fo"] = ((str(response.oid)).split('.'))[-1]

        request = self.argoss_snmp.snmp_table(oid_indice)

        for response in request:
            if str(response.oid_index) == str(oid_desc_found["ro"]):
                indice_found = ((str(response.oid)).split('.'))[-1]
                ro = float(
                    self.argoss_snmp.fetch_oid(oid_value + "." + indice_found))
                i += 1
            if str(response.oid_index) == str(oid_desc_found["fo"]):
                indice_found = ((str(response.oid)).split('.'))[-1]
                fo = float(
                    self.argoss_snmp.fetch_oid(oid_value + "." + indice_found))
                i += 1
        if i == 2:
            yield nagiosplugin.Metric('alert_cpu_percent', (fo + ro) / 2,
                                      context='alert_cpu_percent')
        else:
            raise nagiosplugin.CheckError('CPU not found')
Exemple #16
0
 def load_memory(self):
     data = {}
     memory = {'used': 0, 'free': 0}
     self.argoss_snmp.gather_data(data,
                                  '1.3.6.1.4.1.9.9.221.1.1.1.1',
                                  {6: 'valid',
                                   18: 'used',
                                   20: 'free'})
     self.argoss_snmp.gather_data(data,
                                  '1.3.6.1.4.1.9.9.48.1.1.1',
                                  {4: 'valid',
                                   5: 'used',
                                   6: 'free'})
     for label in data:
         if label != 'valid':
             for value in data[label]:
                 if int(data['valid'][value]) == 1:
                     memory[label] = (int(memory[label]) +
                                      int(data[label][value]))
     try:
         return (
             memory['used'],
             memory['used']+memory['free'],
             round(100*(memory['used']/(memory['free']+memory['used'])), 2))
     except ZeroDivisionError:
         raise nagiosplugin.CheckError('Memory pool not available')
Exemple #17
0
    def probe(self):
        logger.debug("EventCountCheck.probe started")
        # send an event for testing
        dsn = self._sentry_dsn
        if not dsn.startswith("requests+"):
            dsn = "requests+" + dsn
        client = raven.Client(dsn=dsn)
        logger.debug("send monitoring messaage to sentry")
        client.captureMessage("sentry monitoring")

        try:
            r = requests.get(self._url,
                             auth=(self._public_key, self._secret_key),
                             verify=self._verify_ssl)
            data = r.json()
            logger.debug("response: %s", data)

            events = 0
            for group in data:
                events += int(group["count"])
            logger.debug("number of events: %d", events)

            return [nagiosplugin.Metric('number_of_events', events, min=0)]

        except requests.ConnectionError as err:
            raise nagiosplugin.CheckError("Could not connect to Sentry: %s",
                                          err)
Exemple #18
0
    def pkg_audit(self, jail=None):
        """Run pkg audit.

           We choose here to raise UNKNOWN status if we encoutered a host|jail
           which in pkg audit -F has not been runned.
        """
        self.audit_cmd = 'pkg audit'
        if jail is not None:
            self.audit_cmd = 'pkg -j %s audit' % jail
            self.hostname = jail

        _log.debug('querying system with "%s" command', self.audit_cmd)

        stdout, stderr = _popen(self.audit_cmd.split())

        if not isinstance(stderr, str):  # pragma: no cover
            stderr = stderr.decode()
        if not isinstance(stdout, str):  # pragma: no cover
            stdout = stdout.decode()

        if stderr:
            message = stderr.splitlines()[-1]

            if message.startswith('pkg: vulnxml file'):
                # message = "Try running 'pkg audit -F' first"
                message = stderr.split('.')[-1]
            message = "%s %s" % (self.hostname, message)
            _log.info(message)
            raise nagiosplugin.CheckError(message)

        else:
            stdout = stdout.splitlines()[-1]
            problems = int(stdout.split()[0])

            return problems
Exemple #19
0
def main():
    argp = argparse.ArgumentParser(prog='check_dns', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    argp.add_argument('--domain', '-d', type=str, required=True, action='store',
                      help='Domain to check')
    argp.add_argument('--insecure-is-ok', action='store_true', default=False,
                      help='If the delegation is insecure, do not warn')
    argp.add_argument('--expire-warn', default=72, metavar='HOURS',
                      help='Warn if RRSIG expiry is within this many hours')
    argp.add_argument('--expire-crit', default=48, metavar='HOURS',
                      help='Crit if RRSIG expiry is within this many hours')
    argp.add_argument('--verbose', '-v', action='count', default=0,
                      help='Be more verbose')
    args = argp.parse_args()

    if args.expire_crit > args.expire_warn:
        raise nagiosplugin.CheckError('RRSIG Critical value ({}) higher than Warning value ({})'.format(
            args.expire_crit, args.expire_warn
        ))
    check = nagiosplugin.Check(
        DNS(args.domain, args.insecure_is_ok),
        DNSSECContext('dnssec'),
        RRSIGContext('rrsig'),
        RRSIGExpitationContext('rrsig_expiration',
                               warn_seconds=args.expire_warn * 60 * 60,
                               crit_seconds=args.expire_crit * 60 * 60),
        DNSSummary())
    check.main(args.verbose)
    def probe(self):
        r = None

        proxies = {'http': '', 'https': ''}
        try:
            r = requests.get(self.url, proxies=proxies, timeout=2)
        except requests.exceptions.ConnectionError:
            print("Connection refused")

        if r:
            """soup = BeautifulSoup(r.content, "lxml")
            parsed = soup.find(re.compile('int|real'))

            try:
                val = float(parsed['val'])
            except (KeyError, TypeError):
                raise nagiosplugin.CheckError(
                    'Value not available')"""
            parsed = xmltodict.parse(r.content)

            try:
                val = float(parsed[list(parsed.keys())[0]]['@val'])
            except (KeyError, TypeError):
                raise nagiosplugin.CheckError('Value is not available')

            yield nagiosplugin.Metric(self.metric,
                                      val,
                                      context='alert_dogate_' + self.metric)
Exemple #21
0
    def __init__(self, dsn_file):
        try:
            loaded = unserialize_yaml(dsn_file, critical=True)
            sentry_dsn = loaded["dsn"]
            self._sentry_dsn = sentry_dsn
            logger.debug("sentry_dsn: %s", sentry_dsn)
        except IOError as err:
            logger.error(
                "Sentry monitoring DSN file %s does not exist"
                "or is unreadable. Error: %s", dsn_file, err)
            raise nagiosplugin.CheckError(
                "Invalid sentry monitoring DSN file: {}".format(dsn_file))

        dsn_parsed = raven.load(sentry_dsn)
        self._public_key = dsn_parsed["SENTRY_PUBLIC_KEY"]
        self._secret_key = dsn_parsed["SENTRY_SECRET_KEY"]
        transport_options = dsn_parsed["SENTRY_TRANSPORT_OPTIONS"]
        self._verify_ssl = False if (
            "verify_ssl" in transport_options
            and transport_options["verify_ssl"] == "0") else True

        # API format: GET /api/0/projects/{project_id}/groups/
        server = dsn_parsed["SENTRY_SERVERS"][0]
        project_id = dsn_parsed["SENTRY_PROJECT"]
        parsed = urlparse.urlparse(server)
        self._url = urlparse.urlunparse((
            parsed.scheme,
            parsed.netloc,
            "/api/0/projects/{}/groups/".format(project_id),
            parsed.params,
            parsed.query,
            parsed.fragment,
        ))
 def probe(self):
     try:
         self.fetch_status()
     except pymysql.err.OperationalError as e:
         raise nagiosplugin.CheckError(e)
     yield nagiosplugin.Metric('status_ap',
                               self.status,
                               context='status_ap')
 def probe(self):
     oid = '1.3.6.1.4.1.12356.101.4.1.8'
     request = argoss_libs.snmp.tools.snmp_table(oid, self.community,
                                                 self.host, self.port)
     value = None
     for errorIndication, errorStatus, errorIndex, varBinds in request:
         if errorIndication:
             raise nagiosplugin.CheckError(errorIndication)
         elif errorStatus:
             raise nagiosplugin.CheckError(errorStatus)
         value = varBinds[0][1]
     if value is None:
         raise nagiosplugin.CheckError('Session not found')
     yield nagiosplugin.Metric('alert_session',
                               value,
                               None,
                               context='alert_session')
 def get_node_by_certname(self, certname):
     """ gets a pypuppetdb node object given a certname"""
     try:
         node = self.pdb.node(certname)
     except requests.exceptions.HTTPError as e:
         # catch 404 - node not found; raise anything else
         if e.message == '404 Client Error: Not Found':
             raise nagiosplugin.CheckError(
                 'UNKNOWN: Node %s not found in PuppetDB on %s' %
                 (certname, self.puppetdb_host))
         raise e
     if not node:
         raise nagiosplugin.CheckError(
             'UNKNOWN: Node %s not found in PuppetDB on %s' %
             (certname, self.puppetdb_host))
     _log.info("Found node in PuppetDB")
     return node
 def fetch_status(self):
     connection = pymysql.connect(host=self.host,
                                  user='******',
                                  password='******',
                                  db='WIFI',
                                  charset='utf8',
                                  connect_timeout=4,
                                  cursorclass=pymysql.cursors.DictCursor)
     with connection.cursor() as cursor:
         sql = 'SELECT * FROM ap_alarms WHERE name LIKE %s'
         cursor.execute(sql, (self.ap))
         if cursor.rowcount == 0:
             raise nagiosplugin.CheckError('No equipment available')
         for row in cursor:
             if 'status' in row:
                 self.status = row['status']
             else:
                 raise nagiosplugin.CheckError('No status available')
 def evaluate(self, metric, resource):
     if str(metric) == 'Up':
         return nagiosplugin.result.Result(nagiosplugin.state.Ok,
                                           'Borne OK', metric)
     elif str(metric) == 'Down':
         return nagiosplugin.result.Result(nagiosplugin.state.Critical,
                                           'Borne KO', metric)
     else:
         raise nagiosplugin.CheckError('Unknown status')
def query_from_authority(zone):
    """Send query using external tool to fetch serial from NS servers of the zone"""
    proc = subprocess.Popen([executable, zone]+parameters_from_authority.split(),
                          stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE)
    answers,error = proc.communicate()
    if not isinstance(answers, str):  # pragma: no cover
        answers = answers.decode()
    if not isinstance(error, str):  # pragma: no cover
        error = error.decode()

    if answers.find("timed out") >= 0:
        raise nagiosplugin.CheckError("A server timed out")
    if not len(answers):
        raise nagiosplugin.CheckError("No result. Domain probably does not exist")

    answers = answers.strip().splitlines()
    return [(int(arguments[3]), arguments[10]) for arguments in (answer.split() for answer in answers)]
    def probe(self):
        # Execute systemctl is-active and get output
        try:
            p = subprocess.Popen(['systemctl', 'is-active', self.service],
                                 stderr=subprocess.PIPE,
                                 stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE)
            stdout, stderr = p.communicate()
        except OSError as e:
            raise nagiosplugin.CheckError(e)

        if stderr:
            raise nagiosplugin.CheckError(stderr)
        if stdout:
            for line in io.StringIO(stdout.decode('utf-8')):
                yield nagiosplugin.Metric(self.service,
                                          line.strip(),
                                          context='systemd')
    def __request(self, url):

        login_header = {}
        if len(self.token) != 0:
            login_header['Private-Token'] = self.token

        try:
            response = requests.get(self.url + url, headers=login_header)
        except requests.exceptions.ConnectionError:
            raise nagiosplugin.CheckError('connection to {0} failed'.format(
                self.url))

        if response.status_code != 200:
            raise nagiosplugin.CheckError(
                self.http_response_messages.get(response.status_code,
                                                'unknown failure'))

        return response.json()
Exemple #30
0
    def probe(self):
        """
        Run restic and parse its return code

        :return:
        """

        # For some reason, check.main() is the only place where exceptions are
        # printed nicely
        if not self.repo and not os.environ.get('RESTIC_REPOSITORY'):
            raise nagiosplugin.CheckError(
                'Please specify repository location (-r, --repo or '
                '$RESTIC_REPOSITORY)')
        if not self.password_file and \
           not (os.environ.get('RESTIC_PASSWORD') or
                os.environ.get('RESTIC_PASSWORD_FILE')):
            raise nagiosplugin.CheckError(
                'Please specify password or its location (-p, --password-file,'
                ' $RESTIC_PASSWORD or $RESTIC_PASSWORD_FILE)')

        cmd = [self.restic_bin, 'check', '--quiet', '--no-lock']

        if self.sudo:
            cmd = ['sudo'] + cmd

        if self.repo:
            cmd.extend(['--repo', self.repo])
        if self.password_file:
            cmd.extend(['--password-file', self.password_file])

        _log.info('Using command: %s' % ' '.join(cmd))

        try:
            restic_result = subprocess.check_output(cmd,
                                                    stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            yield nagiosplugin.Metric(self.repo, False, context='health_check')
            self.stderr = e.output.decode()
        except IOError as e:
            raise nagiosplugin.CheckError('Failed to run %s: %s' %
                                          (' '.join(cmd), e))
        else:
            _log.debug('Got output: %s' % restic_result)
            yield nagiosplugin.Metric(self.repo, True, context='health_check')