-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcmon.sh
executable file
·185 lines (143 loc) · 5.25 KB
/
cmon.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
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
#!/usr/bin/env bash
# Copyright 2010 Alexandre Gomes (alegomes at gmail)
#
# This file is part of Catalina Monitor (C'Mon) Suite.
#
# C'Mon is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# C'Mon is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with C'Mon. If not, see <http://www.gnu.org/licenses/>.
LOGDIR=/var/log
#CATALINA_HOME=/Users/alegomes/workspace/sea/workshop_perftuning/liferay-portal-5.2-ee-sp4/tomcat-6.0.18
CATALINA_HOME=/usr/local/liferay/tomcat
CATALINA_STAT=$LOGDIR/cmon
THREADDUMP_STAT=$LOGDIR/threaddump
JSTAT_TEMP=/var/tmp/jstat.ale
THREADDUMP_TEMP=/var/tmp/threaddump.ale
function usage() {
echo
echo "./cmon.sh <load_threshold>"
echo
exit -1
}
function load() {
HEADER="Date/Time;Load;AjpEst;AjpTw;MySQLEst;Young(%);Old(%);Perm(%);YGC(#);FGC(#);Threads(#);ThRun(%);ThBlk(%);ThTw(%)\n"
last_load=0
count=0
while (true); do
now=`date '+%d/%m/%Y %H:%M:%S'`
today=`date '+%Y%m%d'`
catalina_pid=$(jps | grep -i bootstrap | awk '{print $1}')
if [ -z "$(uname | grep Darwin)" ]; then
# Linux
load=$(cat /proc/loadavg | awk '{print $1}')
else
# MacOS
load=$(sysctl vm.loadavg | awk '{print $3}')
fi
#
# Warn the user if load has increased more than 5 points.
#
# On linux boxes, printf decimal symbol is comma and it doesn't work in [ ] statements
load_diff=$(echo "$load - $last_load" | bc | xargs printf "%1.1f" | sed s/,/./g)
if [ $(echo "$load_diff > 5" | bc) -eq 1 ]; then
# A little mark to find overloaded moments
loadincreasing="<---"
else
loadincreasing=""
fi
#
# Save last thread dump if load has increased more than 15 points.
#
if [ $(echo "$load_diff > 15" | bc) -eq 1 ]; then
cp $THREADDUMP_TEMP $THREADDUMP_STAT.$(date +%Y%m%d%H%M).JustBeforeLoadPeak.$(hostname).txt
fi
# Networkd conections
ajp_estab=$(netstat -an | grep 8009 | grep -i estab | wc -l)
ajp_timewait=$(netstat -an | grep 8009 | grep -i wait | wc -l)
mysql_estab=$(netstat -an | grep 3306 | grep -i estab | wc -l)
# JVM memory
jstat -gcutil $catalina_pid | grep -v S0 > $JSTAT_TEMP
jvm_eden=$(grep -v S0 $JSTAT_TEMP | awk '{print $3}')
jvm_old=$(grep -v S0 $JSTAT_TEMP | awk '{print $4}')
jvm_perm=$(grep -v S0 $JSTAT_TEMP | awk '{print $5}')
jvm_ygc=$(grep -v S0 $JSTAT_TEMP | awk '{print $6}')
jvm_fgc=$(grep -v S0 $JSTAT_TEMP | awk '{print $8}')
# JVM threads
lines_before=$(cat $CATALINA_HOME/logs/catalina.out | wc -l)
kill -3 $catalina_pid
lines_after=$(cat $CATALINA_HOME/logs/catalina.out | wc -l)
thread_dump=$(expr $lines_after - $lines_before)
tail -$thread_dump $CATALINA_HOME/logs/catalina.out > $THREADDUMP_TEMP
jvm_threads=$(grep "java.lang.Thread.State" $THREADDUMP_TEMP | wc -l)
jvm_th_run=$(grep "java.lang.Thread.State: RUNNABLE" $THREADDUMP_TEMP | wc -l)
jvm_th_blk=$(grep "java.lang.Thread.State: BLOCK" $THREADDUMP_TEMP | wc -l)
jvm_th_wait=$(grep WAITING $THREADDUMP_TEMP | wc -l)
if [ ! -z $jvm_threads ] && [ ! $(echo "$jvm_threads" | bc) -eq 0 ]; then
jvm_th_run_perc=$(echo "scale=2; (${jvm_th_run}/${jvm_threads})*100" | bc | xargs printf "%1.0f")
jvm_th_blk_perc=$(echo "scale=2; (${jvm_th_blk}/${jvm_threads})*100" | bc | xargs printf "%1.0f")
jvm_th_wait_perc=$(echo "scale=2; (${jvm_th_wait}/${jvm_threads})*100" | bc| xargs printf "%1.0f")
fi
#
# Print results in $LOGDIR/javastat.${today}.log
#
line=$(echo $HEADER \
"$now;" \
"$load;" \
"$ajp_estab;" \
"$ajp_timewait;" \
"$mysql_estab;" \
"$jvm_eden;" \
"$jvm_old;" \
"$jvm_perm;" \
"$jvm_ygc;" \
"$jvm_fgc;" \
"$jvm_threads;" \
"$jvm_th_run_perc;" \
"$jvm_th_blk_perc;" \
"$jvm_th_wait_perc;" \
"$loadincreasing"
)
let no_header=$count%15
if (( $no_header )); then
echo -e $line | column -t -s\; | grep -v Date >> $CATALINA_STAT.${today}.$(hostname).log
else
echo -e $line | column -t -s\; >> $CATALINA_STAT.${today}.$(hostname).log
fi
(( count = $count + 1 ))
#
# Check if there was a load spike. Saves a thread dump if any.
#
overloaded=$(echo "$load > ${THRESHOLD}" | bc)
if [[ "$overloaded" -eq "1" ]]; then
cp $THREADDUMP_TEMP $THREADDUMP_STAT.$(date +%Y%m%d%H%M).$(hostname).txt
fi
#
# Save time of the last dump
#
last_dump_time=$(echo $now | awk '{print $2}')
#
# Save last load
#
last_load=$load
sleep 15
done
}
######################
#
# Script start
#
######################
if [[ -z "$1" ]]; then
usage
fi
THRESHOLD=$1
load