-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem_monitor.sh
More file actions
65 lines (55 loc) · 2.36 KB
/
system_monitor.sh
File metadata and controls
65 lines (55 loc) · 2.36 KB
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
#!/bin/bash
# Define the path to your folder
set -euo pipefail
if [ ! -d /var/log/system_logs ]; then
sudo mkdir -p /var/log/system_logs
sudo chown $USER:$USER /var/log/system_logs
fi
if [ ! -f /var/log/system_logs/system_logs.log ]; then
sudo touch /var/log/system_logs/system_logs.log
sudo chown $USER:$USER /var/log/system_logs/system_logs.log
fi
log_path="/var/log/system_logs/system_logs.log"
#Log the current date and time with audit label
echo "===========================================" >> $log_path
echo "System Audit: $(date)">> $log_path
echo "===========================================" >> $log_path
#Parse CPU information from lscpu and extract specified fields
lscpu | while IFS=: read -r key value; do
case "$key" in
"CPU(s)")
echo "Total Number of CPU(s): $value" ;;
"CPU family")
echo "Processor Family ID: $value" ;;
"Model")
echo "Specific Model ID: $value" ;;
"Thread(s) per core")
echo "Hyper-threading Threads: $value" ;;
"Core(s) per socket")
echo "Physical Core Count: $value" ;;
"Model name")
echo "PC Model Name: $value" ;;
esac
done >> $log_path
#Memory Usage
(printf "Memory Usage \n" && free -h) >> $log_path;
Threshold=80
# Get total and used memory in Megabytes
Total_memory=$(free -m | awk '/^Mem:/{print $2}')
Used_memory=$(free -m | awk '/^Mem:/{print $3}')
# Calculate percentage
Percentage_used=$(echo "scale=2; $Used_memory / $Total_memory * 100" | bc | cut -d. -f1)
#Check if memory usage exceeds the set threshold and display a worning message
if [ "$Percentage_used" -ge "$Threshold" ]; then
echo "[WARN] Memory usage is critical: ${Percentage_used}% (Used_memory MB)" >> $log_path
/mnt/c/Windows/System32/cmd.exe /c msg \* "[Critical] Memory Usage: ${Percentage_used}%" & #pop up message in windows
else
echo "[INFO] Memory usage is healthy: ${Percentage_used}%" >> $log_path
fi
#Extract the IP address from eth0 interface and append network IP address
IP=$(timeout 10 ip addr show eth0 | grep "inet " | awk '{print $2}')
printf "Network IP: %s\n" "$IP" >> $log_path
#System uptime in human-readable format
echo "System Uptime: $(uptime -p)" >> $log_path
#Count and log the number of processes running for the current user
echo "Number of processes: $(ps -u abim --no-header| wc -l )" >> $log_path;