-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinux_Memory_Usage_Check.sh
72 lines (61 loc) · 2.12 KB
/
Linux_Memory_Usage_Check.sh
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
#!/bin/bash
# Checks for the percentage of memory free and errors if it is below the configured value. Note: Check is for both physical and swap memory usage
# Script accepts 3 env variables:
# "ok" should be passed in the script in the format: ok=30. If an env var is not set, the script will assume the threshold of 30.
# "min" should be passed in the script in the format: min=10. If an env var is not set, the script will assume the threshold of 10.
# "errormin" should be passed in the script in the format: errormin=2. If an env var is not set, the script will assume the threshold of 2.
# If free physical mem is less than min, the script will check swap.
# If swap is more than ok amount, the script gives a warning.
# If swap is less than ok amount, the script gives a warning.
# If swap is also lower than min, the script gives an error.
for ARGUMENT in "$@"
do
KEY=$(echo $ARGUMENT | cut -f1 -d=)
KEY_LENGTH=${#KEY}
VALUE="${ARGUMENT:$KEY_LENGTH+1}"
export "$KEY"="$VALUE"
done
# Setting variables
if [ -z "$ok" ];
then
ok=30
fi
if [ -z "$min" ];
then
min=10
fi
if [ -z "$errormin" ];
then
errormin=2
fi
# Get memory usage
MEMORY=$(free | grep Mem | awk '{print $3/$2 * 100.0}')
MEMFREE=$(awk -v s="$MEMORY" 'BEGIN {printf("%d\n", 100 - int(s))}')
SWAP=$(free | grep Swap | awk '{print $3/$2 * 100.0}')
SWAPFREE=$(awk -v s="$SWAP" 'BEGIN {printf("%d\n", 100 - int(s))}')
# Show memory usage
echo "Memory usage: $MEMORY%"
echo "Swap usage: $SWAP%"
echo "Free swap: $SWAPFREE%"
echo "Free mem: $MEMFREE%"
echo -e "\n"
# Compare to configured limits
if [ $MEMFREE -ge "$min" ];
then
echo "$MEMFREE% memory available. More than configured $min%"
exit 0
fi
if [ $SWAPFREE -gt "$ok" ];
then
echo "$MEMFREE% free physical memory less than $min%, but $SWAPFREE% free swap greater than $ok%."
echo "Performance may be affected"
exit 2
fi
if [ $MEMFREE -lt "$errormin" ];
then
echo "$MEMFREE% free physical memory less than $errormin%. This is critically low!"
exit 1
fi
echo "Both phyical memory and swap are getting low."
echo "Free physical memory is $MEMFREE% and Free swap is $SWAPFREE%"
exit 2