-
Notifications
You must be signed in to change notification settings - Fork 2
/
get_memery_usage.sh
64 lines (54 loc) · 1.42 KB
/
get_memery_usage.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
#!/bin/bash
#the script accept two argv. one argv1: mem ,swap. argv2: total,free,pused
get_memery_total(){
total=$(free -m|egrep 'Mem'|awk '{print $2}')
total=$(echo "scale=2;$total/1024"|bc|awk '{printf "%.2f",$1}')
printf $total
}
get_swap_total(){
total=$(free -m|egrep 'Swap'|awk '{print $2}')
total=$(echo "scale=2;$total/1024"|bc|awk '{printf "%.2f",$1}')
printf $total
}
get_swap_free(){
free=$(free -m|egrep 'Swap'|awk '{print $4}')
free=$(echo "scale=2;$free/1024"|bc|awk '{printf "%.2f",$1}')
printf $free
}
get_memery_free(){
free=$(free -m|egrep 'Mem'|awk '{print $4}')
free=$(echo "scale=2;$free/1024"|bc|awk '{printf "%.2f",$1}')
printf $free
}
get_memery_percent(){
total=$(free -m|egrep 'Mem'|awk '{print $2}')
used=$(free -m|egrep 'Mem'|awk '{print $3}')
percent=$(echo "scale=2;$used/$total*100"|bc|awk '{printf "%.2f",$1}')
printf $percent
}
main(){
if [ $1 == "mem" ]
then
case $2 in
"total")
get_memery_total $2
;;
"free")
get_memery_free $2
;;
"pused")
get_memery_percent $2
;;
esac
else
case $2 in
"total")
get_swap_total $2
;;
"free")
get_swap_free $2
;;
esac
fi
}
main $1 $2