From 153ec06a474dd5c79cd93d1fa6f487953a0fa793 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Fri, 4 Oct 2024 12:30:35 +0530 Subject: [PATCH 01/48] Update Shell --- Shell | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Shell b/Shell index 345e6ae..0e2e0fb 100644 --- a/Shell +++ b/Shell @@ -1 +1,4 @@ -Test +FIDS=$(ps -eo user | sort | uniq) + +echo "List of all unique FIDs running on the machine:" + echo "$FIDS" From f20fd6bea4e5f511053efa60e2b6cdd58a12948d Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Fri, 4 Oct 2024 12:32:43 +0530 Subject: [PATCH 02/48] Update Shell --- Shell | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/Shell b/Shell index 0e2e0fb..d668763 100644 --- a/Shell +++ b/Shell @@ -1,4 +1,94 @@ +#!/bin/bash + +# 1. Capture date and time +DATE=$(date '+%Y-%m-%d %H:%M:%S') +TIMESTAMP=$(date '+%Y%m%d_%H%M%S') + +# 2. Define directory and file path +OUTPUT_DIR="/tmp/CPU_Utilization" +OUTPUT_FILE="$OUTPUT_DIR/CPU_Utilization_$TIMESTAMP.txt" + +# 3. Check if the directory exists, if not create it +if [ ! -d "$OUTPUT_DIR" ]; then + mkdir -p "$OUTPUT_DIR" +fi + +# 4. Capture hostname +HOSTNAME=$(hostname) + +# 5. Capture CPU utilization using 'top' command +CPU_USAGE_TOP=$(top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}') + +# 6. Capture CPU utilization using 'vmstat' command +CPU_USAGE_VMSTAT=$(vmstat 1 2 | tail -1 | awk '{print $13+$14}') + +# 7. Capture per-core CPU usage using 'mpstat' command (from sysstat package) +MPSTAT_OUTPUT=$(mpstat -P ALL 1 1) + +# 8. Capture detailed CPU statistics using 'iostat' command +IOSTAT_OUTPUT=$(iostat -c 1 1) + +# 9. Read CPU utilization data directly from /proc/stat file +PROC_STAT=$(cat /proc/stat | grep '^cpu ' | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage}') + +# 10. Capture disk usage of the root '/' partition using 'df -h' +DISK_USAGE=$(df -h / | grep / | awk '{print $5}') + +# 11. Capture memory usage using 'free' command +MEMORY_USAGE=$(free -h | grep "Mem:" | awk '{print "Total: "$2", Used: "$3", Free: "$4}') + +# 12. Capture system uptime and load average using 'uptime' +UPTIME=$(uptime -p) +LOAD_AVG=$(uptime | awk -F'load average:' '{ print $2 }') + +# 13. Capture number of active TCP connections using 'ss' (if installed) or 'netstat' +TCP_CONNECTIONS=$(ss -t | wc -l) + +# 14. Capture number of running processes using 'ps' +RUNNING_PROCESSES=$(ps -e --no-headers | wc -l) + +# 15. Capture top 5 memory-hogging processes +TOP_MEMORY_PROCESSES=$(ps aux --sort=-%mem | head -n 6) + +# 16. Capture top 5 CPU-hogging processes +TOP_CPU_PROCESSES=$(ps aux --sort=-%cpu | head -n 6) + +# 17. Capture all unique FIDs running on the machine FIDS=$(ps -eo user | sort | uniq) -echo "List of all unique FIDs running on the machine:" +# 18. Write the information to both the output file and the console using 'tee' +{ + echo "[$DATE] Hostname: $HOSTNAME" + echo "CPU Utilization (top): $CPU_USAGE_TOP%" + echo "CPU Utilization (vmstat): $CPU_USAGE_VMSTAT%" + echo "Disk Usage: $DISK_USAGE" + echo + echo "Memory Usage:" + echo "$MEMORY_USAGE" + echo + echo "Uptime: $UPTIME" + echo "Load Average: $LOAD_AVG" + echo + echo "Active TCP Connections: $TCP_CONNECTIONS" + echo "Number of Running Processes: $RUNNING_PROCESSES" + echo + echo "Top 5 Memory-Hogging Processes:" + echo "$TOP_MEMORY_PROCESSES" + echo + echo "Top 5 CPU-Hogging Processes:" + echo "$TOP_CPU_PROCESSES" + echo + echo "Per-core CPU Utilization (mpstat):" + echo "$MPSTAT_OUTPUT" + echo + echo "Detailed CPU stats (iostat):" + echo "$IOSTAT_OUTPUT" + echo + echo "CPU Usage from /proc/stat: $PROC_STAT%" + echo + echo "List of all unique FIDs running on the machine:" echo "$FIDS" +} | tee "$OUTPUT_FILE" + +# Optional message to confirm that the output has been saved +echo "CPU utilization, memory, and other system information saved to $OUTPUT_FILE" From 4865188d537197d960507139b75a7ad15f7efc7b Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Fri, 4 Oct 2024 12:36:20 +0530 Subject: [PATCH 03/48] Update Shell --- Shell | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Shell b/Shell index d668763..6da9f2f 100644 --- a/Shell +++ b/Shell @@ -53,8 +53,8 @@ TOP_MEMORY_PROCESSES=$(ps aux --sort=-%mem | head -n 6) # 16. Capture top 5 CPU-hogging processes TOP_CPU_PROCESSES=$(ps aux --sort=-%cpu | head -n 6) -# 17. Capture all unique FIDs running on the machine -FIDS=$(ps -eo user | sort | uniq) +# 17. Calculate top 5 FIDs by total CPU utilization +TOP_FID_CPU=$(ps -eo user,%cpu --sort=-%cpu | awk '{cpu[$1]+=$2} END {for (u in cpu) print u, cpu[u]}' | sort -k2 -nr | head -n 5) # 18. Write the information to both the output file and the console using 'tee' { @@ -86,8 +86,9 @@ FIDS=$(ps -eo user | sort | uniq) echo echo "CPU Usage from /proc/stat: $PROC_STAT%" echo - echo "List of all unique FIDs running on the machine:" - echo "$FIDS" + echo "Top 5 FIDs by Total CPU Utilization:" + echo "FID, Total CPU%" + echo "$TOP_FID_CPU" } | tee "$OUTPUT_FILE" # Optional message to confirm that the output has been saved From 3887a5f708673e83e516c1bf68dc8a876475523f Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Fri, 4 Oct 2024 12:41:13 +0530 Subject: [PATCH 04/48] Create CPU_Utili --- CPU_Utili | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 CPU_Utili diff --git a/CPU_Utili b/CPU_Utili new file mode 100644 index 0000000..2e8c145 --- /dev/null +++ b/CPU_Utili @@ -0,0 +1,114 @@ +#!/bin/bash + +# 1. Capture date and time +DATE=$(date '+%Y-%m-%d %H:%M:%S') +TIMESTAMP=$(date '+%Y%m%d_%H%M%S') + +# 2. Define directory and file path +OUTPUT_DIR="/tmp/CPU_Utilization" +OUTPUT_FILE="$OUTPUT_DIR/CPU_Utilization_$TIMESTAMP.txt" + +# 3. Check if the directory exists, if not create it +if [ ! -d "$OUTPUT_DIR" ]; then + mkdir -p "$OUTPUT_DIR" +fi + +# 4. Capture hostname +HOSTNAME=$(hostname) + +# 5. Capture CPU utilization using 'top' command +CPU_USAGE_TOP=$(top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}') + +# 6. Capture CPU utilization using 'vmstat' command +CPU_USAGE_VMSTAT=$(vmstat 1 2 | tail -1 | awk '{print $13+$14}') + +# 7. Capture per-core CPU usage using 'mpstat' command (from sysstat package) +MPSTAT_OUTPUT=$(mpstat -P ALL 1 1) + +# 8. Capture detailed CPU statistics using 'iostat' command +IOSTAT_OUTPUT=$(iostat -c 1 1) + +# 9. Read CPU utilization data directly from /proc/stat file +PROC_STAT=$(cat /proc/stat | grep '^cpu ' | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage}') + +# 10. Capture disk usage of the root '/' partition using 'df -h' +DISK_USAGE=$(df -h / | grep / | awk '{print $5}') + +# 11. Capture memory usage using 'free' command +MEMORY_USAGE=$(free -h | grep "Mem:" | awk '{print "Total: "$2", Used: "$3", Free: "$4}') + +# 12. Capture system uptime and load average using 'uptime' +UPTIME=$(uptime -p) +LOAD_AVG=$(uptime | awk -F'load average:' '{ print $2 }') + +# 13. Capture number of active TCP connections using 'ss' (if installed) or 'netstat' +TCP_CONNECTIONS=$(ss -t | wc -l) + +# 14. Capture number of running processes using 'ps' +RUNNING_PROCESSES=$(ps -e --no-headers | wc -l) + +# 15. Capture top 5 memory-hogging processes +TOP_MEMORY_PROCESSES=$(ps aux --sort=-%mem | head -n 6) + +# 16. Capture top 5 CPU-hogging processes +TOP_CPU_PROCESSES=$(ps aux --sort=-%cpu | head -n 6) + +# 17. Calculate top 5 FIDs by total CPU utilization +TOP_FID_CPU=$(ps -eo user,%cpu --sort=-%cpu | awk '{cpu[$1]+=$2} END {for (u in cpu) print u, cpu[u]}' | sort -k2 -nr | head -n 5) + +# 18. Display the top 5 FIDs to the user +echo "Top 5 FIDs by Total CPU Utilization:" +echo "No. FID Total CPU%" +echo "$TOP_FID_CPU" | nl -w2 -s'. ' + +# 19. Prompt the user to select an FID +echo +read -p "Enter the number of the FID you want to view detailed process info for: " FID_SELECTION + +# 20. Extract the FID based on the user's selection +SELECTED_FID=$(echo "$TOP_FID_CPU" | sed -n "${FID_SELECTION}p" | awk '{print $1}') + +# 21. Display the top 10 CPU-consuming processes for the selected FID +echo +echo "Top 10 CPU-consuming processes for FID '$SELECTED_FID':" +ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,comm | head -n 10 | tee -a "$OUTPUT_FILE" + +# 22. Write the main system information and top FIDs to the output file +{ + echo "[$DATE] Hostname: $HOSTNAME" + echo "CPU Utilization (top): $CPU_USAGE_TOP%" + echo "CPU Utilization (vmstat): $CPU_USAGE_VMSTAT%" + echo "Disk Usage: $DISK_USAGE" + echo + echo "Memory Usage:" + echo "$MEMORY_USAGE" + echo + echo "Uptime: $UPTIME" + echo "Load Average: $LOAD_AVG" + echo + echo "Active TCP Connections: $TCP_CONNECTIONS" + echo "Number of Running Processes: $RUNNING_PROCESSES" + echo + echo "Top 5 Memory-Hogging Processes:" + echo "$TOP_MEMORY_PROCESSES" + echo + echo "Top 5 CPU-Hogging Processes:" + echo "$TOP_CPU_PROCESSES" + echo + echo "Per-core CPU Utilization (mpstat):" + echo "$MPSTAT_OUTPUT" + echo + echo "Detailed CPU stats (iostat):" + echo "$IOSTAT_OUTPUT" + echo + echo "CPU Usage from /proc/stat: $PROC_STAT%" + echo + echo "Top 5 FIDs by Total CPU Utilization:" + echo "$TOP_FID_CPU" + echo + echo "Top 10 CPU-consuming processes for FID '$SELECTED_FID':" + ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,comm | head -n 10 +} >> "$OUTPUT_FILE" + +# Confirm that the information has been saved +echo "Detailed CPU utilization for FID '$SELECTED_FID' saved to $OUTPUT_FILE" From ed43103c003200e0779f7a31836a9fdd4d9d8f3f Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Fri, 4 Oct 2024 12:48:05 +0530 Subject: [PATCH 05/48] Update CPU_Utili --- CPU_Utili | 128 ++++++++++++++++++++---------------------------------- 1 file changed, 48 insertions(+), 80 deletions(-) diff --git a/CPU_Utili b/CPU_Utili index 2e8c145..a09287f 100644 --- a/CPU_Utili +++ b/CPU_Utili @@ -1,114 +1,82 @@ #!/bin/bash -# 1. Capture date and time +# Helper function for headers with clear separation +print_header() { + echo + echo "====== $1 ======" +} + +# Optional: Function to add color for critical information (e.g., CPU usage) +print_critical() { + tput setaf 1; echo "$1"; tput sgr0 +} + +# Capture date and time DATE=$(date '+%Y-%m-%d %H:%M:%S') TIMESTAMP=$(date '+%Y%m%d_%H%M%S') - -# 2. Define directory and file path OUTPUT_DIR="/tmp/CPU_Utilization" OUTPUT_FILE="$OUTPUT_DIR/CPU_Utilization_$TIMESTAMP.txt" -# 3. Check if the directory exists, if not create it -if [ ! -d "$OUTPUT_DIR" ]; then - mkdir -p "$OUTPUT_DIR" -fi +# Create directory if not exists +[ ! -d "$OUTPUT_DIR" ] && mkdir -p "$OUTPUT_DIR" -# 4. Capture hostname +# Capture general system information HOSTNAME=$(hostname) - -# 5. Capture CPU utilization using 'top' command CPU_USAGE_TOP=$(top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}') - -# 6. Capture CPU utilization using 'vmstat' command CPU_USAGE_VMSTAT=$(vmstat 1 2 | tail -1 | awk '{print $13+$14}') - -# 7. Capture per-core CPU usage using 'mpstat' command (from sysstat package) MPSTAT_OUTPUT=$(mpstat -P ALL 1 1) - -# 8. Capture detailed CPU statistics using 'iostat' command IOSTAT_OUTPUT=$(iostat -c 1 1) - -# 9. Read CPU utilization data directly from /proc/stat file PROC_STAT=$(cat /proc/stat | grep '^cpu ' | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage}') - -# 10. Capture disk usage of the root '/' partition using 'df -h' DISK_USAGE=$(df -h / | grep / | awk '{print $5}') - -# 11. Capture memory usage using 'free' command MEMORY_USAGE=$(free -h | grep "Mem:" | awk '{print "Total: "$2", Used: "$3", Free: "$4}') - -# 12. Capture system uptime and load average using 'uptime' UPTIME=$(uptime -p) LOAD_AVG=$(uptime | awk -F'load average:' '{ print $2 }') - -# 13. Capture number of active TCP connections using 'ss' (if installed) or 'netstat' TCP_CONNECTIONS=$(ss -t | wc -l) - -# 14. Capture number of running processes using 'ps' RUNNING_PROCESSES=$(ps -e --no-headers | wc -l) - -# 15. Capture top 5 memory-hogging processes TOP_MEMORY_PROCESSES=$(ps aux --sort=-%mem | head -n 6) - -# 16. Capture top 5 CPU-hogging processes TOP_CPU_PROCESSES=$(ps aux --sort=-%cpu | head -n 6) - -# 17. Calculate top 5 FIDs by total CPU utilization TOP_FID_CPU=$(ps -eo user,%cpu --sort=-%cpu | awk '{cpu[$1]+=$2} END {for (u in cpu) print u, cpu[u]}' | sort -k2 -nr | head -n 5) -# 18. Display the top 5 FIDs to the user -echo "Top 5 FIDs by Total CPU Utilization:" -echo "No. FID Total CPU%" -echo "$TOP_FID_CPU" | nl -w2 -s'. ' - -# 19. Prompt the user to select an FID -echo -read -p "Enter the number of the FID you want to view detailed process info for: " FID_SELECTION - -# 20. Extract the FID based on the user's selection -SELECTED_FID=$(echo "$TOP_FID_CPU" | sed -n "${FID_SELECTION}p" | awk '{print $1}') - -# 21. Display the top 10 CPU-consuming processes for the selected FID -echo -echo "Top 10 CPU-consuming processes for FID '$SELECTED_FID':" -ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,comm | head -n 10 | tee -a "$OUTPUT_FILE" - -# 22. Write the main system information and top FIDs to the output file { - echo "[$DATE] Hostname: $HOSTNAME" - echo "CPU Utilization (top): $CPU_USAGE_TOP%" - echo "CPU Utilization (vmstat): $CPU_USAGE_VMSTAT%" - echo "Disk Usage: $DISK_USAGE" - echo - echo "Memory Usage:" + echo "Report generated on: [$DATE] for Hostname: $HOSTNAME" + print_header "CPU Utilization (top)" + echo "Current CPU Usage: $CPU_USAGE_TOP%" + print_header "CPU Utilization (vmstat)" + echo "CPU Usage (vmstat): $CPU_USAGE_VMSTAT%" + print_header "Disk Usage" + echo "Disk Usage of Root Partition: $DISK_USAGE" + print_header "Memory Usage" echo "$MEMORY_USAGE" - echo + print_header "System Uptime and Load Average" echo "Uptime: $UPTIME" echo "Load Average: $LOAD_AVG" - echo + print_header "TCP Connections and Running Processes" echo "Active TCP Connections: $TCP_CONNECTIONS" - echo "Number of Running Processes: $RUNNING_PROCESSES" - echo - echo "Top 5 Memory-Hogging Processes:" + echo "Running Processes: $RUNNING_PROCESSES" + print_header "Top 5 Memory-Consuming Processes" echo "$TOP_MEMORY_PROCESSES" - echo - echo "Top 5 CPU-Hogging Processes:" + print_header "Top 5 CPU-Consuming Processes" echo "$TOP_CPU_PROCESSES" - echo - echo "Per-core CPU Utilization (mpstat):" + print_header "Per-Core CPU Utilization" echo "$MPSTAT_OUTPUT" - echo - echo "Detailed CPU stats (iostat):" + print_header "Detailed CPU Stats (iostat)" echo "$IOSTAT_OUTPUT" - echo - echo "CPU Usage from /proc/stat: $PROC_STAT%" - echo - echo "Top 5 FIDs by Total CPU Utilization:" - echo "$TOP_FID_CPU" - echo - echo "Top 10 CPU-consuming processes for FID '$SELECTED_FID':" - ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,comm | head -n 10 -} >> "$OUTPUT_FILE" + print_header "CPU Usage from /proc/stat" + echo "CPU Usage: $PROC_STAT%" +} | tee "$OUTPUT_FILE" -# Confirm that the information has been saved -echo "Detailed CPU utilization for FID '$SELECTED_FID' saved to $OUTPUT_FILE" +# Show top FIDs and prompt for user input +echo +print_header "Top 5 FIDs by Total CPU Utilization" +echo "No. FID Total CPU%" +echo "$TOP_FID_CPU" | nl -w2 -s'. ' +echo + +read -p "Select an FID by entering the corresponding number: " FID_SELECTION +SELECTED_FID=$(echo "$TOP_FID_CPU" | sed -n "${FID_SELECTION}p" | awk '{print $1}') + +echo +print_header "Top 10 CPU-consuming processes for FID '$SELECTED_FID'" +ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,comm | head -n 10 | tee -a "$OUTPUT_FILE" +print_header "End of Report" +echo "Detailed information saved to: $OUTPUT_FILE" From 0a16b80b9d3499db1f289bc4e6e7df9726bd3c3c Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Fri, 4 Oct 2024 12:54:08 +0530 Subject: [PATCH 06/48] Update CPU_Utili --- CPU_Utili | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/CPU_Utili b/CPU_Utili index a09287f..8953e0e 100644 --- a/CPU_Utili +++ b/CPU_Utili @@ -6,11 +6,6 @@ print_header() { echo "====== $1 ======" } -# Optional: Function to add color for critical information (e.g., CPU usage) -print_critical() { - tput setaf 1; echo "$1"; tput sgr0 -} - # Capture date and time DATE=$(date '+%Y-%m-%d %H:%M:%S') TIMESTAMP=$(date '+%Y%m%d_%H%M%S') @@ -77,6 +72,6 @@ SELECTED_FID=$(echo "$TOP_FID_CPU" | sed -n "${FID_SELECTION}p" | awk '{print $1 echo print_header "Top 10 CPU-consuming processes for FID '$SELECTED_FID'" -ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,comm | head -n 10 | tee -a "$OUTPUT_FILE" +ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,cmd | head -n 10 | tee -a "$OUTPUT_FILE" print_header "End of Report" echo "Detailed information saved to: $OUTPUT_FILE" From 34c462bff13d6b99e318fcab6d4603194767add2 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Fri, 4 Oct 2024 12:58:41 +0530 Subject: [PATCH 07/48] Update CPU_Utili --- CPU_Utili | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CPU_Utili b/CPU_Utili index 8953e0e..a5e359e 100644 --- a/CPU_Utili +++ b/CPU_Utili @@ -72,6 +72,12 @@ SELECTED_FID=$(echo "$TOP_FID_CPU" | sed -n "${FID_SELECTION}p" | awk '{print $1 echo print_header "Top 10 CPU-consuming processes for FID '$SELECTED_FID'" -ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,cmd | head -n 10 | tee -a "$OUTPUT_FILE" +# Display truncated command line for readability and save full command to file +echo "PID %CPU COMMAND" +ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,cmd | while read -r pid cpu cmd; do + truncated_cmd=$(echo "$cmd" | cut -c 1-100) # Limit to 100 characters + printf "%-8s %-5s %s\n" "$pid" "$cpu" "$truncated_cmd" + echo "$pid $cpu $cmd" >> "$OUTPUT_FILE" # Full command in the file +done print_header "End of Report" echo "Detailed information saved to: $OUTPUT_FILE" From da249a4ad1ea7285d78edc9161c27f19cc44c331 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Fri, 4 Oct 2024 13:02:08 +0530 Subject: [PATCH 08/48] Update CPU_Utili --- CPU_Utili | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/CPU_Utili b/CPU_Utili index a5e359e..e2fba3c 100644 --- a/CPU_Utili +++ b/CPU_Utili @@ -50,8 +50,13 @@ TOP_FID_CPU=$(ps -eo user,%cpu --sort=-%cpu | awk '{cpu[$1]+=$2} END {for (u in echo "Running Processes: $RUNNING_PROCESSES" print_header "Top 5 Memory-Consuming Processes" echo "$TOP_MEMORY_PROCESSES" + echo "*****" print_header "Top 5 CPU-Consuming Processes" - echo "$TOP_CPU_PROCESSES" + ps aux --sort=-%cpu -o pid,%cpu,cmd | head -n 6 | while read -r pid cpu cmd; do + echo "PID: $pid | CPU: $cpu%" + echo "Command: $(echo "$cmd" | cut -c 1-300)" + echo "*****" + done print_header "Per-Core CPU Utilization" echo "$MPSTAT_OUTPUT" print_header "Detailed CPU Stats (iostat)" @@ -73,11 +78,11 @@ SELECTED_FID=$(echo "$TOP_FID_CPU" | sed -n "${FID_SELECTION}p" | awk '{print $1 echo print_header "Top 10 CPU-consuming processes for FID '$SELECTED_FID'" # Display truncated command line for readability and save full command to file -echo "PID %CPU COMMAND" -ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,cmd | while read -r pid cpu cmd; do - truncated_cmd=$(echo "$cmd" | cut -c 1-100) # Limit to 100 characters - printf "%-8s %-5s %s\n" "$pid" "$cpu" "$truncated_cmd" - echo "$pid $cpu $cmd" >> "$OUTPUT_FILE" # Full command in the file +ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,cmd | head -n 10 | while read -r pid cpu cmd; do + echo "PID: $pid | CPU: $cpu%" + echo "Command: $(echo "$cmd" | cut -c 1-300)" + echo "*****" + echo "$pid $cpu $cmd" >> "$OUTPUT_FILE" # Save full command to file done print_header "End of Report" echo "Detailed information saved to: $OUTPUT_FILE" From 9d9460df3c74d3d207163f3915ff984b3227fafe Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Fri, 4 Oct 2024 13:04:35 +0530 Subject: [PATCH 09/48] Create 2_CPU --- 2_CPU | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 2_CPU diff --git a/2_CPU b/2_CPU new file mode 100644 index 0000000..a5e359e --- /dev/null +++ b/2_CPU @@ -0,0 +1,83 @@ +#!/bin/bash + +# Helper function for headers with clear separation +print_header() { + echo + echo "====== $1 ======" +} + +# Capture date and time +DATE=$(date '+%Y-%m-%d %H:%M:%S') +TIMESTAMP=$(date '+%Y%m%d_%H%M%S') +OUTPUT_DIR="/tmp/CPU_Utilization" +OUTPUT_FILE="$OUTPUT_DIR/CPU_Utilization_$TIMESTAMP.txt" + +# Create directory if not exists +[ ! -d "$OUTPUT_DIR" ] && mkdir -p "$OUTPUT_DIR" + +# Capture general system information +HOSTNAME=$(hostname) +CPU_USAGE_TOP=$(top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}') +CPU_USAGE_VMSTAT=$(vmstat 1 2 | tail -1 | awk '{print $13+$14}') +MPSTAT_OUTPUT=$(mpstat -P ALL 1 1) +IOSTAT_OUTPUT=$(iostat -c 1 1) +PROC_STAT=$(cat /proc/stat | grep '^cpu ' | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage}') +DISK_USAGE=$(df -h / | grep / | awk '{print $5}') +MEMORY_USAGE=$(free -h | grep "Mem:" | awk '{print "Total: "$2", Used: "$3", Free: "$4}') +UPTIME=$(uptime -p) +LOAD_AVG=$(uptime | awk -F'load average:' '{ print $2 }') +TCP_CONNECTIONS=$(ss -t | wc -l) +RUNNING_PROCESSES=$(ps -e --no-headers | wc -l) +TOP_MEMORY_PROCESSES=$(ps aux --sort=-%mem | head -n 6) +TOP_CPU_PROCESSES=$(ps aux --sort=-%cpu | head -n 6) +TOP_FID_CPU=$(ps -eo user,%cpu --sort=-%cpu | awk '{cpu[$1]+=$2} END {for (u in cpu) print u, cpu[u]}' | sort -k2 -nr | head -n 5) + +{ + echo "Report generated on: [$DATE] for Hostname: $HOSTNAME" + print_header "CPU Utilization (top)" + echo "Current CPU Usage: $CPU_USAGE_TOP%" + print_header "CPU Utilization (vmstat)" + echo "CPU Usage (vmstat): $CPU_USAGE_VMSTAT%" + print_header "Disk Usage" + echo "Disk Usage of Root Partition: $DISK_USAGE" + print_header "Memory Usage" + echo "$MEMORY_USAGE" + print_header "System Uptime and Load Average" + echo "Uptime: $UPTIME" + echo "Load Average: $LOAD_AVG" + print_header "TCP Connections and Running Processes" + echo "Active TCP Connections: $TCP_CONNECTIONS" + echo "Running Processes: $RUNNING_PROCESSES" + print_header "Top 5 Memory-Consuming Processes" + echo "$TOP_MEMORY_PROCESSES" + print_header "Top 5 CPU-Consuming Processes" + echo "$TOP_CPU_PROCESSES" + print_header "Per-Core CPU Utilization" + echo "$MPSTAT_OUTPUT" + print_header "Detailed CPU Stats (iostat)" + echo "$IOSTAT_OUTPUT" + print_header "CPU Usage from /proc/stat" + echo "CPU Usage: $PROC_STAT%" +} | tee "$OUTPUT_FILE" + +# Show top FIDs and prompt for user input +echo +print_header "Top 5 FIDs by Total CPU Utilization" +echo "No. FID Total CPU%" +echo "$TOP_FID_CPU" | nl -w2 -s'. ' +echo + +read -p "Select an FID by entering the corresponding number: " FID_SELECTION +SELECTED_FID=$(echo "$TOP_FID_CPU" | sed -n "${FID_SELECTION}p" | awk '{print $1}') + +echo +print_header "Top 10 CPU-consuming processes for FID '$SELECTED_FID'" +# Display truncated command line for readability and save full command to file +echo "PID %CPU COMMAND" +ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,cmd | while read -r pid cpu cmd; do + truncated_cmd=$(echo "$cmd" | cut -c 1-100) # Limit to 100 characters + printf "%-8s %-5s %s\n" "$pid" "$cpu" "$truncated_cmd" + echo "$pid $cpu $cmd" >> "$OUTPUT_FILE" # Full command in the file +done +print_header "End of Report" +echo "Detailed information saved to: $OUTPUT_FILE" From c36d33f66decfd33629540ad7eae94ce67879f81 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Fri, 4 Oct 2024 13:09:06 +0530 Subject: [PATCH 10/48] Update 2_CPU --- 2_CPU | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/2_CPU b/2_CPU index a5e359e..9a90116 100644 --- a/2_CPU +++ b/2_CPU @@ -4,6 +4,7 @@ print_header() { echo echo "====== $1 ======" + echo } # Capture date and time @@ -29,35 +30,61 @@ LOAD_AVG=$(uptime | awk -F'load average:' '{ print $2 }') TCP_CONNECTIONS=$(ss -t | wc -l) RUNNING_PROCESSES=$(ps -e --no-headers | wc -l) TOP_MEMORY_PROCESSES=$(ps aux --sort=-%mem | head -n 6) -TOP_CPU_PROCESSES=$(ps aux --sort=-%cpu | head -n 6) TOP_FID_CPU=$(ps -eo user,%cpu --sort=-%cpu | awk '{cpu[$1]+=$2} END {for (u in cpu) print u, cpu[u]}' | sort -k2 -nr | head -n 5) { + echo echo "Report generated on: [$DATE] for Hostname: $HOSTNAME" + print_header "CPU Utilization (top)" echo "Current CPU Usage: $CPU_USAGE_TOP%" + echo + print_header "CPU Utilization (vmstat)" echo "CPU Usage (vmstat): $CPU_USAGE_VMSTAT%" + echo + print_header "Disk Usage" echo "Disk Usage of Root Partition: $DISK_USAGE" + echo + print_header "Memory Usage" echo "$MEMORY_USAGE" + echo + print_header "System Uptime and Load Average" echo "Uptime: $UPTIME" echo "Load Average: $LOAD_AVG" + echo + print_header "TCP Connections and Running Processes" echo "Active TCP Connections: $TCP_CONNECTIONS" echo "Running Processes: $RUNNING_PROCESSES" + echo + print_header "Top 5 Memory-Consuming Processes" echo "$TOP_MEMORY_PROCESSES" + echo "*****" + print_header "Top 5 CPU-Consuming Processes" - echo "$TOP_CPU_PROCESSES" + ps aux --sort=-%cpu -o pid,%cpu,cmd | head -n 6 | while read -r pid cpu cmd; do + echo "PID: $pid | CPU: $cpu%" + echo "Command: $(echo "$cmd" | cut -c 1-300)" + echo "*****" + done + echo + print_header "Per-Core CPU Utilization" echo "$MPSTAT_OUTPUT" + echo + print_header "Detailed CPU Stats (iostat)" echo "$IOSTAT_OUTPUT" + echo + print_header "CPU Usage from /proc/stat" echo "CPU Usage: $PROC_STAT%" + echo } | tee "$OUTPUT_FILE" # Show top FIDs and prompt for user input @@ -73,11 +100,11 @@ SELECTED_FID=$(echo "$TOP_FID_CPU" | sed -n "${FID_SELECTION}p" | awk '{print $1 echo print_header "Top 10 CPU-consuming processes for FID '$SELECTED_FID'" # Display truncated command line for readability and save full command to file -echo "PID %CPU COMMAND" -ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,cmd | while read -r pid cpu cmd; do - truncated_cmd=$(echo "$cmd" | cut -c 1-100) # Limit to 100 characters - printf "%-8s %-5s %s\n" "$pid" "$cpu" "$truncated_cmd" - echo "$pid $cpu $cmd" >> "$OUTPUT_FILE" # Full command in the file +ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,cmd | head -n 10 | while read -r pid cpu cmd; do + echo "PID: $pid | CPU: $cpu%" + echo "Command: $(echo "$cmd" | cut -c 1-300)" + echo "*****" + echo "$pid $cpu $cmd" >> "$OUTPUT_FILE" # Save full command to file done print_header "End of Report" echo "Detailed information saved to: $OUTPUT_FILE" From 7f46ab160cbdb7aae72fcc7c38a6e4bab5995874 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Fri, 4 Oct 2024 13:14:46 +0530 Subject: [PATCH 11/48] Update 2_CPU --- 2_CPU | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2_CPU b/2_CPU index 9a90116..4027825 100644 --- a/2_CPU +++ b/2_CPU @@ -67,7 +67,7 @@ TOP_FID_CPU=$(ps -eo user,%cpu --sort=-%cpu | awk '{cpu[$1]+=$2} END {for (u in echo "*****" print_header "Top 5 CPU-Consuming Processes" - ps aux --sort=-%cpu -o pid,%cpu,cmd | head -n 6 | while read -r pid cpu cmd; do + ps aux --sort=-%cpu | awk '{print $2, $3, substr($0, index($0, $11))}' | head -n 6 | while read -r pid cpu cmd; do echo "PID: $pid | CPU: $cpu%" echo "Command: $(echo "$cmd" | cut -c 1-300)" echo "*****" From 4630ff6627d196d9ff1b5c96b51fa56357fb73d7 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Fri, 4 Oct 2024 13:17:54 +0530 Subject: [PATCH 12/48] Update 2_CPU --- 2_CPU | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/2_CPU b/2_CPU index 4027825..9e20cd5 100644 --- a/2_CPU +++ b/2_CPU @@ -29,7 +29,6 @@ UPTIME=$(uptime -p) LOAD_AVG=$(uptime | awk -F'load average:' '{ print $2 }') TCP_CONNECTIONS=$(ss -t | wc -l) RUNNING_PROCESSES=$(ps -e --no-headers | wc -l) -TOP_MEMORY_PROCESSES=$(ps aux --sort=-%mem | head -n 6) TOP_FID_CPU=$(ps -eo user,%cpu --sort=-%cpu | awk '{cpu[$1]+=$2} END {for (u in cpu) print u, cpu[u]}' | sort -k2 -nr | head -n 5) { @@ -63,8 +62,12 @@ TOP_FID_CPU=$(ps -eo user,%cpu --sort=-%cpu | awk '{cpu[$1]+=$2} END {for (u in echo print_header "Top 5 Memory-Consuming Processes" - echo "$TOP_MEMORY_PROCESSES" - echo "*****" + ps aux --sort=-%mem | awk '{print $2, $4, substr($0, index($0, $11))}' | head -n 6 | while read -r pid mem cmd; do + echo "PID: $pid | MEM: $mem%" + echo "Command: $(echo "$cmd" | cut -c 1-300)" + echo "*****" + done + echo print_header "Top 5 CPU-Consuming Processes" ps aux --sort=-%cpu | awk '{print $2, $3, substr($0, index($0, $11))}' | head -n 6 | while read -r pid cpu cmd; do From 6b9942940fb2c0d7b881a9cd5c145574334371b0 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Fri, 4 Oct 2024 13:36:41 +0530 Subject: [PATCH 13/48] Update 2_CPU --- 2_CPU | 64 ++++++++++++++++++++--------------------------------------- 1 file changed, 21 insertions(+), 43 deletions(-) diff --git a/2_CPU b/2_CPU index 9e20cd5..c2850d4 100644 --- a/2_CPU +++ b/2_CPU @@ -4,7 +4,6 @@ print_header() { echo echo "====== $1 ======" - echo } # Capture date and time @@ -23,7 +22,7 @@ CPU_USAGE_VMSTAT=$(vmstat 1 2 | tail -1 | awk '{print $13+$14}') MPSTAT_OUTPUT=$(mpstat -P ALL 1 1) IOSTAT_OUTPUT=$(iostat -c 1 1) PROC_STAT=$(cat /proc/stat | grep '^cpu ' | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage}') -DISK_USAGE=$(df -h / | grep / | awk '{print $5}') +DISK_USAGE=$(df -h) MEMORY_USAGE=$(free -h | grep "Mem:" | awk '{print "Total: "$2", Used: "$3", Free: "$4}') UPTIME=$(uptime -p) LOAD_AVG=$(uptime | awk -F'load average:' '{ print $2 }') @@ -32,42 +31,14 @@ RUNNING_PROCESSES=$(ps -e --no-headers | wc -l) TOP_FID_CPU=$(ps -eo user,%cpu --sort=-%cpu | awk '{cpu[$1]+=$2} END {for (u in cpu) print u, cpu[u]}' | sort -k2 -nr | head -n 5) { - echo echo "Report generated on: [$DATE] for Hostname: $HOSTNAME" - print_header "CPU Utilization (top)" - echo "Current CPU Usage: $CPU_USAGE_TOP%" - echo - - print_header "CPU Utilization (vmstat)" - echo "CPU Usage (vmstat): $CPU_USAGE_VMSTAT%" - echo - - print_header "Disk Usage" - echo "Disk Usage of Root Partition: $DISK_USAGE" - echo - - print_header "Memory Usage" - echo "$MEMORY_USAGE" - echo - - print_header "System Uptime and Load Average" - echo "Uptime: $UPTIME" - echo "Load Average: $LOAD_AVG" - echo - - print_header "TCP Connections and Running Processes" - echo "Active TCP Connections: $TCP_CONNECTIONS" - echo "Running Processes: $RUNNING_PROCESSES" - echo - print_header "Top 5 Memory-Consuming Processes" ps aux --sort=-%mem | awk '{print $2, $4, substr($0, index($0, $11))}' | head -n 6 | while read -r pid mem cmd; do echo "PID: $pid | MEM: $mem%" echo "Command: $(echo "$cmd" | cut -c 1-300)" echo "*****" done - echo print_header "Top 5 CPU-Consuming Processes" ps aux --sort=-%cpu | awk '{print $2, $3, substr($0, index($0, $11))}' | head -n 6 | while read -r pid cpu cmd; do @@ -75,32 +46,38 @@ TOP_FID_CPU=$(ps -eo user,%cpu --sort=-%cpu | awk '{cpu[$1]+=$2} END {for (u in echo "Command: $(echo "$cmd" | cut -c 1-300)" echo "*****" done - echo - print_header "Per-Core CPU Utilization" - echo "$MPSTAT_OUTPUT" - echo + print_header "Additional System Information" + echo "CPU Utilization (top): $CPU_USAGE_TOP%" + echo "CPU Utilization (vmstat): $CPU_USAGE_VMSTAT%" + echo "Memory Usage: $MEMORY_USAGE" + echo "System Uptime and Load Average: $(uptime)" + echo "TCP Connections: $TCP_CONNECTIONS" + echo "Running Processes: $RUNNING_PROCESSES" - print_header "Detailed CPU Stats (iostat)" - echo "$IOSTAT_OUTPUT" - echo + # print_header "Per-Core CPU Utilization" + # echo "$MPSTAT_OUTPUT" + + # print_header "Detailed CPU Stats" + # echo "$IOSTAT_OUTPUT" + + print_header "Disk Usage (All Mounted Filesystems)" + echo "$DISK_USAGE" + + print_header "Uptime (Human-Readable)" + echo "System has been $UPTIME" - print_header "CPU Usage from /proc/stat" - echo "CPU Usage: $PROC_STAT%" - echo } | tee "$OUTPUT_FILE" -# Show top FIDs and prompt for user input echo print_header "Top 5 FIDs by Total CPU Utilization" echo "No. FID Total CPU%" echo "$TOP_FID_CPU" | nl -w2 -s'. ' -echo +echo read -p "Select an FID by entering the corresponding number: " FID_SELECTION SELECTED_FID=$(echo "$TOP_FID_CPU" | sed -n "${FID_SELECTION}p" | awk '{print $1}') -echo print_header "Top 10 CPU-consuming processes for FID '$SELECTED_FID'" # Display truncated command line for readability and save full command to file ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,cmd | head -n 10 | while read -r pid cpu cmd; do @@ -109,5 +86,6 @@ ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,cmd | head -n 10 | while read -r echo "*****" echo "$pid $cpu $cmd" >> "$OUTPUT_FILE" # Save full command to file done + print_header "End of Report" echo "Detailed information saved to: $OUTPUT_FILE" From a8df622ca299d676584d7f60e3e79c070900d8be Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Fri, 4 Oct 2024 13:45:47 +0530 Subject: [PATCH 14/48] Update 2_CPU --- 2_CPU | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/2_CPU b/2_CPU index c2850d4..5027c30 100644 --- a/2_CPU +++ b/2_CPU @@ -24,7 +24,8 @@ IOSTAT_OUTPUT=$(iostat -c 1 1) PROC_STAT=$(cat /proc/stat | grep '^cpu ' | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage}') DISK_USAGE=$(df -h) MEMORY_USAGE=$(free -h | grep "Mem:" | awk '{print "Total: "$2", Used: "$3", Free: "$4}') -UPTIME=$(uptime -p) +UPTIME_P=$(uptime -p) +UPTIME_S=$(uptime -s) LOAD_AVG=$(uptime | awk -F'load average:' '{ print $2 }') TCP_CONNECTIONS=$(ss -t | wc -l) RUNNING_PROCESSES=$(ps -e --no-headers | wc -l) @@ -32,28 +33,35 @@ TOP_FID_CPU=$(ps -eo user,%cpu --sort=-%cpu | awk '{cpu[$1]+=$2} END {for (u in { echo "Report generated on: [$DATE] for Hostname: $HOSTNAME" + echo "*****" print_header "Top 5 Memory-Consuming Processes" ps aux --sort=-%mem | awk '{print $2, $4, substr($0, index($0, $11))}' | head -n 6 | while read -r pid mem cmd; do echo "PID: $pid | MEM: $mem%" - echo "Command: $(echo "$cmd" | cut -c 1-300)" + echo "Command: $(echo "$cmd" | cut -c 1-1000)" echo "*****" done print_header "Top 5 CPU-Consuming Processes" ps aux --sort=-%cpu | awk '{print $2, $3, substr($0, index($0, $11))}' | head -n 6 | while read -r pid cpu cmd; do echo "PID: $pid | CPU: $cpu%" - echo "Command: $(echo "$cmd" | cut -c 1-300)" + echo "Command: $(echo "$cmd" | cut -c 1-1000)" echo "*****" done print_header "Additional System Information" echo "CPU Utilization (top): $CPU_USAGE_TOP%" + echo "*****" echo "CPU Utilization (vmstat): $CPU_USAGE_VMSTAT%" + echo "*****" echo "Memory Usage: $MEMORY_USAGE" + echo "*****" echo "System Uptime and Load Average: $(uptime)" + echo "*****" echo "TCP Connections: $TCP_CONNECTIONS" + echo "*****" echo "Running Processes: $RUNNING_PROCESSES" + echo "*****" # print_header "Per-Core CPU Utilization" # echo "$MPSTAT_OUTPUT" @@ -63,9 +71,15 @@ TOP_FID_CPU=$(ps -eo user,%cpu --sort=-%cpu | awk '{cpu[$1]+=$2} END {for (u in print_header "Disk Usage (All Mounted Filesystems)" echo "$DISK_USAGE" + echo "*****" print_header "Uptime (Human-Readable)" - echo "System has been $UPTIME" + echo "System has been $UPTIME_P" + echo "*****" + + print_header "System Start Time" + echo "System started on: $UPTIME_S" + echo "*****" } | tee "$OUTPUT_FILE" @@ -73,6 +87,7 @@ echo print_header "Top 5 FIDs by Total CPU Utilization" echo "No. FID Total CPU%" echo "$TOP_FID_CPU" | nl -w2 -s'. ' +echo "*****" echo read -p "Select an FID by entering the corresponding number: " FID_SELECTION @@ -82,7 +97,7 @@ print_header "Top 10 CPU-consuming processes for FID '$SELECTED_FID'" # Display truncated command line for readability and save full command to file ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,cmd | head -n 10 | while read -r pid cpu cmd; do echo "PID: $pid | CPU: $cpu%" - echo "Command: $(echo "$cmd" | cut -c 1-300)" + echo "Command: $(echo "$cmd" | cut -c 1-1000)" echo "*****" echo "$pid $cpu $cmd" >> "$OUTPUT_FILE" # Save full command to file done From ad1aecb6bfd1dbde6913921df95b67e9b2ba1b35 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Fri, 4 Oct 2024 13:52:59 +0530 Subject: [PATCH 15/48] Create Parallel --- Parallel | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 Parallel diff --git a/Parallel b/Parallel new file mode 100644 index 0000000..59d343a --- /dev/null +++ b/Parallel @@ -0,0 +1,106 @@ +#!/bin/bash + +# Helper function for headers with clear separation +print_header() { + echo + echo "====== $1 ======" +} + +# Capture date and time +DATE=$(date '+%Y-%m-%d %H:%M:%S') +TIMESTAMP=$(date '+%Y%m%d_%H%M%S') +OUTPUT_DIR="/tmp/CPU_Utilization" +OUTPUT_FILE="$OUTPUT_DIR/CPU_Utilization_$TIMESTAMP.txt" + +# Create directory if not exists +[ ! -d "$OUTPUT_DIR" ] && mkdir -p "$OUTPUT_DIR" + +# Capture general system information in parallel +HOSTNAME=$(hostname) +DISK_USAGE=$(df -h) & +CPU_USAGE_TOP=$(top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}') & +CPU_USAGE_VMSTAT=$(vmstat 1 1 | tail -1 | awk '{print $13+$14}') & +MEMORY_USAGE=$(free -h | grep "Mem:" | awk '{print "Total: "$2", Used: "$3", Free: "$4}') & +UPTIME_P=$(uptime -p) & +UPTIME_S=$(uptime -s) & +TCP_CONNECTIONS=$(ss -t | wc -l) & +RUNNING_PROCESSES=$(ps -e --no-headers | wc -l) & +# Wait for all background processes to finish +wait + +# Capture top FIDs after parallel execution +TOP_FID_CPU=$(ps -eo user,%cpu --sort=-%cpu | awk '{cpu[$1]+=$2} END {for (u in cpu) print u, cpu[u]}' | sort -k2 -nr | head -n 5) + +{ + echo "Report generated on: [$DATE] for Hostname: $HOSTNAME" + echo "*****" + + print_header "Top 5 Memory-Consuming Processes" + ps aux --sort=-%mem | awk '{print $2, $4, substr($0, index($0, $11))}' | head -n 6 | while read -r pid mem cmd; do + echo "PID: $pid | MEM: $mem%" + echo "Command: $(echo "$cmd" | cut -c 1-1000)" + echo "*****" + done + + print_header "Top 5 CPU-Consuming Processes" + ps aux --sort=-%cpu | awk '{print $2, $3, substr($0, index($0, $11))}' | head -n 6 | while read -r pid cpu cmd; do + echo "PID: $pid | CPU: $cpu%" + echo "Command: $(echo "$cmd" | cut -c 1-1000)" + echo "*****" + done + + print_header "Additional System Information" + echo "CPU Utilization (top): $CPU_USAGE_TOP%" + echo "*****" + echo "CPU Utilization (vmstat): $CPU_USAGE_VMSTAT%" + echo "*****" + echo "Memory Usage: $MEMORY_USAGE" + echo "*****" + echo "System Uptime and Load Average: $(uptime)" + echo "*****" + echo "TCP Connections: $TCP_CONNECTIONS" + echo "*****" + echo "Running Processes: $RUNNING_PROCESSES" + echo "*****" + + # print_header "Per-Core CPU Utilization" + # echo "$MPSTAT_OUTPUT" + + # print_header "Detailed CPU Stats" + # echo "$IOSTAT_OUTPUT" + + print_header "Disk Usage (All Mounted Filesystems)" + echo "$DISK_USAGE" + echo "*****" + + print_header "Uptime (Human-Readable)" + echo "System has been $UPTIME_P" + echo "*****" + + print_header "System Start Time" + echo "System started on: $UPTIME_S" + echo "*****" + +} | tee "$OUTPUT_FILE" + +echo +print_header "Top 5 FIDs by Total CPU Utilization" +echo "No. FID Total CPU%" +echo "$TOP_FID_CPU" | nl -w2 -s'. ' +echo "*****" + +echo +read -p "Select an FID by entering the corresponding number: " FID_SELECTION +SELECTED_FID=$(echo "$TOP_FID_CPU" | sed -n "${FID_SELECTION}p" | awk '{print $1}') + +print_header "Top 10 CPU-consuming processes for FID '$SELECTED_FID'" +# Display truncated command line for readability and save full command to file +ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,cmd | head -n 10 | while read -r pid cpu cmd; do + echo "PID: $pid | CPU: $cpu%" + echo "Command: $(echo "$cmd" | cut -c 1-1000)" + echo "*****" + echo "$pid $cpu $cmd" >> "$OUTPUT_FILE" # Save full command to file +done + +print_header "End of Report" +echo "Detailed information saved to: $OUTPUT_FILE" From 745891a182655752a1cc0ec488a1ba4635e1c2aa Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Fri, 4 Oct 2024 16:11:50 +0530 Subject: [PATCH 16/48] Update 2_CPU --- 2_CPU | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/2_CPU b/2_CPU index 5027c30..9df2db1 100644 --- a/2_CPU +++ b/2_CPU @@ -104,3 +104,20 @@ done print_header "End of Report" echo "Detailed information saved to: $OUTPUT_FILE" + + +ORIGINAL_USER=$(who | head -n 1 | awk '{print $1}') +USER_EMAIL="${ORIGINAL_USER}@xyz.com" + +# Rest of the script... + +# Prompt to send the file via email +read -p "Do you want to send the report to your email address ($USER_EMAIL)? (y/n): " SEND_EMAIL + +if [[ $SEND_EMAIL == [Yy]* ]]; then + echo "Sending report to $USER_EMAIL..." + mail -s "CPU Utilization Report" -a "$OUTPUT_FILE" "$USER_EMAIL" <<< "Please find the attached CPU utilization report." + echo "Report sent to $USER_EMAIL." +else + echo "Report not sent." +fi From f93082855d468f820442d28fa60a380beee6ec2d Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Fri, 4 Oct 2024 16:17:43 +0530 Subject: [PATCH 17/48] Create csv --- csv | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 csv diff --git a/csv b/csv new file mode 100644 index 0000000..ef924fa --- /dev/null +++ b/csv @@ -0,0 +1,106 @@ +#!/bin/bash + +# Helper function for headers with clear separation +print_header() { + echo + echo "====== $1 ======" +} + +# Capture date and time +DATE=$(date '+%Y-%m-%d %H:%M:%S') +TIMESTAMP=$(date '+%Y%m%d_%H%M%S') +OUTPUT_DIR="/tmp/CPU_Utilization" +OUTPUT_FILE="$OUTPUT_DIR/CPU_Utilization_$TIMESTAMP.csv" + +# Create directory if not exists +[ ! -d "$OUTPUT_DIR" ] && mkdir -p "$OUTPUT_DIR" + +# Capture general system information +HOSTNAME=$(hostname) +CPU_USAGE_TOP=$(top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}') +CPU_USAGE_VMSTAT=$(vmstat 1 2 | tail -1 | awk '{print $13+$14}') +MPSTAT_OUTPUT=$(mpstat -P ALL 1 1) +IOSTAT_OUTPUT=$(iostat -c 1 1) +PROC_STAT=$(cat /proc/stat | grep '^cpu ' | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage}') +DISK_USAGE=$(df -h) +MEMORY_USAGE=$(free -h | grep "Mem:" | awk '{print "Total: "$2", Used: "$3", Free: "$4}') +UPTIME_P=$(uptime -p) +UPTIME_S=$(uptime -s) +LOAD_AVG=$(uptime | awk -F'load average:' '{ print $2 }') +TCP_CONNECTIONS=$(ss -t | wc -l) +RUNNING_PROCESSES=$(ps -e --no-headers | wc -l) +TOP_FID_CPU=$(ps -eo user,%cpu --sort=-%cpu | awk '{cpu[$1]+=$2} END {for (u in cpu) print u, cpu[u]}' | sort -k2 -nr | head -n 5) + +{ + echo "Report generated on:,[$DATE],for Hostname:,$HOSTNAME" + + print_header "Top 5 Memory-Consuming Processes" + echo "PID,Memory (%),Command" + ps aux --sort=-%mem | awk '{print $2, $4, substr($0, index($0, $11))}' | head -n 6 | while read -r pid mem cmd; do + echo "$pid,$mem,\"$(echo "$cmd" | cut -c 1-1000)\"" + done + echo "*****" + + print_header "Top 5 CPU-Consuming Processes" + echo "PID,CPU (%),Command" + ps aux --sort=-%cpu | awk '{print $2, $3, substr($0, index($0, $11))}' | head -n 6 | while read -r pid cpu cmd; do + echo "$pid,$cpu,\"$(echo "$cmd" | cut -c 1-1000)\"" + done + echo "*****" + + print_header "Additional System Information" + echo "Metric,Value" + echo "CPU Utilization (top),$CPU_USAGE_TOP%" + echo "CPU Utilization (vmstat),$CPU_USAGE_VMSTAT%" + echo "Memory Usage,$MEMORY_USAGE" + echo "System Uptime and Load Average,\"$(uptime)\"" + echo "TCP Connections,$TCP_CONNECTIONS" + echo "Running Processes,$RUNNING_PROCESSES" + + # print_header "Per-Core CPU Utilization" + # echo "$MPSTAT_OUTPUT" + + # print_header "Detailed CPU Stats" + # echo "$IOSTAT_OUTPUT" + + print_header "Disk Usage (All Mounted Filesystems)" + echo "Filesystem,Size,Used,Available,Use%,Mounted on" + df -h | awk 'NR>1 {print $1 "," $2 "," $3 "," $4 "," $5 "," $6}' + + echo "Uptime (Human-Readable),$UPTIME_P" + echo "System Start Time,$UPTIME_S" + +} | tee "$OUTPUT_FILE" + +echo +print_header "Top 5 FIDs by Total CPU Utilization" +echo "No.,FID,Total CPU (%)" +echo "$TOP_FID_CPU" | nl -w2 -s'. ' | awk '{print $1 "," $2 "," $3}' + +echo +read -p "Select an FID by entering the corresponding number: " FID_SELECTION +SELECTED_FID=$(echo "$TOP_FID_CPU" | sed -n "${FID_SELECTION}p" | awk '{print $1}') + +print_header "Top 10 CPU-consuming processes for FID '$SELECTED_FID'" +# Display truncated command line for readability and save full command to file +echo "PID,CPU (%),Command" >> "$OUTPUT_FILE" +ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,cmd | head -n 10 | while read -r pid cpu cmd; do + echo "$pid,$cpu,\"$(echo "$cmd" | cut -c 1-1000)\"" + echo "$pid,$cpu,\"$cmd\"" >> "$OUTPUT_FILE" # Save full command to file in CSV format +done + +print_header "End of Report" +echo "Detailed information saved to: $OUTPUT_FILE" + +# Prompt to send the file via email +ORIGINAL_USER=$(who | head -n 1 | awk '{print $1}') +USER_EMAIL="${ORIGINAL_USER}@xyz.com" +read -p "Do you want to send the report to your email address ($USER_EMAIL)? (y/n): " SEND_EMAIL + +if [[ $SEND_EMAIL == [Yy]* ]]; then + echo "Sending report to $USER_EMAIL..." + mail -s "CPU Utilization Report" -a "$OUTPUT_FILE" "$USER_EMAIL" <<< "Please find the attached CPU utilization report." + echo "Report sent to $USER_EMAIL." +else + echo "Report not sent." +fi From 170dc5533e4f45ac0bbd99a608f974c2ebccc70c Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Fri, 4 Oct 2024 16:23:52 +0530 Subject: [PATCH 18/48] Update csv From 09bff49253b7c84f46c504fec55f454aa1c0bbd4 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Fri, 4 Oct 2024 16:24:34 +0530 Subject: [PATCH 19/48] Update csv From 3908314489fb06a3c0643a3b06b6514f0fafe9a9 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Fri, 4 Oct 2024 16:26:24 +0530 Subject: [PATCH 20/48] Create new_Csv --- new_Csv | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 new_Csv diff --git a/new_Csv b/new_Csv new file mode 100644 index 0000000..ef924fa --- /dev/null +++ b/new_Csv @@ -0,0 +1,106 @@ +#!/bin/bash + +# Helper function for headers with clear separation +print_header() { + echo + echo "====== $1 ======" +} + +# Capture date and time +DATE=$(date '+%Y-%m-%d %H:%M:%S') +TIMESTAMP=$(date '+%Y%m%d_%H%M%S') +OUTPUT_DIR="/tmp/CPU_Utilization" +OUTPUT_FILE="$OUTPUT_DIR/CPU_Utilization_$TIMESTAMP.csv" + +# Create directory if not exists +[ ! -d "$OUTPUT_DIR" ] && mkdir -p "$OUTPUT_DIR" + +# Capture general system information +HOSTNAME=$(hostname) +CPU_USAGE_TOP=$(top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}') +CPU_USAGE_VMSTAT=$(vmstat 1 2 | tail -1 | awk '{print $13+$14}') +MPSTAT_OUTPUT=$(mpstat -P ALL 1 1) +IOSTAT_OUTPUT=$(iostat -c 1 1) +PROC_STAT=$(cat /proc/stat | grep '^cpu ' | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage}') +DISK_USAGE=$(df -h) +MEMORY_USAGE=$(free -h | grep "Mem:" | awk '{print "Total: "$2", Used: "$3", Free: "$4}') +UPTIME_P=$(uptime -p) +UPTIME_S=$(uptime -s) +LOAD_AVG=$(uptime | awk -F'load average:' '{ print $2 }') +TCP_CONNECTIONS=$(ss -t | wc -l) +RUNNING_PROCESSES=$(ps -e --no-headers | wc -l) +TOP_FID_CPU=$(ps -eo user,%cpu --sort=-%cpu | awk '{cpu[$1]+=$2} END {for (u in cpu) print u, cpu[u]}' | sort -k2 -nr | head -n 5) + +{ + echo "Report generated on:,[$DATE],for Hostname:,$HOSTNAME" + + print_header "Top 5 Memory-Consuming Processes" + echo "PID,Memory (%),Command" + ps aux --sort=-%mem | awk '{print $2, $4, substr($0, index($0, $11))}' | head -n 6 | while read -r pid mem cmd; do + echo "$pid,$mem,\"$(echo "$cmd" | cut -c 1-1000)\"" + done + echo "*****" + + print_header "Top 5 CPU-Consuming Processes" + echo "PID,CPU (%),Command" + ps aux --sort=-%cpu | awk '{print $2, $3, substr($0, index($0, $11))}' | head -n 6 | while read -r pid cpu cmd; do + echo "$pid,$cpu,\"$(echo "$cmd" | cut -c 1-1000)\"" + done + echo "*****" + + print_header "Additional System Information" + echo "Metric,Value" + echo "CPU Utilization (top),$CPU_USAGE_TOP%" + echo "CPU Utilization (vmstat),$CPU_USAGE_VMSTAT%" + echo "Memory Usage,$MEMORY_USAGE" + echo "System Uptime and Load Average,\"$(uptime)\"" + echo "TCP Connections,$TCP_CONNECTIONS" + echo "Running Processes,$RUNNING_PROCESSES" + + # print_header "Per-Core CPU Utilization" + # echo "$MPSTAT_OUTPUT" + + # print_header "Detailed CPU Stats" + # echo "$IOSTAT_OUTPUT" + + print_header "Disk Usage (All Mounted Filesystems)" + echo "Filesystem,Size,Used,Available,Use%,Mounted on" + df -h | awk 'NR>1 {print $1 "," $2 "," $3 "," $4 "," $5 "," $6}' + + echo "Uptime (Human-Readable),$UPTIME_P" + echo "System Start Time,$UPTIME_S" + +} | tee "$OUTPUT_FILE" + +echo +print_header "Top 5 FIDs by Total CPU Utilization" +echo "No.,FID,Total CPU (%)" +echo "$TOP_FID_CPU" | nl -w2 -s'. ' | awk '{print $1 "," $2 "," $3}' + +echo +read -p "Select an FID by entering the corresponding number: " FID_SELECTION +SELECTED_FID=$(echo "$TOP_FID_CPU" | sed -n "${FID_SELECTION}p" | awk '{print $1}') + +print_header "Top 10 CPU-consuming processes for FID '$SELECTED_FID'" +# Display truncated command line for readability and save full command to file +echo "PID,CPU (%),Command" >> "$OUTPUT_FILE" +ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,cmd | head -n 10 | while read -r pid cpu cmd; do + echo "$pid,$cpu,\"$(echo "$cmd" | cut -c 1-1000)\"" + echo "$pid,$cpu,\"$cmd\"" >> "$OUTPUT_FILE" # Save full command to file in CSV format +done + +print_header "End of Report" +echo "Detailed information saved to: $OUTPUT_FILE" + +# Prompt to send the file via email +ORIGINAL_USER=$(who | head -n 1 | awk '{print $1}') +USER_EMAIL="${ORIGINAL_USER}@xyz.com" +read -p "Do you want to send the report to your email address ($USER_EMAIL)? (y/n): " SEND_EMAIL + +if [[ $SEND_EMAIL == [Yy]* ]]; then + echo "Sending report to $USER_EMAIL..." + mail -s "CPU Utilization Report" -a "$OUTPUT_FILE" "$USER_EMAIL" <<< "Please find the attached CPU utilization report." + echo "Report sent to $USER_EMAIL." +else + echo "Report not sent." +fi From c2884417d5375549e79f0bf1aa4c25097d5f5bb9 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Fri, 4 Oct 2024 16:31:59 +0530 Subject: [PATCH 21/48] Update new_Csv --- new_Csv | 75 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/new_Csv b/new_Csv index ef924fa..e1b3839 100644 --- a/new_Csv +++ b/new_Csv @@ -31,24 +31,20 @@ TCP_CONNECTIONS=$(ss -t | wc -l) RUNNING_PROCESSES=$(ps -e --no-headers | wc -l) TOP_FID_CPU=$(ps -eo user,%cpu --sort=-%cpu | awk '{cpu[$1]+=$2} END {for (u in cpu) print u, cpu[u]}' | sort -k2 -nr | head -n 5) +# Save to CSV format { echo "Report generated on:,[$DATE],for Hostname:,$HOSTNAME" - - print_header "Top 5 Memory-Consuming Processes" + echo "Top 5 Memory-Consuming Processes" echo "PID,Memory (%),Command" ps aux --sort=-%mem | awk '{print $2, $4, substr($0, index($0, $11))}' | head -n 6 | while read -r pid mem cmd; do echo "$pid,$mem,\"$(echo "$cmd" | cut -c 1-1000)\"" done - echo "*****" - - print_header "Top 5 CPU-Consuming Processes" + echo "Top 5 CPU-Consuming Processes" echo "PID,CPU (%),Command" ps aux --sort=-%cpu | awk '{print $2, $3, substr($0, index($0, $11))}' | head -n 6 | while read -r pid cpu cmd; do echo "$pid,$cpu,\"$(echo "$cmd" | cut -c 1-1000)\"" done - echo "*****" - - print_header "Additional System Information" + echo "Additional System Information" echo "Metric,Value" echo "CPU Utilization (top),$CPU_USAGE_TOP%" echo "CPU Utilization (vmstat),$CPU_USAGE_VMSTAT%" @@ -56,43 +52,56 @@ TOP_FID_CPU=$(ps -eo user,%cpu --sort=-%cpu | awk '{cpu[$1]+=$2} END {for (u in echo "System Uptime and Load Average,\"$(uptime)\"" echo "TCP Connections,$TCP_CONNECTIONS" echo "Running Processes,$RUNNING_PROCESSES" - - # print_header "Per-Core CPU Utilization" - # echo "$MPSTAT_OUTPUT" - - # print_header "Detailed CPU Stats" - # echo "$IOSTAT_OUTPUT" - - print_header "Disk Usage (All Mounted Filesystems)" + echo "Disk Usage (All Mounted Filesystems)" echo "Filesystem,Size,Used,Available,Use%,Mounted on" df -h | awk 'NR>1 {print $1 "," $2 "," $3 "," $4 "," $5 "," $6}' - echo "Uptime (Human-Readable),$UPTIME_P" echo "System Start Time,$UPTIME_S" - -} | tee "$OUTPUT_FILE" +} > "$OUTPUT_FILE" + +# Display human-readable output in terminal +echo "Report generated on: [$DATE] for Hostname: $HOSTNAME" +print_header "Top 5 Memory-Consuming Processes" +ps aux --sort=-%mem | awk '{printf "PID: %-10s Memory: %-5s Command: %s\n", $2, $4, substr($0, index($0, $11))}' | head -n 6 +echo "*****" + +print_header "Top 5 CPU-Consuming Processes" +ps aux --sort=-%cpu | awk '{printf "PID: %-10s CPU: %-5s Command: %s\n", $2, $3, substr($0, index($0, $11))}' | head -n 6 +echo "*****" + +print_header "Additional System Information" +echo "CPU Utilization (top): $CPU_USAGE_TOP%" +echo "*****" +echo "CPU Utilization (vmstat): $CPU_USAGE_VMSTAT%" +echo "*****" +echo "Memory Usage: $MEMORY_USAGE" +echo "*****" +echo "System Uptime and Load Average: $(uptime)" +echo "*****" +echo "TCP Connections: $TCP_CONNECTIONS" +echo "*****" +echo "Running Processes: $RUNNING_PROCESSES" +echo "*****" +print_header "Disk Usage (All Mounted Filesystems)" +df -h +echo "*****" +echo "Uptime (Human-Readable): $UPTIME_P" +echo "*****" +echo "System Start Time: $UPTIME_S" +echo "*****" -echo print_header "Top 5 FIDs by Total CPU Utilization" -echo "No.,FID,Total CPU (%)" -echo "$TOP_FID_CPU" | nl -w2 -s'. ' | awk '{print $1 "," $2 "," $3}' +echo "No. FID Total CPU (%)" +echo "$TOP_FID_CPU" | nl -w2 -s'. ' | awk '{printf "%-4s %-10s %-10s\n", $1, $2, $3}' +echo "*****" -echo read -p "Select an FID by entering the corresponding number: " FID_SELECTION SELECTED_FID=$(echo "$TOP_FID_CPU" | sed -n "${FID_SELECTION}p" | awk '{print $1}') print_header "Top 10 CPU-consuming processes for FID '$SELECTED_FID'" -# Display truncated command line for readability and save full command to file -echo "PID,CPU (%),Command" >> "$OUTPUT_FILE" -ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,cmd | head -n 10 | while read -r pid cpu cmd; do - echo "$pid,$cpu,\"$(echo "$cmd" | cut -c 1-1000)\"" - echo "$pid,$cpu,\"$cmd\"" >> "$OUTPUT_FILE" # Save full command to file in CSV format -done - -print_header "End of Report" -echo "Detailed information saved to: $OUTPUT_FILE" +ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,cmd | head -n 10 | awk '{printf "PID: %-10s CPU: %-5s Command: %s\n", $1, $2, substr($0, index($0, $3))}' -# Prompt to send the file via email +# Prompt to email the CSV file ORIGINAL_USER=$(who | head -n 1 | awk '{print $1}') USER_EMAIL="${ORIGINAL_USER}@xyz.com" read -p "Do you want to send the report to your email address ($USER_EMAIL)? (y/n): " SEND_EMAIL From 4ec4d6c5c9711af72291cbc5484424fb7ba05f94 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Fri, 4 Oct 2024 16:44:05 +0530 Subject: [PATCH 22/48] Update new_Csv --- new_Csv | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/new_Csv b/new_Csv index e1b3839..4cc1a39 100644 --- a/new_Csv +++ b/new_Csv @@ -31,18 +31,18 @@ TCP_CONNECTIONS=$(ss -t | wc -l) RUNNING_PROCESSES=$(ps -e --no-headers | wc -l) TOP_FID_CPU=$(ps -eo user,%cpu --sort=-%cpu | awk '{cpu[$1]+=$2} END {for (u in cpu) print u, cpu[u]}' | sort -k2 -nr | head -n 5) -# Save to CSV format +# Save initial information to CSV { echo "Report generated on:,[$DATE],for Hostname:,$HOSTNAME" echo "Top 5 Memory-Consuming Processes" echo "PID,Memory (%),Command" ps aux --sort=-%mem | awk '{print $2, $4, substr($0, index($0, $11))}' | head -n 6 | while read -r pid mem cmd; do - echo "$pid,$mem,\"$(echo "$cmd" | cut -c 1-1000)\"" + echo "$pid,$mem,\"$cmd\"" done echo "Top 5 CPU-Consuming Processes" echo "PID,CPU (%),Command" ps aux --sort=-%cpu | awk '{print $2, $3, substr($0, index($0, $11))}' | head -n 6 | while read -r pid cpu cmd; do - echo "$pid,$cpu,\"$(echo "$cmd" | cut -c 1-1000)\"" + echo "$pid,$cpu,\"$cmd\"" done echo "Additional System Information" echo "Metric,Value" @@ -62,11 +62,15 @@ TOP_FID_CPU=$(ps -eo user,%cpu --sort=-%cpu | awk '{cpu[$1]+=$2} END {for (u in # Display human-readable output in terminal echo "Report generated on: [$DATE] for Hostname: $HOSTNAME" print_header "Top 5 Memory-Consuming Processes" -ps aux --sort=-%mem | awk '{printf "PID: %-10s Memory: %-5s Command: %s\n", $2, $4, substr($0, index($0, $11))}' | head -n 6 +ps aux --sort=-%mem | awk '{print $2, $4, substr($0, index($0, $11))}' | head -n 6 | while read -r pid mem cmd; do + printf "PID: %-10s Memory: %-5s Command: %s\n" "$pid" "$mem" "$(echo "$cmd" | cut -c 1-1000)" +done echo "*****" print_header "Top 5 CPU-Consuming Processes" -ps aux --sort=-%cpu | awk '{printf "PID: %-10s CPU: %-5s Command: %s\n", $2, $3, substr($0, index($0, $11))}' | head -n 6 +ps aux --sort=-%cpu | awk '{print $2, $3, substr($0, index($0, $11))}' | head -n 6 | while read -r pid cpu cmd; do + printf "PID: %-10s CPU: %-5s Command: %s\n" "$pid" "$cpu" "$(echo "$cmd" | cut -c 1-1000)" +done echo "*****" print_header "Additional System Information" @@ -95,11 +99,18 @@ echo "No. FID Total CPU (%)" echo "$TOP_FID_CPU" | nl -w2 -s'. ' | awk '{printf "%-4s %-10s %-10s\n", $1, $2, $3}' echo "*****" +# User selects FID and the details are appended to CSV file read -p "Select an FID by entering the corresponding number: " FID_SELECTION SELECTED_FID=$(echo "$TOP_FID_CPU" | sed -n "${FID_SELECTION}p" | awk '{print $1}') print_header "Top 10 CPU-consuming processes for FID '$SELECTED_FID'" -ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,cmd | head -n 10 | awk '{printf "PID: %-10s CPU: %-5s Command: %s\n", $1, $2, substr($0, index($0, $3))}' +echo "PID,CPU (%),Command" >> "$OUTPUT_FILE" +ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,cmd | head -n 10 | while read -r pid cpu cmd; do + # Display in terminal with truncation + printf "PID: %-10s CPU: %-5s Command: %s\n" "$pid" "$cpu" "$(echo "$cmd" | cut -c 1-1000)" + # Save full command to CSV file + echo "$pid,$cpu,\"$cmd\"" >> "$OUTPUT_FILE" +done # Prompt to email the CSV file ORIGINAL_USER=$(who | head -n 1 | awk '{print $1}') From 0b1d991cc1224703f1c6716152c0757bc8de5139 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Mon, 7 Oct 2024 11:26:04 +0530 Subject: [PATCH 23/48] Create webpage --- webpage | 277 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 277 insertions(+) create mode 100644 webpage diff --git a/webpage b/webpage new file mode 100644 index 0000000..238b212 --- /dev/null +++ b/webpage @@ -0,0 +1,277 @@ + + + + + Server Port Details + + + +
DETAILS
+ + +
+

Enter Server Name for Download

+ + + +

Select Details to Download

+
+ +
+ + +
+
+ + + +
+
+ + + +
+
+ + + +
+
+ + +
+ +
+
+ + +
+

Gather Information for Multiple Apps

+ + + + + + + + + + + +

Select Details to Download

+
+ +
+ + +
+
+ + + +
+
+ + + +
+
+ + + +
+
+ + +
+ +
+
+ +
Contact us at: contact@example.com
+ + + + From 8f474aac3421b9ac7de0fefd32cb64ff0c0211d2 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Mon, 7 Oct 2024 18:48:24 +0530 Subject: [PATCH 24/48] Create Ansible --- Ansible | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Ansible diff --git a/Ansible b/Ansible new file mode 100644 index 0000000..f6c896f --- /dev/null +++ b/Ansible @@ -0,0 +1,22 @@ +--- +- name: Fetch details from multiple servers + hosts: all + tasks: + - name: Gather server details and save to a file + shell: | + echo "Server: {{ inventory_hostname }}" >> /tmp/server_info.txt + df -h >> /tmp/server_info.txt # Example command to fetch disk usage + register: command_output + ignore_errors: yes + + - name: Fetch the file to control node + fetch: + src: /tmp/server_info.txt + dest: /tmp/fetched/{{ inventory_hostname }}_server_info.txt + flat: yes + +- name: Move fetched files to abc server + hosts: localhost + tasks: + - name: Copy fetched files to abc + command: scp /tmp/fetched/* user@abc:/path/to/store From 6a001f960c5d88963e59a9f70c583ec8507223cd Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Mon, 7 Oct 2024 20:30:52 +0530 Subject: [PATCH 25/48] Create 2ansible --- 2ansible | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 2ansible diff --git a/2ansible b/2ansible new file mode 100644 index 0000000..9c85d8c --- /dev/null +++ b/2ansible @@ -0,0 +1,22 @@ +--- +- name: Fetch details from multiple servers + hosts: all + tasks: + - name: Gather server details and save to a file + shell: | + echo "Server: {{ inventory_hostname }}" >> /tmp/server_info.txt + df -h >> /tmp/server_info.txt # Example command to fetch disk usage + register: command_output + ignore_errors: yes + + - name: Fetch the file to control node + fetch: + src: /tmp/server_info.txt + dest: /tmp/fetched/{{ inventory_hostname }}_server_info.txt + flat: yes + +- name: Move fetched files to 123456 + hosts: localhost + tasks: + - name: Copy fetched files to 123456 + command: scp /tmp/fetched/* user@123456:/tmp/ From 3870437c6918bbed8b9e9fddebe048f007531226 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Mon, 7 Oct 2024 20:36:36 +0530 Subject: [PATCH 26/48] Create 3 ansible --- 3 ansible | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 3 ansible diff --git a/3 ansible b/3 ansible new file mode 100644 index 0000000..326e912 --- /dev/null +++ b/3 ansible @@ -0,0 +1,22 @@ +--- +- name: Fetch details from multiple servers + hosts: all + tasks: + - name: Gather server details and save to a file + shell: | + echo "Server: {{ inventory_hostname }}" >> /tmp/server_info.txt + df -h >> /tmp/server_info.txt # Example command to fetch disk usage + register: command_output + ignore_errors: yes + + - name: Fetch the file to control node + fetch: + src: /tmp/server_info.txt + dest: /tmp/fetched/{{ inventory_hostname }}_server_info.txt + flat: yes + +- name: Move fetched files to 123456 + hosts: localhost + tasks: + - name: Copy fetched files to /tmp/ on 123456 + command: scp /tmp/fetched/* user@123456:/tmp/ From 2fa512f11c449afee407ccdcbb3b244db18584ea Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Mon, 7 Oct 2024 20:55:13 +0530 Subject: [PATCH 27/48] Create Scribble --- Scribble | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 Scribble diff --git a/Scribble b/Scribble new file mode 100644 index 0000000..ffd36f2 --- /dev/null +++ b/Scribble @@ -0,0 +1,5 @@ +awk -F: '{ print "FID: " $1 ", UID: " $3 ", GID: " $4 }' /etc/passwd + +group + +awk -F: '{ print "Group: " $1 ", GID: " $3 }' /etc/group From c12b6acb4b9ad1bc90059c7f61935170916f6d96 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Tue, 8 Oct 2024 20:02:35 +0530 Subject: [PATCH 28/48] Update Scribble --- Scribble | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Scribble b/Scribble index ffd36f2..e6c13b9 100644 --- a/Scribble +++ b/Scribble @@ -3,3 +3,8 @@ awk -F: '{ print "FID: " $1 ", UID: " $3 ", GID: " $4 }' /etc/passwd group awk -F: '{ print "Group: " $1 ", GID: " $3 }' /etc/group + + +GID + +ps -eo pid,user,uid,gid,pcpu,pmem,args --sort=-pcpu | grep java From 0d9f6a3f1e3725aa381192421fd9a1020ce04529 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Tue, 8 Oct 2024 20:14:23 +0530 Subject: [PATCH 29/48] Create 2_ansible --- 2_ansible | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 2_ansible diff --git a/2_ansible b/2_ansible new file mode 100644 index 0000000..4cbd4df --- /dev/null +++ b/2_ansible @@ -0,0 +1,11 @@ +--- +- hosts: all_servers + gather_facts: no + tasks: + - name: Run ps command to get PID, FID (ignoring first two letters), GID, and CPU usage + shell: | + ps -eo pid,user,uid,gid,pcpu,pmem,args --sort=-pcpu | grep | awk '{print $1, substr($2, 3), $3, $4, $5, $6, $7}' + register: ps_output + + - name: Write output to a file on the control machine + local_action: copy content="{{ ps_output.stdout }}" dest="./output_{{ inventory_hostname }}.txt" From 96313dbd926e381af17db22b6575906448a52802 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Tue, 8 Oct 2024 20:14:55 +0530 Subject: [PATCH 30/48] Update 2_ansible --- 2_ansible | 2 ++ 1 file changed, 2 insertions(+) diff --git a/2_ansible b/2_ansible index 4cbd4df..e600885 100644 --- a/2_ansible +++ b/2_ansible @@ -9,3 +9,5 @@ - name: Write output to a file on the control machine local_action: copy content="{{ ps_output.stdout }}" dest="./output_{{ inventory_hostname }}.txt" + +ansible-playbook -i hosts.ini run_ps_command.yml From eaa3d13a72ecd392629210928a389eb460e3995e Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Tue, 8 Oct 2024 20:34:09 +0530 Subject: [PATCH 31/48] Update 2_ansible --- 2_ansible | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/2_ansible b/2_ansible index e600885..e24d8c7 100644 --- a/2_ansible +++ b/2_ansible @@ -4,10 +4,12 @@ tasks: - name: Run ps command to get PID, FID (ignoring first two letters), GID, and CPU usage shell: | - ps -eo pid,user,uid,gid,pcpu,pmem,args --sort=-pcpu | grep | awk '{print $1, substr($2, 3), $3, $4, $5, $6, $7}' + ps -eo pid,user,uid,gid,pcpu,pmem,args --sort=-pcpu | \ + awk '!seen[$1]++' | grep -vE "^[a-z]{2}[0-9]+" register: ps_output - name: Write output to a file on the control machine local_action: copy content="{{ ps_output.stdout }}" dest="./output_{{ inventory_hostname }}.txt" + ansible-playbook -i hosts.ini run_ps_command.yml From 404a5b50c866469c0a98b21749ffbd560814c925 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Tue, 8 Oct 2024 20:46:47 +0530 Subject: [PATCH 32/48] Create 4_ansible --- 4_ansible | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 4_ansible diff --git a/4_ansible b/4_ansible new file mode 100644 index 0000000..d9904c9 --- /dev/null +++ b/4_ansible @@ -0,0 +1,16 @@ +--- +- hosts: all_servers + gather_facts: no + tasks: + - name: Run ps command to get PID, FID (ignoring first two letters), GID, and CPU usage + shell: | + ps -eo pid,user,uid,gid,pcpu,pmem,args --sort=-pcpu | \ + awk '!seen[$1]++' | grep -vE "^[a-z]{2}[0-9]+" + register: ps_output + + - name: Show ps command output + debug: + var: ps_output.stdout + + - name: Write output to a file on the control machine + local_action: copy content="{{ ps_output.stdout }}" dest="./output_{{ inventory_hostname }}.txt" From 23f3bffa0b6ef28fb63bfc6f32bd121a88985a64 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Wed, 9 Oct 2024 09:52:50 +0530 Subject: [PATCH 33/48] Create 9th_Ansible_01 --- 9th_Ansible_01 | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 9th_Ansible_01 diff --git a/9th_Ansible_01 b/9th_Ansible_01 new file mode 100644 index 0000000..4e58647 --- /dev/null +++ b/9th_Ansible_01 @@ -0,0 +1,21 @@ +--- +- name: Collect disk usage and save to CSV + hosts: servers + become: yes # If root privileges are required + tasks: + + - name: Ensure the /tmp/fid directory exists + file: + path: /tmp/fid + state: directory + mode: '0777' + + - name: Run df -h and save output to CSV + shell: df -h > /tmp/fid/output_$(date +%Y%m%d%H%M%S).csv + args: + executable: /bin/bash + + - name: Set permissions for the output CSV file + file: + path: /tmp/fid/output_*.csv + mode: '0777' From 57acc3777fa7c27791a13dadbaeb317153760b78 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Wed, 9 Oct 2024 10:16:18 +0530 Subject: [PATCH 34/48] Create 9thAnsible_02 --- 9thAnsible_02 | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 9thAnsible_02 diff --git a/9thAnsible_02 b/9thAnsible_02 new file mode 100644 index 0000000..ddea8cf --- /dev/null +++ b/9thAnsible_02 @@ -0,0 +1,13 @@ +--- +- name: Collect remote server hostname + hosts: servers + become: yes + tasks: + - name: Show the remote server's hostname + debug: + var: ansible_hostname + + - name: Save the hostname to a file + shell: echo {{ ansible_hostname }} > /tmp/fid/hostname_$(date +%Y%m%d%H%M%S).txt + args: + executable: /bin/bash From 5a19d20f9a7f11368c50f2bcbb19be6e2c3e374d Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Wed, 9 Oct 2024 10:32:43 +0530 Subject: [PATCH 35/48] Create Shell Scripting --- Shell Scripting | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Shell Scripting diff --git a/Shell Scripting b/Shell Scripting new file mode 100644 index 0000000..97cdcf1 --- /dev/null +++ b/Shell Scripting @@ -0,0 +1,9 @@ +ps -eo user,gid,pid,comm --sort=user | awk '{ system("id -gn " $1 " 2>/dev/null") | getline group; $2=group; print }' | grep -vE "[a-z]{2}[0-9]+" | uniq + + +ps -eo user,gid,pid,comm --sort=user: This lists the user, GID, PID, and the command, sorted by user. +awk: For each user, it uses system("id -gn " $1) to dynamically fetch the group name for the user. It then replaces the GID field with the group name. +getline group: Captures the group name and stores it in the variable group. +2>/dev/null: Suppresses error messages in case id doesn't return anything (for example, if there's an issue with the user lookup). +grep -vE "[a-z]{2}[0-9]+": Filters out unwanted results as per your original command. +uniq: Removes duplicate entries. From e0a4ef39caf8174bfe15ff31ebd28d784356c932 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Wed, 9 Oct 2024 10:36:16 +0530 Subject: [PATCH 36/48] Update Shell Scripting --- Shell Scripting | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Shell Scripting b/Shell Scripting index 97cdcf1..fb60616 100644 --- a/Shell Scripting +++ b/Shell Scripting @@ -7,3 +7,13 @@ getline group: Captures the group name and stores it in the variable group. 2>/dev/null: Suppresses error messages in case id doesn't return anything (for example, if there's an issue with the user lookup). grep -vE "[a-z]{2}[0-9]+": Filters out unwanted results as per your original command. uniq: Removes duplicate entries. + + + + +******************************************* + +ps -eo user,gid,pid,comm --sort=user | while read user gid pid comm; do + group=$(id -gn "$user" 2>/dev/null); + printf "%-10s %-10s %-10s %-10s\n" "$user" "$group" "$pid" "$comm"; +done | grep -vE "[a-z]{2}[0-9]+" | uniq From 0d162e09598694a791af4142f646769eaccbae42 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Wed, 9 Oct 2024 10:42:42 +0530 Subject: [PATCH 37/48] Update Shell Scripting --- Shell Scripting | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Shell Scripting b/Shell Scripting index fb60616..b5ced59 100644 --- a/Shell Scripting +++ b/Shell Scripting @@ -17,3 +17,11 @@ ps -eo user,gid,pid,comm --sort=user | while read user gid pid comm; do group=$(id -gn "$user" 2>/dev/null); printf "%-10s %-10s %-10s %-10s\n" "$user" "$group" "$pid" "$comm"; done | grep -vE "[a-z]{2}[0-9]+" | uniq + + +**************************************** + +ps -eo user,gid,pid,comm --sort=user | (head -n 1 && tail -n +2 | while read user gid pid comm; do + group=$(id -gn "$user" 2>/dev/null); + printf "%-10s %-10s %-10s %-10s\n" "$user" "$group" "$pid" "$comm"; +done | grep -vE "[a-z]{2}[0-9]+" | sort -u -k1,2) From 39df9e7e2e5e11505d1e674d16057c381b7a8f83 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Wed, 9 Oct 2024 10:54:11 +0530 Subject: [PATCH 38/48] Create 9th_Ansible_03 --- 9th_Ansible_03 | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 9th_Ansible_03 diff --git a/9th_Ansible_03 b/9th_Ansible_03 new file mode 100644 index 0000000..467298c --- /dev/null +++ b/9th_Ansible_03 @@ -0,0 +1,22 @@ +--- +- name: Run shell command to gather user, group, PID, and command data + hosts: + - server + - server2 + - server3 + become: yes + tasks: + + - name: Run shell command and save output to /tmp directory + shell: | + ps -eo user,gid,pid,comm --sort=user | (head -n 1 && tail -n +2 | while read user gid pid comm; do + group=$(id -gn "$user" 2>/dev/null); + printf "%-10s %-10s %-10s %-10s\n" "$user" "$group" "$pid" "$comm"; + done | grep -vE "[a-z]{2}[0-9]+" | sort -u -k1,2) > /tmp/user_group_pid_comm_$(date +%Y%m%d%H%M%S).txt + args: + executable: /bin/bash + register: command_output + + - name: Display the output file created + debug: + var: command_output From 50fcc941a367a924f26f34c5bee4233d23b57396 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Wed, 9 Oct 2024 19:41:36 +0530 Subject: [PATCH 39/48] Update Scribble --- Scribble | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Scribble b/Scribble index e6c13b9..b47f64d 100644 --- a/Scribble +++ b/Scribble @@ -8,3 +8,14 @@ awk -F: '{ print "Group: " $1 ", GID: " $3 }' /etc/group GID ps -eo pid,user,uid,gid,pcpu,pmem,args --sort=-pcpu | grep java + + +************* + +ps -eo user,gid,pid,comm --sort=user | ( + head -n 1 + tail -n +2 | while read user gid pid comm; do + group=$(id -gn "$user" 2>/dev/null); + printf "%-10s %-10s %-10s %-10s\n" "$user" "$group" "$pid" "$comm"; + done +) | grep -vE "[a-z]{2}[0-9]+" | sort -u -k1,2 From d19e83784b187eb4719433718b925e385264d95b Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Wed, 9 Oct 2024 20:03:02 +0530 Subject: [PATCH 40/48] Update Scribble --- Scribble | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Scribble b/Scribble index b47f64d..5a42543 100644 --- a/Scribble +++ b/Scribble @@ -19,3 +19,8 @@ ps -eo user,gid,pid,comm --sort=user | ( printf "%-10s %-10s %-10s %-10s\n" "$user" "$group" "$pid" "$comm"; done ) | grep -vE "[a-z]{2}[0-9]+" | sort -u -k1,2 + + +******************** + +ps -eo user,gid,pid,comm --sort=user | awk 'NR==1 {print "USER GROUP PID COMMAND"} NR>1 { system("id -gn " $1 " 2>/dev/null") | getline group; print $1, group, $3, $4 }' | grep -vE "[a-z]{2}[0-9]+" | sort -u -k1,2 From 9bab774f671a4ae4bef5d388b20971e42bd50917 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Wed, 9 Oct 2024 20:06:53 +0530 Subject: [PATCH 41/48] Update Scribble --- Scribble | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Scribble b/Scribble index 5a42543..3649bca 100644 --- a/Scribble +++ b/Scribble @@ -24,3 +24,8 @@ ps -eo user,gid,pid,comm --sort=user | ( ******************** ps -eo user,gid,pid,comm --sort=user | awk 'NR==1 {print "USER GROUP PID COMMAND"} NR>1 { system("id -gn " $1 " 2>/dev/null") | getline group; print $1, group, $3, $4 }' | grep -vE "[a-z]{2}[0-9]+" | sort -u -k1,2 + + +***** + +ps -eo user,gid,pid,comm --sort=user | awk 'NR==1 {print "USER GROUP PID COMMAND"} NR>1 { system("id -gn " $1 " 2>/dev/null") | getline group; if (!seen[$1, group]++) printf "%-10s %-10s %-10s %-10s\n", $1, group, $3, $4 }' | grep -vE "[a-z]{2}[0-9]+" | sort -u -k1,2 From e89ef9bca2674f3cc5b442179049039cc014005e Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Wed, 9 Oct 2024 20:08:31 +0530 Subject: [PATCH 42/48] Update Scribble --- Scribble | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Scribble b/Scribble index 3649bca..858933e 100644 --- a/Scribble +++ b/Scribble @@ -29,3 +29,11 @@ ps -eo user,gid,pid,comm --sort=user | awk 'NR==1 {print "USER GROUP ***** ps -eo user,gid,pid,comm --sort=user | awk 'NR==1 {print "USER GROUP PID COMMAND"} NR>1 { system("id -gn " $1 " 2>/dev/null") | getline group; if (!seen[$1, group]++) printf "%-10s %-10s %-10s %-10s\n", $1, group, $3, $4 }' | grep -vE "[a-z]{2}[0-9]+" | sort -u -k1,2 + +*********** + +ps -eo user,gid,pid,comm --sort=user | ( + head -n 1 + tail -n +2 | awk '{ system("id -gn " $1 " 2>/dev/null") | getline group; if (!seen[$1, group]++) printf "%-10s %-10s %-10s %-10s\n", $1, group, $3, $4 }' +) | grep -vE "[a-z]{2}[0-9]+" | sort -u -k1,2 + From de6ac600e0fe0e65698c7007049a3568395ea635 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Wed, 9 Oct 2024 20:12:56 +0530 Subject: [PATCH 43/48] Update Scribble --- Scribble | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Scribble b/Scribble index 858933e..927dd22 100644 --- a/Scribble +++ b/Scribble @@ -37,3 +37,8 @@ ps -eo user,gid,pid,comm --sort=user | ( tail -n +2 | awk '{ system("id -gn " $1 " 2>/dev/null") | getline group; if (!seen[$1, group]++) printf "%-10s %-10s %-10s %-10s\n", $1, group, $3, $4 }' ) | grep -vE "[a-z]{2}[0-9]+" | sort -u -k1,2 +*************** + +ps -eo user,gid,pid,comm --sort=user | awk 'NR==1 {print "USER GROUP PID COMMAND"} NR>1 { "id -gn " $1 | getline group; if (!seen[$1, group]++) print $1, group, $3, $4 }' | grep -vE "[a-z]{2}[0-9]+" | sort -u -k1,2 + + From 3167a340163f5f57f8fb7ff8f41dd135bf320403 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Wed, 9 Oct 2024 20:16:54 +0530 Subject: [PATCH 44/48] Update Scribble --- Scribble | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Scribble b/Scribble index 927dd22..f087c36 100644 --- a/Scribble +++ b/Scribble @@ -30,6 +30,11 @@ ps -eo user,gid,pid,comm --sort=user | awk 'NR==1 {print "USER GROUP ps -eo user,gid,pid,comm --sort=user | awk 'NR==1 {print "USER GROUP PID COMMAND"} NR>1 { system("id -gn " $1 " 2>/dev/null") | getline group; if (!seen[$1, group]++) printf "%-10s %-10s %-10s %-10s\n", $1, group, $3, $4 }' | grep -vE "[a-z]{2}[0-9]+" | sort -u -k1,2 +*********** + +ps -eo user,gid,pid,comm --sort=user | awk 'NR==1 {printf "%-10s %-10s %-10s %-10s\n", "USER", "GROUP", "PID", "COMMAND"} NR>1 { "id -gn " $1 | getline group; if (!seen[$1, group]++) printf "%-10s %-10s %-10s %-10s\n", $1, group, $3, $4 }' | grep -vE "[a-z]{2}[0-9]+" | sort -u -k1,2 + + *********** ps -eo user,gid,pid,comm --sort=user | ( From cdbfd15a2e93173a1fa176e61bf547a2f03faf49 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Sun, 13 Oct 2024 23:07:37 +0530 Subject: [PATCH 45/48] Create agent --- agent | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 agent diff --git a/agent b/agent new file mode 100644 index 0000000..55073d0 --- /dev/null +++ b/agent @@ -0,0 +1,31 @@ +import subprocess +from flask import Flask, request, jsonify + +app = Flask(__name__) + +@app.route('/execute', methods=['POST']) +def execute_command(): + data = request.get_json() + commands = data.get('commands') + + if not commands: + return jsonify({"error": "No commands provided"}), 400 + + # Execute the commands locally on the server where this agent is running + command_output = {} + for command in commands: + try: + result = subprocess.run(command, shell=True, capture_output=True, text=True) + command_output[command] = { + "success": result.returncode == 0, + "output": result.stdout.strip() if result.returncode == 0 else None, + "error": result.stderr.strip() if result.returncode != 0 else None + } + except Exception as e: + command_output[command] = {"success": False, "error": str(e)} + + return jsonify({"commands_executed": command_output}) + +if __name__ == '__main__': + # Start Flask app, listening on port 5001 + app.run(host='0.0.0.0', port=5001) From da214bb721994cf3679905255401111a467071c3 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Sun, 13 Oct 2024 23:07:59 +0530 Subject: [PATCH 46/48] Create frontend --- frontend | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 frontend diff --git a/frontend b/frontend new file mode 100644 index 0000000..98562aa --- /dev/null +++ b/frontend @@ -0,0 +1,105 @@ +from flask import Flask, request, render_template_string, jsonify, Response +import requests +import json # Import to handle JSON + +app = Flask(__name__) + +# Basic HTML template to take user input and show results +html_template = """ + + + + + Execute Command + + +

Execute Command on Remote Server

+
+ +

+ + +

+ + +
+ + {% if result %} +

Command Output:

+
{{ result }}
+ + +
+ + +
+ {% endif %} + + +""" + +@app.route('/', methods=['GET']) +def index(): + return render_template_string(html_template) + +@app.route('/execute', methods=['POST']) +def execute_command(): + server_ip = request.form['server_ip'] + command = request.form['command'] + + # Prepare the data to send to the agent + payload = { + "commands": [command] + } + + # Send the request to the agent running on the specified server + try: + url = f"http://{server_ip}:5001/execute" + response = requests.post(url, json=payload) + result = response.json() # Properly parsed JSON response from the agent + + # Serialize the result to a JSON string to safely pass it in the hidden field + result_json = json.dumps(result) + + except Exception as e: + result = f"Error connecting to the agent: {str(e)}" + result_json = json.dumps({"error": str(e)}) + + return render_template_string(html_template, result=result, result_json=result_json) + +@app.route('/download', methods=['GET']) +def download_output(): + output = request.args.get('output', '') + + # Decode the string back into JSON + try: + output_json = json.loads(output) # Safely parse JSON + except Exception as e: + return f"Error parsing output: {str(e)}" + + # Convert the output to a human-readable format + output_text = format_output(output_json) + + # Create a response to download as a text file + return Response( + output_text, + mimetype="text/plain", + headers={"Content-Disposition": "attachment;filename=command_output.txt"} + ) + +def format_output(output): + # Convert JSON output into a human-readable format + formatted_output = "" + for command, details in output.get("commands_executed", {}).items(): + formatted_output += f"Command: {command}\n" + formatted_output += f"Success: {details['success']}\n" + if details['success']: + formatted_output += f"Output:\n{details['output']}\n" + else: + formatted_output += f"Error:\n{details['error']}\n" + formatted_output += "-" * 50 + "\n" + + return formatted_output + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=3001) From 327d8ff4061196300db935cfc5f0498eaaf94a33 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Mon, 14 Oct 2024 07:49:46 +0530 Subject: [PATCH 47/48] Create For Agent Requirement --- For Agent Requirement | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 For Agent Requirement diff --git a/For Agent Requirement b/For Agent Requirement new file mode 100644 index 0000000..121dc55 --- /dev/null +++ b/For Agent Requirement @@ -0,0 +1,14 @@ +Python 3 +Flask +requests + +sudo ufw allow 5001 +sudo ufw allow 3001 +sudo ufw reload + +requests: Used to make HTTP requests in the second script. +subprocess: Part of the Python standard library (no need to install). +json: Part of the Python standard library (no need to install). +flask: Already covered above. + + From 441cae7775fd2118cb3b6fbace4d45e398632db7 Mon Sep 17 00:00:00 2001 From: PythonscriptingLab Date: Tue, 7 Jan 2025 12:26:48 +0530 Subject: [PATCH 48/48] Create Test --- Test | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Test diff --git a/Test b/Test new file mode 100644 index 0000000..95a3fd9 --- /dev/null +++ b/Test @@ -0,0 +1,55 @@ +#!/bin/bash + +# Updated by: You +# Script to check disk space and email affected servers + +# Variables +OPLOG="/apps/install/script/fsck.log" +TMPPS="/tmp/" +MAILTO="your_email@domain.com" # Update with the target email +TEST_EMAIL="rs97558@citi.com,other_email@domain.com" # Add all test emails here +HOSTLIST="/apps/install/script/m.txt" # File containing hostnames +AFFECTED_SERVERS="" + +# Clear log file +if [ -f ${OPLOG} ]; then + rm ${OPLOG} +fi + +# Header for email/log +echo "/opt* /apps /logs File Systems are Important to RUN Business" > ${OPLOG} +echo " " >> ${OPLOG} + +# Start checking servers +for i in $(cat ${HOSTLIST}); do + echo "Checking server: $i" + + # Determine OS and get disk usage + OST=$(ssh -o "BatchMode yes" $i uname 2>/dev/null) + if [ "$OST" == "AIX" ]; then + ssh -o "BatchMode yes" $i df -k | egrep -v "proc|mnt|net|boot|rel" | \ + awk -v hostname="$i" '{if ($3/$2*100 <= 95) print hostname": "$3"(KB free)\t"$4}' >> ${TMPPS}${i} + elif [ "$OST" == "Linux" ]; then + ssh -o "BatchMode yes" $i df -k | egrep -v "proc|mnt|net|boot|rel" | \ + awk -v hostname="$i" '{if ($3/$1*100 <= 95) print hostname": "$3"(KB free)\t"$4}' >> ${TMPPS}${i} + fi + + # Check if the server has issues + LNCNT=$(wc -l ${TMPPS}${i} 2>/dev/null | awk '{print $1}') + if [ "$LNCNT" -gt 0 ]; then + echo "Adding $i to affected servers" + cat ${TMPPS}${i} >> ${OPLOG} + echo " " >> ${OPLOG} + AFFECTED_SERVERS="${AFFECTED_SERVERS} $i" + rm ${TMPPS}${i} + fi +done + +# Email affected servers +if [ -n "${AFFECTED_SERVERS}" ]; then + echo "Affected servers: ${AFFECTED_SERVERS}" >> ${OPLOG} + cat ${OPLOG} | mailx -s "IGNORE_TEST_EMAIL Attention L2 !! Please check & clear disk spaces" -r "ebizmob@citi.com" ${TEST_EMAIL} + echo "Email sent successfully to: ${TEST_EMAIL}" +else + echo "No affected servers found. No email sent." +fi