diff --git a/CHANGELOG.md b/CHANGELOG.md index fad6f94..d046883 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - πŸ’„ β€” Improve email and gotify notifications - ✨ β€” Add command to test gotify configuration +- ✨ β€” Add nagios command to use as a Nagios probe ## 0.3.1 diff --git a/argos/commands.py b/argos/commands.py index ffd69f7..31af2b9 100644 --- a/argos/commands.py +++ b/argos/commands.py @@ -678,5 +678,58 @@ async def test_gotify(config, domain, severity): ) +@server.command(short_help="Nagios compatible severities report") +@click.option( + "--config", + default="argos-config.yaml", + help="Path of the configuration file. " + "If ARGOS_YAML_FILE environment variable is set, its value will be used instead.", + envvar="ARGOS_YAML_FILE", + callback=validate_config_access, +) +@coroutine +async def nagios(config): + """Output a report of current severities suitable for Nagios + with a Nagios compatible exit code""" + os.environ["ARGOS_YAML_FILE"] = config + + # The imports are made here otherwise the agent will need server configuration files. + from argos.server import queries + + exit_nb = 0 + db = await get_db() + severities = await queries.get_severity_counts(db) + + if severities["warning"] != 0: + exit_nb = 1 + if severities["critical"] != 0: + exit_nb = 2 + if severities["unknown"] != 0: + exit_nb = 2 + + stats = ( + f"ok={severities['ok']}; warning={severities['warning']}; " + f"critical={severities['critical']}; unknown={severities['unknown']};" + ) + + if exit_nb == 0: + print("OK β€” All sites are ok|{stats}") + elif exit_nb == 1: + print(f"WARNING β€” {severities['warning']} sites are in warning state|{stats}") + elif severities["critical"] == 0: + print(f"UNKNOWN β€” {severities['unknown']} sites are in unknown state|{stats}") + elif severities["unknown"] == 0: + print( + f"CRITICAL β€” {severities['critical']} sites are in critical state|{stats}" + ) + else: + print( + f"CRITICAL/UNKNOWN β€” {severities['critical']} sites are in critical state " + f"and {severities['unknown']} sites are in unknown state|{stats}" + ) + + sysexit(exit_nb) + + if __name__ == "__main__": cli() diff --git a/docs/cli.md b/docs/cli.md index 30cb428..58b4610 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -83,6 +83,7 @@ Commands: generate-config Output a self-documented example config file. generate-token Generate a token for agents migrate Run database migrations + nagios Nagios compatible severities report reload-config Load or reload tasks’ configuration start Starts the server (use only for testing or development!) test-gotify Send a test gotify notification @@ -468,9 +469,33 @@ Options: +#### Use as a nagios probe + +You can directly use Argos to get an output and an exit code usable with Nagios. + + + +```man +Usage: argos server nagios [OPTIONS] + + Output a report of current severities suitable for Nagios with a Nagios + compatible exit code + +Options: + --config TEXT Path of the configuration file. If ARGOS_YAML_FILE environment + variable is set, its value will be used instead. + --help Show this message and exit. +``` + + + #### Test the email settings -You can verify that your mail settings are ok by sending a test email +You can verify that your mail settings are ok by sending a test email.