Skip to content

Instantly share code, notes, and snippets.

@mayurah
Created February 25, 2025 12:35
Show Gist options
  • Save mayurah/40e98790b3c496e25713b6135f5c4f0c to your computer and use it in GitHub Desktop.
Save mayurah/40e98790b3c496e25713b6135f5c4f0c to your computer and use it in GitHub Desktop.
Track any shared CPU amongst neighbors (useful to detect oversold CPU VMs on Cloud providers)

Great! Let’s set up the monitoring and logging step by step. I’ll guide you through the process.

Step 1: Install Necessary Tools

We need to install mpstat (from the sysstat package) for monitoring CPU steal time and vnstat for bandwidth monitoring.

  1. Install sysstat for mpstat: Run the following command based on your Linux distribution:

    For Ubuntu/Debian:

    sudo apt-get install sysstat

    For CentOS/RHEL:

    sudo yum install sysstat
  2. Install vnstat: Similarly, install vnstat:

    For Ubuntu/Debian:

    sudo apt-get install vnstat

    For CentOS/RHEL:

    sudo yum install vnstat

Step 2: Create CPU Steal Monitoring Script

  1. Create the script to monitor CPU steal time.

    Open your preferred text editor to create the script:

    nano /home/user/check_cpu_steal.sh
  2. 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
  3. Make the script executable:

    chmod +x /home/user/check_cpu_steal.sh

Step 3: Create Bandwidth Monitoring Script

  1. Create the script to monitor bandwidth usage:

    Open your text editor to create the script:

    nano /home/user/check_bandwidth.sh
  2. 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 running ip a).

  3. Make the script executable:

    chmod +x /home/user/check_bandwidth.sh

Step 4: Set Up Cron Jobs

  1. Edit your crontab file to run the scripts every minute:

    crontab -e
  2. 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.


Step 5: Check the Logs

  1. Check the CPU steal log:

    You can check the cpu_steal.log file by running:

    tail -f /var/log/cpu_steal.log
  2. 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.


Optional: Set Up Alerts (Optional)

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:

  1. Open your check_cpu_steal.sh script:

    nano /home/user/check_cpu_steal.sh
  2. 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 or postfix).


Step 6: Verify Everything Works

Once everything is set up:

  1. Wait a minute or so for the cron jobs to run.
  2. 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

Done! 🎉

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment