Skip to content

Instantly share code, notes, and snippets.

@victorpendleton
Created November 27, 2017 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save victorpendleton/d08d1530e50ab38b0018f3ad73dd2539 to your computer and use it in GitHub Desktop.
Save victorpendleton/d08d1530e50ab38b0018f3ad73dd2539 to your computer and use it in GitHub Desktop.
Script used to fire off action when load crosses threshold
#!/bin/bash
RUNTIME=$(date +'%F %T'); # Get date/time
NUMOFCPUS=$(grep -c ^processor /proc/cpuinfo); # Grab the number of CPUs
THRESHOLD=$(echo "${NUMOFCPUS}*.5" | bc); # Calculate the threshold
CPU=$(top -bn1 | grep load | awk '{printf "%.2f\t\t\n", $(NF-2)}'); # Grab the current load from top NumberFields minus 2 should be last minute average
EXCEEDED=$(echo "${CPU} > ${THRESHOLD}" | bc); # Determine if current load is higher than threshold
if (( ${EXCEEDED} > 0 ))
then
echo "${RUNTIME} - Current load: ${CPU}" >> /some/file;
echo "${RUNTIME} - CPU has execeed threshold of: ${THRESHOLD}. Capturing processlist..." >> /some/file;
# Use a mysql properties file or store the user/password information in this script
/usr/bin/mysql --socket= --user --execute="SHOW PROCESSLIST" >> /some/file;
echo "" >> /some/file;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment