Skip to main content

CI Release Version

Python library for building InfraSonar Probes

This library is created for building InfraSonar probes.

Environment variable

Variable Default Description
AGENTCORE_HOST 127.0.0.1 Hostname or Ip address of the AgentCore.
AGENTCORE_PORT 8750 AgentCore port to connect to.
ENCRYPTION_KEY default Use your own encryption key for encrypting secrets in the YAML file.
INFRASONAR_CONF /data/config/infrasonar.yaml File with probe and asset configuration like credentials.
MAX_PACKAGE_SIZE 500 Maximum package size in kilobytes (1..2000).
MAX_CHECK_TIMEOUT 300 Check time-out is 80% of the interval time with MAX_CHECK_TIMEOUT in seconds as absolute maximum.
DRY_RUN none Do not run demonized, just return checks and assets specified in the given yaml (see the Dry run section below).
LOG_LEVEL warning Log level (debug, info, warning, error or critical).
LOG_COLORIZED 0 Log using colors (0=disabled, 1=enabled).
LOG_FTM %y%m%d %H:%M:%S Log format prefix.
OUTPUT_TYPE JSON Set the output type to JSON or PPRINT (Only for a dry run).

Usage

Building an InfraSonar.get_state

import logging
from libprobe import logger
from libprobe.asset import Asset
from libprobe.probe import Probe
from libprobe.check import Check
from libprobe.severity import Severity
from libprobe.exceptions import (
    CheckException,
    IgnoreResultException,
    IgnoreCheckException,
    IncompleteResultException,
    NoCountException,
)

__version__ = "0.1.0"


class MyFirstCheck(Check):

    key = 'myFirstCheck'
    unchanged_eol = 0  # Can be for example 14400, to prevent sending the same
                       # check result for the next 4 hours (0=disabled)

    @staticmethod
    async def run(asset: Asset, local_config: dict, config: dict) -> dict:
        """My first check.
        Arguments:
        asset:
            Asset contains an id, name and check which should be used
            for logging;
        local_config:
            local configuration for this asset, for example
            credentials;
        config:
            configuration for this check; contains for example the
            interval at which the check is running and an address of
            the asset to probe;
        """
        if "ignore_this_check_iteration":
            # nothing will be send to InfraSonar for this check iteration;
            raise IgnoreResultException()

        if "no_longer_try_this_check":
            # nothing will be send to InfraSonar for this check iteration and
            # the check will not start again until the probe restarts or
            # configuration has been changed;
            raise IgnoreCheckException()

        if "something_has_happened":
            # send a check error to InfraSonar because something has happened
            # which prevents us from building a check result; The default
            # severity for a CheckException is MEDIUM but this can be
            # overwritten;
            raise CheckException("something went wrong", severity=Severity.LOW)

        if "something_unexpected_has_happened":
            # exceptions will be converted to CheckException, MEDIUM severity
            raise Exception("something went wrong")

        # A check result may have multiple types, items, and/or metrics
        result = {"myType": [{"name": "my item"}]}

        if "result_is_incomplete":
            # optionally, IncompleteResultException with severity;
            # the default severity is LOW.
            raise IncompleteResultException('missing type x', result)

        if "not_count_as_check_result":
            # optionally, NoCountException can be raised in which case the
            # check result is not counted by InfraSonar; Thus, the last seen
            # services will not "see" this check result.
            # A severity can be given if we also want a check error;
            # (similar to the IncompleteResultException exception)
            raise NoCountException('do not count this check result', result)

        # Use the asset in logging; includes asset info and the check key
        logging.info(f"log something; {asset}")

        # In alpha versions and debug logging enabled, unknown exception will
        # be logged when debug logging is enabled.
        # You may use logger.exception() yourself if you want exception
        # logging for debug logging only.
        try:
            42 / 0  # ZeroDivision error for example
        except Exception:
            logger.exception()  # log the exception only when DEBUG logging

        # Return the check result
        return result


if __name__ == "__main__":
    checks = (
        MyFirstCheck
    )

    # Initialize the probe with a name, version and checks
    probe = Probe("myProbe", __version__, checks)

    # Start the probe
    probe.start()

On CLose

Using the set_on_close() method, it is possible to configure a method which will be called before the probe is stopped. This can be useful in case you want to nicely close some connections.

async def custom_on_close():
    ...

probe = Probe("myProbe", '3.0.0', {})
probe.set_on_close(custom_on_close)

ASCII item names

InfraSonar requires each item to have a unique name property. The value for name must be a string with ASCII compatible character. When your name is not guaranteed to be ASCII compatible, the following code replaces the incompatible characters with question marks (?):

name = name.encode('ascii', errors='replace').decode()

Config

When using a password or secret within a config section, the library will encrypt the value so it will be unreadable by users. This must not be regarded as true encryption as the encryption key is publicly available.

Example yaml configuration:

exampleProbe:
  config:
    username: alice
    password: secret_password
  assets:
  - id: 123
    config:
      username: bob
      password: "my secret"
  - id: [456, 789]
    config:
      username: charlie
      password: "my other secret"
otherProbe:
  use: exampleProbe  # use the exampleProbe config for this probe

Dry run

Create a yaml file, for example (test.yaml):

asset:
  name: "foo.local"
  check: "system"
  config:
    address: "192.168.1.2"

Run the probe with the DRY_RUN environment variable set the the yaml file above.

DRY_RUN=test.yaml python main.py

Note: Optionally an asset id might be given which can by used to find asset configuration in the local asset configuration file. Asset config is also optional.

Dump to JSON

A dry run writes all log to stderr and only the JSON dump is written to stdout. Therefore, writing the output to JSON is easy:

DRY_RUN=test.yaml python main.py > dump.json

Metadata

Release files for libprobe 2.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for libprobe 2.0.6
File Size Uploaded
libprobe-2.0.6.tar.gz 31.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for libprobe 2.0.6
File Interpreter ABI Platform
libprobe-2.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 63.2 kB

Release files / libprobe-2.0.6.tar.gz

Download URL libprobe-2.0.6.tar.gz
Size 31.5 kB
Tags Source
SHA-256 checksum
How to use checksums
dd2edc46af040cc8691c77c2ac809985970b92a3a566956f07af2408adb34513
BLAKE2b-256 checksum
How to use checksums
c3d1bcc2978ac32e453433a8305eb52f666b1e2bce3ec5009bcdcaac9c4cc3b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / libprobe-2.0.6-py3-none-any.whl

Download URL libprobe-2.0.6-py3-none-any.whl
Size 31.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7269c80e7e86fd0148cd8820411f762c00b8c5411e2dd2f51b08eda568a38427
BLAKE2b-256 checksum
How to use checksums
fa6d8d342c8e537cacd7504e869561783d6b928e12a3c617662dc753c67db71e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release history Release notifications | RSS feed

This release

2.0.6 This release

2 release files

2.0.5

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

1 release file

2.0.1

1 release file

2.0.0

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.3.4

1 release file

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.36

1 release file

0.2.35

1 release file

0.2.34

1 release file

0.2.33

1 release file

0.2.32

1 release file

0.2.31

1 release file

0.2.30

1 release file

0.2.29

1 release file

0.2.28

1 release file

0.2.27

1 release file

0.2.26

1 release file

0.2.25

1 release file

0.2.24

1 release file

0.2.23

1 release file

0.2.22

1 release file

0.2.21

1 release file

0.2.20

1 release file

0.2.19

1 release file

0.2.18

1 release file

0.2.17

1 release file

0.2.16

1 release file

0.2.15

1 release file

0.2.14

1 release file

0.2.13

1 release file

0.2.12

1 release file

0.2.11

1 release file

0.2.10

1 release file

0.2.9

1 release file

0.2.8

1 release file

0.2.7

1 release file

0.2.6

1 release file

0.2.5

1 release file

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page