Last active
August 29, 2015 14:00
-
-
Save hfm/11160974 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -ue | |
| usage() { | |
| cat <<EOT | |
| Usage: `basename $0` [-w|-c|-h] | |
| -w warning_threshold | |
| set warning threshold (default 20) | |
| -c critical_threshold | |
| set critical threshold (default 10) | |
| -h print the help message and exit | |
| EOT | |
| } | |
| # Exit codes | |
| state_ok=0 | |
| state_warning=1 | |
| state_critical=2 | |
| state_unknown=3 | |
| # Defaults | |
| warning_threshold="1024" | |
| critical_threshold="512" | |
| while getopts "w:c:h" opt; do | |
| case $opt in | |
| w) | |
| warning_threshold=$OPTARG | |
| ;; | |
| c) | |
| critical_threshold=$OPTARG | |
| ;; | |
| *) | |
| usage | |
| exit $state_unknown | |
| ;; | |
| esac | |
| done | |
| # Exit if overcommit_memory is not 2 | |
| overcommit_memory=$(cat /proc/sys/vm/overcommit_memory) | |
| if [ $overcommit_memory -lt 2 ]; then | |
| info="overcommit_memory is $overcommit_memory (CommitLimit is useless)" | |
| echo $info | |
| exit $state_ok | |
| fi | |
| limit=$(awk '$1 ~ /CommitLimit/ {print $2}' /proc/meminfo) | |
| committed=$(awk '$1 ~ /Committed_AS/ {print $2}' /proc/meminfo) | |
| remain=$((limit - committed)) | |
| if [ $remain -lt $critical_threshold ]; then | |
| exitstatus=$state_critical | |
| info="(CRITICAL)" | |
| elif [ $remain -lt $warning_threshold ]; then | |
| exitstatus=$state_warning | |
| info="(WARNING)" | |
| else | |
| exitstatus=$state_ok | |
| info="(OK)" | |
| fi | |
| info="${info} remain: ${remain} (Committed: ${committed} / Limit: ${limit})" | |
| echo $info | |
| exit $exitstatus |
Author
Author
remain: 2678488 (Committed: 371540 / Limit: 3050028)
Author
[hfm@sc ~]$ ./get_meminfo.sh
(CRITICAL) remain: -3471396 (Committed: 5030160 / Limit: 1558764)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sample