Great! Let’s set up the monitoring and logging step by step. I’ll guide you through the process.
We need to install mpstat
(from the sysstat
package) for monitoring CPU steal time and vnstat
for bandwidth monitoring.
-
Install
sysstat
formpstat
: Run the following command based on your Linux distribution:For Ubuntu/Debian:
sudo apt-get install sysstat
For CentOS/RHEL:
sudo yum install sysstat
-
Install
vnstat
: Similarly, installvnstat
:For Ubuntu/Debian:
sudo apt-get install vnstat
For CentOS/RHEL:
sudo yum install vnstat
-
Create the script to monitor CPU steal time.
Open your preferred text editor to create the script:
nano /home/user/check_cpu_steal.sh
-
Add the following code to check CPU steal time and log it:
#!/bin/bash # Get the current date and time DATE=$(date "+%Y-%m-%d %H:%M:%S") # Get the CPU steal time using mpstat STEAL=$(mpstat -P ALL 1 1 | grep "all" | awk '{print $11}') # Log the result into a log file echo "$DATE - CPU Steal: $STEAL%" >> /var/log/cpu_steal.log
-
Make the script executable:
chmod +x /home/user/check_cpu_steal.sh
-
Create the script to monitor bandwidth usage:
Open your text editor to create the script:
nano /home/user/check_bandwidth.sh
-
Add the following code to monitor bandwidth usage and log it:
#!/bin/bash # Get the current date and time DATE=$(date "+%Y-%m-%d %H:%M:%S") # Get the bandwidth usage from vnstat BANDWIDTH=$(vnstat -i eth0 --oneline | awk -F ';' '{print $9}') # Log the result into a log file echo "$DATE - Bandwidth: $BANDWIDTH" >> /var/log/bandwidth.log
Note: Replace
eth0
with your actual network interface name if it’s different (you can check it by runningip a
). -
Make the script executable:
chmod +x /home/user/check_bandwidth.sh
-
Edit your crontab file to run the scripts every minute:
crontab -e
-
Add the following lines at the bottom of the crontab file to run the scripts every minute:
* * * * * /home/user/check_cpu_steal.sh * * * * * /home/user/check_bandwidth.sh
This will execute both scripts every minute and log the results.
-
Check the CPU steal log:
You can check the
cpu_steal.log
file by running:tail -f /var/log/cpu_steal.log
-
Check the bandwidth log:
Similarly, check the
bandwidth.log
file:tail -f /var/log/bandwidth.log
This will show real-time updates of the logs.
If you'd like to be alerted when the CPU steal time exceeds a certain threshold, you can add an email notification to your CPU script.
For example, if you want to send an email when CPU steal time is more than 5%, modify your CPU script like this:
-
Open your
check_cpu_steal.sh
script:nano /home/user/check_cpu_steal.sh
-
Add the alert logic just before the
echo
statement:# Send an email alert if CPU steal time is more than 5% if (( $(echo "$STEAL > 5" | bc -l) )); then echo "High CPU Steal: $STEAL%" | mail -s "CPU Steal Alert" [email protected] fi
You’ll need to have a working mail service set up on your server for this to work (like
sendmail
orpostfix
).
Once everything is set up:
- Wait a minute or so for the cron jobs to run.
- Check the logs by using
tail
:tail -f /var/log/cpu_steal.log tail -f /var/log/bandwidth.log
You should see entries like:
2025-02-25 14:01:01 - CPU Steal: 0.05%
2025-02-25 14:01:01 - Bandwidth: 1.5GB
You now have a system that logs both CPU steal time and bandwidth usage on a regular basis. If you set up email alerts, you will be notified if the CPU steal time exceeds a threshold.
other options