1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-21 18:41:03 +00:00

Merge pull request #1117 from shtrom/nextcloud_fixups

various fixups of the Nextcloud plugin
This commit is contained in:
Lars Kruse 2020-09-13 20:48:50 +02:00 committed by GitHub
commit 11861370c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,5 @@
#!/bin/sh
# shellcheck shell=dash
set -e
@ -28,6 +29,10 @@ Set username and password in your munin-node configuration
[nextcloud_cloud.domain.tld]
env.username <nexcloud_user>
env.password <nextcloud_password>
env.api_path <default: /ocs/v2.php/apps/serverinfo/api/v1/info>
env.scheme <default: https>
env.timeout <default: 2s>
env.updates_warning <default: 1>
It's advised to set an app password (for this plugin) in your nextcloud
instance and not to use the "real" password of your nextcloud user.
@ -47,44 +52,54 @@ GPLv2
=cut
# shellcheck disable=SC1090
. "$MUNIN_LIBDIR/plugins/plugin.sh"
API_PATH="/ocs/v2.php/apps/serverinfo/api/v1/info?format=json"
if [ "${MUNIN_DEBUG:-0}" = 1 ]; then
set -x
fi
API_PATH="${api_path:-/ocs/v2.php/apps/serverinfo/api/v1/info}?format=json"
DOMAIN="${0##*nextcloud_}"
SCHEME="${scheme:-https}://"
TIMEOUT="${timeout:-2}"
UPDATES_WARNING="${updates_warning:-1}"
CLEANDOMAIN="$(clean_fieldname "$DOMAIN")"
USERNAME="${username:-}"
PASSWORD="${password:-}"
print_json_data() {
local FIRST="$1"
[ -z "$FIRST" ] && exit 0
local VALUE="U"
shift 1
test -z "$FIRST" && echo "missing data for 'print_json_data "" $*'">&2
for KEY in "$@"; do
VALUE=$(echo "$FIRST" | jq -cr ".$KEY")
if [ -n "$FIRST" ]; then
VALUE=$(echo "$FIRST" | jq -cr ".$KEY")
fi
echo "$KEY.value $VALUE"
done
}
test_https() {
[ -z "$DOMAIN" ] && exit 0
curl -s -f -m 2 -I "https://$DOMAIN" > /dev/null && echo true && exit 0
fetch_url () {
curl -s -f -m "${TIMEOUT}" "$@"
}
case $1 in
autoconf)
if [ -x /usr/bin/curl ]; then
if [ -x /usr/bin/jq ]; then
[ "$(test_https)" ] && DOMAIN="https://$DOMAIN" || DOMAIN="http://$DOMAIN"
curl -s -f -m 2 -u "$USERNAME:$PASSWORD" -I "$DOMAIN$API_PATH" | grep -iq "Content-Type: application/json" && echo "yes" && exit 0 || echo "no (invalid or empty response from nextlcoud serverinfo api)" && exit 0
else
echo "no (jq not found)" && exit 0
fi
else
echo "no (/usr/bin/curl not found)" && exit 0
fi
;;
if [ ! -x "$(command -v curl)" ]; then
echo "no (curl not found)"
elif [ ! -x "$(command -v jq)" ]; then
echo "no (jq not found)"exit 0
else
fetch_url -I -u "$USERNAME:$PASSWORD" -I "${SCHEME}${DOMAIN}${API_PATH}" \
| grep -iq "Content-Type: application/json" \
&& echo "yes" \
|| echo "no (invalid or empty response from nextlcoud serverinfo api)"
fi
exit 0
;;
config)
cat << EOM
@ -190,7 +205,7 @@ graph_category cloud
num_updates_available.label available app updates
num_updates_available.info number of available app updates
num_updates_available.min 0
num_updates_available.warning 1
num_updates_available.warning ${UPDATES_WARNING}
num_installed.label installed apps
num_installed.info number of installed apps
num_installed.min 0
@ -201,8 +216,9 @@ EOM
esac
# Get JSON data
[ "$(test_https)" ] && DOMAIN="https://$DOMAIN" || DOMAIN="http://$DOMAIN"
JSONSTATS=$(curl -s -f -m 2 -u "$USERNAME:$PASSWORD" "$DOMAIN$API_PATH" | sed 's/\\/\\\\/g' | jq -cr ".ocs.data")
JSONSTATS=$(
fetch_url -u "$USERNAME:$PASSWORD" "${SCHEME}${DOMAIN}${API_PATH}" | sed 's/\\/\\\\/g' | jq -cr ".ocs.data" 2>&1
)
USERS=$(echo "$JSONSTATS" | jq -cr ".activeUsers")
STORAGE=$(echo "$JSONSTATS" | jq -cr ".nextcloud.storage")
SHARES=$(echo "$JSONSTATS" | jq -cr ".nextcloud.shares")