diff options
author | Igor Pashev <pashev.igor@gmail.com> | 2018-12-19 19:55:07 +0300 |
---|---|---|
committer | Igor Pashev <pashev.igor@gmail.com> | 2018-12-19 19:55:07 +0300 |
commit | 1c7dd274bfa6d8c436750f9d7797aaadca561324 (patch) | |
tree | 84f76c7599a335296363ee6f8579c1ba094f41b3 /pkgs | |
parent | 0e50bd3cf87814dfc6d168a361a6adc34b692bdb (diff) | |
download | nixsap-1c7dd274bfa6d8c436750f9d7797aaadca561324.tar.gz |
Add check_json
Diffstat (limited to 'pkgs')
-rwxr-xr-x | pkgs/check_json/check_json | 144 | ||||
-rw-r--r-- | pkgs/check_json/check_json.conf | 18 | ||||
-rw-r--r-- | pkgs/check_json/default.nix | 22 |
3 files changed, 184 insertions, 0 deletions
diff --git a/pkgs/check_json/check_json b/pkgs/check_json/check_json new file mode 100755 index 0000000..e5c542a --- /dev/null +++ b/pkgs/check_json/check_json @@ -0,0 +1,144 @@ +#!/usr/bin/env bash + +set -euo pipefail + +warn='' +crit='' +url='' +query='.' +title='' +netrc='' +name='value' +unit='' + +usage() { + cat <<USAGE + +$0: reduce JSON response to an integer, check against thresholds. + +This script uses: + jq - https://stedolan.github.io/jq/ + cURL - https://curl.haxx.se/ + +Usage: + $0 [options] + +Options: + + -q <expression> jq query, must return an integer (default: $query) + -u <url> URL to fetch + + -t <title> short description of the parameter + -p <name> name of the parameter (default: $name) + -m <unit> unit of measure (default: $unit) + + -n <file> netrc file with credentials + + -w <spec> warning threshold + -c <spec> critical threshold + +Examples: + + $0 -u example.com/errors_list -w 1 -c 4 -q '. | length' + $0 -u example.net/counter -w 1:4 -m c + +USAGE +} + +die () { + echo "$0: " "$@" >&2 + exit 1 +} + +while [ $# -gt 0 ]; do + case "$1" in + -q) query="$2"; shift 2;; + -u) url="$2"; shift 2;; + -t) title="$2"; shift 2;; + -p) name="$2"; shift 2;; + -m) unit="$2"; shift 2;; + -n) netrc="$2"; shift 2;; + -w) warn="$2"; shift 2;; + -c) crit="$2"; shift 2;; + -h|--help) usage; exit 1;; + *) die "unsupported argument: $1";; + esac +done + +inrange () { + local r v + local v1 v2 + local outter + local sIFS + + r="$1" + v="$2" + + case "$r" in + @*) outter=true; r="${r/@/}";; + *) outter=false;; + esac + + sIFS=$IFS + + IFS=: + set -- $r + v1=${1-} + v2=${2-} + IFS=$sIFS + + case "$v1" in + $r) v2=$v1; v1=0;; + ~*) v1=;; + esac + + if $outter; then + { [ -n "$v1" ] && [ "$v" -le "$v1" ]; } || { [ -n "$v2" ] && [ "$v" -ge "$v2" ]; } + else + { [ -z "$v1" ] || [ "$v" -ge "$v1" ]; } && { [ -z "$v2" ] || [ "$v" -le "$v2" ]; } + fi +} + +[ -n "$url" ] || die "missing url" + +cmd=(curl --fail --silent --show-error --location) +if [ -n "$netrc" ]; then + cmd+=(--netrc-file "$netrc") +fi +cmd+=("$url") + +if ! out=$("${cmd[@]}" 2>&1); then + printf 'UNKNOWN: %s\n' "$(echo "$out" | grep . | head -n 1)" + exit 3 +fi + +json=$out + +if ! value=$(echo "$json" | jq -c -r "$query" 2>&1); then + printf 'UNKNOWN: %s\n' "$(echo "$value" | grep . | head -n 1)" + exit 3 +fi + +if [[ ! "$value" =~ ^[-+]?[0-9]+$ ]]; then + echo "UNKNOWN: not an integer: $value" + exit 3 +fi + +stat="$name=$value$unit;$warn;$crit" + +[ -n "$title" ] || title="some $name" +text="$value - $title|$stat" + +if [ -n "$crit" ] && ! inrange "$crit" "$value"; then + echo "CRITICAL: $text" + exit 2 +fi + +if [ -n "$warn" ] && ! inrange "$warn" "$value"; then + echo "WARNING: $text" + exit 1 +fi + +echo "OK: $text" +exit 0 + diff --git a/pkgs/check_json/check_json.conf b/pkgs/check_json/check_json.conf new file mode 100644 index 0000000..8ee9e15 --- /dev/null +++ b/pkgs/check_json/check_json.conf @@ -0,0 +1,18 @@ +object CheckCommand "http_json" { + import "plugin-check-command" + + command = [ "check_json" ] + + arguments = { + "-q" = "$http_json_query$" + "-u" = "$http_json_url$" + "-t" = "$http_json_title$" + "-p" = "$http_json_name$" + "-m" = "$http_json_unit$" + "-n" = "$http_json_netrc$" + "-w" = "$http_json_warn$" + "-c" = "$http_json_crit$" + } + vars.http_json_title = "$service.name$" +} + diff --git a/pkgs/check_json/default.nix b/pkgs/check_json/default.nix new file mode 100644 index 0000000..eb9ceed --- /dev/null +++ b/pkgs/check_json/default.nix @@ -0,0 +1,22 @@ +{ stdenv, pkgs, makeWrapper }: + +stdenv.mkDerivation { + name = "check_json"; + outputs = [ "out" "conf" ]; + unpackPhase = ":"; + nativeBuildInputs = [ makeWrapper ]; + installPhase = '' + mkdir -p $out/bin + + cp ${./check_json} $out/bin/check_json + cp ${./check_json.conf} $conf + + chmod +x "$out/bin/"* + + substituteInPlace "$conf" \ + --replace check_json "$out/bin/check_json" + + wrapProgram "$out/bin/check_json" \ + --prefix PATH : "${pkgs.curl.bin}/bin:${pkgs.gnugrep}/bin:${pkgs.jq}/bin" + ''; +} |