-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclusterstats
More file actions
executable file
·175 lines (137 loc) · 4.33 KB
/
clusterstats
File metadata and controls
executable file
·175 lines (137 loc) · 4.33 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
#! /bin/sh
# This script is in the public domain, and comes with NO WARRANTY OF ANY KIND
# This script reads a list of all hosts in a cluster from the Couchbase
# REST API, and then logs stats from each host repeatedly. When some error
# is happening infrequently, this can be a helpful tool to correlate the time
# of the error with before and after statistics, to identify what was happening
# on the cluster at that specific time.
# This script depends on curl and Python 2.6 for getting the list of hosts. If
# that's a portability concern, you could hard-code the list of hosts instead.
cluster="localhost:8091"
dir="cbstats-`date +%Y%m%d-%H%M%S`"
sleepSeconds=10
export PATH
PATH=/opt/couchbase/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin
die () { echo "Fatal error: $0:" "$@" >&2; exit 1; }
wantHelp=no
usageError=
while [ $# -gt 0 ]; do
case $1 in
'-?'|-h|--help)
wantHelp=yes
shift
;;
-c|--cluster|--cluster=*)
if echo "$1" | grep '=' > /dev/null; then
cluster=`echo "$1" | sed 's/^[^=]*=//'`
else
cluster=$2
shift
fi
if expr "$cluster" : '.*:[0-9][0-9]*$' > /dev/null; then
:
else
cluster="$cluster:8091"
fi
shift
;;
-d|--dir|--dir=*)
if echo "$1" | grep '=' > /dev/null; then
dir=`echo "$1" | sed 's/^[^=]*=//'`
else
dir=$2
shift
fi
shift
;;
-s|--sleep|--sleep=*)
if echo "$1" | grep '=' > /dev/null; then
sleepSeconds=`echo "$1" | sed 's/^[^=]*=//'`
else
sleepSeconds=$2
shift
fi
shift
;;
--)
shift
break
;;
-*)
usageError=$1
wantHelp=yes
break;
;;
*)
# Non-option argument
break
;;
esac
done
if [ $wantHelp = yes ]; then
if [ -n "$usageError" ]; then
exec 1>&2 # Write to stderr
exitStatus=2
echo "Unrecognized option: $usageError"
else
exitStatus=0
fi
cat <<EOF
Usage: $0 [OPTIONS] [STATS]
Options:
-c|--cluster HOST[:PORT] $cluster
-d|--dir DIRECTORY $dir
-s|--sleep SECONDS $sleepSeconds
Stats:
Any that the 'cbstats' command recognizes. Defaults are:
all timings tap
Also, the moxi stats 'proxy' and 'proxy timings' are recognized
Example:
$0 -c cb-1.example.com --dir=\$TMP/cbstats all 'dispatcher logs'
$0 -c 10.4.2.14:8091 -s 5
EOF
exit $exitStatus
fi
if [ $# -eq 0 ]; then
set -- all tap timings
fi
# Get the list of hosts from the Couchbase REST API. This is somewhat brittle
# depending on the output formatting of Python's json.tool helper
hosts=`curl -sS http://"$cluster"/pools/default | python2.6 -mjson.tool | grep "hostname" | cut -d\" -f4 | cut -d: -f1 | xargs`
[ $? -ne 0 -o -z "$hosts" ] && die "could not get hosts from cluster at '$cluster'"
echo "Every $sleepSeconds seconds, grabbing [$@] stats"
echo "Cluster hosts: $hosts"
echo "Writing to $dir/stats-HOST.txt files"
echo " "
echo "Create a file called $dir/STOP, to stop"
echo " "
[ -d "$dir" ] || mkdir -p "$dir" || die "could not create output directory '$dir'"
# Let the message be read
sleep 2
rm -f "$dir"/STOP
for host in $hosts; do
(
while true; do
[ -r "$dir"/STOP ] && exit 0;
echo " $host `date`" >&2
for stat in "$@"; do
echo "=== $stat `date`"
case $stat in
'proxy'|'proxy timings')
# These are moxi-specific, cbstats doesn't handle them
echo stats $stat | nc "$host" 11211
;;
*)
# NB: stat could be multi-word (e.g., 'dispatcher logs')
cbstats -a "$host":11210 $stat _admin _admin
;;
esac
done
[ -r STOP ] && exit 0;
sleep $sleepSeconds
done
) > "$dir"/stats-"$host".txt &
done
trap 'echo "Quitting ..."' 2 3 15
wait
touch "$dir"/STOP