From e527db57a53d6456a68e3512e7f5aa2f222e07f2 Mon Sep 17 00:00:00 2001 From: Pierre-Alain TORET Date: Tue, 27 Mar 2018 18:04:29 +0200 Subject: [PATCH] Improve plugins call to status and few other fixes Signed-off-by: Pierre-Alain TORET --- plugins/syncthing/strelaysrv_ | 36 +++++++++++++++++++++------------- plugins/syncthing/syncthing_ | 37 +++++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 29 deletions(-) diff --git a/plugins/syncthing/strelaysrv_ b/plugins/syncthing/strelaysrv_ index 525a0c9f..c5a9fe3d 100755 --- a/plugins/syncthing/strelaysrv_ +++ b/plugins/syncthing/strelaysrv_ @@ -1,4 +1,4 @@ -#!/usr/bin/env sh +#!/bin/sh : <<=cut =head1 NAME strelaysrv_ - Plugin to monitor Syncthing relay server @@ -31,6 +31,10 @@ Pierre-Alain TORET MIT =cut +getstatus() { + "$CURL" -s "http://$syncthing_relaysrv_host:$syncthing_relaysrv_port/status" +} + num() { case $1 in config) @@ -45,15 +49,15 @@ strelaysrv_num_proxies.label proxies EOM exit 0;; *) - STATUS=$($CURL -s http://"$syncthing_relaysrv_host":"$syncthing_relaysrv_port"/status) + STATUS=$(getstatus) NS=$(echo "$STATUS" | $JQ '.numActiveSessions ') NC=$(echo "$STATUS" | $JQ '.numConnections ') NK=$(echo "$STATUS" | $JQ '.numPendingSessionKeys ') NP=$(echo "$STATUS" | $JQ '.numProxies ') - printf "strelaysrv_num_sessions.value %s\\n" "$NS" - printf "strelaysrv_num_connections.value %s\\n" "$NC" - printf "strelaysrv_num_pending.value %s\\n" "$NK" - printf "strelaysrv_num_proxies.value %s\\n" "$NP" + printf "strelaysrv_num_sessions.value %s\n" "$NS" + printf "strelaysrv_num_connections.value %s\n" "$NC" + printf "strelaysrv_num_pending.value %s\n" "$NK" + printf "strelaysrv_num_proxies.value %s\n" "$NP" esac } @@ -68,8 +72,9 @@ strelaysrv_uptime.label uptime EOM exit 0;; *) - UPTIME=$($CURL -s http://"$syncthing_relaysrv_host":"$syncthing_relaysrv_port"/status | $JQ '.uptimeSeconds') - printf "strelaysrv_uptime.value %s\\n" "$UPTIME" + STATUS=$(getstatus) + UPTIME=$(echo "$STATUS" | "$JQ" '.uptimeSeconds') + printf "strelaysrv_uptime.value %s\n" "$UPTIME" esac } @@ -84,8 +89,9 @@ strelaysrv_goroutine.label routines EOM exit 0;; *) - GOROUTINE=$($CURL -s http://"$syncthing_relaysrv_host":"$syncthing_relaysrv_port"/status | $JQ '.goNumRoutine') - printf "strelaysrv_goroutine.value %s\\n" "$GOROUTINE" + STATUS=$(getstatus) + GOROUTINE=$(echo "$STATUS" | "$JQ" '.goNumRoutine') + printf "strelaysrv_goroutine.value %s\n" "$GOROUTINE" esac } @@ -102,8 +108,9 @@ strelaysrv_proxied.cdef strelaysrv_proxied,8,* EOM exit 0;; *) - BP=$($CURL -s http://"$syncthing_relaysrv_host":"$syncthing_relaysrv_port"/status | $JQ '.bytesProxied ') - printf "strelaysrv_proxied.value %s\\n" "$BP" + STATUS=$(getstatus) + BP=$(echo "$STATUS" | "$JQ" '.bytesProxied ') + printf "strelaysrv_proxied.value %s\n" "$BP" esac } @@ -120,8 +127,9 @@ strelaysrv_transfer.cdef strelaysrv_transfer,1000,* EOM exit 0;; *) - TRANSFER=$($CURL -s http://"$syncthing_relaysrv_host":"$syncthing_relaysrv_port"/status | $JQ '.kbps10s1m5m15m30m60m[2] ') - printf "strelaysrv_transfer.value %s\\n" "$TRANSFER" + STATUS=$(getstatus) + TRANSFER=$(echo "$STATUS" | "$JQ" '.kbps10s1m5m15m30m60m[2] ') + printf "strelaysrv_transfer.value %s\n" "$TRANSFER" esac } diff --git a/plugins/syncthing/syncthing_ b/plugins/syncthing/syncthing_ index a26236cc..25ac50c0 100755 --- a/plugins/syncthing/syncthing_ +++ b/plugins/syncthing/syncthing_ @@ -1,4 +1,4 @@ -#!/usr/bin/env sh +#!/bin/sh # -*- sh -*- : <<=cut =head1 NAME @@ -34,6 +34,10 @@ Pierre-Alain TORET MIT =cut +getstatus() { + "$CURL" -s -X GET -H "X-API-Key: $syncthing_apikey" "$syncthing_proto://$syncthing_host:$syncthing_port/rest/system/status" +} + cpu() { case $1 in config) @@ -46,8 +50,9 @@ syncthing_cpu.label cpu EOM exit 0;; *) - CPU=$($CURL -s -X GET -H "X-API-Key: $syncthing_apikey" "$syncthing_proto"://"$syncthing_host":"$syncthing_port"/rest/system/status | $JQ '.cpuPercent') - printf "syncthing_cpu.value %s\\n" "$CPU" + STATUS=$(getstatus) + CPU=$(echo "$STATUS" | "$JQ" '.cpuPercent') + printf "syncthing_cpu.value %s\n" "$CPU" esac } @@ -67,11 +72,11 @@ syncthing_mem_sys.cdef syncthing_mem_sys,8,* EOM exit 0;; *) - STATUS=$($CURL -s -X GET -H "X-API-Key: $syncthing_apikey" "$syncthing_proto"://"$syncthing_host":"$syncthing_port"/rest/system/status) - ALL=$(echo "$STATUS" | $JQ '.alloc') - SYS=$(echo "$STATUS" | $JQ '.sys') - printf "syncthing_mem_all.value %s\\n" "$ALL" - printf "syncthing_mem_sys.value %s\\n" "$SYS" + STATUS=$(getstatus) + ALL=$(echo "$STATUS" | "$JQ" '.alloc') + SYS=$(echo "$STATUS" | "$JQ" '.sys') + printf "syncthing_mem_all.value %s\n" "$ALL" + printf "syncthing_mem_sys.value %s\n" "$SYS" esac } @@ -86,8 +91,9 @@ syncthing_uptime.label uptime EOM exit 0;; *) - UPTIME=$($CURL -s -X GET -H "X-API-Key: $syncthing_apikey" "$syncthing_proto"://"$syncthing_host":"$syncthing_port"/rest/system/status | $JQ '.uptime') - printf "syncthing_uptime.value %s\\n" "$UPTIME" + STATUS=$(getstatus) + UPTIME=$(echo "$STATUS" | "$JQ" '.uptime') + printf "syncthing_uptime.value %s\n" "$UPTIME" esac } @@ -102,8 +108,9 @@ syncthing_goroutine.label routines EOM exit 0;; *) - GOROUTINES=$($CURL -s -X GET -H "X-API-Key: $syncthing_apikey" "$syncthing_proto"://"$syncthing_host":"$syncthing_port"/rest/system/status | $JQ '.goroutines') - printf "syncthing_goroutine.value %s\\n" "$GOROUTINES" + STATUS=$(getstatus) + GOROUTINES=$(echo "$STATUS" | "$JQ" '.goroutines') + printf "syncthing_goroutine.value %s\n" "$GOROUTINES" esac } @@ -127,9 +134,9 @@ syncthing_transfer_up.cdef syncthing_transfer_up,8,* EOM exit 0;; *) - CONNECTIONS=$($CURL -s -X GET -H "X-API-Key: $syncthing_apikey" "$syncthing_proto"://"$syncthing_host":"$syncthing_port"/rest/system/connections) - IBT=$(echo "$CONNECTIONS" | $JQ '.total | .inBytesTotal') - OBT=$(echo "$CONNECTIONS" | $JQ '.total | .outBytesTotal') + CONNECTIONS=$("$CURL" -s -X GET -H "X-API-Key: $syncthing_apikey" "$syncthing_proto://$syncthing_host:$syncthing_port/rest/system/connections") + IBT=$(echo "$CONNECTIONS" | "$JQ" '.total | .inBytesTotal') + OBT=$(echo "$CONNECTIONS" | "$JQ" '.total | .outBytesTotal') printf "syncthing_transfer_up.value %s\\n" "$IBT" printf "syncthing_transfer_down.value %s\\n" "$OBT" esac