From 0c300ee3df22424e34b97e7da131eb863db12b0a Mon Sep 17 00:00:00 2001 From: hhftechnologies Date: Mon, 25 Nov 2024 18:03:09 +0530 Subject: [PATCH] update --- monitoring/monitoring.sh | 227 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 227 insertions(+) create mode 100644 monitoring/monitoring.sh diff --git a/monitoring/monitoring.sh b/monitoring/monitoring.sh new file mode 100644 index 0000000..48b68dc --- /dev/null +++ b/monitoring/monitoring.sh @@ -0,0 +1,227 @@ +#!/bin/bash + +#-----------------------------------# +# VARIABLES # +#-----------------------------------# + +this_script_url="https://git.hhf.technology/hhf/script-management-cloudpanel/raw/branch/main/monitoring/monitoring.sh" +this_script_name="System Monitoring Script" +formatter_url="https://git.hhf.technology/hhf/TaskFormatter/raw/branch/main/bash_task_formatter/formatter_new.sh" +scriptname=$0 + +# Initialize success flag +success=0 + +# Determine the user (use the first argument if provided, otherwise fallback) +USER_TO_RUN_AS="${1:-$SUDO_USER}" +USER_HOME=$(eval echo ~$USER_TO_RUN_AS) + +#-----------------------------------# +# FORMATTER # +#-----------------------------------# + +# Download and source the formatter with error handling +download_formatter() { + if [ ! -f "task_formatter.sh" ]; then + if ! wget "$formatter_url" --no-check-certificate -O task_formatter.sh > /dev/null 2>&1; then + echo "Error: Failed to download task_formatter.sh" + exit 1 + fi + fi + + if [ ! -f "task_formatter.sh" ]; then + echo "Error: task_formatter.sh not found after download attempt" + exit 1 + fi + + # Make the formatter executable + chmod +x task_formatter.sh + + # Source the formatter with error checking + source ./task_formatter.sh || { echo "Error: Failed to source task_formatter.sh"; exit 1; } + + # Check if print_header is available after sourcing + if ! declare -f print_header > /dev/null; then + echo "Error: print_header function not found after sourcing." + exit 1 + fi +} + +# Call the download_formatter function +download_formatter + +#-----------------------------------# +# FUNCTIONS # +#-----------------------------------# + +# Function to install required packages +install_dependencies() { + if ! dpkg -l | grep -q sysstat || ! dpkg -l | grep -q ifstat; then + sudo apt-get update > /dev/null 2>&1 + sudo apt-get install -y sysstat ifstat > /dev/null 2>&1 + if [ $? -eq 0 ]; then + echo -e "Dependencies installed $CHECK_MARK" + return 0 + else + echo -e "Failed to install dependencies $CROSS_MARK" + return 1 + fi + else + echo -e "Dependencies already installed $CHECK_MARK" + return 0 + fi +} + +# Function to check CPU usage +check_cpu_usage() { + mpstat 1 1 | awk '/all/ {print "CPU Load: " $3 "% idle"}' > /tmp/cpu_stats + if [ -s /tmp/cpu_stats ]; then + cat /tmp/cpu_stats + rm /tmp/cpu_stats + echo -e "CPU check completed $CHECK_MARK" + return 0 + else + echo -e "CPU check failed $CROSS_MARK" + return 1 + fi +} + +# Function to check memory usage +check_memory_usage() { + echo "Memory Statistics:" + free -h | awk '/Mem/ {print "Total Memory: " $2 "\nUsed: " $3 "\nFree: " $4}' + echo "Swap Statistics:" + free -h | awk '/Swap/ {print "Total: " $2 ", Used: " $3 ", Free: " $4}' + echo -e "Memory check completed $CHECK_MARK" + return 0 +} + +# Function to check disk usage +check_disk_usage() { + df -h | grep '^/dev' | awk '{print $1 ": " $5 " used, " $4 " available"}' + echo -e "Disk check completed $CHECK_MARK" + return 0 +} + +# Function to check network traffic +check_network_traffic() { + if ifstat -i eth0 1 1 > /tmp/network_stats 2>/dev/null; then + awk 'NR==3 {print "RX: " $1 " KB/s, TX: " $2 " KB/s"}' /tmp/network_stats + rm /tmp/network_stats + echo -e "Network check completed $CHECK_MARK" + return 0 + else + echo -e "Network check failed $CROSS_MARK" + return 1 + fi +} + +# Function to check top processes +check_top_processes() { + echo "Top 5 Memory Consuming Processes:" + ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -n 6 + + echo -e "\nTop 5 CPU Consuming Processes:" + ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head -n 6 + echo -e "Process check completed $CHECK_MARK" + return 0 +} + +# Function to check system logs +check_system_logs() { + if journalctl -p 3 -xb --no-pager | tail -n 10 > /tmp/system_logs 2>/dev/null; then + cat /tmp/system_logs + rm /tmp/system_logs + echo -e "Log check completed $CHECK_MARK" + return 0 + else + echo -e "Log check failed $CROSS_MARK" + return 1 + fi +} + +# Remove created files on cleanup +cleanup_files() { + rm -f /tmp/cpu_stats /tmp/network_stats /tmp/system_logs + echo -e "Cleaned up temporary files $CHECK_MARK" + return 0 +} + +# Remove the script itself +remove_script() { + if [ -f "$0" ]; then + echo "Deleted monitoring script..." + rm -- "$0" + fi + if [ -f "task_formatter.sh" ]; then + rm task_formatter.sh + fi + echo -e "Cleaned up $CHECK_MARK" + return 0 +} + +#-----------------------------------# +# MAIN LOGIC # +#-----------------------------------# + +# Check if print_header function exists +if ! command -v print_header >/dev/null 2>&1; then + echo "Error: print_header function not found. Formatter may not be properly sourced." + exit 1 +fi + +# Print header +print_header "$this_script_name" "$this_script_url" + +echo -e "Running as User: $USER_TO_RUN_AS\nUser Home: $USER_HOME\n" + +# Run the monitoring functions with formatted output +if ! format_output install_dependencies "Installing Dependencies"; then + cleanup_files + success=1 +fi + +if ! format_output check_cpu_usage "Checking CPU Usage"; then + cleanup_files + success=1 +fi + +if ! format_output check_memory_usage "Checking Memory Usage"; then + cleanup_files + success=1 +fi + +if ! format_output check_disk_usage "Checking Disk Usage"; then + cleanup_files + success=1 +fi + +if ! format_output check_network_traffic "Checking Network Traffic"; then + cleanup_files + success=1 +fi + +if ! format_output check_top_processes "Checking Top Processes"; then + cleanup_files + success=1 +fi + +if ! format_output check_system_logs "Checking System Logs"; then + cleanup_files + success=1 +fi + +format_output cleanup_files "Cleaning up temporary files" +format_output remove_script "Removing script" + +# Check if final_message function exists +if ! command -v final_message >/dev/null 2>&1; then + echo "Error: final_message function not found. Formatter may not be properly sourced." + exit 1 +fi + +# Print final message +final_message "$this_script_name" $success + +# Exit with appropriate status +exit $success \ No newline at end of file