[Zabbix] zabbix监控cassandra数据库


本文总阅读量

1、安装java_gate_way,如果当前监控架构为client-server将zabbix-java-gate-way安装在server端口,如果当前架构为client-proxy-server,将zabbix-java-gate-way安装在proxy端,安装步骤如下(下面以client-proxy-server架构为例):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
创建zabbix用户,安装依赖包:
sudo bash
mkdir /home/zabbix
useradd -r -s /sbin/nologin zabbix
yum -y install gcc gcc-c++ pcre-devel openssl-devel
yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel

上传zabbix安装包,并安装java-gate-way:
mv /data/download/zabbix-3.4.2.tar.gz /home/zabbix/
cd /home/zabbix/
tar -zxvf zabbix-3.4.2.tar.gz
cd zabbix-3.4.2
./configure --prefix=/data/zabbix --enable-java
(如果zabbix-porxy或zabbix-server已安装,可指定一个新路径,gate-way与proxy、server并没有直接关系,可不必一起安装;如果都没有安装,为了管理方便,亦可放到同一目录)
make && make install

安装完成之后,会在指定目录下存在以下文件:

1
2
3
4
5
6
7
ll /data/zabbix/sbin/zabbix_java/
total 12
drwxr-xr-x. 2 root root 65 Aug 23 02:41 bin
drwxr-xr-x. 2 root root 177 Aug 23 02:36 lib
-rw-r--r--. 1 root root 791 Aug 23 02:36 settings.sh
-rwxr-xr-x. 1 root root 545 Aug 23 02:36 shutdown.sh
-rwxr-xr-x. 1 root root 2025 Aug 23 02:36 startup.sh

包括启动脚本,关闭脚本,配置文件,依赖库,运行命令等文件。
启动java_gate_way:

1
./startup.sh

2、安装完成之后,修改zabbix_proxy.conf配置文件,添加以下参数:

1
2
3
JavaGateway=127.0.0.1
JavaGatewayPort=10052
StartJavaPollers=5

默认端口为10052,因为zabbix-proxy与gate-way在同一台服务器上,因此使用127.0.0.1即可。必须指定java轮询器的预分叉实例数,默认情况下启动不会启动与jmx监控相关的进程。
修改完成之后重新启动zabbix-proxy服务,是修改参数生效。

3、修改cassandra-java启动参数,使其jmx监控进程允许其他服务器访问

cat /etc/cassandra/conf/cassandra-env.sh |grep -v ‘^#’ |grep -v ‘^$’

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
calculate_heap_sizes()
{
case "`uname`" in
Linux)
system_memory_in_mb=`free -m | awk '/:/ {print $2;exit}'`
system_cpu_cores=`egrep -c 'processor([[:space:]]+):.*' /proc/cpuinfo`
;;
FreeBSD)
system_memory_in_bytes=`sysctl hw.physmem | awk '{print $2}'`
system_memory_in_mb=`expr $system_memory_in_bytes / 1024 / 1024`
system_cpu_cores=`sysctl hw.ncpu | awk '{print $2}'`
;;
SunOS)
system_memory_in_mb=`prtconf | awk '/Memory size:/ {print $3}'`
system_cpu_cores=`psrinfo | wc -l`
;;
Darwin)
system_memory_in_bytes=`sysctl hw.memsize | awk '{print $2}'`
system_memory_in_mb=`expr $system_memory_in_bytes / 1024 / 1024`
system_cpu_cores=`sysctl hw.ncpu | awk '{print $2}'`
;;
*)
# assume reasonable defaults for e.g. a modern desktop or
# cheap server
system_memory_in_mb="2048"
system_cpu_cores="2"
;;
esac
# some systems like the raspberry pi don't report cores, use at least 1
if [ "$system_cpu_cores" -lt "1" ]
then
system_cpu_cores="1"
fi
# set max heap size based on the following
# max(min(1/2 ram, 1024MB), min(1/4 ram, 8GB))
# calculate 1/2 ram and cap to 1024MB
# calculate 1/4 ram and cap to 8192MB
# pick the max
half_system_memory_in_mb=`expr $system_memory_in_mb / 2`
quarter_system_memory_in_mb=`expr $half_system_memory_in_mb / 2`
if [ "$half_system_memory_in_mb" -gt "1024" ]
then
half_system_memory_in_mb="1024"
fi
if [ "$quarter_system_memory_in_mb" -gt "8192" ]
then
quarter_system_memory_in_mb="8192"
fi
if [ "$half_system_memory_in_mb" -gt "$quarter_system_memory_in_mb" ]
then
max_heap_size_in_mb="$half_system_memory_in_mb"
else
max_heap_size_in_mb="$quarter_system_memory_in_mb"
fi
MAX_HEAP_SIZE="${max_heap_size_in_mb}M"
# Young gen: min(max_sensible_per_modern_cpu_core * num_cores, 1/4 * heap size)
max_sensible_yg_per_core_in_mb="100"
max_sensible_yg_in_mb=`expr $max_sensible_yg_per_core_in_mb "*" $system_cpu_cores`
desired_yg_in_mb=`expr $max_heap_size_in_mb / 4`
if [ "$desired_yg_in_mb" -gt "$max_sensible_yg_in_mb" ]
then
HEAP_NEWSIZE="${max_sensible_yg_in_mb}M"
else
HEAP_NEWSIZE="${desired_yg_in_mb}M"
fi
}
java_ver_output=`"${JAVA:-java}" -version 2>&1`
jvmver=`echo "$java_ver_output" | grep '[openjdk|java] version' | awk -F'"' 'NR==1 {print $2}' | cut -d\- -f1`
JVM_VERSION=${jvmver%_*}
JVM_PATCH_VERSION=${jvmver#*_}
if [ "$JVM_VERSION" \< "1.8" ] ; then
echo "Cassandra 3.0 and later require Java 8u40 or later."
exit 1;
fi
if [ "$JVM_VERSION" \< "1.8" ] && [ "$JVM_PATCH_VERSION" -lt 40 ] ; then
echo "Cassandra 3.0 and later require Java 8u40 or later."
exit 1;
fi
jvm=`echo "$java_ver_output" | grep -A 1 'java version' | awk 'NR==2 {print $1}'`
case "$jvm" in
OpenJDK)
JVM_VENDOR=OpenJDK
# this will be "64-Bit" or "32-Bit"
JVM_ARCH=`echo "$java_ver_output" | awk 'NR==3 {print $2}'`
;;
"Java(TM)")
JVM_VENDOR=Oracle
# this will be "64-Bit" or "32-Bit"
JVM_ARCH=`echo "$java_ver_output" | awk 'NR==3 {print $3}'`
;;
*)
# Help fill in other JVM values
JVM_VENDOR=other
JVM_ARCH=unknown
;;
esac
JVM_OPTS="$JVM_OPTS -Xloggc:/var/log/cassandra/gc.log"
JVM_OPTS_FILE=$CASSANDRA_CONF/jvm.options
for opt in `grep "^-" $JVM_OPTS_FILE`
do
JVM_OPTS="$JVM_OPTS $opt"
done
echo $JVM_OPTS | grep -q Xmn
DEFINED_XMN=$?
echo $JVM_OPTS | grep -q Xmx
DEFINED_XMX=$?
echo $JVM_OPTS | grep -q Xms
DEFINED_XMS=$?
echo $JVM_OPTS | grep -q UseConcMarkSweepGC
USING_CMS=$?
echo $JVM_OPTS | grep -q UseG1GC
USING_G1=$?
if [ "x$MAX_HEAP_SIZE" = "x" ] && [ "x$HEAP_NEWSIZE" = "x" -o $USING_G1 -eq 0 ]; then
calculate_heap_sizes
elif [ "x$MAX_HEAP_SIZE" = "x" ] || [ "x$HEAP_NEWSIZE" = "x" -a $USING_G1 -ne 0 ]; then
echo "please set or unset MAX_HEAP_SIZE and HEAP_NEWSIZE in pairs when using CMS GC (see cassandra-env.sh)"
exit 1
fi
if [ "x$MALLOC_ARENA_MAX" = "x" ] ; then
export MALLOC_ARENA_MAX=4
fi
if [ $DEFINED_XMX -ne 0 ] && [ $DEFINED_XMS -ne 0 ]; then
JVM_OPTS="$JVM_OPTS -Xms${MAX_HEAP_SIZE}"
JVM_OPTS="$JVM_OPTS -Xmx${MAX_HEAP_SIZE}"
elif [ $DEFINED_XMX -ne 0 ] || [ $DEFINED_XMS -ne 0 ]; then
echo "Please set or unset -Xmx and -Xms flags in pairs on jvm.options file."
exit 1
fi
if [ $DEFINED_XMN -eq 0 ] && [ $DEFINED_XMX -ne 0 ]; then
echo "Please set or unset -Xmx and -Xmn flags in pairs on jvm.options file."
exit 1
elif [ $DEFINED_XMN -ne 0 ] && [ $USING_CMS -eq 0 ]; then
JVM_OPTS="$JVM_OPTS -Xmn${HEAP_NEWSIZE}"
fi
if [ "$JVM_ARCH" = "64-Bit" ] && [ $USING_CMS -eq 0 ]; then
JVM_OPTS="$JVM_OPTS -XX:+UseCondCardMark"
fi
JVM_OPTS="$JVM_OPTS -XX:CompileCommandFile=$CASSANDRA_CONF/hotspot_compiler"
JVM_OPTS="$JVM_OPTS -javaagent:$CASSANDRA_HOME/lib/jamm-0.3.0.jar"
if [ "x$CASSANDRA_HEAPDUMP_DIR" != "x" ]; then
JVM_OPTS="$JVM_OPTS -XX:HeapDumpPath=$CASSANDRA_HEAPDUMP_DIR/cassandra-`date +%s`-pid$$.hprof"
fi
JVM_ON_OUT_OF_MEMORY_ERROR_OPT="-XX:OnOutOfMemoryError=kill -9 %p"
JVM_OPTS="$JVM_OPTS -Djava.rmi.server.hostname=10.0.7.50"
if [ "x$LOCAL_JMX" = "x" ]; then
LOCAL_JMX=no
fi
JMX_PORT="7199"
if [ "$LOCAL_JMX" = "yes" ]; then
JVM_OPTS="$JVM_OPTS -Dcassandra.jmx.local.port=$JMX_PORT"
JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
else
JVM_OPTS="$JVM_OPTS -Dcassandra.jmx.remote.port=$JMX_PORT"
# if ssl is enabled the same port cannot be used for both jmx and rmi so either
# pick another value for this property or comment out to use a random port (though see CASSANDRA-7087 for origins)
JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.rmi.port=$JMX_PORT"
# turn on JMX authentication. See below for further options
JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
# jmx ssl options
JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.ssl=false"
#JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.ssl.need.client.auth=true"
#JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.ssl.enabled.protocols=<enabled-protocols>"
#JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.ssl.enabled.cipher.suites=<enabled-cipher-suites>"
#JVM_OPTS="$JVM_OPTS -Djavax.net.ssl.keyStore=/path/to/keystore"
#JVM_OPTS="$JVM_OPTS -Djavax.net.ssl.keyStorePassword=<keystore-password>"
#JVM_OPTS="$JVM_OPTS -Djavax.net.ssl.trustStore=/path/to/truststore"
#JVM_OPTS="$JVM_OPTS -Djavax.net.ssl.trustStorePassword=<truststore-password>"
fi
JVM_OPTS="$JVM_OPTS -Djava.library.path=$CASSANDRA_HOME/lib/sigar-bin"
JVM_OPTS="$JVM_OPTS $MX4J_ADDRESS"
JVM_OPTS="$JVM_OPTS $MX4J_PORT"
JVM_OPTS="$JVM_OPTS $JVM_EXTRA_OPTS"

主要修改一下内容,默认LOCAL_JMX=yes

1
2
3
if [ "x$LOCAL_JMX" = "x" ]; then
LOCAL_JMX=no
fi

将true改为false,修改远程访问认证无需密码:

1
2
3
4
5
6
7
8
9
10
if [ "$LOCAL_JMX" = "yes" ]; then
JVM_OPTS="$JVM_OPTS -Dcassandra.jmx.local.port=$JMX_PORT"
JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
else
JVM_OPTS="$JVM_OPTS -Dcassandra.jmx.remote.port=$JMX_PORT"
# if ssl is enabled the same port cannot be used for both jmx and rmi so either
# pick another value for this property or comment out to use a random port (though see CASSANDRA-7087 for origins)
JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.rmi.port=$JMX_PORT"
# turn on JMX authentication. See below for further options
JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.authenticate=false"

4、在zabbix-proxy端测试,获取被监控端的数据是否正常,监控脚本如下:

cat /data/zabbix/sbin/zabbix_java/bin/zabbix_get_jmx

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
#!/usr/bin/env bash

if [ $# != 5 ]
then
echo "Usage: $0 <JAVA_GATEWAY_HOST> <JAVA_GATEWAY_PORT> <JMX_SERVER> <JMX_PORT> <KEY>"
exit;
fi

# create connection
exec 3<>/dev/tcp/$1/$2

# compose message
MSG="{\"request\": \"java gateway jmx\",\"jmx_endpoint\":\"service:jmx:rmi:///jndi/rmi://$3:$4/jmxrmi\",\"keys\": [\"$5\"]}"
# write message length as zero-padded 16-digit hexadecimal number
printf -v LEN '%016x' "${#MSG}"

# prepare message length in little endian representation
BYTES=""
for i in {0..14..2}
do
BYTES="\\x${LEN:$i:2}$BYTES"
done

# prepend protocol header and message length
printf "ZBXD\\1$BYTES%s" "$MSG" >&3

# output the result skipping 5 bytes of "ZBXD\\1" header and 8 bytes of message length
tail -c+13 <&3

该脚本可自行存放,没有规定,测试命令如下:

1
./zabbix_get_jmx 127.0.0.1 10052 10.0.7.50 7199 'jmx[\"org.apache.cassandra.metrics:type=ThreadPools,path=internal,scope=MemtablePostFlush,name=ActiveTasks\",\"Value\"]'

127.0.0.1:为java-gate-way地址;
10052:为java-gate-way端口;
10.0.7.50:为被监控端的ip地址;
7199:为被监控端jmx的端口;
org.apache**:为要获取的参数,需要获取哪些参数可查看官方文档

5、zabbix官网提供了对cassandra的监控模板,其中有部分参数已失效,如需下载官方模板,点击此处,该模板为纯英文,且里面参数没有详细描述,下面为根据个人理解翻译后的模板,点击此处下载

目录
  1. 1. 1、安装java_gate_way,如果当前监控架构为client-server将zabbix-java-gate-way安装在server端口,如果当前架构为client-proxy-server,将zabbix-java-gate-way安装在proxy端,安装步骤如下(下面以client-proxy-server架构为例):
  2. 2. 2、安装完成之后,修改zabbix_proxy.conf配置文件,添加以下参数:
  3. 3. 3、修改cassandra-java启动参数,使其jmx监控进程允许其他服务器访问
  4. 4. 4、在zabbix-proxy端测试,获取被监控端的数据是否正常,监控脚本如下:
  5. 5. 5、zabbix官网提供了对cassandra的监控模板,其中有部分参数已失效,如需下载官方模板,点击此处,该模板为纯英文,且里面参数没有详细描述,下面为根据个人理解翻译后的模板,点击此处下载

Proudly powered by Hexo and Theme by Lap
本站访客数人次
© 2020 zeven0707's blog