Skip to content

Instantly share code, notes, and snippets.

@icheko
Created March 16, 2022 21:27
Show Gist options
  • Save icheko/1abfcc3f8276115769be16c43acde146 to your computer and use it in GitHub Desktop.
Save icheko/1abfcc3f8276115769be16c43acde146 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# A probe to check current CPU and RAM capacity before scheduling jobs
#
CURRENT_CPU=$(uptime | awk -F'load average:' '{ print $2 }' | awk -F',' '{print $2}' | tr -d " \t")
THRESHOLD_CPU=12.0
THRESHOLD_RAM=4096
CURRENT_AVAILABLE_RAM=$(free -m | grep "Mem:" | awk '{print $7}')
if [ "$(echo "$CURRENT_CPU > $THRESHOLD_CPU" | bc -l)" -eq 1 ]; then
echo "`date --rfc-3339=seconds` - CPU Overloaded, currently $CURRENT_CPU"
exit -1
fi
if [ "$(echo "$CURRENT_AVAILABLE_RAM < $THRESHOLD_RAM" | bc -l)" -eq 1 ]; then
echo "`date --rfc-3339=seconds` - RAM below threshold ($THRESHOLD_RAM MB), currently $CURRENT_AVAILABLE_RAM MB"
exit -1
fi
echo "`date --rfc-3339=seconds` - CPU 5min. load at $CURRENT_CPU and RAM at $CURRENT_AVAILABLE_RAM MB available"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment