#!/bin/bash function check_network_traffic() { total_inbound=0 total_outbound=0 for ((i=1; i<=$MEASUREMENT_COUNT; i++)); do usage=$(ifstat -i $INTERFACE $INTERVAL 1 | tail -n 1) if [[ -z "$usage" ]]; then log_error "No output from ifstat on measurement $i." exit 2 fi inbound=$(echo $usage | awk '{print $1}') outbound=$(echo $usage | awk '{print $2}') total_inbound=$(echo "$total_inbound + $inbound" | bc) total_outbound=$(echo "$total_outbound + $outbound" | bc) done avg_inbound=$(echo "scale=2; $total_inbound / $MEASUREMENT_COUNT" | bc) avg_outbound=$(echo "scale=2; $total_outbound / $MEASUREMENT_COUNT" | bc) if (( $(echo "$avg_inbound < $THRESHOLD_INBOUND" | bc -l) )) || (( $(echo "$avg_outbound < $THRESHOLD_OUTBOUND" | bc -l) )); then log_critical "Network traffic below thresholds. Avg In: $avg_inbound KB/s, Avg Out: $avg_outbound KB/s." restart_service exit 2 # Exit with error status if thresholds are not met else log_info "Network traffic is above thresholds. Avg In: $avg_inbound KB/s, Avg Out: $avg_outbound KB/s. Nothing to do." exit 0 # Exit with success status fi }