diff --git a/.shellcheckrc b/.shellcheckrc index 89e74a2..ff279dd 100755 --- a/.shellcheckrc +++ b/.shellcheckrc @@ -8,4 +8,5 @@ disable=SC2012 # low Use find instead of ls to better handle non-alphanumeric f disable=SC2035 # Use ./*glob* or -- *glob* so names with dashes won't become options. disable=SC2076 # Remove quotes from right-hand side of =~ to match as a regex rather than literally. disable=SC2001 # See if you can use ${variable//search/replace} instead. - +disable=SC2030 # Modification of nickname is local (to subshell caused by pipeline). +disable=SC2031 # nickname was modified in a subshell. That change might be lost. diff --git a/metrics.sh b/metrics.sh index d5e1fdb..02deeed 100755 --- a/metrics.sh +++ b/metrics.sh @@ -4,12 +4,15 @@ # use node_exporter's "textfile" feature to send metrics to Prometheus -function _orport2nickname() { - local opt=${1:-UNSET} +# local hack +function _orportFromIpsetName() { + local name=${1?NAME IS UNSET} + + local orport=$(cut -f 3 -d '-' -s <<<$name) # set the Prometheus label "nickname" to the value of the Tor server ones echo -n "fuchs" - case $opt in + case $orport in 443) echo "1" ;; 9001) echo "2" ;; 8443) echo "3" ;; @@ -53,8 +56,6 @@ function printMetrics() { local count local mode local name - local nickname - local orport local var ############################### @@ -67,8 +68,7 @@ function printMetrics() { ipset list -n | grep '^tor-'${mode}${v}'-' | while read -r name; do - orport=$(cut -f 3 -d '-' -s <<<$name) - nickname=$(_orport2nickname $orport) + nickname=${NICKNAME:-$(_orportFromIpsetName $name)} { ipset list -s $name | sed -e '1,8d' | _histogram >$tmpfile.$name.tmp chmod a+r $tmpfile.$name.tmp @@ -98,8 +98,7 @@ function printMetrics() { grep ' DROP ' | grep ' match-set tor-ddos'$v'-' | while read -r pkts remain; do name=$(grep -Eo ' tor-ddos.* ' <<<$remain | tr -d ' ') - orport=$(cut -f 3 -d '-' -s <<<$name) - nickname=$(_orport2nickname $orport) + nickname=${NICKNAME:-$(_orportFromIpsetName $name)} echo "$var{ipver=\"${v:-4}\",nickname=\"$nickname\"} $pkts" done done @@ -121,8 +120,7 @@ function printMetrics() { if [[ $name =~ 'trust' ]]; then echo "$var{ipver=\"${v:-4}\",mode=\"$mode\"} $size" else - orport=$(cut -f 3 -d '-' -s <<<$name) - nickname=$(_orport2nickname $orport) + nickname=${NICKNAME:-$(_orportFromIpsetName $name)} echo "$var{ipver=\"${v:-4}\",mode=\"$mode\",nickname=\"$nickname\"} $size" fi done @@ -138,8 +136,7 @@ function printMetrics() { wc -l /proc/net/ip${v}t_hashlimit/tor-$mode-* | grep -v ' total' | while read -r count name; do - orport=$(cut -f 3 -d '-' -s <<<$name) - nickname=$(_orport2nickname $orport) + nickname=${NICKNAME:-$(_orportFromIpsetName $name)} echo "$var{ipver=\"${v:-4}\",nickname=\"$nickname\",mode=\"$mode\"} $count" done done @@ -150,11 +147,12 @@ set -eu export LANG=C.utf8 export PATH=/usr/sbin:/usr/bin:/sbin/:/bin -tmpfile=$(mktemp /tmp/metrics_torutils_XXXXXX.tmp) +datadir=${1:-/var/lib/node_exporter} # default at least under Gentoo Linux +NICKNAME=${2:-$(grep "^Nickname " /etc/tor/torrc 2>/dev/null | awk '{ print $2 }')} # if not given nor found then derive it from the orport +export NICKNAME -datadir=${1:-/var/lib/node_exporter} # default directory under Gentoo Linux +tmpfile=$(mktemp /tmp/metrics_torutils_XXXXXX.tmp) cd $datadir - echo "# $0 $(date -R)" >$tmpfile printMetrics >>$tmpfile chmod a+r $tmpfile