1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-22 02:51:03 +00:00

[isp/internode_usage] Code cleanup

Also make the daily graph more flexible, by always reporting the second
last value (presumably the previous, but sometimes the API lags a bit,
and actively requesting by date result in never reporting some late
data).

Signed-off-by: Olivier Mehani <shtrom@ssji.net>
This commit is contained in:
Olivier Mehani 2019-09-12 23:10:25 +10:00 committed by Lars Kruse
parent 3aa213d497
commit c49207419e

View file

@ -1,4 +1,4 @@
#!/bin/sh -u #!/bin/sh -eu
# -*- sh -*- # -*- sh -*-
: << =cut : << =cut
@ -45,7 +45,9 @@ SPDX-License-Identifier: GPL-3.0-or-later
# shellcheck disable=SC1090 # shellcheck disable=SC1090
. "${MUNIN_LIBDIR:-.}/plugins/plugin.sh" . "${MUNIN_LIBDIR:-.}/plugins/plugin.sh"
CURL_ARGS='-s'
if [ "${MUNIN_DEBUG:-0}" = 1 ]; then if [ "${MUNIN_DEBUG:-0}" = 1 ]; then
CURL_ARGS='-v'
set -x set -x
fi fi
@ -67,47 +69,73 @@ if [ -z "${internode_api_url:-}" ]; then
fi fi
xpath_extract() { xpath_extract() {
xpath="$1" # shellcheck disable=SC2039
xpath -q -n -e "${xpath}" | \ local xpath="$1"
sed 's/<\([^>]*\)>\([^<]*\)<[^>]*>/\2/' # shellcheck disable=SC2039
local node="$(xpath -q -n -e "${xpath}")" \
|| { echo "error extracting ${xpath}" >&2; false; }
echo "${node}" | sed 's/<\([^>]*\)>\([^<]*\)<[^>]*>/\2/;s^N/A^U^'
} }
xpath_extract_attribute() { xpath_extract_attribute() {
xpath="$1" # shellcheck disable=SC2039
xpath -q -n -e "${xpath}" | \ local xpath="$1"
sed 's/.*="\([^"]\+\)".*/\1/' # shellcheck disable=SC2039
local node="$(xpath -q -n -e "${xpath}")" \
|| { echo "error extracting attribute at ${xpath}" >&2; false; }
echo "${node}" | sed 's/.*="\([^"]\+\)".*/\1/'
} }
fetch() { fetch() {
# shellcheck disable=SC2154 # shellcheck disable=SC2154
curl -u "${internode_api_login}:${internode_api_password}" -s "$@" curl -u "${internode_api_login}:${internode_api_password}" -f ${CURL_ARGS} "$@" \
|| { echo "error fetching ${*} for user ${internode_api_login}" >&2; false; }
} }
get_data() { get_data() {
SERVICE_XML="$(fetch "${internode_api_url}/${SERVICE_ID}/service")" SERVICE_USERNAME='n/a'
SERVICE_USERNAME="$(echo "${SERVICE_XML}" | xpath_extract "internode/api/service/username")" SERVICE_QUOTA='n/a'
SERVICE_QUOTA="$(echo "${SERVICE_XML}" | xpath_extract "internode/api/service/quota")" SERVICE_PLAN='n/a'
SERVICE_PLAN="$(echo "${SERVICE_XML}" | xpath_extract "internode/api/service/plan")" SERVICE_ROLLOVER='n/a'
SERVICE_ROLLOVER="$(echo "${SERVICE_XML}" | xpath_extract "internode/api/service/rollover")" IDEAL_USAGE=''
SERVICE_INTERVAL="$(echo "${SERVICE_XML}" | xpath_extract "internode/api/service/plan-interval" | sed 's/ly$//')" USAGE_CRITICAL=''
SERVICE_XML="$(fetch "${internode_api_url}/${SERVICE_ID}/service" \
|| true)"
if [ -n "${SERVICE_XML}" ]; then
SERVICE_USERNAME="$(echo "${SERVICE_XML}" | xpath_extract "internode/api/service/username")"
SERVICE_QUOTA="$(echo "${SERVICE_XML}" | xpath_extract "internode/api/service/quota")"
SERVICE_PLAN="$(echo "${SERVICE_XML}" | xpath_extract "internode/api/service/plan")"
SERVICE_ROLLOVER="$(echo "${SERVICE_XML}" | xpath_extract "internode/api/service/rollover")"
SERVICE_INTERVAL="$(echo "${SERVICE_XML}" | xpath_extract "internode/api/service/plan-interval" | sed 's/ly$//')"
HISTORY_XML="$(fetch "${internode_api_url}/${SERVICE_ID}/history")" TODAY="$(date +%s)"
TODAY_TIMESTAMP="$(date +%s)" FIRST_DAY="$(date +%s --date "${SERVICE_ROLLOVER} -1 ${SERVICE_INTERVAL}")"
DAILY_DATE="$(date +%Y-%m-%d -d yesterday)" LAST_DAY="$(date +%s --date "${SERVICE_ROLLOVER}")"
DAILY_TIMESTAMP="$(date -d "${DAILY_DATE} $(date +%H:%M:%S)" +%s)" BILLING_PERIOD="(${LAST_DAY}-${FIRST_DAY})"
DAILY_USAGE="$(echo "${HISTORY_XML}" | xpath_extract "internode/api/usagelist/usage[@day=\"${DAILY_DATE}\"]/traffic")" IDEAL_USAGE="$(echo "${SERVICE_QUOTA}-(${SERVICE_QUOTA}*(${LAST_DAY}-${TODAY})/${BILLING_PERIOD})" | bc -ql)"
USAGE_XML="$(fetch "${internode_api_url}/${SERVICE_ID}/usage")" USAGE_CRITICAL="${SERVICE_QUOTA}"
SERVICE_USAGE="$(echo "${USAGE_XML}" | xpath_extract "internode/api/traffic")" fi
USAGE_CRITICAL="${SERVICE_QUOTA}" DAILY_TIMESTAMP=N
USAGE_WARNING="$(echo "${SERVICE_QUOTA}*.75" | bc -ql)" DAILY_USAGE=U
HISTORY_XML="$(fetch "${internode_api_url}/${SERVICE_ID}/history" \
|| true)"
if [ -n "${HISTORY_XML}" ]; then
DAILY_USAGE="$(echo "${HISTORY_XML}" | xpath_extract "internode/api/usagelist/usage[last()-1]/traffic")"
DAILY_DATE="$(echo "${HISTORY_XML}" | xpath_extract_attribute "internode/api/usagelist/usage[last()-1]/@day")"
DAILY_TIMESTAMP="$(date -d "${DAILY_DATE} $(date +%H:%M:%S)" +%s \
|| echo N)"
fi
TODAY="$(date +%s)" SERVICE_USAGE='U'
FIRST_DAY="$(date +%s --date "${SERVICE_ROLLOVER} -1 ${SERVICE_INTERVAL}")" USAGE_XML="$(fetch "${internode_api_url}/${SERVICE_ID}/usage" \
LAST_DAY="$(date +%s --date "${SERVICE_ROLLOVER}")" || true)"
BILLING_PERIOD="(${LAST_DAY}-${FIRST_DAY})" if [ -n "${USAGE_XML}" ]; then
IDEAL_USAGE="$(echo "${SERVICE_QUOTA}-(${SERVICE_QUOTA}*(${LAST_DAY}-${TODAY})/${BILLING_PERIOD})" | bc -ql)" SERVICE_USAGE="$(echo "${USAGE_XML}" | xpath_extract "internode/api/traffic")"
fi
} }
graph_config() { graph_config() {
@ -178,14 +206,14 @@ graph_data() {
echo "multigraph internode_usage_${SERVICE_ID}${graph}" echo "multigraph internode_usage_${SERVICE_ID}${graph}"
case "${graph}" in case "${graph}" in
.current) .current)
echo "hourly_rate.value ${TODAY_TIMESTAMP}:${SERVICE_USAGE:-U}" echo "hourly_rate.value ${SERVICE_USAGE:-U}"
;; ;;
.daily) .daily)
echo "daily_rate.value ${DAILY_TIMESTAMP}:${DAILY_USAGE:-U}" echo "daily_rate.value ${DAILY_TIMESTAMP}:${DAILY_USAGE:-U}"
;; ;;
*) *)
echo "usage.value ${TODAY_TIMESTAMP}:${SERVICE_USAGE:-U}" echo "usage.value ${SERVICE_USAGE:-U}"
echo "ideal.value ${TODAY_TIMESTAMP}:${IDEAL_USAGE:-U}" echo "ideal.value ${IDEAL_USAGE:-U}"
;; ;;
esac esac
echo echo
@ -214,11 +242,16 @@ main() {
# Determine the service ID from the name of the symlink # Determine the service ID from the name of the symlink
SERVICE_ID="$(echo "${0}" | sed -n 's/^.*internode_usage_//p')" SERVICE_ID="$(echo "${0}" | sed -n 's/^.*internode_usage_//p')"
if [ -z "${SERVICE_ID}" ]; then if [ -z "${SERVICE_ID}" ]; then
API_XML="$(fetch "${internode_api_url}")"
# Otherwise, get the first service in the list # Otherwise, get the first service in the list
API_XML="$(fetch "${internode_api_url}" \
|| true)"
if [ -z "${API_XML}" ]; then
echo "unable to determine service ID for user ${internode_api_login}" >&2
exit 1
fi
SERVICE_ID="$(echo "${API_XML}" | xpath_extract "internode/api/services/service")" SERVICE_ID="$(echo "${API_XML}" | xpath_extract "internode/api/services/service")"
fi fi
get_data get_data
main ${1:-} main "${1:-}"