File size: 10,339 Bytes
cfeea46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<!--
Copyright 2002-2021 The Apache Software Foundation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
//-->

# ZooKeeper Monitor Guide

* [New Metrics System](#Metrics-System)
    * [Metrics](#Metrics)
    * [Prometheus](#Prometheus)
    * [Alerting with Prometheus](#Alerting)
    * [Grafana](#Grafana)
    * [InfluxDB](#influxdb)

* [JMX](#JMX)

* [Four letter words](#four-letter-words)

<a name="Metrics-System"></a>

## New Metrics System
The feature:`New Metrics System` has been available since 3.6.0 which provides the abundant metrics
to help users monitor the ZooKeeper on the topic: znode, network, disk, quorum, leader election,
client, security, failures, watch/session, requestProcessor, and so forth.

<a name="Metrics"></a>

### Metrics
All the metrics are included in the `ServerMetrics.java`.

<a name="Prometheus"></a>


### Pre-requisites:
- Enable the `Prometheus MetricsProvider` by setting the following in `zoo.cfg`:
  ```conf
  metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
  ```

- The port for Prometheus metrics can be configured using:
  ```conf
  metricsProvider.httpPort=7000  # Default port is 7000
  ```

#### Enabling HTTPS for Prometheus Metrics:

ZooKeeper also supports SSL for Prometheus metrics, which provides secure data transmission. To enable this, configure an HTTPS port and set up SSL certificates as follows:

- Define the HTTPS port:
  ```conf
  metricsProvider.httpsPort=4443
  ```

- Configure the SSL key store (holds the server’s private key and certificates):
  ```conf
  metricsProvider.ssl.keyStore.location=/path/to/keystore.jks
  metricsProvider.ssl.keyStore.password=your_keystore_password
  metricsProvider.ssl.keyStore.type=jks  # Default is JKS
  ```

- Configure the SSL trust store (used to verify client certificates):
  ```conf
  metricsProvider.ssl.trustStore.location=/path/to/truststore.jks
  metricsProvider.ssl.trustStore.password=your_truststore_password
  metricsProvider.ssl.trustStore.type=jks  # Default is JKS
  ```

- **Note**: You can enable both HTTP and HTTPS simultaneously by defining both ports:
  ```conf
  metricsProvider.httpPort=7000
  metricsProvider.httpsPort=4443
  ```
### Prometheus
- Running a [Prometheus](https://prometheus.io/) monitoring service is the easiest way to ingest and record ZooKeeper's metrics.

- Install Prometheus:
  Go to the official website download [page](https://prometheus.io/download/), download the latest release.
  
- Set Prometheus's scraper to target the ZooKeeper cluster endpoints:

    ```bash
    cat > /tmp/test-zk.yaml <<EOF
    global:
      scrape_interval: 10s
    scrape_configs:
      - job_name: test-zk
        static_configs:
        - targets: ['192.168.10.32:7000','192.168.10.33:7000','192.168.10.34:7000']
    EOF
    cat /tmp/test-zk.yaml
    ```

- Set up the Prometheus handler:

    ```bash
    nohup /tmp/prometheus \
        --config.file /tmp/test-zk.yaml \
        --web.listen-address ":9090" \
        --storage.tsdb.path "/tmp/test-zk.data" >> /tmp/test-zk.log  2>&1 &
    ```

- Now Prometheus will scrape zk metrics every 10 seconds.

<a name="Alerting"></a>

### Alerting with Prometheus
- We recommend that you read [Prometheus Official Alerting Page](https://prometheus.io/docs/practices/alerting/) to explore
  some principles of alerting

- We recommend that you use [Prometheus Alertmanager](https://www.prometheus.io/docs/alerting/latest/alertmanager/) which can
  help users to receive alerting email or instant message(by webhook) in a more convenient way

- We provide an alerting example where these metrics should be taken a special attention. Note: this is for your reference only,
  and you need to adjust them according to your actual situation and resource environment


        use ./promtool check rules rules/zk.yml to check the correctness of the config file
        cat rules/zk.yml

        groups:
        - name: zk-alert-example
          rules:
          - alert: ZooKeeper server is down
            expr:  up == 0
            for: 1m
            labels:
              severity: critical
            annotations:
              summary: "Instance {{ $labels.instance }} ZooKeeper server is down"
              description: "{{ $labels.instance }} of job {{$labels.job}} ZooKeeper server is down: [{{ $value }}]."

          - alert: create too many znodes
            expr: znode_count > 1000000
            for: 1m
            labels:
              severity: warning
            annotations:
              summary: "Instance {{ $labels.instance }} create too many znodes"
              description: "{{ $labels.instance }} of job {{$labels.job}} create too many znodes: [{{ $value }}]."

          - alert: create too many connections
            expr: num_alive_connections > 50 # suppose we use the default maxClientCnxns: 60
            for: 1m
            labels:
              severity: warning
            annotations:
              summary: "Instance {{ $labels.instance }} create too many connections"
              description: "{{ $labels.instance }} of job {{$labels.job}} create too many connections: [{{ $value }}]."

          - alert: znode total occupied memory is too big
            expr: approximate_data_size /1024 /1024 > 1 * 1024 # more than 1024 MB(1 GB)
            for: 1m
            labels:
              severity: warning
            annotations:
              summary: "Instance {{ $labels.instance }} znode total occupied memory is too big"
              description: "{{ $labels.instance }} of job {{$labels.job}} znode total occupied memory is too big: [{{ $value }}] MB."

          - alert: set too many watch
            expr: watch_count > 10000
            for: 1m
            labels:
              severity: warning
            annotations:
              summary: "Instance {{ $labels.instance }} set too many watch"
              description: "{{ $labels.instance }} of job {{$labels.job}} set too many watch: [{{ $value }}]."

          - alert: a leader election happens
            expr: increase(election_time_count[5m]) > 0
            for: 1m
            labels:
              severity: warning
            annotations:
              summary: "Instance {{ $labels.instance }} a leader election happens"
              description: "{{ $labels.instance }} of job {{$labels.job}} a leader election happens: [{{ $value }}]."

          - alert: open too many files
            expr: open_file_descriptor_count > 300
            for: 1m
            labels:
              severity: warning
            annotations:
              summary: "Instance {{ $labels.instance }} open too many files"
              description: "{{ $labels.instance }} of job {{$labels.job}} open too many files: [{{ $value }}]."

          - alert: fsync time is too long
            expr: rate(fsynctime_sum[1m]) > 100
            for: 1m
            labels:
              severity: warning
            annotations:
              summary: "Instance {{ $labels.instance }} fsync time is too long"
              description: "{{ $labels.instance }} of job {{$labels.job}} fsync time is too long: [{{ $value }}]."

          - alert: take snapshot time is too long
            expr: rate(snapshottime_sum[5m]) > 100
            for: 1m
            labels:
              severity: warning
            annotations:
              summary: "Instance {{ $labels.instance }} take snapshot time is too long"
              description: "{{ $labels.instance }} of job {{$labels.job}} take snapshot time is too long: [{{ $value }}]."

          - alert: avg latency is too high
            expr: avg_latency > 100
            for: 1m
            labels:
              severity: warning
            annotations:
              summary: "Instance {{ $labels.instance }} avg latency is too high"
              description: "{{ $labels.instance }} of job {{$labels.job}} avg latency is too high: [{{ $value }}]."

          - alert: JvmMemoryFillingUp
            expr: jvm_memory_bytes_used / jvm_memory_bytes_max{area="heap"} > 0.8
            for: 5m
            labels:
              severity: warning
            annotations:
              summary: "JVM memory filling up (instance {{ $labels.instance }})"
              description: "JVM memory is filling up (> 80%)\n labels: {{ $labels }}  value = {{ $value }}\n"


<a name="Grafana"></a>

### Grafana
- Grafana has built-in Prometheus support; just add a Prometheus data source:

    ```bash
    Name:   test-zk
    Type:   Prometheus
    Url:    http://localhost:9090
    Access: proxy
    ```
- Then download and import the default ZooKeeper dashboard [template](https://grafana.com/grafana/dashboards/10465) and customize.
- Users can ask for Grafana dashboard account if having any good improvements by writing a email to **dev@zookeeper.apache.org**.

<a name="influxdb"></a>

### InfluxDB

InfluxDB is an open source time series data that is often used to store metrics
from Zookeeper. You can [download](https://portal.influxdata.com/downloads/) the
open source version or create a [free](https://cloud2.influxdata.com/signup)
account on InfluxDB Cloud. In either case, configure the [Apache Zookeeper
Telegraf plugin](https://www.influxdata.com/integration/apache-zookeeper/) to
start collecting and storing metrics from your Zookeeper clusters into your
InfluxDB instance. There is also an [Apache Zookeeper InfluxDB
template](https://www.influxdata.com/influxdb-templates/zookeeper-monitor/) that
includes the Telegraf configurations and a dashboard to get you set up right
away.

<a name="JMX"></a>
## JMX
More details can be found in [here](http://zookeeper.apache.org/doc/current/zookeeperJMX.html)

<a name="four-letter-words"></a>
## Four letter words
More details can be found in [here](http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_zkCommands)