-
Notifications
You must be signed in to change notification settings - Fork 3
/
jkmagent.sh
executable file
·216 lines (172 loc) · 6.17 KB
/
jkmagent.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
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
#!/bin/bash
#
# Copyright 2010 Alexandre Gomes
#
# This program 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
# (at your option) any later version.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
PORT=8977
function usage() {
echo
echo "Usage: ./jmeteragent.sh [-c] [-h?] [-p 9999]"
echo
echo -e "\t* no argument starts listening for connections on port 8977."
echo -e "\t* -c Collects data in the form:\n" \
"\t\thttpd_conn|\n" \
"\t\ttomcat_conn|\n" \
"\t\tsys_load|\n" \
"\t\tmem_free|\n" \
"\t\tprocsrunning|\n" \
"\t\tprocsblocked|\n" \
"\t\tliferay_threads|\n" \
"\t\tliferay_runnable_threads|\n" \
"\t\tliferay_blocked_threads|\n" \
"\t\tliferay_waiting_threads|\n" \
"\t\tliferay_eden_usage|\n" \
"\t\tliferay_old_usage|\n" \
"\t\tliferay_perm_usage|\n" \
"\t\tdb_conn_estab|\t\t(if database port given with -p)\n" \
"\t\tdb_conn_tw\t\t(if database port given with -p)"
echo -e "\t* -h or -? Shows this (beautiful) message."
echo
exit
}
function killJMeter() {
IFS=$'\n'
for p in $(ps axu | grep $0 | grep -v grep); do
JMETER_PID=$(echo $p | awk '{print $2}')
echo "Finalizando processo $JMETER_PID"
kill -9 $JMETER_PID &2> /dev/null
done
exit
}
function collect_data() {
# NOW=`date '+%d/%m/%Y %H:%M:%S'`
# echo "$NOW - Collecting metrics"
if [[ `uname` == 'Darwin' ]]; then
echo
echo "Sorry, but jkmagent only works on linux machines."
echo "Want to port it to something else? Fork it you! http://github.com/alegomes/jkilometer ;-)"
echo
exit -1
fi
# TODO Review not used variables and their names
httpd_conn=$(netstat -an | grep -i ":80 " | grep -i estab | wc -l)
tomcat_conn=$(netstat -an | grep -i ":8080 " | grep -i estab | wc -l)
sys_load=$(cat /proc/loadavg | awk '{print $1 }')
mem_free=$(grep MemFree /proc/meminfo | awk '{print $2}')
procsrunning=$(grep "procs_running" /proc/stat | awk '{print $2}')
procsblocked=$(grep "procs_blocked" /proc/stat | awk '{print $2}')
# Testing Tomcat
javaserver_pid=$(jps | grep -i bootstrap | awk '{print $1}')
# Testing JBoss
if [[ -z "$javaserver_pid" ]]; then
javaserver_pid=$(jps | grep -i jboss | cut -d ' ' -f1)
fi
# Testing Glassfish
if [[ -z "$javaserver_pid" ]]; then
javaserver_pid=$(ps aux | grep -i com.sun.enterprise.glassfish | grep -v grep | awk '{print $2}')
fi
# No compatiple server
if [[ -z "$javaserver_pid" ]]; then
echo 'No java server found!'
exit 1
fi
# Getting java process owner
tomcat_user=$(ps aux | grep -i $javaserver_pid | grep -i java | awk '{print $1}')
# Sometimes ps shows uid instead of username
[[ $(echo $tomcat_user | egrep '^[0-9]+$') ]] && tomcat_user=$(getent passwd $tomcat_user | cut -d: -f1)
# echo "$(tomcat_user) password can be asked next"
su - $tomcat_user -s /bin/bash -c "jstack $javaserver_pid > /tmp/liferay_stack"
su - $tomcat_user -s /bin/bash -c "jstat -gcutil $javaserver_pid > /tmp/liferay_stat"
liferay_threads=$(grep -i java.lang.Thread.State /tmp/liferay_stack | wc -l)
liferay_blocked_threads=$(grep -i java.lang.Thread.State /tmp/liferay_stack | grep -i block | wc -l)
liferay_runnable_threads=$(grep -i java.lang.Thread.State /tmp/liferay_stack | grep -i runn | wc -l)
liferay_waiting_threads=$(grep -i java.lang.Thread.State /tmp/liferay_stack | grep -i wait | wc -l)
liferay_eden_usage=$(grep -v S0 /tmp/liferay_stat | awk '{print $3}')
liferay_old_usage=$(grep -v S0 /tmp/liferay_stat | awk '{print $4}')
liferay_perm_usage=$(grep -v S0 /tmp/liferay_stat | awk '{print $5}')
if [ $liferay_blocked_threads -gt 300 ]; then
cp /tmp/liferay_stack /tmp/liferay_stack_$liferay_blocked_threads
fi
db_conn_estab=$(netstat -an | grep -i -e ":${DBPORT}[ ]*estab" | wc -l)
db_conn_tw=$(netstat -an | grep -i -e ":${DBPORT}[ ]*time_wait" | wc -l)
METRICS=$(echo -e "$httpd_conn;" \
"$tomcat_conn;" \
"$sys_load;" \
"$liferay_threads;" \
"$liferay_runnable_threads;" \
"$liferay_blocked_threads;" \
"$liferay_waiting_threads;" \
"$liferay_eden_usage;" \
"$liferay_old_usage;" \
"$liferay_perm_usage"
)
if [[ ! -z "$DBPORT" ]]; then
METRICS=$(echo -e "$METRICS;" \
"$db_conn_estab;" \
"$db_conn_tw"
)
fi
if [[ -z $httpd_conn || -z $tomcat_conn || -z $sys_load || -z $liferay_threads || -z $liferay_runnable_threads || -z liferay_blocked_threads || -z $liferay_waiting_threads || -z $liferay_eden_usage || -z $liferay_old_usage || -z $liferay_perm_usage ]]; then
METRICS="X;X;X;X;X;X;X;X;X;X;X;X"
fi
echo $METRICS
exit 0
}
function start_listening_to_jmeter_script() {
NC="nc -l -p $PORT -vv -c \"$0 -c\""
# There must be a better way to check netcat version
$(echo $NC) 2> /tmp/nc.log
if [[ ! -z $(cat /tmp/nc.log | grep -i 'invalid option') ]]; then
UBUNTU="looks like"
fi
echo
echo "JMeter Agent listeting on port ${PORT}."
echo "Now you can start your tests...."
echo
if [[ -z "$DBPORT" ]]; then
CMD="$0 -c"
else
CMD="$0 -p $DBPORT -c"
fi
while true; do
if [[ -z $UBUNTU ]]; then
nc -l -p $PORT -vv -c "$CMD" 2> /dev/null
else
echo `$CMD` | nc -l $PORT
fi
trap "killJMeter" HUP
trap "killJMeter" INT
trap "killJMeter" QUIT
trap "killJMeter" PIPE
trap "killJMeter" TERM
trap "killJMeter" KILL
echo -n "."
done
}
# -------------------------------------------------------------
# Script execution starts here.
# -------------------------------------------------------------
while getopts "chp:?" OPT; do
case "$OPT" in
p) DBPORT="$OPTARG" ;;
c) SINGLE_COLLECT="true" ;;
h) usage;;
\?) usage;;
esac
done
if [[ -z "$SINGLE_COLLECT" ]]; then
start_listening_to_jmeter_script
else
collect_data
fi