Skip to content

Instantly share code, notes, and snippets.

@OsamaJBR
Last active December 4, 2017 09:04
Show Gist options
  • Select an option

  • Save OsamaJBR/584c6f18284ed0483cadececa3131f33 to your computer and use it in GitHub Desktop.

Select an option

Save OsamaJBR/584c6f18284ed0483cadececa3131f33 to your computer and use it in GitHub Desktop.
Fix sensu-plugin-memory-check in Amazon Linux
#!/bin/bash
# What Is This:
# -------------
# In https://github.com/sensu-plugins/sensu-plugins-memory-checks/tree/master/bin
# you can see that sensu is using ( $ free -m ) to check memory
# code:
# FreeMem=$(free -m | grep buffers/cache | awk '{ print $4 }')
# if [ $? -ne 0 ]; then
# FreeMem=$(free -m | grep Mem | awk '{ print $7 }')
# fi
# and since Amazon Linux uses old version of free command ( 3.2.8 ) it has no availabe mem info, so
# it reads the free column which is totally wrong and keeps sending alert
# This script override free command in away to have the new version of free output
# How To Deploy:
# --------------
# $ mv /usr/bin/free /usr/bin/ofree
# $ cp this_file /usr/bin/free
# $ chmod +x /usr/bin/free
os=$(uname)
free_version=$(ofree -V | awk '{ print $NF }' )
verlte() {
[ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
}
verlt() {
[ "$1" = "$2" ] && return 1 || verlte $1 $2
}
verlt $free_version 3.3.0 && {
# Less than 3.3.0
tabs 4
echo -e "\t\ttotal\tused\tfree\tshared\tbuff/cache\tavailable"
ofree "$@" | grep Mem | awk '{
print "Mem:","\t",$2,"\t",$3,"\t",$4,"\t",$5,"\t",$5+$6,"\t",$5+$6-$4
}'
ofree "$@" | grep Swap | awk '{
print "Swap:","\t",$2,"\t",$3,"\t",$4
}'
} || {
# More than 3.3.0
ofree "$@"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment