-
Notifications
You must be signed in to change notification settings - Fork 4
/
MemWatch.sh
133 lines (108 loc) · 3.48 KB
/
MemWatch.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
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
#!/bin/sh
#######################################################################################
######################## Author : Vinayaka V Ladwa ####################################
######################## Email : vinayakladwa@gmail.com ###############################
#######################################################################################
export $1
if [ $# -lt 1 ];
then
echo "Process name is missing as argument, pass the process name as argument to script"
echo "example ${1} process, $0"
exit 1
fi
result=`which sed`
if [ -z $result ]; then
echo "Error: sed command seems not to be present in this OS."
echo "System defaults are missing.Sorry, Quitting from installation"
echo "Thank You"
exit 1
else
SED=$result
fi
result=`which grep`
if [ -z $result ]; then
echo "Error: grep command seems not to be present in this OS."
echo "System defaults are missing.Sorry, Quitting from installation"
echo "Thank You"
exit 1
else
GREP=$result
fi
result=`which awk`
if [ -z $result ]; then
echo "Error: awk command seems not to be present in this OS."
echo "System defaults are missing.Sorry, Quitting from installation"
echo "Thank You"
exit 1
else
AWK=$result
fi
result=`which ps`
if [ -z $result ]; then
echo "Error: ps command seems not to be present in this OS."
echo "System defaults are missing.Sorry, Quitting from installation"
echo "Thank You"
exit 1
else
PS=$result
fi
result=`which tail`
if [ -z $result ]; then
echo "Error: tail command seems not to be present in this OS."
echo "System defaults are missing.Sorry, Quitting from installation"
echo "Thank You"
exit 1
else
TAIL=$result
fi
result=`which tail`
if [ -z $result ]; then
echo "Error: tail command seems not to be present in this OS."
echo "System defaults are missing.Sorry, Quitting from installation"
echo "Thank You"
exit 1
else
TAIL=$result
fi
result=`which top`
if [ -z $result ]; then
echo "Error: top command seems not to be present in this OS."
echo "System defaults are missing.Sorry, Quitting from installation"
echo "Thank You"
exit 1
else
TOP=$result
fi
result=`which lsof`
if [ -z $result ]; then
echo "Error: losf command seems not to be present in this OS."
echo "System defaults are missing.Sorry, Quitting from installation"
echo "Thank You"
exit 1
else
LSOF=$result
fi
fileName=`echo $0 |$SED s/\.sh/\.log/`
fileName=$fileName.`hostname`;
CPULOG=/tmp/$$.log
java=$($PS -u $LOGNAME -o pid,command | $GREP "$1" | $GREP -v grep | $AWK 'NR==1{print $1}')
if [ -z $java ]
then
echo "Process is not running, Please start the process :-) "
exit 1
fi
${TOP} -b -n 1 -p $java >$CPULOG
javaCpu=$(cat $CPULOG | grep $1 | awk 'NR==1{print $9}')
javaMem=$(cat $CPULOG | grep $1 | awk 'NR==1{print $10}')
javalsof=$(${LSOF} -p $java |wc -l)
/bin/rm -rf $CPULOG
#javaRMVM=$(ps -u $LOGNAME -o rss,vsz,command -ae | grep "java" | grep -v grep| grep -v safe | awk 'NR==1{print $1 "|" $2}')
### Add header and set seqNo=1 when the log file is created for the first time
if [ ! -f $fileName ]
then
seqNo=1
echo "SeqNo , DateTime , $1 Cpu , $1 Mem , JAVA lsof" > $fileName
fi
seqNo=`$TAIL -1 $fileName |grep -v DateTime |$AWK '{print $1}'`
seqNo=`expr $seqNo + 1`
echo "$seqNo , $( date +'%Y-%m-%d %H:%M:%S') , $javaCpu , $javaMem , $javalsof" >> $fileName