diff --git a/check-plugins/nextcloud-stats/README.md b/check-plugins/nextcloud-stats/README.md index a176a778..8f20b190 100644 --- a/check-plugins/nextcloud-stats/README.md +++ b/check-plugins/nextcloud-stats/README.md @@ -29,23 +29,26 @@ Tested with Nextcloud 15+. ```text usage: nextcloud-stats [-h] [-V] [--insecure] [--no-proxy] --password PASSWORD - [--timeout TIMEOUT] [--url URL] [--username USERNAME] + [--timeout TIMEOUT] [--failure-is-critical] [--url URL] + [--username USERNAME] This plugin lets you track the number of active users over time, the number of shares in various categories and some storage statistics against a Nextcloud server. options: - -h, --help show this help message and exit - -V, --version show program's version number and exit - --insecure This option explicitly allows to perform "insecure" SSL - connections. Default: False - --no-proxy Do not use a proxy. Default: False - --password PASSWORD Nextcloud API password. - --timeout TIMEOUT Network timeout in seconds. Default: 8 (seconds) - --url URL Nextcloud API URL. Default: http://localhost/nextcloud/ - ocs/v2.php/apps/serverinfo/api/v1/info - --username USERNAME Nextcloud API username. Default: admin + -h, --help show this help message and exit + -V, --version show program's version number and exit + --insecure This option explicitly allows to perform "insecure" SSL + connections. Default: False + --no-proxy Do not use a proxy. Default: False + --password PASSWORD Nextcloud API password. + --timeout TIMEOUT Network timeout in seconds. Default: 8 (seconds) + --failure-is-critical Return CRITICAL instead of UNKNOWN when any failure occurs + (timeout, connection error, etc.). Default: False + --url URL Nextcloud API URL. Default: http://localhost/nextcloud/ + ocs/v2.php/apps/serverinfo/api/v1/info + --username USERNAME Nextcloud API username. Default: admin ``` @@ -70,7 +73,9 @@ Output: ## States -* Always returns OK. +* Returns OK on success. +* Returns UNKNOWN on errors (such as connection failures, DNS resolution errors, authentication errors, and timeouts by default). +* Returns CRITICAL on any failure (timeout, connection error, etc.) when `--failure-is-critical` is used. ## Perfdata / Metrics diff --git a/check-plugins/nextcloud-stats/nextcloud-stats b/check-plugins/nextcloud-stats/nextcloud-stats index 2b66a224..ba4981e8 100755 --- a/check-plugins/nextcloud-stats/nextcloud-stats +++ b/check-plugins/nextcloud-stats/nextcloud-stats @@ -20,7 +20,7 @@ import lib.base # pylint: disable=C0413 import lib.human # pylint: disable=C0413 import lib.txt # pylint: disable=C0413 import lib.url # pylint: disable=C0413 -from lib.globals import STATE_OK, STATE_UNKNOWN # pylint: disable=C0413 +from lib.globals import STATE_OK, STATE_UNKNOWN, STATE_CRIT # pylint: disable=C0413 __author__ = 'Linuxfabrik GmbH, Zurich/Switzerland' __version__ = '2025100601' @@ -81,6 +81,15 @@ def parse_args(): default=DEFAULT_TIMEOUT, ) + parser.add_argument( + '--failure-is-critical', + help='Return CRITICAL instead of UNKNOWN when any failure occurs (timeout, connection error, etc.). ' + 'Default: %(default)s', + dest='FAILURE_IS_CRITICAL', + action='store_true', + default=False, + ) + parser.add_argument( '--url', help='Nextcloud API URL. ' @@ -125,15 +134,28 @@ def main(): 'OCS-APIRequest': 'true', } # and get the info from the API - jsonst = lib.base.coe( - lib.url.fetch( - url, - header=header, - insecure=args.INSECURE, - no_proxy=args.NO_PROXY, - timeout=args.TIMEOUT, - ) + fetch_result = lib.url.fetch( + url, + header=header, + insecure=args.INSECURE, + no_proxy=args.NO_PROXY, + timeout=args.TIMEOUT, ) + # lib.url.fetch returns (False, error_message) on error, or the content on success + if isinstance(fetch_result, tuple) and len(fetch_result) == 2 and not fetch_result[0]: + # This is an error + error_message = fetch_result[1] + if args.FAILURE_IS_CRITICAL: + lib.base.oao( + 'Connection failure occurred while fetching Nextcloud API data: {}'.format(error_message), + STATE_CRIT, + ) + return # Exit early, oao() should have already exited but just in case + # Let lib.base.coe() handle the error normally (will return UNKNOWN) + jsonst = lib.base.coe(fetch_result) + else: + # Success case + jsonst = lib.base.coe(fetch_result) try: result = json.loads(jsonst) except: