Skip to content

Commit fcd3a21

Browse files
committed
feat(json-values): add bearer token auth, custom headers, and per-key thresholds
* --token adds HTTP bearer authentication * --header (curl-style "Name: Value", repeatable) sends arbitrary custom headers * --warning-key/--warning and --critical-key/--critical alert on a numeric value at a specific JSON key with Nagios range syntax; the overall state is the worst of the JSON state and the two key-level thresholds * all --*-key parameters now support dot-notation for nested keys (e.g. --state-key=meta.state, --critical-key=detailedInfo.count1) * empty --state-key is treated as an explicit opt-out so threshold-only monitoring of schema-free JSON responses no longer collapses to UNKNOWN * add unit tests covering flat/nested fixtures, state coercion edge cases, and the threshold semantics, with a fixture copied verbatim from the issue body Closes #1005
1 parent 9302783 commit fcd3a21

File tree

16 files changed

+858
-77
lines changed

16 files changed

+858
-77
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Monitoring Plugins:
3636
* infomaniak-swiss-backup-devices: add `--ignore-customer`, `--ignore-name`, `--ignore-tag`, `--ignore-user` parameters to skip devices by regex
3737
* infomaniak-swiss-backup-products: add `--ignore-customer`, `--ignore-tag` parameters to skip products by regex
3838
* ipmi-sel: add `--ignore` parameter to filter out SEL entries by regex, e.g. auto-generated "Log area reset/cleared" entries after `ipmitool sel clear` ([#982](https://github.com/Linuxfabrik/monitoring-plugins/issues/982))
39+
* json-values: add `--token` and `--header` parameters for HTTP bearer-token and custom header authentication when fetching JSON from protected endpoints, add `--warning-key` / `--warning` and `--critical-key` / `--critical` to alert on a numeric value at a specific JSON key (with Nagios range syntax), and support dot-notation for nested keys in all `--*-key` parameters (e.g. `--state-key=meta.state`) ([#1005](https://github.com/Linuxfabrik/monitoring-plugins/issues/1005))
3940
* librenms-alerts: add device-type `management`
4041
* librenms-health: add device-type `management`
4142
* nextcloud-enterprise: provides information about an installed Nextcloud Enterprise subscription

check-plugins/json-values/README.md

Lines changed: 92 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ Parses a JSON object from a local file, HTTP/HTTPS URL, or SMB share and extract
1111
* Extracts the output message from the key specified by `--message-key` (default: `message`)
1212
* Extracts the perfdata string from the key specified by `--perfdata-key` (default: `perfdata`)
1313
* Extracts the return state from the key specified by `--state-key` (default: `state`)
14+
* All `--*-key` parameters support dot-notation for nested keys (e.g. `--state-key=meta.state`)
15+
* HTTP endpoints can be authenticated with a bearer token (`--token`) and/or custom request headers (`--header`, curl-style `"Name: Value"`, can be specified multiple times)
16+
* In addition to the state extracted from the JSON itself, the value at a specific JSON key can be alerted on via `--warning-key`/`--warning` and `--critical-key`/`--critical` (Nagios range syntax); the value at the referenced key must be numeric, otherwise the check returns UNKNOWN. The overall check state is the worst of the JSON state and the two key-level thresholds.
1417

1518

1619
## Fact Sheet
@@ -29,41 +32,90 @@ Parses a JSON object from a local file, HTTP/HTTPS URL, or SMB share and extract
2932
## Help
3033

3134
```text
32-
usage: json-values [-h] [-V] [--always-ok] [--filename FILENAME] [--insecure]
33-
[--message-key MESSAGE_KEY] [--no-proxy]
34-
[--password PASSWORD] [--perfdata-key PERFDATA_KEY]
35-
[--state-key STATE_KEY] [--timeout TIMEOUT] [-u URL]
36-
[--username USERNAME]
37-
38-
Parses a JSON array from a file, URL, or SMB share and extracts message,
39-
state, and perfdata values from specific keys. Useful for integrating custom
40-
applications or APIs that provide monitoring data in JSON format. Alerts based
41-
on the state value extracted from the JSON data.
35+
usage: json-values [-h] [-V] [--always-ok] [-c CRIT]
36+
[--critical-key CRITICAL_KEY] [--filename FILENAME]
37+
[--header HEADER] [--insecure] [--message-key MESSAGE_KEY]
38+
[--no-proxy] [--password PASSWORD]
39+
[--perfdata-key PERFDATA_KEY] [--state-key STATE_KEY]
40+
[--timeout TIMEOUT] [--token TOKEN] [-u URL]
41+
[--username USERNAME] [-w WARN] [--warning-key WARNING_KEY]
42+
43+
Parses a JSON object from a file, URL, or SMB share and extracts message,
44+
state, and perfdata values from configurable keys. Useful for integrating
45+
custom applications or APIs that expose monitoring data as JSON. Supports HTTP
46+
bearer-token and arbitrary header authentication, dot-notation for nested JSON
47+
keys, and per-key Nagios-range thresholds in addition to the state value
48+
extracted from the JSON itself.
4249
4350
options:
4451
-h, --help show this help message and exit
4552
-V, --version show program's version number and exit
4653
--always-ok Always returns OK.
54+
-c, --critical CRIT Nagios range expression evaluated for the CRIT state
55+
against the value of the JSON key referenced by
56+
--critical-key. Example: `--critical-key=days
57+
--critical=1`. Example: `--critical-
58+
key=detailedInfo.count1 --critical=@90:100`. Default:
59+
None
60+
--critical-key CRITICAL_KEY
61+
Name of the JSON key whose value should be evaluated
62+
against --critical for the CRIT state. Supports dot-
63+
notation for nested keys (e.g. `detailedInfo.count1`).
64+
The value at the resolved key must be numeric in the
65+
JSON, otherwise the check returns UNKNOWN. Example:
66+
`--critical-key=days`. Example: `--critical-
67+
key=detailedInfo.count1`. Default: None
4768
--filename FILENAME Path to a local JSON file. Mutually exclusive with -u
4869
/ --url.
70+
--header HEADER Custom HTTP request header to send when fetching the
71+
URL. curl-style format `"Name: Value"`. Can be
72+
specified multiple times. Example: `--header="X-API-
73+
Key: linuxfabrik"`. Example: `--header="Accept:
74+
application/json"`. Default: None
4975
--insecure This option explicitly allows insecure SSL
5076
connections.
5177
--message-key MESSAGE_KEY
52-
Name of the JSON array key containing the output
53-
message. Default: message
78+
Name of the JSON key containing the output message.
79+
Supports dot-notation for nested keys (e.g.
80+
`meta.message`). Example: `--message-
81+
key=meta.message`. Default: message
5482
--no-proxy Do not use a proxy.
5583
--password PASSWORD Password for SMB authentication.
5684
--perfdata-key PERFDATA_KEY
57-
Name of the JSON array key containing the perfdata.
58-
Default: perfdata
85+
Name of the JSON key containing the perfdata. Supports
86+
dot-notation for nested keys (e.g. `meta.perfdata`).
87+
Example: `--perfdata-key=meta.perfdata`. Default:
88+
perfdata
5989
--state-key STATE_KEY
60-
Name of the JSON array key containing the state.
61-
Default: state
90+
Name of the JSON key containing the state. Supports
91+
dot-notation for nested keys (e.g. `meta.state`).
92+
Example: `--state-key=meta.state`. Default: state
6293
--timeout TIMEOUT Network timeout in seconds. Default: 3 (seconds)
94+
--token TOKEN HTTP bearer token. Adds an `Authorization: Bearer
95+
<token>` header to the URL request. Can be combined
96+
with --header to send additional custom headers (e.g.
97+
`--token=linuxfabrik --header="Accept:
98+
application/json"`); a manual
99+
`--header="Authorization: ..."` is overridden by
100+
--token if both are specified. Default: None
63101
-u, --url URL URL of the JSON file, starting with "http://",
64102
"https://", or "smb://". Mutually exclusive with
65103
--filename.
66104
--username USERNAME Username for SMB authentication.
105+
-w, --warning WARN Nagios range expression evaluated for the WARN state
106+
against the value of the JSON key referenced by
107+
--warning-key. Example: `--warning-key=days
108+
--warning=7`. Example: `--warning-
109+
key=detailedInfo.count1 --warning=@80:90`. Default:
110+
None
111+
--warning-key WARNING_KEY
112+
Name of the JSON key whose value should be evaluated
113+
against --warning for the WARN state. Supports dot-
114+
notation for nested keys (e.g. `detailedInfo.count1`).
115+
The value at the resolved key must be numeric in the
116+
JSON, otherwise the check returns UNKNOWN. Example:
117+
`--warning-key=days`. Example: `--warning-
118+
key=detailedInfo.count1`. Default: None
67119
```
68120

69121

@@ -90,11 +142,33 @@ Output:
90142
This is a test message|'cpu-usage'=5.6%;80;90;0;100
91143
```
92144

145+
Fetching from a protected HTTP endpoint with a bearer token and an additional `Accept` header, and reading a nested state key:
146+
147+
```bash
148+
./json-values \
149+
--url=https://api.example.com/monitoring \
150+
--token=linuxfabrik \
151+
--header='Accept: application/json' \
152+
--state-key=meta.state \
153+
--message-key=meta.message \
154+
--perfdata-key=meta.perfdata
155+
```
156+
157+
Alerting on a numeric value inside a nested JSON key with Nagios range expressions (`@80:90` means alert when inside the range, inclusive):
158+
159+
```bash
160+
./json-values \
161+
--filename=/tmp/example.json \
162+
--warning-key=detailedInfo.count1 --warning=@80:90 \
163+
--critical-key=detailedInfo.count1 --critical=@90:100
164+
```
165+
93166

94167
## States
95168

96-
* Returns the state value from the JSON object (0=OK, 1=WARN, 2=CRIT, 3=UNKNOWN).
97-
* UNKNOWN if `--filename` and `--url` are used together, if the URL protocol is unsupported, if JSON parsing fails, or if the state key is missing.
169+
* Returns the worst of three states: the state extracted from the JSON object (0=OK, 1=WARN, 2=CRIT, 3=UNKNOWN), the state computed from `--warning-key` / `--warning`, and the state computed from `--critical-key` / `--critical`.
170+
* UNKNOWN if `--filename` and `--url` are used together, if the URL protocol is unsupported, if JSON parsing fails, if the state key is missing or does not resolve to an integer in the range `0..3`, or if a threshold key cannot be resolved or is not numeric.
171+
* `--warning` / `--warning-key` and `--critical` / `--critical-key` must be specified together (range without a key has nothing to compare, key without a range would silently never alert).
98172
* `--always-ok` suppresses all alerts and always returns OK.
99173

100174

0 commit comments

Comments
 (0)