-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemstat.sh
More file actions
272 lines (211 loc) · 5.82 KB
/
memstat.sh
File metadata and controls
272 lines (211 loc) · 5.82 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/bin/bash
# Make sure only root can run our script
#if [ "$(id -u)" != "0" ]; then
#echo "This script must be run as root" 1>&2
#exit 1
#fi
#su -c 'sh -s' <<EOF
#spawn sudo -s <<EOF
#expect "Password for username:"
#send -- "user-password\r"
#sudo -kSs << EOF
#sudo su
#(( EUID != 0 )) && exec sudo -- "$0" "$@"
#unset PASSWD
#echo $PASSWD
SELF_PATH=$(cd -P -- "$(dirname -- "$0")" && pwd -P) && SELF_PATH=$SELF_PATH/$(basename -- "$0")
real=$( realpath "$SELF_PATH")
#SYM=$( readlink "$SELF_PATH")
DIR=$(dirname -- "$real")
cd "$DIR"
if [ "$(id -u)" != "0" ]; then
while [ "$senhaok" == "" ]; do
PASSWD=""
PASSWD=$(zenity --entry --hide-text --title "MemStat" --text "$psswmsg\nDigite a senha de root")
if [[ $? -eq 1 ]]; then
exit
else
if echo -e $PASSWD | sudo -k -S sudo pwd >/dev/null 2>&1; then
senhaok=1
else
#unset PASSWD
psswmsg="Senha errada!"
fi
fi
done
#exec sudo -- "$0" "$@"
#echo -e $PASSWD | sudo -S sudo -- "$0" "$@"
#exit
#fi
#echo $EUID
#sleep 5
if [ ! -t 0 ]; then # script is executed outside the terminal?
# execute the script inside a terminal window with same arguments
# echo -e $PASSWD | sudo -S sudo -- x-terminal-emulator -hold -e ""$0" "$@"; exec bash -p """
echo -e $PASSWD | sudo -k -S sudo -- x-terminal-emulator -e ""$0" "$@""
# and abort running the rest of it
exit 0
else
echo -e $PASSWD | sudo -k -S sudo -- "$0" "$@"
fi
exit
fi
running (){
echo -e "\nLendo, aguarde..."
### Functions
#This function will count memory statistic for passed PID
get_process_mem ()
{
PID=$1
#we need to check if 2 files exist
if [ -f /proc/$PID/status ];
then
if [ -f /proc/$PID/smaps ];
then
#here we count memory usage, Pss, Private and Shared = Pss-Private
Pss=`cat /proc/$PID/smaps | grep -e "^Pss:" | awk '{print $2}'| paste -sd+ | bc `
Private=`cat /proc/$PID/smaps | grep -e "^Private" | awk '{print $2}'| paste -sd+ | bc `
#we need to be sure that we count Pss and Private memory, to avoid errors
if [ x"$Rss" != "x" -o x"$Private" != "x" ];
then
let Shared=${Pss}-${Private}
Name=`cat /proc/$PID/status | grep -e "^Name:" |cut -d':' -f2`
#we keep all results in bytes
let Shared=${Shared}*1024
let Private=${Private}*1024
let Sum=${Shared}+${Private}
echo -e "$Private + $Shared = $Sum \t $Name"
fi
fi
fi
}
#this function make conversion from bytes to Kb or Mb or Gb
convert()
{
value=$1
power=0
#if value 0, we make it like 0.00
if [ "$value" = "0" ];
then
value="0.00"
fi
#We make conversion till value bigger than 1024, and if yes we divide by 1024
while [ $(echo "${value} > 1024"|bc) -eq 1 ]
do
value=$(echo "scale=2;${value}/1024" |bc)
let power=$power+1
done
#this part get b,kb,mb or gb according to number of divisions
case $power in
0) reg=b;;
1) reg=kb;;
2) reg=mb;;
3) reg=gb;;
esac
echo -n "${value} ${reg} "
}
#to ensure that temp files not exist
[[ -f /tmp/res ]] && rm -f /tmp/res
[[ -f /tmp/res2 ]] && rm -f /tmp/res2
[[ -f /tmp/res3 ]] && rm -f /tmp/res3
#if argument passed script will show statistic only for that pid, of not – we list all processes in /proc/ #and get statistic for all of them, all result we store in file /tmp/res
if [ $# -eq 0 ]
then
pids=`ls /proc | grep -e [0-9] | grep -v [A-Za-z] `
for i in $pids
do
get_process_mem $i >> /tmp/res
done
else
get_process_mem $1>> /tmp/res
fi
#This will sort result by memory usage
#cat /tmp/res | sort -gr -k 5 > /tmp/res2 #ordem reversa
cat /tmp/res | sort -g -k 5 > /tmp/res2
#this part will get uniq names from process list, and we will add all lines with same process list
#we will count nomber of processes with same name, so if more that 1 process where will be
# process(2) in output
for Name in `cat /tmp/res2 | awk '{print $6}' | sort | uniq`
do
count=`cat /tmp/res2 | awk -v src=$Name '{if ($6==src) {print $6}}'|wc -l| awk '{print $1}'`
if [ $count = "1" ];
then
count=""
else
count="(${count})"
fi
VmSizeKB=`cat /tmp/res2 | awk -v src=$Name '{if ($6==src) {print $1}}' | paste -sd+ | bc`
VmRssKB=`cat /tmp/res2 | awk -v src=$Name '{if ($6==src) {print $3}}' | paste -sd+ | bc`
total=`cat /tmp/res2 | awk '{print $5}' | paste -sd+ | bc`
Sum=`echo "${VmRssKB}+${VmSizeKB}"|bc`
#all result stored in /tmp/res3 file
echo -e "$VmSizeKB + $VmRssKB = $Sum \t ${Name}${count}" >>/tmp/res3
done
#this make sort once more.
#cat /tmp/res3 | sort -g -k 5 | uniq > /tmp/res #ordem reversa
cat /tmp/res3 | sort -g -k 5 | uniq > /tmp/res
#now we print result , first header
echo -e "Private \t + \t Shared \t = \t RAM used \t Program"
#after we read line by line of temp file
while read line
do
echo $line | while read a b c d e f
do
#we print all processes if Ram used if not 0
if [ $e != "0" ]; then
#here we use function that make conversion
echo -en "`convert $a` \t $b \t `convert $c` \t $d \t `convert $e` \t $f"
echo ""
fi
done
done < /tmp/res
#this part print footer, with counted Ram usage
echo "--------------------------------------------------------"
echo -e "\t\t\t\t\t\t `convert $total`"
echo "========================================================"
# we clean temporary file
[[ -f /tmp/res ]] && rm -f /tmp/res
[[ -f /tmp/res2 ]] && rm -f /tmp/res2
[[ -f /tmp/res3 ]] && rm -f /tmp/res3
# unset PASSWD
# read -p "Pressione ENTER para sair..."
#EOF
}
running
# echo "=== MENU EM BASH ==="
menu(){
#PS3="Enter a number: "
#select i in repetir sair
#do
# case "$i" in
# repetir)
# echo "repetindo"
# running
# menu
# ;;
# sair)
# echo "saindo do programa"
# unset PASSWD
# #break
# exit 0
# ;;
# *)
# echo "opcao invalida, tente de novo"
# ;;
# esac
#done
unset $rs
read -n1 -p "Repetir ou Sair? [r/s] " rs
case $rs in
[Rr]* )
running
menu;;
[Ss]* )
unset PASSWD
exit;;
* ) echo " - Opção inválida"
menu;;
esac
}
menu
exit 0