diff --git a/2_CPU b/2_CPU new file mode 100644 index 0000000..9df2db1 --- /dev/null +++ b/2_CPU @@ -0,0 +1,123 @@ +#!/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) +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" + 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" + + +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 diff --git a/2_ansible b/2_ansible new file mode 100644 index 0000000..e24d8c7 --- /dev/null +++ b/2_ansible @@ -0,0 +1,15 @@ +--- +- 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: 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 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/ 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/ 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" 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 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' 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 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 diff --git a/CPU_Utili b/CPU_Utili new file mode 100644 index 0000000..e2fba3c --- /dev/null +++ b/CPU_Utili @@ -0,0 +1,88 @@ +#!/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" + 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 + 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)" + 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 +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" 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. + + 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" diff --git a/Scribble b/Scribble new file mode 100644 index 0000000..f087c36 --- /dev/null +++ b/Scribble @@ -0,0 +1,49 @@ +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 + + +************* + +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 + + +******************** + +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 + +*********** + +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 | ( + 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 + +*************** + +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 + + diff --git a/Shell b/Shell index 345e6ae..6da9f2f 100644 --- a/Shell +++ b/Shell @@ -1 +1,95 @@ -Test +#!/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. 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 "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 +echo "CPU utilization, memory, and other system information saved to $OUTPUT_FILE" diff --git a/Shell Scripting b/Shell Scripting new file mode 100644 index 0000000..b5ced59 --- /dev/null +++ b/Shell Scripting @@ -0,0 +1,27 @@ +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. + + + + +******************************************* + +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) 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 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) 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 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 = """ + + +
+ +{{ 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)
diff --git a/new_Csv b/new_Csv
new file mode 100644
index 0000000..4cc1a39
--- /dev/null
+++ b/new_Csv
@@ -0,0 +1,126 @@
+#!/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)
+
+# 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,\"$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,\"$cmd\""
+ done
+ echo "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"
+ 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"
+} > "$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 '{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 '{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"
+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 "*****"
+
+print_header "Top 5 FIDs by Total CPU Utilization"
+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'"
+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}')
+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
diff --git a/webpage b/webpage
new file mode 100644
index 0000000..238b212
--- /dev/null
+++ b/webpage
@@ -0,0 +1,277 @@
+
+
+
+
+