Merge branch 'enhance_mikrotik_system_with_error_return'

This commit is contained in:
Lars Kruse 2021-12-12 21:46:29 +01:00
commit 00cbc8d37e
1 changed files with 84 additions and 55 deletions

View File

@ -39,27 +39,49 @@ munin-node.
=cut
EOF
# setze Variablen mit default-Werten
# set variables with default values
ssh_user=${ssh_user:-user}
ssh_password=${ssh_password:-password}
ssh_host=${ssh_host:-192.168.2.1}
c=0 # zähler; wird für verschiedene Schleifen benötigt
c=0 # counter, it is used in a few loops
# Funktionen
# Function to get stderr from command
# USAGE: catch STDOUT STDERR cmd args..
# Source: https://stackoverflow.com/a/41069638
catch()
{
eval "$({
__2="$(
{ __1="$("${@:3}")"; } 2>&1;
ret=$?;
printf '%q=%q\n' "$1" "$__1" >&2;
exit $ret
)";
ret="$?";
printf '%s=%q\n' "$2" "$__2" >&2;
printf '( exit %q )' "$ret" >&2;
} 2>&1 )";
}
# functions
function get_name {
while read -r line; do
local fname
IFS=$'\n'; for line in $data; do
if echo "$line" | grep -q 'name:'; then
name=$(echo "$line" | grep name: | awk '{ print $2 }')
fname="$(echo "$line" | cut -d: -f2 | xargs | sed 's/[\s\n\r]*$//g' | sed 's/[^A-Za-z0-9]/_/g')"
if [ -n "$fname" ]; then
name="$fname"
fi
fi
done <<< "$data"
done
}
function get_ros_version {
while read -r line; do
if echo "$line" | grep -q 'version:'; then
if echo "$line" | cut -f2 -d" " | grep --quiet "^7\."; then
ros_version="7"
else
if echo "$line" | grep -q 'version: '; then
if echo "$line" | awk '/version:/{print $2}' | grep -q "^[0-6]\."; then
ros_version="6"
else
ros_version="7"
fi
fi
done <<< "$data"
@ -72,8 +94,16 @@ function get_cpu_count {
done <<< "$data"
}
function get_data {
# hole daten per ssh
data=$(sshpass -p "$ssh_password" ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$ssh_user"@"$ssh_host" -q ':delay 6s; /system health print; /system resource print; /system resource cpu print; /system identity print')
# fetch data per ssh
catch data stderr sshpass -p "$ssh_password" ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$ssh_user"@"$ssh_host" ':delay 6s; /system health print; /system resource print; /system resource cpu print; /system identity print'
data_ret=$?
}
function validate_data {
if [ $data_ret -ne 0 ]; then
echo "SSH returned errorcode = $data_ret:"
echo "$stderr"
exit $data_ret
fi
}
function get_mem_total {
mem_total=$(
@ -89,12 +119,12 @@ function get_mem_free {
}
function get_voltage_label {
while read -r line; do # für jede zeile in... ; siehe "done"
# gebe die zeile aus
# suche mit awk nach "voltage:"
# wenn gefunden:
# gebe jede zeile per print text aus
# externe/bash-variablen mit "'"<var>"'"
while read -r line; do # for every line
# output line
# search with awk for "voltage:"
# if found
# print line
# external/bash-variables with "'"<var>"'"
echo "$line" | awk '/voltage:/{
print "multigraph voltage_graph_""'"$name"'";
print "graph_title voltage " "'"$name"'";
@ -108,17 +138,16 @@ function get_voltage_label {
}
function get_voltage_value {
while read -r line; do
# funktion wie bei den "label"-funktionen
# gib überschrift aus wenn dirtyconfig nicht gesetzt ist
# weil aufruf dann nicht in "config" erfolgt
# wenn dirtyconfig nicht gesetzt ist oder =0, gebe überschrift aus
# like the label functions
# print title if dirtyconfig is not set
# because the call does not come in "config"
if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
echo "$line" | awk '/voltage:/{
print "multigraph voltage_graph_""'"$name"'";
}'
fi
# entferne mit gsub das zeichen % in wert $2
# gebe $2 aus
# remove with gsub the char % in argument $2
# print $2
echo "$line" | awk '/voltage:/{
gsub(/V/,"",$2);
print "voltage.value " $2
@ -133,10 +162,10 @@ function get_bad_blocks_label {
print "graph_title bad blocks " "'"$name"'";
print "graph_vlabel %";
print "graph_category mikrotik";
print "graph_args -l 0 --upper-limit 100";
print "graph_args -l 0 --upper-limit 5";
print "bad_blocks.label bad_blocks";
print "bad_blocks.warning 50";
print "bad_blocks.critical 90";
print "bad_blocks.warning 3";
print "bad_blocks.critical 4";
print "graph_info Percentage of Bad Blocks."
}'
done <<< "$data"
@ -295,9 +324,9 @@ function get_memory_label {
get_mem_total
get_mem_free
while read -r line; do
echo "$line" | awk '/free-memory:/{
print "multigraph memory_graph_""'"$name"'";
print "graph_title memory " "'"$name"'";
echo "$line" | awk -v name=$name '/free-memory:/{
printf "multigraph memory_graph_%s\n", name;
printf "graph_title memory %s\n", name;
print "graph_vlabel MiB";
print "graph_category mikrotik";
print "graph_args -l 0";
@ -306,38 +335,38 @@ function get_memory_label {
print "free_memory.label free memory";
print "graph_info Total, Used & free RAM.";
gsub(/MiB/,"",$2);
print "used_memory.critical " $2*0.9;
print "used_memory.warning " $2*0.7 }'
printf "used_memory.critical %.0f\n", $2*0.9;
printf "used_memory.warning %.0f\n", $2*0.7 }'
done <<< "$data"
}
function get_memory_value {
get_mem_total
get_mem_free
if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
echo multigraph memory_"$name"
echo multigraph memory_graph_"$name"
fi
while read -r line; do
echo "$line" | awk '/total-memory:/{
gsub(/MiB/,"",$2);
print "total_memory.value " $2
printf "total_memory.value %.0f\n", $2
}'
done <<< "$data"
while read -r line; do
echo "$line" | awk '/free-memory:/{
gsub(/MiB/,"",$2);
print "free_memory.value " $2
printf "free_memory.value %.0f\n", $2
}'
done <<< "$data"
# berechne used-memory
# gesamt + frei = benutzt
echo used_memory.value "$(echo $mem_total $mem_free | awk '{print ($1 - $2)}')"
# calculate used-memory
# total - free = used
printf "used_memory.value %.0f\n" "$(echo $mem_total $mem_free | awk '{print ($1 - $2)}')"
}
function get_disk_label {
while read -r line; do
echo "$line" | awk '/free-hdd-space:/{
print "multigraph disk_graph_""'"$name"'";
print "graph_title disk " "'"$name"'";
echo "$line" | awk -v name=$name '/free-hdd-space:/{
printf "multigraph disk_graph_%s\n", name;
printf "graph_title disk %s\n", name;
print "graph_vlabel Bytes";
print "graph_category mikrotik";
print "graph_args -l 0";
@ -348,30 +377,31 @@ function get_disk_label {
}
function get_disk_value {
if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
echo multigraph disk_"$name"
echo multigraph disk_graph_"$name"
fi
while read -r line; do
echo "$line" | grep KiB | awk '/free-hdd-space:/ {
gsub(/KiB/,"",$2)
print "free_disk.value " $2*1024 }'
printf "free_disk.value %d\n", $2*1024 }'
echo "$line" | grep MiB | awk '/free-hdd-space:/ {
gsub(/MiB/,"",$2)
print "free_disk.value " $2*1048576}'
printf "free_disk.value %d\n", $2*1048576}'
echo "$line" | grep KiB | awk '/total-hdd-space:/ {
gsub(/KiB/,"",$2)
print "total_disk.value " $2*1024 }'
printf "total_disk.value %d\n", $2*1024 }'
echo "$line" | grep MiB | awk '/total-hdd-space:/ {
gsub(/MiB/,"",$2)
print "total_disk.value " $2*1048576 }'
printf "total_disk.value %d\n", $2*1048576 }'
done <<< "$data"
}
# rufe funktionen auf, reihenfolge ist wichtig
# call functions, the order is important
get_data
validate_data
get_name
get_cpu_count
# munin-Logik
# wenn $1 = X; dann
# munin-Logic
# id $1 = X; then
if [ "$1" = "autoconf" ]; then
if ! command -v sshpass &> /dev/null; then
echo "[ERROR] sshpass could not be found!"
@ -381,8 +411,8 @@ if [ "$1" = "autoconf" ]; then
exit 0
fi
if [ "$1" = "config" ]; then
# gebe label aus
# wenn dirtyconfig gesetzt rufe value funktion auf
# print label
# if dirtyconfig is set print the value
get_voltage_label
if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
get_voltage_value
@ -417,8 +447,7 @@ if [ "$1" = "config" ]; then
fi
exit 0
fi
# wird nie aufgerufen wenn dirtyconfig gesetzt ist
# dann wird nur config aufgerufen und dabei werden zusätzlich die values ausgegeben
# does not get called if dirtyconfig is set
get_voltage_value
get_bad_blocks_value
get_write_sect_total_value
@ -429,7 +458,7 @@ get_memory_value
get_disk_value
exit 0
# Beispieloutput ROS 6.4*
# Example Output ROS 6.4*
# voltage: 24.5V
# cpu-temperature: 31C
# uptime: 4w3d21h28m58s
@ -455,7 +484,7 @@ exit 0
# 1 cpu1 19% 0% 0%
# name: crs309
# Beispieloutput ROS 7*
# Example Output ROS 7*
# Columns: NAME, VALUE, TYPE
# # NAME VA T
# 0 cpu-temperature 44 C