#!/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/agents/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 get_cpu_data(){ read -r _ u n s id io ir sir st _ < /proc/stat echo $((u+n+s+id+io+ir+sir+st)) $((id+io)) } function get_cpu_value(){ local total_base=$1 local idle_base=$2 local total= local idle= local cpu= read -r total idle <<< $(get_cpu_data) cpu=$(awk -v total_base="$total_base" -v idle_base="$idle_base" -v total="$total" -v idle="$idle" 'BEGIN{ total_final = total - total_base; printf "%.6f", 100 * (total_final - (idle - idle_base)) / total_final; }') echo $cpu $total $idle } 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 swaps=$(cat /proc/swaps|grep /dev/|awk '{ gsub(/\/dev\//, "", $1); results = $1 ":" ($3 - $4) * 1024; swaps = swaps != "" ? swaps "," results : results; }END{ print swaps; }') local disks=$(lsblk -b -n -o name,kname,size,fsavail,mountpoint|grep -E '└─|├─'|awk -v swap_map="$swaps" 'BEGIN{ l = split(swap_map, free, ","); for(i = 1; i <= l; i++){ split(free[i], item, ":"); swaps[item[1]] = item[2]; } }{ gsub(/├─|└─/, "", $1); device = $1; total = $3; if($5 == "[SWAP]" || $4 == "[SWAP]"){ available = swaps[$2] != "" ? swaps[$2] : 0; mountpoint = "null"; }else{ available = $4 ~ /^\// || $4 == "" ? 0 : $4; mountpoint = $5 != "" ? "\"" $5 "\"" : $4 ~ /^\// ? "null" : "\"" $4 "\""; } array = "[\"" device "\"," total "," available "," mountpoint "]"; json = json == "" ? array : json "," array; }END{ print "[" json "]"; }') local iterations=0 local cpu_total= local cpu_idle= 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) read -r cpu_total cpu_idle <<< $(get_cpu_data) while true; do memory=$(cat /proc/meminfo|grep MemAvailable:|awk '{print $2 * 1024}') memory_average=$(echo "$memory_average $memory"|awk '{print $1 + $2}') memory_out=$memory iterations=$((iterations + 1)) if [ -z "$memory_in" ]; then memory_in=$memory memory_minimum=$memory memory_maximum=$memory else awk -v t="$memory" -v m="$memory_minimum" 'BEGIN{exit !(t < m)}' && memory_minimum=$memory awk -v t="$memory" -v m="$memory_maximum" 'BEGIN{exit !(t > m)}' && memory_maximum=$memory if [ $(date +%s) -ge $limit_seconds ]; then break fi fi sleep $candle_sleep_seconds read -r cpu cpu_total cpu_idle <<< $(get_cpu_value $cpu_total $cpu_idle) cpu_average=$(echo "$cpu_average $cpu"|awk '{print $1 + $2}') cpu_out=$cpu if [ -z "$cpu_in" ]; then cpu_in=$cpu cpu_minimum=$cpu cpu_maximum=$cpu else awk -v t="$cpu" -v m="$cpu_minimum" 'BEGIN{exit !(t < m)}' && cpu_minimum=$cpu awk -v t="$cpu" -v m="$cpu_maximum" 'BEGIN{exit !(t > m)}' && cpu_maximum=$cpu fi done if [ -z "$domain" ]; then domain="null" else domain="\"$domain\"" fi cpu_average=$(echo "$cpu_average $iterations"|awk '{printf "%.6f", $1 / $2}') memory_average=$(echo "$memory_average $iterations"|awk '{printf "%.6f", $1 / $2}') 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