1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-09-19 00:53:19 +00:00

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

View file

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