NucelarMonitor/Bash/NucelarMonitor.debian.script.sh

160 lines
4.8 KiB
Bash
Executable File

#!/bin/bash
# Settings.
key="kyman_9750h"
candle_seconds=10
candle_sleep_seconds=1
execute_sleep_seconds=0
show_json=true
send_json=true
url_server="http://192.168.1.131:13000/debian"
# Settings.
function get_net_data(){
echo $(cat /proc/net/dev|tail -n +3|awk '{
interface = $1;
gsub(/:/, "", interface);
array = "[\"" interface "\"," $2 "," $3 "," $4 "," $10 "," $11 "," $12 "]";
json = json == "" ? array : json "," array;
}END{
print "[" json "]";
}')
}
function execute(){
local domain=$(hostname -d)
local ips=$(ip -o addr|awk '{
is_ipv6 = $3 == "inet6" ? "true" : "false";
split($1, i, ":");
split($4, ip, "/");
array = "[" i[1] ",\"" $2 "\"," is_ipv6 ",\"" ip[1] "\"," ip[2] "]";
json = json == "" ? array : json "," array;
}END{
print "[" json "]";
}')
local hostnames=$(hostname -A|awk '{
json = json == "" ? "\"" $1 "\"" : json ",\"" $1 "\"";
}END{
print "[" json "]";
}')
local disks=$(lsblk -b -n -o name,size,fsavail,mountpoint|grep -E '└─|├─'|awk '{
device = $1;
total = $2;
available = $3 ~ /^\// || $3 == "" ? 0 : $3;
mountpoint = $4 != "" ? "\"" $4 "\"" : $3 ~ /^\// ? "null" : "\"" $3 "\"";
gsub(/├─|└─/, "", device);
array = "[\"" device "\"," total "," available "," mountpoint "]";
json = json == "" ? array : json "," array;
}END{
print "[" json "]";
}')
local iterations=0
local cpu_in=
local cpu_out=
local cpu_minimum=
local cpu_maximum=
local cpu_average=0
local memory_in=
local memory_out=
local memory_minimum=
local memory_maximum=
local memory_average=0
local memory_total=$(cat /proc/meminfo|grep MemTotal:|awk '{print $2 * 1024}')
local limit_seconds=$(($(date +%s) + $candle_seconds))
local json="["
local net_data="["
local candle_start=$(date +%s)
local candle_end=
net_data=$net_data$(get_net_data)
while true; do
cpu=$(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}')
# memory=$(cat /proc/meminfo|grep MemFree:|awk '{print $2 * 1024}')
memory=$(cat /proc/meminfo|grep MemAvailable:|awk '{print $2 * 1024}')
# cpu_average=$((cpu_average + cpu))
# memory_average=$((memory_average + memory))
cpu_average=$(echo "$cpu_average + $cpu"|bc -l)
memory_average=$(echo "$memory_average + $memory"|bc -l)
cpu=${cpu/,/.}
cpu_out=$cpu
memory_out=$memory
iterations=$((iterations + 1))
if [ -z "$cpu_in" ]; then
cpu_in=$cpu
memory_in=$memory
cpu_minimum=$cpu
cpu_maximum=$cpu
memory_minimum=$memory
memory_maximum=$memory
else
if [ "$(echo "$cpu < $cpu_minimum"|bc -l)" -eq 1 ];then cpu_minimum=$cpu;fi
if [ "$(echo "$cpu > $cpu_maximum"|bc -l)" -eq 1 ];then cpu_maximum=$cpu;fi
if [ "$(echo "$memory < $memory_minimum"|bc -l)" -eq 1 ];then memory_minimum=$memory;fi
if [ "$(echo "$memory > $memory_maximum"|bc -l)" -eq 1 ];then memory_maximum=$memory;fi
# cpu_minimum=$(echo -e "$cpu_minimum\n$cpu" | sort -n | head -1)
# cpu_maximum=$(echo -e "$cpu_maximum\n$cpu" | sort -n | tail -1)
# memory_minimum=$(echo -e "$memory_minimum\n$memory" | sort -n | head -1)
# memory_maximum=$(echo -e "$memory_maximum\n$memory" | sort -n | tail -1)
if [ $(date +%s) -ge $limit_seconds ]; then
break
fi
sleep 1
fi
done
if [ -z "$domain" ]; then
domain="null"
else
domain="\"$domain\""
fi
# cpu_average=$((cpu_average / iterations))
# memory_average=$((memory_average / iterations))
cpu_average=$(echo "scale=6; $cpu_average / $iterations"|bc|awk '{printf $1 + 0}')
memory_average=$(echo "scale=6; $memory_average / $iterations"|bc|awk '{printf $1 + 0}')
net_data="$net_data,$(get_net_data)]"
candle_end=$(date +%s)
json="$json$hostnames,$domain,$ips,$disks,$iterations"
json="$json,[$candle_start,$candle_end]"
json="$json,[${cpu_in//,/.},${cpu_out//,/.},${cpu_minimum//,/.},${cpu_maximum//,/.},$cpu_average]"
json="$json,[$memory_total,$memory_in,$memory_out,$memory_minimum,$memory_maximum,$memory_average]"
json="$json,$net_data"
json="$json]"
if [ "$show_json" = true ]; then echo "$json";fi
if [ "$send_json" = true ]; then
local response=$(echo "$json"|curl -s -X POST -H "Content-Type: application/json" -d @- "$url_server/$key")
fi
}
while true; do
execute
if [ $execute_sleep_seconds -gt 0 ]; then
sleep $execute_sleep_seconds
fi
done