Skip to content

Commit

Permalink
Rework project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Skeen committed Feb 24, 2020
1 parent 609225c commit 21148df
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Build shell script for release
run: |
mkdir build
bash compile.sh > build/output.sh
bash tools/compile.sh > build/output.sh
chmod +x build/output.sh
- name: Create Release
id: create_release
Expand Down
50 changes: 3 additions & 47 deletions check_prometheus_metric.sh → src/check_prometheus_metric.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
#
# check_prometheus_metric.sh - Nagios plugin wrapper for checking Prometheus
# metrics. Requires curl and jq to be in $PATH.
source preamble.bash
source parse.bash
source usage.bash

# Avoid locale complications:
export LC_ALL=C
# Main

# Default configuration:
CURL_OPTS=()
Expand Down Expand Up @@ -42,51 +43,6 @@ function check_dependencies() {
fi
}

function usage() {

cat <<'EoL'
check_prometheus_metric.sh - Nagios plugin wrapper for checking Prometheus
metrics. Requires curl and jq to be in $PATH.
Usage:
check_prometheus_metric.sh -H HOST -q QUERY -w FLOAT[:FLOAT] -c FLOAT[:FLOAT] -n NAME [-m METHOD] [-O] [-i] [-p] [-t QUERY_TYPE]
Options:
-H HOST URL of Prometheus host to query.
-q QUERY Prometheus query, in single quotes, that returns by default a float or int (see -t).
-w FLOAT[:FLOAT] Warning level value (must be a float or nagios-interval).
-c FLOAT[:FLOAT] Critical level value (must be a float or nagios-interval).
-n NAME A name for the metric being checked.
-m METHOD Comparison method, one of gt, ge, lt, le, eq, ne.
(Defaults to ge unless otherwise specified.)
-C CURL_OPTS Additional flags to pass to curl.
Can be passed multiple times. Options and option values must be passed separately.
e.g. -C --conect-timetout -C 10 -C --cacert -C /path/to/ca.crt
-O Accept NaN as an "OK" result .
-i Print the extra metric information into the Nagios message.
-p Add perfdata to check output.
Examples:
check_prometheus_metric -q 'up{job=\"job_name\"}' :1 -c :1 # Check that job is up.
check_prometheus_metric -q 'node_load1' -w :0.05 -c :0.1 # Check load is OK.
# Aka. that load is below 0.05 and 0.1 respectively.
check_prometheus_metric -q 'go_threads' -w 15:25 -c : # Check thread count is OK.
# Aka. OK if we have 15-25 threads, outside of this; warning, never critical.
EoL
}

function is_float_or_interval() {
if is_float "${1}" || is_interval "${1}"; then
return 0
fi
return 1
}


function process_command_line {

while getopts ':H:q:w:c:m:n:C:Oipt:' OPT "$@"
Expand Down
7 changes: 7 additions & 0 deletions parse.bash → src/parse.bash
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ function is_interval() {
return ${_IS_INTERVAL}
}

function is_float_or_interval() {
if is_float "${1}" || is_interval "${1}"; then
return 0
fi
return 1
}

function decode_range() {
# Decode Nagios Threshold format string.
#
Expand Down
2 changes: 2 additions & 0 deletions src/preamble.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Avoid locale complications:
export LC_ALL=C
36 changes: 36 additions & 0 deletions src/usage.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function usage() {

cat <<'EoL'
check_prometheus_metric.sh - Nagios plugin wrapper for checking Prometheus
metrics. Requires curl and jq to be in $PATH.
Usage:
check_prometheus_metric.sh -H HOST -q QUERY -w FLOAT[:FLOAT] -c FLOAT[:FLOAT] -n NAME [-m METHOD] [-O] [-i] [-p] [-t QUERY_TYPE]
Options:
-H HOST URL of Prometheus host to query.
-q QUERY Prometheus query, in single quotes, that returns by default a float or int (see -t).
-w FLOAT[:FLOAT] Warning level value (must be a float or nagios-interval).
-c FLOAT[:FLOAT] Critical level value (must be a float or nagios-interval).
-n NAME A name for the metric being checked.
-m METHOD Comparison method, one of gt, ge, lt, le, eq, ne.
(Defaults to ge unless otherwise specified.)
-C CURL_OPTS Additional flags to pass to curl.
Can be passed multiple times. Options and option values must be passed separately.
e.g. -C --conect-timetout -C 10 -C --cacert -C /path/to/ca.crt
-O Accept NaN as an "OK" result .
-i Print the extra metric information into the Nagios message.
-p Add perfdata to check output.
Examples:
check_prometheus_metric -q 'up{job=\"job_name\"}' :1 -c :1 # Check that job is up.
check_prometheus_metric -q 'node_load1' -w :0.05 -c :0.1 # Check load is OK.
# Aka. that load is below 0.05 and 0.1 respectively.
check_prometheus_metric -q 'go_threads' -w 15:25 -c : # Check thread count is OK.
# Aka. OK if we have 15-25 threads, outside of this; warning, never critical.
EoL
}
5 changes: 4 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/bin/bash

PLUGIN_SCRIPT=check_prometheus_metric.sh
NETWORK_NAME=nagios_plugins_testnetwork
PROMETHEUS_NAME=nagios_plugins_prometheus
PROMETHEUS_PORT=8090
Expand Down Expand Up @@ -63,6 +62,10 @@ echo "Waiting until prometheus sees pushed metric"
wait_for_metric "scalar(pi)" ${PROMETHEUS_PORT}
echo ""

mkdir build/
bash tools/compile.sh > build/output.sh
PLUGIN_SCRIPT=build/output.sh

echo "
PLUGIN_SCRIPT=${PLUGIN_SCRIPT}
PROMETHEUS_PORT=${PROMETHEUS_PORT}
Expand Down
2 changes: 1 addition & 1 deletion tests/decode_range.bats
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bats

load ../parse
load ../src/parse

@test "--- ${BATS_TEST_FILENAME} ---" {
true
Expand Down
2 changes: 1 addition & 1 deletion tests/is_float.bats
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bats

load ../parse
load ../src/parse

@test "--- ${BATS_TEST_FILENAME} ---" {
true
Expand Down
2 changes: 1 addition & 1 deletion tests/is_interval.bats
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bats

load ../parse
load ../src/parse

@test "--- ${BATS_TEST_FILENAME} ---" {
true
Expand Down
2 changes: 1 addition & 1 deletion tests/test_config.bash
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

PLUGIN_SCRIPT=check_prometheus_metric.sh
PLUGIN_SCRIPT=build/output.sh
PROMETHEUS_PORT=8090
PUSHGATEWAY_PORT=8091

8 changes: 6 additions & 2 deletions compile.sh → tools/compile.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#!/bin/bash
cd src
MAIN=check_prometheus_metric.sh

# Find all source lines
SOURCE_LINES=$(grep "source .*" check_prometheus_metric.sh)
SOURCE_LINES=$(grep "source .*" ${MAIN})

# Process the main script, by replacing source lines with their content
OUTPUT=$(cat check_prometheus_metric.sh)
OUTPUT=$(cat ${MAIN})
while IFS= read -r LINE; do
FILENAME=$(echo "${LINE}" | cut -f2 -d" ")
OUTPUT=$(echo "${OUTPUT}" | sed -e "s/source ${FILENAME}/\n# Included: ${FILENAME}\nsource ${FILENAME}/g")
OUTPUT=$(echo "${OUTPUT}" | sed -e "/source ${FILENAME}/{ r ${FILENAME}" -e "d}")
done <<< "${SOURCE_LINES}"

Expand Down

0 comments on commit 21148df

Please sign in to comment.