Skip to content
Snippets Groups Projects
Commit 951f349d authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

Merge branch '6338-snmpv3' into 'master'

6338 snmpv3

See merge request !68
parents 20e89ef5 be54eddc
No related branches found
No related tags found
1 merge request!686338 snmpv3
...@@ -26,16 +26,30 @@ ...@@ -26,16 +26,30 @@
# DATA:load:data:load5:1.3.6.1.4.1.2021.10.1.5.2: # DATA:load:data:load5:1.3.6.1.4.1.2021.10.1.5.2:
# DATA:load:data:load15:1.3.6.1.4.1.2021.10.1.5.3: # DATA:load:data:load15:1.3.6.1.4.1.2021.10.1.5.3:
# #
# DATA:netio:label:Network IO (experimental) # DATA:mem:label:Memory status
# DATA:mem:data:memTotalSwap:1.3.6.1.4.1.2021.4.3.0:
# DATA:mem:data:memAvailSwap:1.3.6.1.4.1.2021.4.4.0:
# DATA:mem:data:memTotalReal:1.3.6.1.4.1.2021.4.5.0:
# DATA:mem:data:memAvailReal:1.3.6.1.4.1.2021.4.6.0:
# DATA:mem:data:memTotalFree:1.3.6.1.4.1.2021.4.11.0:
# DATA:mem:data:memShared:1.3.6.1.4.1.2021.4.13.0:
# DATA:mem:data:memBuffer:1.3.6.1.4.1.2021.4.14.0:
# DATA:mem:data:memCached:1.3.6.1.4.1.2021.4.15.0:
#
# DATA:netio:label:Network IO
# DATA:netio:data:in:1.3.6.1.2.1.4.3.0: # DATA:netio:data:in:1.3.6.1.2.1.4.3.0:
# DATA:netio:data:out:1.3.6.1.2.1.4.10.0: # DATA:netio:data:out:1.3.6.1.2.1.4.10.0:
# #
# DATA:disc:label:Synology Disc usage
# DATA:disc:data:disc-free:1.3.6.1.4.1.6574.3.1.1.2::1.3.6.1.4.1.6574.3.1.1.4
# DATA:disc:data:disc-space:1.3.6.1.4.1.6574.3.1.1.2::1.3.6.1.4.1.6574.3.1.1.5
#
# DATA:synotemp:label:Synology NAS temperature # DATA:synotemp:label:Synology NAS temperature
# DATA:synotemp:data:temp:1.3.6.1.4.1.6574.1.2.0:°C # DATA:synotemp:data:temp:1.3.6.1.4.1.6574.1.2.0:°C
# #
# DATA:disc:label:Disc usage # DATA:synoio:label:Synology Space IO
# DATA:disc:data:disc-free:1.3.6.1.4.1.6574.3.1.1.2::1.3.6.1.4.1.6574.3.1.1.4 # DATA:synoio:data:syno-io-read:1.3.6.1.4.1.6574.102.1.1.12.1
# DATA:disc:data:disc-space:1.3.6.1.4.1.6574.3.1.1.2::1.3.6.1.4.1.6574.3.1.1.5 # DATA:synoio:data:syno-io-write:1.3.6.1.4.1.6574.102.1.1.13.1
# #
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# 2020-08-11 <axel.hahn@iml.unibe.ch> initial version # 2020-08-11 <axel.hahn@iml.unibe.ch> initial version
...@@ -43,20 +57,16 @@ ...@@ -43,20 +57,16 @@
# 2022-10-21 v0.4 <axel.hahn@unibe.ch> remove grep: warning: stray \ before white space # 2022-10-21 v0.4 <axel.hahn@unibe.ch> remove grep: warning: stray \ before white space
# 2022-10-21 v0.5 <andrea.gottsponer@unibe.ch> remove grep: warning: stray \ before white space # 2022-10-21 v0.5 <andrea.gottsponer@unibe.ch> remove grep: warning: stray \ before white space
# 2022-10-25 v0.6 <axel.hahn@unibe.ch> fix empty value in performance data; shell fixes # 2022-10-25 v0.6 <axel.hahn@unibe.ch> fix empty value in performance data; shell fixes
# 2023-05-05 v0.7 <axel.hahn@unibe.ch> source shared func, support snmpv3; add synoio
# ====================================================================== # ======================================================================
. $(dirname $0)/inc_pluginfunctions . $(dirname $0)/inc_pluginfunctions
. $(dirname $0)/check_snmp_includes
# --- basic vars # --- basic vars
self_APPNAME=$( basename $0 | tr [:lower:] [:upper:] ) self_APPNAME=$( basename $0 | tr [:lower:] [:upper:] )
self_APPVERSION=0.6 self_APPVERSION=0.7
SNMPCOMMUNITY="public"
SNMPVERSION="2c"
SNMPWALK=$(which snmpwalk)
SNMPGET=$(which snmpget)
HOSTNAME="127.0.0.1"
METHOD="" METHOD=""
# --- OID declarations # --- OID declarations
...@@ -119,8 +129,14 @@ _get(){ ...@@ -119,8 +129,14 @@ _get(){
# is given oid a counter? # is given oid a counter?
# function resturns "true" or "false" # function resturns "true" or "false"
_iscounter(){ _iscounter(){
_getresult $1 | cut -d ":" -f1 | grep -i "Counter" >/dev/null if _getresult $1 | cut -d ":" -f1 | grep -i "Counter" >/dev/null; then
if [ $? -eq 0 ]; then echo "true"
else
echo "false"
fi
}
_isinteger(){
if _getresult $1 | cut -d ":" -f1 | grep -i "Integer" >/dev/null; then
echo "true" echo "true"
else else
echo "false" echo "false"
...@@ -130,6 +146,15 @@ _iscounter(){ ...@@ -130,6 +146,15 @@ _iscounter(){
#--------------------------------------------------- #---------------------------------------------------
# --- show usage # --- show usage
function showHelp(){ function showHelp(){
_self=$( basename $0 )
local _methods=$(
_cfg_getMethods | while read -r line
do
METHOD=$(echo "$line" | cut -f 1 -d ":")
descr=$(_cfg_getlabel)
printf " %-10s %-50s\n" "$METHOD" "$descr"
done
)
cat <<EOH cat <<EOH
______________________________________________________________________ ______________________________________________________________________
...@@ -143,31 +168,34 @@ ______________________________________________________________________ ...@@ -143,31 +168,34 @@ ______________________________________________________________________
SNMP performance data of Synology storages. SNMP performance data of Synology storages.
USAGE: USAGE:
./check_snmp_data -h hostname [-C communitystring] -m method $_self [options] -h SNMPTARGET
PARAMETERS: PARAMETERS:
-h hostname as fqdn or ip address -a STRING
-C communitystr optional: community string for snmp; default is $SNMPCOMMUNITY authentication params for snmpwalk/ snmpget to connect to target;
-v verbose output default: \"-v2c -c public\" (Snmpv2 with community string \"public\")
-h SNMPTARGET
as fqdn or ip address; default: localhost
-f FILE
read authentication from config file
default: "/etc/icinga2/snmp.cfg"
-m method what to show -m method what to show
method is one of ... method is one of ...
EOH $_methods
-v verbose output
# --- dynamic: add current methods
_cfg_getMethods | while read -r line
do
METHOD=$(echo "$line" | cut -f 1 -d ":")
descr=$(_cfg_getlabel)
printf " %-10s %-50s\n" "$METHOD" "$descr"
done
# --- finish usage CONFIG FILE:
cat <<EOH2 The config file can be multiline and has the syntax
[SNMPTARGET]:[auth parameters]
The auth parameters set the version and all needed values to connect.
Snmp v2 uses a community string.
Snmp v3 is highly recommended (you should disable Snmp v2) and needs
a user and password.
EXAMPLE: EXAMPLE:
./check_snmp_data -h 192.168.100.12 -v -m cpu $_self -h 192.168.100.12 -v -m cpu
EOH2 EOH
ph.abort "" ph.abort ""
} }
...@@ -176,16 +204,22 @@ EOH2 ...@@ -176,16 +204,22 @@ EOH2
# MAIN # MAIN
#--------------------------------------------------- #---------------------------------------------------
while getopts h:m:v OPTNAME; do while getopts a:h:m:v OPTNAME; do
case "$OPTNAME" in case "$OPTNAME" in
h) a)
HOSTNAME="$OPTARG" SNMPAUTH="$OPTARG"
option_found=1
;; ;;
C) C)
SNMPCOMMUNITY="$OPTARG" SNMPCOMMUNITY="$OPTARG"
;; ;;
f)
SNMPCONFIG="$OPTARG"
;;
h)
SNMPTARGET="$OPTARG"
option_found=1
;;
m) m)
METHOD="$OPTARG" METHOD="$OPTARG"
;; ;;
...@@ -199,11 +233,13 @@ while getopts h:m:v OPTNAME; do ...@@ -199,11 +233,13 @@ while getopts h:m:v OPTNAME; do
esac esac
done done
if [ "$option_found" = "0" ] || [ "$HOSTNAME" = "" ] ; then if [ "$option_found" = "0" ] || [ "$SNMPTARGET" = "" ] ; then
usage usage
# remark: script aborts ... # remark: script aborts ...
fi fi
test -z "$SNMPAUTH" && read_config
# --- read metadata of the selected METHOD # --- read metadata of the selected METHOD
info=$(_cfg_getlabel) info=$(_cfg_getlabel)
if [ -z "$info" ]; then if [ -z "$info" ]; then
...@@ -218,7 +254,7 @@ looplist=( $(_cfg_dataitem 7) ) ...@@ -218,7 +254,7 @@ looplist=( $(_cfg_dataitem 7) )
# --- check for looplist # --- check for looplist
if [ -n "$looplist" ]; then if [ -n "$looplist" ]; then
nb=$($SNMPWALK -OQne -t 10 -v $SNMPVERSION -c $SNMPCOMMUNITY $HOSTNAME ${oidlist[*]} 2> /dev/null | wc -l) nb=$($SNMPWALK -OQne -t $SNMPTIMEOUT $SNMPAUTH $SNMPTARGET ${oidlist[*]} 2> /dev/null | wc -l)
declare -a tmpPrflist=() declare -a tmpPrflist=()
declare -a tmpOidlist=() declare -a tmpOidlist=()
...@@ -238,16 +274,16 @@ if [ -n "$looplist" ]; then ...@@ -238,16 +274,16 @@ if [ -n "$looplist" ]; then
fi fi
# --- SNMPGET to all wanted oids # --- SNMPGET to all wanted oids
SYNO=$($SNMPGET -One -t 10 -v $SNMPVERSION -c $SNMPCOMMUNITY $HOSTNAME ${oidlist[*]} 2> /dev/null) SYNO=$($SNMPGET -One -t $SNMPTIMEOUT $SNMPAUTH $SNMPTARGET ${oidlist[*]} 2> /dev/null)
if [ $? -ne 0 ] ; then if [ $? -ne 0 ] ; then
$SNMPGET -One -t 10 -v $SNMPVERSION -c $SNMPCOMMUNITY $HOSTNAME ${oidlist[*]} $SNMPGET -One -t $SNMPTIMEOUT $SNMPAUTH $SNMPTARGET ${oidlist[*]}
ph.abort "Problem with SNMP request" ph.abort "Problem with SNMP request"
fi fi
# --- performance data # --- performance data
typeset -i value # typeset -i value
for index in ${!oidlist[*]} for index in ${!oidlist[*]}
do do
...@@ -261,9 +297,13 @@ do ...@@ -261,9 +297,13 @@ do
else else
# handle counter data # handle counter data
if [ "$(_iscounter ${oidlist[$index]})" = "true" ]; then if [ "$(_iscounter ${oidlist[$index]})" = "true" ]; then
value2=$(ph.perfdeltaspeed "snmp-data-${HOSTNAME}-${method}-${label}" $value) value2=$(ph.perfdeltaspeed "snmp-data-${SNMPTARGET}-${method}-${label}" $value)
_wd "$( printf '%-14s total: %-14s delta: %6s %s per sec' $label $value $value2 $unit )" _wd "$( printf '%-14s total: %-14s delta: %6s %s per sec' $label $value $value2 $unit )"
value=$value2 value=$value2
elif [ "$(_isinteger ${oidlist[$index]})" = "true" ]; then
_wd "$( printf '%-14s %s %s' $label $value $unit ) (INTEGER)"
value2=$( echo "${value}" | sed -E "s#([0-9]*) (.).*#\1\2#g")
value=${value2}
else else
_wd "$( printf '%-14s %s %s' $label $value $unit )" _wd "$( printf '%-14s %s %s' $label $value $unit )"
fi fi
......
#!/bin/sh
# ----------------------------------------------------------------------
# Variables
# ----------------------------------------------------------------------
SNMPAUTH=
SNMPCONFIG=/etc/icinga2/snmp.cfg
SNMPCOMMUNITY="public"
SNMPVERSION="2c"
SNMPWALK=$(which snmpwalk)
SNMPGET=$(which snmpget)
SNMPTIMEOUT=3
SNMPTARGET="127.0.0.1"
# ----------------------------------------------------------------------
# FUNCTIONS
# ----------------------------------------------------------------------
# read config data for authentication; it sets the variable SNMPAUTH
# that can be used for snmpwalk / snmpget
#
# no params
#
# used global vars:
# - SNMPVERSION + SNMPCOMMUNITY to set the default
# - SNMPCONFIG - filename of the config to read
# - SNMPTARGET - SNMPTARGET (ip or fqdn) of target to connect
#
read_config(){
SNMPAUTH="-v $SNMPVERSION -c $SNMPCOMMUNITY"
if [ -r "$SNMPCONFIG" ]; then
if grep "^${SNMPTARGET}:" "$SNMPCONFIG" >/dev/null; then
SNMPAUTH="$( grep "^${SNMPTARGET}:" "$SNMPCONFIG" | cut -f 2- -d ':' )"
else
SNMPAUTH="$( grep "^DEFAULT:" "$SNMPCONFIG" | cut -f 2- -d ':' )"
fi
else
ph.setStatus "unknown"
echo "ERROR: unable to read config file [$SNMPCONFIG]."
ph.exit
fi
}
# ----------------------------------------------------------------------
...@@ -17,20 +17,13 @@ ...@@ -17,20 +17,13 @@
#--------------------------------------------------- #---------------------------------------------------
. $(dirname $0)/inc_pluginfunctions . $(dirname $0)/inc_pluginfunctions
. $(dirname $0)/check_snmp_includes
# --- basic vars # --- basic vars
self_APPNAME=$( basename $0 | tr [:lower:] [:upper:] ) self_APPNAME=$( basename $0 | tr [:lower:] [:upper:] )
self_APPVERSION=1.2 self_APPVERSION=1.2
SNMPCOMMUNITY="public"
SNMPVERSION="2c"
SNMPWALK=$(which snmpwalk)
SNMPGET=$(which snmpget)
SNMPCONFIG=/etc/icingaclient/snmp.cfg
HOSTNAME="localhost"
option_found=0 option_found=0
healthString="" healthString=""
verbose="no" verbose="no"
...@@ -88,20 +81,22 @@ On higher cpu usage it can show processes that cause cpu waits and ...@@ -88,20 +81,22 @@ On higher cpu usage it can show processes that cause cpu waits and
with most cpu consumption. with most cpu consumption.
SYNTAX: SYNTAX:
$_self [options] $_self [options] -h TARGET
OPTIONS: OPTIONS:
-a authentication params for snmpwalk/ snmpget to connect to target; -a STRING
authentication params for snmpwalk/ snmpget to connect to target;
default: \"-v2c -c public\" (Snmpv2 with community string \"public\") default: \"-v2c -c public\" (Snmpv2 with community string \"public\")
-h host to connect as hostname or ip address -h SNMPTARGET
as fqdn or ip address; default: localhost
-f FILE -f FILE
read authentication from config file read authentication from config file
default: \"/etc/icingaclient/snmp.cfg\" default: \"/etc/icinga2/snmp.cfg\"
-v detailed output -v detailed output
CONFIG FILE: CONFIG FILE:
The config file can be multiline and has the syntax The config file can be multiline and has the syntax
[hostname]:[auth parameters] [SNMPTARGET]:[auth parameters]
The auth parameters set the version and all needed values to connect. The auth parameters set the version and all needed values to connect.
Snmp v2 uses a community string. Snmp v2 uses a community string.
Snmp v3 is highly recommended (you should disable Snmp v2) and needs Snmp v3 is highly recommended (you should disable Snmp v2) and needs
...@@ -114,7 +109,7 @@ CONFIG FILE: ...@@ -114,7 +109,7 @@ CONFIG FILE:
EXAMPLE: EXAMPLE:
$_self -h server-01.example.com -v $_self -h server-01.example.com -v
Show Synology status of server-01 using connect data from /etc/icingaclient/snmp.cfg Show Synology status of server-01 using connect data from /etc/icinga2/snmp.cfg
$_self -h server-01.example.com -v -f /opt/somewhere/snmp.conf $_self -h server-01.example.com -v -f /opt/somewhere/snmp.conf
Show Synology status of server-01 using connect data from custom config Show Synology status of server-01 using connect data from custom config
...@@ -144,21 +139,6 @@ usage() ...@@ -144,21 +139,6 @@ usage()
ph.abort "$USAGE" ph.abort "$USAGE"
} }
read_config(){
SNMPAUTH="-v $SNMPVERSION -c $SNMPCOMMUNITY"
if [ -r "$SNMPCONFIG" ]; then
if grep "^${HOSTNAME}:" "$SNMPCONFIG" >/dev/null; then
SNMPAUTH="$( grep "^${HOSTNAME}:" "$SNMPCONFIG" | cut -f 2- -d ':' )"
else
SNMPAUTH="$( grep "^DEFAULT:" "$SNMPCONFIG" | cut -f 2- -d ':' )"
fi
else
ph.setStatus "unknown"
echo "ERROR: unable to read config file [$SNMPCONFIG]."
ph.exit
fi
}
#--------------------------------------------------- #---------------------------------------------------
# MAIN # MAIN
...@@ -173,7 +153,7 @@ while getopts a:h:f:v OPTNAME; do ...@@ -173,7 +153,7 @@ while getopts a:h:f:v OPTNAME; do
SNMPCONFIG="$OPTARG" SNMPCONFIG="$OPTARG"
;; ;;
h) h)
HOSTNAME="$OPTARG" SNMPTARGET="$OPTARG"
option_found=1 option_found=1
;; ;;
v) v)
...@@ -191,8 +171,8 @@ done ...@@ -191,8 +171,8 @@ done
test -z "$SNMPAUTH" && read_config test -z "$SNMPAUTH" && read_config
# --- read raid and disks to get its single OIDs # --- read raid and disks to get its single OIDs
nbDisk=$($SNMPWALK -OQne -t 10 ${SNMPAUTH} $HOSTNAME $OID_diskID 2> /dev/null | wc -l ) nbDisk="$( $SNMPWALK -OQne -t $SNMPTIMEOUT ${SNMPAUTH} $SNMPTARGET $OID_diskID 2> /dev/null | wc -l )"
nbRAID=$($SNMPWALK -OQne -t 10 ${SNMPAUTH} $HOSTNAME $OID_RAIDName 2> /dev/null | wc -l) nbRAID="$( $SNMPWALK -OQne -t $SNMPTIMEOUT ${SNMPAUTH} $SNMPTARGET $OID_RAIDName 2> /dev/null | wc -l )"
for i in $(seq 1 $nbDisk); for i in $(seq 1 $nbDisk);
do do
...@@ -204,7 +184,7 @@ do ...@@ -204,7 +184,7 @@ do
done done
# --- SNPGET to all wanted oids # --- SNPGET to all wanted oids
syno=$($SNMPGET -OQne -t 10 ${SNMPAUTH} $HOSTNAME $OID_model $OID_serialNumber $OID_DSMVersion $OID_DSMUpdateAvailable $OID_systemStatus $OID_powerStatus $OID_systemFanStatus $OID_CPUFanStatus $OID_temp $OID_disk $OID_RAID 2> /dev/null | sed 's/^[ \t]*//;s/[ \t]*$//') syno=$($SNMPGET -OQne -t $SNMPTIMEOUT ${SNMPAUTH} $SNMPTARGET $OID_model $OID_serialNumber $OID_DSMVersion $OID_DSMUpdateAvailable $OID_systemStatus $OID_powerStatus $OID_systemFanStatus $OID_CPUFanStatus $OID_temp $OID_disk $OID_RAID 2> /dev/null | sed 's/^[ \t]*//;s/[ \t]*$//')
if [ "$?" != "0" ] ; then if [ "$?" != "0" ] ; then
ph.abort "Problem with SNMP request" ph.abort "Problem with SNMP request"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment