shellcheck

This commit is contained in:
Michael Grote 2021-09-24 09:59:00 +02:00
parent 18392feade
commit 7ff0b5857f

View file

@ -20,10 +20,6 @@
# env.ssh_password hallowelt
# env.ssh_host 192.168.2.1
# TODO:
# - [ ] uptime: 1w4d22h29m28s
# - [ ] linter
# setze Variablen mit default-Werten
ssh_user=${ssh_user:-user}
ssh_password=${ssh_password:-password}
@ -33,15 +29,15 @@ c=0 # zähler; wird für verschiedene Schleifen benötigt
# 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 }')
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]')
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"
}
@ -59,18 +55,18 @@ function check_bc {
}
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')
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.]'
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.]'
echo "$line" | awk '/free-memory:/{ gsub(/MiB/,"",$2); print $2 }' | tr -dc '0-9.'
done <<< "$data")
}
@ -81,7 +77,7 @@ function get_voltage_label {
# wenn gefunden:
# gebe jede zeile per print text aus
# externe/bash-variablen mit "'"<var>"'"
echo $line | awk '/voltage:/{
echo "$line" | awk '/voltage:/{
print "multigraph voltage_graph_""'"$name"'";
print "graph_title voltage " "'"$name"'";
print "graph_vlabel volt";
@ -99,13 +95,13 @@ function get_voltage_value {
# 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:/{
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:/{
echo "$line" | awk '/voltage:/{
gsub(/V/,"",$2);
print "voltage.value " $2
}'
@ -114,7 +110,7 @@ function get_voltage_value {
function get_bad_blocks_label {
while read -r line; do
echo $line | awk '/bad-blocks:/{
echo "$line" | awk '/bad-blocks:/{
print "multigraph bad_blocks_graph_""'"$name"'";
print "graph_title bad blocks " "'"$name"'";
print "graph_vlabel %";
@ -130,11 +126,11 @@ function get_bad_blocks_label {
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:/{
echo "$line" | awk '/bad-blocks:/{
print "multigraph bad_blocks_graph_""'"$name"'";
}'
fi
echo $line | awk '/bad-blocks:/{
echo "$line" | awk '/bad-blocks:/{
gsub(/%/,"",$2);
print "bad_blocks.value " $2
}'
@ -143,7 +139,7 @@ function get_bad_blocks_value {
function get_write_sect_total_label {
while read -r line; do
echo $line | awk '/write-sect-total:/{
echo "$line" | awk '/write-sect-total:/{
print "multigraph write_sect_total_graph_""'"$name"'";
print "graph_title Total sector writes " "'"$name"'";
print "graph_vlabel count";
@ -157,11 +153,11 @@ function get_write_sect_total_label {
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:/{
echo "$line" | awk '/write-sect-total:/{
print "multigraph write_sect_total_graph_""'"$name"'";
}'
fi
echo $line | awk '/write-sect-total:/{
echo "$line" | awk '/write-sect-total:/{
print "write_sect_total.value " $2
}'
done <<< "$data"
@ -169,7 +165,7 @@ function get_write_sect_total_value {
function get_write_sect_since_reboot_label {
while read -r line; do
echo $line | awk '/write-sect-since-reboot:/{
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";
@ -183,11 +179,11 @@ function get_write_sect_since_reboot_label {
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:/{
echo "$line" | awk '/write-sect-since-reboot:/{
print "multigraph write_sect_since_reboot_graph_""'"$name"'";
}'
fi
echo $line | awk '/write-sect-since-reboot:/{
echo "$line" | awk '/write-sect-since-reboot:/{
print "write_sect_since_reboot.value " $2
}'
done <<< "$data"
@ -195,7 +191,7 @@ function get_write_sect_since_reboot_value {
function get_temperature_label {
while read -r line; do
echo $line | awk '/temperature:/{
echo "$line" | awk '/temperature:/{
print "multigraph temperature_graph_""'"$name"'";
print "graph_title temperature " "'"$name"'";
print "graph_vlabel °C";
@ -210,11 +206,11 @@ function get_temperature_label {
function get_temperature_value {
while read -r line; do
if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
echo $line | awk '/temperature:/{
echo "$line" | awk '/temperature:/{
print "multigraph temperature_graph_""'"$name"'";
}'
fi
echo $line | awk '/temperature:/{
echo "$line" | awk '/temperature:/{
gsub(/C/,"",$2);
print "temperature.value " $2
}'
@ -222,8 +218,8 @@ function get_temperature_value {
}
function get_cpu_label {
echo multigraph cpu_load_graph_$name
echo graph_title cpu load $name
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
@ -234,21 +230,21 @@ function get_cpu_label {
while [ "$c" -lt "$anzahl_cpu" ]; do
while read -r line; do
echo $line | grep cpu$c | awk '{
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 ))
c=$(( c + 1 ))
done
}
function get_cpu_value {
if [ "$MUNIN_CAP_DIRTYCONFIG" = "0" ] || [ -z "$MUNIN_CAP_DIRTYCONFIG" ]; then
echo multigraph cpu_load_graph_$name
echo multigraph cpu_load_graph_"$name"
fi
while read -r line; do
echo $line | awk '/cpu-load:/{
echo "$line" | awk '/cpu-load:/{
gsub(/%/,"",$2);
print "cpu_total.value " $2
}'
@ -256,7 +252,7 @@ function get_cpu_value {
c=0
while [ "$c" -lt "$anzahl_cpu" ]; do
while read -r line; do
echo $line | grep cpu$c | awk '{
echo "$line" | grep cpu$c | awk '{
gsub(/%/,"",$3);
gsub(/%/,"",$4);
gsub(/%/,"",$5);
@ -265,7 +261,7 @@ function get_cpu_value {
print "cpu"'"$c"'"_disk.value " $5
}'
done <<< "$data"
c=$(( $c + 1 ))
c=$(( c + 1 ))
done
}
@ -273,7 +269,7 @@ function get_memory_label {
get_mem_total
get_mem_free
while read -r line; do
echo $line | awk '/free-memory:/{
echo "$line" | awk '/free-memory:/{
print "multigraph memory_graph_""'"$name"'";
print "graph_title memory " "'"$name"'";
print "graph_vlabel MiB";
@ -292,28 +288,28 @@ 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_"$name"
fi
while read -r line; do
echo $line | awk '/total-memory:/{
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:/{
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)
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:/{
echo "$line" | awk '/free-hdd-space:/{
print "multigraph disk_graph_""'"$name"'";
print "graph_title disk " "'"$name"'";
print "graph_vlabel KiB";
@ -326,19 +322,19 @@ 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_"$name"
fi
while read -r line; do
echo $line | grep KiB | awk '/free-hdd-space:/ {
echo "$line" | grep KiB | awk '/free-hdd-space:/ {
gsub(/KiB/,"",$2)
print "free_disk.value " $2 }'
echo $line | grep MiB | awk '/free-hdd-space:/ {
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:/ {
echo "$line" | grep KiB | awk '/total-hdd-space:/ {
gsub(/KiB/,"",$2)
print "total_disk.value " $2 }'
echo $line | grep MiB | awk '/total-hdd-space:/ {
echo "$line" | grep MiB | awk '/total-hdd-space:/ {
gsub(/MiB/,"",$2)
print "total_disk.value " $2*1024 }'
done <<< "$data"