-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions_Task_B.sh
executable file
·146 lines (132 loc) · 4.06 KB
/
Functions_Task_B.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Function to log activities
# Inputs: $1 - Activity message to log
# Outputs: None
# Purpose: Logs the provided activity message with a timestamp to the log file.
log_activity() {
echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" >> $LOG_FILE
}
# Function to list running processes
# Inputs: None
# Outputs: Prints a list of running processes sorted by CPU and memory usage
# Purpose: Displays the current running processes.
list_processes() {
echo
ps aux --sort=-%cpu,-%mem
}
# Function to get detailed process information
# Inputs: User input for PID
# Outputs: Prints detailed information about the specified process
# Purpose: Provides detailed information about a specific process.
process_info() {
echo
read -p "Enter PID: " pid
ps -p $pid -o pid,ppid,user,%cpu,%mem,cmd
}
# Function to kill a process
# Inputs: User input for PID
# Outputs: Prints success or failure message
# Purpose: Terminates the specified process by its PID.
kill_process() {
echo
read -p "Enter PID to kill: " pid
kill $pid
if [ $? -eq 0 ]; then
log_activity "Killed process with PID $pid"
echo "Process $pid terminated."
else
echo "Failed to kill process $pid."
fi
}
# Function to display process statistics
# Inputs: None
# Outputs: Prints total number of processes, CPU load, and memory usage
# Purpose: Displays overall system process statistics.
process_statistics() {
echo
echo "Total Processes: $(ps -e --no-headers | wc -l)"
echo "CPU Load: $(uptime | awk -F'[a-z]:' '{ print $2}')"
free -h | awk 'NR==2{printf "Memory Usage: %s/%s (%.2f%%)\n", $3,$2,$3*100/$2 }'
echo
}
# Function for real-time monitoring
# Inputs: None
# Outputs: Continuously updates the display with running processes
# Purpose: Provides real-time monitoring of running processes.
real_time_monitoring() {
while true; do
echo
clear
list_processes | head -n 20
echo
sleep $UPDATE_INTERVAL
done
}
# Function to search and filter processes
# Inputs: User input for search criteria
# Outputs: Prints processes that match the search criteria
# Purpose: Allows the user to search for processes based on criteria.
search_processes() {
echo
read -p "Enter search criteria: " criteria
ps aux | grep "$criteria" | grep -v grep
}
# Function to check for resource usage alerts
# Inputs: None
# Outputs: Prints and logs high resource usage alerts
# Purpose: Monitors processes and logs alerts for high CPU or memory usage.
check_alerts() {
while true; do
high_cpu=$(ps aux --sort=-%cpu | awk -v threshold=$CPU_THRESHOLD 'NR>1 {if ($3 > threshold) print $0}')
high_mem=$(ps aux --sort=-%mem | awk -v threshold=$MEMORY_THRESHOLD 'NR>1 {if ($4 > threshold) print $0}')
if [ ! -z "$high_cpu" ]; then
echo "High CPU usage detected:"
echo "$high_cpu"
log_activity "High CPU usage detected: $high_cpu"
fi
if [ ! -z "$high_mem" ]; then
echo "High Memory usage detected:"
echo "$high_mem"
log_activity "High Memory usage detected: $high_mem"
fi
sleep $ALERTS_INTERVAL
done
}
# Cleanup function to terminate background processes
# Inputs: None
# Outputs: None
# Purpose: Terminates background processes and exits the script.
cleanup() {
if [[ $ALERTS_PID -ne 0 ]]; then
kill $ALERTS_PID
fi
exit 0
}
# Interactive menu function
# Inputs: User input for menu selection
# Outputs: Executes the selected menu option
# Purpose: Provides an interactive menu for the user to select operations.
interactive_mode() {
while true; do
clear
echo "Simple Process Monitor"
echo "1. List Running Processes"
echo "2. Process Information"
echo "3. Kill a Process"
echo "4. Process Statistics"
echo "5. Real-time Monitoring"
echo "6. Search and Filter"
echo "q. Exit"
read -p "Choose an option: " choice
case $choice in
1) list_processes ;;
2) process_info ;;
3) kill_process ;;
4) process_statistics ;;
5) real_time_monitoring ;;
6) search_processes ;;
q) cleanup ;;
*) echo "Invalid option. Try again." ;;
esac
read -p "Press Enter to continue..."
done
}