tdfd 发表于 2015-4-22 08:52:38

Nagios 监控服务器剩余内存

Linux系统内存机制:在linux中有这么一种思想,内存不用白不用,因此它尽可能的cache和buffer一些数据,以方便下次使用。但实际上这些内存也是可以立刻拿来使用的。所以 空闲内存=free+buffers+cached   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash
USAGE="`basename $0` [-w|--warning]<percent free> [-c|--critical]<percent free>"
THRESHOLD_USAGE="WARNING threshold must be greater than CRITICAL: `basename $0` $*"
calc=/tmp/memcalc
percent_free=/tmp/mempercent
critical=""
warning=""
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
# print usage
if [ $# -lt 4 ]; then
    echo ""
    echo "Wrong Syntax: `basename $0` $*"
    echo ""
    echo "Usage: $USAGE"
    echo ""
    exit 0
fi
# read input
while [ $# -gt 0 ]
do
    case "$1" in
      -w|--warning)
            shift
            warning=$1
            ;;
      -c|--critical)
            shift
            critical=$1
            ;;
    esac
    shift
done
   
   
# verify input
if [[ $warning -eq $critical || $warning -lt $critical ]]; then
    echo ""
    echo "$THRESHOLD_USAGE"
    echo ""
    echo "Usage: $USAGE"
    echo ""
    exit 0
fi
# Total memory available
total=`free -m | head -2 |tail -1 |gawk '{print $2}'`
# Total memory used
used=`free -m | head -3 |tail -1 |awk '{print $3}'`
# Calc total minus used
free=`free -m | head -3 |tail -1 |awk '{print $4}'`
# normal values
#echo "$total"MB total
#echo "$used"MB used
#echo "$free"MB free
# make it into % percent free = ((free mem / total mem) * 100)
echo "5" > $calc # decimal accuracy
echo "k" >> $calc # commit
echo "100" >> $calc # multiply
echo "$free" >> $calc # division integer
echo "$total" >> $calc # division integer
echo "/" >> $calc # division sign
echo "*" >> $calc # multiplication sign
echo "p" >> $calc # print
percent=`/usr/bin/dc $calc|/bin/sed 's/^\./0./'|/usr/bin/tr "." " "|/usr/bin/gawk {'print $1'}`
#percent1=`/usr/bin/dc $calc`
#echo "$percent1"
if [[ "$percent" -le$critical ]];then
    echo "CRITICAL - $free MB ($percent%) Free Memory"
    exit 2
fi
if [[ "$percent" -le$warning ]]; then
    echo "WARNING - $free MB ($percent%) Free Memory"
    exit 1
fi
if [[ "$percent" -gt$warning ]]; then
    echo "OK - $free MB ($percent%) Free Memory"
    exit 0
fi



页: [1]
查看完整版本: Nagios 监控服务器剩余内存