Merge branch 'func'

This commit is contained in:
Michael Grote 2021-09-24 10:05:20 +02:00
commit 4fe11fdbf0

View file

@ -3,172 +3,429 @@
#%# capabilities=autoconf
# Michael Grote
# michael.grote ät posteo.de
# Mail: michael.grote ät posteo.de
# This plugin fetches multiple Values from a mikrotik device.
# tested with a RB750GR3 and CRS309.
# Dependencies:
# - bc
# - sshpass
# config:
# A User is needed for this plugin to work:
# /user add name=munin group=read password=hallowelt address=<munin-server-ip>
# plugin config:
# [mt_system_<name>]
# user root
# env.ssh_user munin
# env.ssh_password hallowelt
# env.ssh_host 192.168.2.1
# env.name hex
# on the router
# /user add name=munin group=read password=hallowelt address=<munin-server-ip>
# pruefe ob sshpass installiert ist
if [[ $(dpkg -l sshpass > /dev/null 2>&1) -ne "0" ]] ; then
echo "could not find sshpass"
exit 1
fi
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')
# setze user und passwort
# setze Variablen mit default-Werten
ssh_user=${ssh_user:-user}
ssh_password=${ssh_password:-password}
ssh_host=${ssh_host:-192.168.2.1}
name=${name:-mikrotik}
c=0 # zähler; wird für verschiedene Schleifen benötigt
# wenn parameter = ...
# Funktionen
function get_name {
while read -r line; do
if echo "$line" | grep 'name:' > /dev/null; then
name=$(echo "$line" | grep name: | awk '{ print $2 }')
fi
done <<< "$data"
}
function get_cpu_count {
while read -r line; do
if echo "$line" | grep 'cpu-count' > /dev/null; then
anzahl_cpu=$(echo "$line" | grep cpu-count: | sed -r 's/(cpu-count: )([0-9]+)/\2/g' | tr -dc '0-9')
fi
done <<< "$data"
}
function check_sshpass {
if [[ $(dpkg -l sshpass > /dev/null 2>&1) -ne "0" ]] ; then
echo "could not find sshpass"
exit 1
fi
}
function check_bc {
if [[ $(dpkg -l bc > /dev/null 2>&1) -ne "0" ]] ; then
echo "could not find bc"
exit 1
fi
}
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')
}
function get_mem_total {
mem_total=$(
while read -r line; do
echo "$line" | awk '/total-memory:/{ gsub(/MiB/,"",$2); print $2 }' | tr -dc '0-9.'
done <<< "$data")
}
function get_mem_free {
mem_free=$(
while read -r line; do
echo "$line" | awk '/free-memory:/{ gsub(/MiB/,"",$2); print $2 }' | tr -dc '0-9.'
done <<< "$data")
}
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>"'"
echo "$line" | awk '/voltage:/{
print "multigraph voltage_graph_""'"$name"'";
print "graph_title voltage " "'"$name"'";
print "graph_vlabel volt";
print "graph_category mikrotik";
print "graph_args -l 0";
print "voltage.label voltage";
print "graph_info Input Voltage."
}'
done <<< "$data" # der variable data
}
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
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
echo "$line" | awk '/voltage:/{
gsub(/V/,"",$2);
print "voltage.value " $2
}'
done <<< "$data"
}
function get_bad_blocks_label {
while read -r line; do
echo "$line" | awk '/bad-blocks:/{
print "multigraph bad_blocks_graph_""'"$name"'";
print "graph_title bad blocks " "'"$name"'";
print "graph_vlabel %";
print "graph_category mikrotik";
print "graph_args -l 0 --upper-limit 100";
print "bad_blocks.label bad_blocks";
print "bad_blocks.warning 50";
print "bad_blocks.critical 90";
print "graph_info Percentage of Bad Blocks."
}'
done <<< "$data"
}
function get_bad_blocks_value {
while read -r line; do
if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
echo "$line" | awk '/bad-blocks:/{
print "multigraph bad_blocks_graph_""'"$name"'";
}'
fi
echo "$line" | awk '/bad-blocks:/{
gsub(/%/,"",$2);
print "bad_blocks.value " $2
}'
done <<< "$data"
}
function get_write_sect_total_label {
while read -r line; do
echo "$line" | awk '/write-sect-total:/{
print "multigraph write_sect_total_graph_""'"$name"'";
print "graph_title Total sector writes " "'"$name"'";
print "graph_vlabel count";
print "graph_category mikrotik";
print "graph_args -l 0";
print "write_sect_total.label write_sect_total";
print "graph_info Total sector writes."
}'
done <<< "$data"
}
function get_write_sect_total_value {
while read -r line; do
if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
echo "$line" | awk '/write-sect-total:/{
print "multigraph write_sect_total_graph_""'"$name"'";
}'
fi
echo "$line" | awk '/write-sect-total:/{
print "write_sect_total.value " $2
}'
done <<< "$data"
}
function get_write_sect_since_reboot_label {
while read -r line; do
echo "$line" | awk '/write-sect-since-reboot:/{
print "multigraph write_sect_since_reboot_graph_""'"$name"'";
print "graph_title Sector writes since reboot " "'"$name"'";
print "graph_vlabel count";
print "graph_category mikrotik";
print "graph_args -l 0";
print "write_sect_since_reboot.label write_sect_since_reboot";
print "graph_info Total Sector writes since last reboot."
}'
done <<< "$data"
}
function get_write_sect_since_reboot_value {
while read -r line; do
if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
echo "$line" | awk '/write-sect-since-reboot:/{
print "multigraph write_sect_since_reboot_graph_""'"$name"'";
}'
fi
echo "$line" | awk '/write-sect-since-reboot:/{
print "write_sect_since_reboot.value " $2
}'
done <<< "$data"
}
function get_temperature_label {
while read -r line; do
echo "$line" | awk '/temperature:/{
print "multigraph temperature_graph_""'"$name"'";
print "graph_title temperature " "'"$name"'";
print "graph_vlabel °C";
print "graph_category mikrotik";
print "graph_args -l 0";
print "temperature.label cpu temperature";
print "temperature.warning 75";
print "temperature.critical 90"
}'
done <<< "$data"
}
function get_temperature_value {
while read -r line; do
if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
echo "$line" | awk '/temperature:/{
print "multigraph temperature_graph_""'"$name"'";
}'
fi
echo "$line" | awk '/temperature:/{
gsub(/C/,"",$2);
print "temperature.value " $2
}'
done <<< "$data"
}
function get_cpu_label {
echo multigraph cpu_load_graph_"$name"
echo graph_title cpu load "$name"
echo graph_vlabel %
echo graph_category mikrotik
echo graph_args -l 0 --upper-limit 100
echo cpu_total.label total load
echo cpu_total.warning 75
echo cpu_total.critical 90
echo graph_info Total CPU Load and Load, IRQ and Disk per Core.
while [ "$c" -lt "$anzahl_cpu" ]; do
while read -r line; do
echo "$line" | grep cpu$c | awk '{
print "cpu"'"$c"'"_load.label " "cpu" "'"$c"'" " load"
print "cpu"'"$c"'"_irq.label " "cpu" "'"$c"'" " irq"
print "cpu"'"$c"'"_disk.label " "cpu" "'"$c"'" " disk"
}'
done <<< "$data"
c=$(( c + 1 ))
done
}
function get_cpu_value {
if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
echo multigraph cpu_load_graph_"$name"
fi
while read -r line; do
echo "$line" | awk '/cpu-load:/{
gsub(/%/,"",$2);
print "cpu_total.value " $2
}'
done <<< "$data"
c=0
while [ "$c" -lt "$anzahl_cpu" ]; do
while read -r line; do
echo "$line" | grep cpu$c | awk '{
gsub(/%/,"",$3);
gsub(/%/,"",$4);
gsub(/%/,"",$5);
print "cpu"'"$c"'"_load.value " $3
print "cpu"'"$c"'"_irq.value " $4
print "cpu"'"$c"'"_disk.value " $5
}'
done <<< "$data"
c=$(( c + 1 ))
done
}
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"'";
print "graph_vlabel MiB";
print "graph_category mikrotik";
print "graph_args -l 0";
print "total_memory.label total memory";
print "used_memory.label used memory";
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 }'
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"
fi
while read -r line; do
echo "$line" | awk '/total-memory:/{
gsub(/MiB/,"",$2);
print "total_memory.value " $2
}'
done <<< "$data"
while read -r line; do
echo "$line" | awk '/free-memory:/{
gsub(/MiB/,"",$2);
print "free_memory.value " $2
}'
done <<< "$data"
# berechne used-memory
# gesamt + frei = benutzt
echo used_memory.value "$(echo "$mem_total"-"$mem_free" | bc)"
}
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"'";
print "graph_vlabel KiB";
print "graph_category mikrotik";
print "graph_args -l 0";
print "total_disk.label total disk space";
print "free_disk.label free disk space";
print "graph_info Total & free disk space."}'
done <<< "$data"
}
function get_disk_value {
if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
echo multigraph disk_"$name"
fi
while read -r line; do
echo "$line" | grep KiB | awk '/free-hdd-space:/ {
gsub(/KiB/,"",$2)
print "free_disk.value " $2 }'
echo "$line" | grep MiB | awk '/free-hdd-space:/ {
gsub(/MiB/,"",$2)
print "free_disk.value " $2*1024}'
echo "$line" | grep KiB | awk '/total-hdd-space:/ {
gsub(/KiB/,"",$2)
print "total_disk.value " $2 }'
echo "$line" | grep MiB | awk '/total-hdd-space:/ {
gsub(/MiB/,"",$2)
print "total_disk.value " $2*1024 }'
done <<< "$data"
}
# rufe funktionen auf, reihenfolge ist wichtig
check_sshpass
check_bc
get_data
get_name
get_cpu_count
# munin-Logik
# wenn $1 = X; dann
if [ "$1" = "autoconf" ]; then
echo yes
exit 0
fi
if [ "$1" = "config" ]; then
if echo $data | grep 'temperature:' > /dev/null; then
# setze optionen
echo multigraph mikrotik_temperature_graph_$name
echo graph_title temperature $name # Titelzeile
echo graph_vlabel °C # Text links, hochkant
echo graph_category mikrotik # Kategorie
echo graph_args -l 0
echo temperature.label cpu temperature
echo temperature.warning 75
echo temperature.critical 90
# gebe label aus
# wenn dirtyconfig gesetzt rufe value funktion auf
get_voltage_label
if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
get_voltage_value
fi
if echo $data | grep 'voltage:' > /dev/null; then
echo multigraph mikrotik_voltage_graph_$name
echo graph_title voltage $name
echo graph_vlabel volt
echo graph_category mikrotik
echo graph_args -l 0
echo voltage.label voltage
get_bad_blocks_label
if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
get_bad_blocks_value
fi
if echo $data | grep 'bad-blocks:' > /dev/null; then
echo multigraph mikrotik_bad_blocks_graph_$name
echo graph_title bad blocks $name
echo graph_vlabel %
echo graph_category mikrotik
echo graph_args -l 0 --upper-limit 100
echo bad_blocks.label bad_blocks
echo bad_blocks.warning 2
echo bad_blocks.critical 25
get_write_sect_total_label
if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
get_write_sect_total_value
fi
if echo $data | grep 'write-sect-total:' > /dev/null; then
echo multigraph mikrotik_write_sect_total_graph_$name
echo graph_title Total sector writes $name
echo graph_vlabel count
echo graph_category mikrotik
echo graph_args -l 0
echo write_sect_total.label write_sect_total
get_write_sect_since_reboot_label
if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
get_write_sect_since_reboot_value
fi
if echo $data | grep 'write-sect-since-reboot:' > /dev/null; then
echo multigraph mikrotik_write_sect_since_reboot_graph_$name
echo graph_title Sector writes since reboot $name
echo graph_vlabel count
echo graph_category mikrotik
echo graph_args -l 0
echo write_sect_since_reboot.label write_sect_since_reboot
get_temperature_label
if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
get_temperature_value
fi
if echo $data | grep 'cpu-load:' > /dev/null; then
echo multigraph mikrotik_cpu_load_graph_$name
echo graph_title cpu load $name
echo graph_vlabel %
echo graph_category mikrotik
echo graph_args -l 0 --upper-limit 100
echo cpu_load.label cpu_load
echo cpu_load.warning 75
echo cpu_load.critical 90
echo cpu_load.draw AREA
get_cpu_label
if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
get_cpu_value
fi
if echo $data | grep 'cpu-count:' > /dev/null; then
echo multigraph mikrotik_cpu_load_detailed_graph_$name
echo graph_title cpu load detailed $name
echo graph_vlabel %
echo graph_category mikrotik
echo graph_args -l 0 --upper-limit 100
echo cpu_load_detailed.label cpu_load_detailed
get_memory_label
if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
get_memory_value
fi
get_disk_label
if [ "$MUNIN_CAP_DIRTYCONFIG" = "1" ]; then
get_disk_value
fi
exit 0
fi
#echo debug ######
#echo $ssh_user
#echo $ssh_password
#echo $ssh_host
while read -r line; do
if echo $line | grep 'temperature:' > /dev/null; then
echo $line | grep 'temperature:' | sed 's/C//g' | awk '{ print "multigraph mikrotik_temperature_graph_" "'"$name"'"; print "temperature.value "$2 }'
fi
if echo $line | grep 'voltage:' > /dev/null; then
echo $line | grep 'voltage:' | sed 's/V//g' | awk '{ print "multigraph mikrotik_voltage_graph_" "'"$name"'"; print "voltage.value "$2 }'
fi
if echo $line | grep 'cpu-load:' > /dev/null; then
echo $line | grep 'cpu-load:' | sed 's/%//g' | awk '{ print "multigraph mikrotik_cpu_load_graph_" "'"$name"'"; print "cpu_load.value "$2 }'
fi
if echo $line | grep 'bad-blocks:' > /dev/null; then
echo $line | grep 'bad-blocks:' | sed 's/%//g' | awk '{ print "multigraph mikrotik_bad_blocks_graph_" "'"$name"'"; print "bad_blocks.value "$2 }'
fi
if echo $line | grep 'write-sect-since-reboot:' > /dev/null; then
echo $line | grep 'write-sect-since-reboot:' | awk '{ print "multigraph mikrotik_write_sect_since_reboot_graph_" "'"$name"'"; print "write_sect_since_reboot.value "$2 }'
fi
if echo $line | grep 'write-sect-total:' > /dev/null; then
echo $line | grep 'write-sect-total:' | awk '{ print "multigraph mikrotik_write_sect_total_graph_" "'"$name"'"; print "write_sect_total.value "$2 }'
fi
if echo $line | grep 'cpu-count' > /dev/null; then
anzahl_cpu=$(echo $line | grep 'cpu-count:' | awk '{ print $2 }' | tr -dc '[:print:]')
# tr?
# error: ")syntax error: invalid arithmetic operator (error token is "
# https://stackoverflow.com/questions/23816264/remove-all-special-characters-and-case-from-string-in-bash
# --> https://www.shell-tips.com/bash/math-arithmetic-calculation/
echo multigraph mikrotik_cpu_load_detailed_graph_$name
echo vor schleife
echo $line | grep cpu0
c=0
echo c $c
echo anzahl $anzahl_cpu
while [ "$c" -lt "$anzahl_cpu" ]
do
echo in schleife
#FEHLER IST DAS GREP NIE CPU0 us ausgibt, hier fehlt before/after
echo $line | grep cpu$c | sed 's/%//g' | awk '{ print "cpu_load_detailed_graph_load cpu" "'"$c"'" $3; print "cpu_load_detailed_graph_irq cpu" "'"$c"'" $4; print "cpu_load_detailed_graph_disk cpu" "'"$c"'" $5}'
c=$(( $c + 1 ))
done
fi
done <<< "$data"
# wird nie aufgerufen wenn dirtyconfig gesetzt ist
# dann wird nur config aufgerufen und dabei werden zusätzlich die values ausgegeben
get_voltage_value
get_bad_blocks_value
get_write_sect_total_value
get_write_sect_since_reboot_value
get_temperature_value
get_cpu_value
get_memory_value
get_disk_value
exit 0
#suchbegriff
#name
# label besser benennen
#free-hdd-space: 4760.0KiB
#total-hdd-space: 16.3MiB
#free-memory: 210.2MiB
#total-memory: 256.0MiB
#uptime: 1w4d22h29m28s
#[admin@crs309] /system resource cpu> print
# CPU LOAD IRQ DISK
#0 cpu0 1% 0% 0%
#1 cpu1 0% 0% 0%
# dirtyconfig
# Beispieloutput:
# voltage: 24.5V
# temperature: 46C
# uptime: 1w6d17h12m25s
# version: 6.48.4 (stable)
# build-time: Aug/18/2021 06:43:27
# factory-software: 6.46.3
# free-memory: 209.9MiB
# total-memory: 256.0MiB
# cpu: MIPS 1004Kc V2.15
# cpu-count: 4
# cpu-frequency: 880MHz
# cpu-load: 0%
# free-hdd-space: 4760.0KiB
# total-hdd-space: 16.3MiB
# write-sect-since-reboot: 46412
# write-sect-total: 62012
# bad-blocks: 0%
# architecture-name: mmips
# board-name: hEX
# platform: MikroTik
# # CPU LOAD IRQ DISK
# 0 cpu0 0% 0% 0%
# 1 cpu1 1% 1% 0%
# 2 cpu2 0% 0% 0%
# 3 cpu3 1% 0% 0%
# name: hEX