运维基础——Zabbix 设置Redis监控

释放双眼,带上耳机,听听看~!

原文地址:http://www.21yunwei.com/archives/4195

下载模板

下载链接: http://pan.baidu.com/s/1pLlFTsZ 密码: nfp7

zabbix web端配置

Configuration-Templates-Import
选择 templates_Redis_info_status_ALL+.xml 并完成导入

找到Redis的host,在Templates里,Link new templates: 输入redis,并选择Template Redis 2,点击:Add-Update

zabbix-agent 端配置


1
2
3
4
5
1mkdir -p /etc/zabbix/script/redis
2cd /etc/zabbix/script/redis
3vim redis.sh
4
5

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
1#!/bin/bash
2REDISPATH="/usr/bin/redis-cli"
3HOST="redis 服务器地址"
4PORT="6379"
5REDIS_PA="$REDISPATH -h $HOST -p $PORT info"
6if [[ $# == 1 ]];then
7    case $1 in
8 cluster)
9        result=`$REDIS_PA|/bin/grep cluster|awk -F":" '{print $NF}'`
10            echo $result
11            ;;
12 uptime_in_seconds)
13        result=`$REDIS_PA|/bin/grep uptime_in_seconds|awk -F":" '{print $NF}'`
14            echo $result
15            ;;
16 connected_clients)
17        result=`$REDIS_PA|/bin/grep connected_clients|awk -F":" '{print $NF}'`
18            echo $result
19            ;;
20 client_longest_output_list)
21        result=`$REDIS_PA|/bin/grep client_longest_output_list|awk -F":" '{print $NF}'`
22            echo $result
23            ;;
24 client_biggest_input_buf)
25        result=`$REDIS_PA|/bin/grep client_biggest_input_buf|awk -F":" '{print $NF}'`
26            echo $result
27            ;;
28 blocked_clients)
29        result=`$REDIS_PA|/bin/grep blocked_clients|awk -F":" '{print $NF}'`
30            echo $result
31            ;;
32#内存
33 used_memory)
34        result=`$REDIS_PA|/bin/grep used_memory|awk -F":" '{print $NF}'|awk 'NR==1'`
35            echo $result
36            ;;
37 used_memory_human)
38        result=`$REDIS_PA|/bin/grep used_memory_human|awk -F":" '{print $NF}'|awk -F'K' '{print $1}'`
39            echo $result
40            ;;
41 used_memory_rss)
42        result=`$REDIS_PA|/bin/grep used_memory_rss|awk -F":" '{print $NF}'`
43            echo $result
44            ;;
45 used_memory_peak)
46        result=`$REDIS_PA|/bin/grep used_memory_peak|awk -F":" '{print $NF}'|awk 'NR==1'`
47            echo $result
48            ;;
49 used_memory_peak_human)
50        result=`$REDIS_PA|/bin/grep used_memory_peak_human|awk -F":" '{print $NF}'|awk -F'K' '{print $1}'`
51            echo $result
52            ;;
53 used_memory_lua)
54        result=`$REDIS_PA|/bin/grep used_memory_lua|awk -F":" '{print $NF}'`
55            echo $result
56            ;;    
57 mem_fragmentation_ratio)
58        result=`$REDIS_PA|/bin/grep mem_fragmentation_ratio|awk -F":" '{print $NF}'`
59            echo $result
60            ;;  
61#rdb
62 rdb_changes_since_last_save)
63        result=`$REDIS_PA|/bin/grep rdb_changes_since_last_save|awk -F":" '{print $NF}'`
64            echo $result
65            ;;  
66 rdb_bgsave_in_progress)
67        result=`$REDIS_PA|/bin/grep rdb_bgsave_in_progress|awk -F":" '{print $NF}'`
68            echo $result
69            ;;  
70 rdb_last_save_time)
71        result=`$REDIS_PA|/bin/grep rdb_last_save_time|awk -F":" '{print $NF}'`
72            echo $result
73            ;;  
74 rdb_last_bgsave_status)
75        result=`$REDIS_PA|/bin/grep -w "rdb_last_bgsave_status" | awk -F':' '{print $2}' | /bin/grep -c ok`
76            echo $result
77            ;;  
78 rdb_current_bgsave_time_sec)
79        result=`$REDIS_PA|/bin/grep -w "rdb_current_bgsave_time_sec" | awk -F':' '{print $2}'`
80            echo $result
81            ;;
82#rdbinfo
83 aof_enabled)
84        result=`$REDIS_PA|/bin/grep -w "aof_enabled" | awk -F':' '{print $2}'`
85            echo $result
86            ;;
87 aof_rewrite_scheduled)
88        result=`$REDIS_PA|/bin/grep -w "aof_rewrite_scheduled" | awk -F':' '{print $2}'`
89            echo $result
90            ;;
91 aof_last_rewrite_time_sec)
92        result=`$REDIS_PA|/bin/grep -w "aof_last_rewrite_time_sec" | awk -F':' '{print $2}'`
93            echo $result
94            ;;
95 aof_current_rewrite_time_sec)
96        result=`$REDIS_PA|/bin/grep -w "aof_current_rewrite_time_sec" | awk -F':' '{print $2}'`
97            echo $result
98            ;;
99 aof_last_bgrewrite_status)
100        result=`$REDIS_PA|/bin/grep -w "aof_last_bgrewrite_status" | awk -F':' '{print $2}' | /bin/grep -c ok`
101            echo $result
102            ;;
103#aofinfo
104 aof_current_size)
105        result=`$REDIS_PA|/bin/grep -w "aof_current_size" | awk -F':' '{print $2}'`
106            echo $result
107            ;;
108 aof_base_size)
109        result=`$REDIS_PA|/bin/grep -w "aof_base_size" | awk -F':' '{print $2}'`
110            echo $result
111            ;;
112 aof_pending_rewrite)
113        result=`$REDIS_PA|/bin/grep -w "aof_pending_rewrite" | awk -F':' '{print $2}'`
114            echo $result
115            ;;
116 aof_buffer_length)
117        result=`$REDIS_PA|/bin/grep -w "aof_buffer_length" | awk -F':' '{print $2}'`
118            echo $result
119            ;;
120 aof_rewrite_buffer_length)
121        result=`$REDIS_PA|/bin/grep -w "aof_rewrite_buffer_length" | awk -F':' '{print $2}'`
122            echo $result
123            ;;  
124 aof_pending_bio_fsync)
125        result=`$REDIS_PA|/bin/grep -w "aof_pending_bio_fsync" | awk -F':' '{print $2}'`
126            echo $result
127            ;;
128 aof_delayed_fsync)
129        result=`$REDIS_PA|/bin/grep -w "aof_delayed_fsync" | awk -F':' '{print $2}'`
130            echo $result
131            ;;                    
132#stats
133 total_connections_received)
134        result=`$REDIS_PA|/bin/grep -w "total_connections_received" | awk -F':' '{print $2}'`
135            echo $result
136            ;;
137 total_commands_processed)
138        result=`$REDIS_PA|/bin/grep -w "total_commands_processed" | awk -F':' '{print $2}'`
139            echo $result
140            ;;
141 instantaneous_ops_per_sec)
142        result=`$REDIS_PA|/bin/grep -w "instantaneous_ops_per_sec" | awk -F':' '{print $2}'`
143            echo $result
144            ;;
145 rejected_connections)
146        result=`$REDIS_PA|/bin/grep -w "rejected_connections" | awk -F':' '{print $2}'`
147            echo $result
148            ;;
149 expired_keys)
150        result=`$REDIS_PA|/bin/grep -w "expired_keys" | awk -F':' '{print $2}'`
151            echo $result
152            ;;
153 evicted_keys)
154        result=`$REDIS_PA|/bin/grep -w "evicted_keys" | awk -F':' '{print $2}'`
155            echo $result
156            ;;
157 keyspace_hits)
158        result=`$REDIS_PA|/bin/grep -w "keyspace_hits" | awk -F':' '{print $2}'`
159            echo $result
160            ;;
161 keyspace_misses)
162        result=`$REDIS_PA|/bin/grep -w "keyspace_misses" | awk -F':' '{print $2}'`
163            echo $result
164            ;;
165 pubsub_channels)
166        result=`$REDIS_PA|/bin/grep -w "pubsub_channels" | awk -F':' '{print $2}'`
167            echo $result
168            ;;
169 pubsub_channels)
170        result=`$REDIS_PA|/bin/grep -w "pubsub_channels" | awk -F':' '{print $2}'`
171            echo $result
172            ;;
173 pubsub_patterns)
174        result=`$REDIS_PA|/bin/grep -w "pubsub_patterns" | awk -F':' '{print $2}'`
175            echo $result
176            ;;
177 latest_fork_usec)
178        result=`$REDIS_PA|/bin/grep -w "latest_fork_usec" | awk -F':' '{print $2}'`
179            echo $result
180            ;;          
181 connected_slaves)
182        result=`$REDIS_PA|/bin/grep -w "connected_slaves" | awk -F':' '{print $2}'`
183            echo $result
184            ;;
185 master_link_status)
186        result=`$REDIS_PA|/bin/grep -w "master_link_status"|awk -F':' '{print $2}'|/bin/grep -c up`
187            echo $result
188            ;;
189 master_last_io_seconds_ago)
190        result=`$REDIS_PA|/bin/grep -w "master_last_io_seconds_ago"|awk -F':' '{print $2}'`
191            echo $result
192            ;;
193 master_sync_in_progress)
194        result=`$REDIS_PA|/bin/grep -w "master_sync_in_progress"|awk -F':' '{print $2}'`
195            echo $result
196            ;;
197 slave_priority)
198        result=`$REDIS_PA|/bin/grep -w "slave_priority"|awk -F':' '{print $2}'`
199            echo $result
200            ;;
201#cpu
202 used_cpu_sys)
203        result=`$REDIS_PA|/bin/grep -w "used_cpu_sys"|awk -F':' '{print $2}'`
204            echo $result
205            ;;
206 used_cpu_user)
207        result=`$REDIS_PA|/bin/grep -w "used_cpu_user"|awk -F':' '{print $2}'`
208            echo $result
209            ;;
210 used_cpu_sys_children)
211        result=`$REDIS_PA|/bin/grep -w "used_cpu_sys_children"|awk -F':' '{print $2}'`
212            echo $result
213            ;;
214 used_cpu_user_children)
215        result=`$REDIS_PA|/bin/grep -w "used_cpu_user_children"|awk -F':' '{print $2}'`
216            echo $result
217            ;;
218        *)
219        echo "Usage:$0{uptime_in_seconds|connected_clients|client_longest_output_list|client_biggest_input_buf|blocked_clients|used_memory|used_memory_human|used_memory_rss|used_memory_peak|used_memory_peak_human|used_memory_lua|mem_fragmentation_ratio|rdb_changes_since_last_save|rdb_bgsave_in_progress|rdb_last_save_time|rdb_last_bgsave_status|rdb_current_bgsave_time_sec|aof_enabled|aof_rewrite_scheduled|aof_last_rewrite_time_sec|aof_current_rewrite_time_sec|aof_last_bgrewrite_status|aof_current_size|aof_base_size|aof_pending_rewrite|aof_buffer_length|aof_rewrite_buffer_length|aof_pending_bio_fsync|aof_delayed_fsync|rejected_connections|instantaneous_ops_per_sec|total_connections_received|total_commands_processed|expired_keys|evicted_keys|keyspace_hits|keyspace_misses|pubsub_channels|pubsub_patterns|latest_fork_usec|connected_slaves|master_link_status|master_sync_in_progress|master_last_io_seconds_ago|connected_slaves|slave_priority|used_cpu_user|used_cpu_sys|used_cpu_sys_children|used_cpu_user_children}"
220        ;;
221esac
222#db0:key
223        elif [[ $# == 2 ]];then
224case $2 in
225  keys)
226        result=`$REDIS_PA| /bin/grep -w "db0"| /bin/grep -w "$1" | /bin/grep -w "keys" | awk -F'=|,' '{print $2}'`
227            echo $result
228            ;;
229 expires)
230        result=`$REDIS_PA| /bin/grep -w "db0"| /bin/grep -w "$1" | /bin/grep -w "expires" | awk -F'=|,' '{print $4}'`
231            echo $result
232            ;;
233 avg_ttl)
234        result=`$REDIS_PA|/bin/grep -w "db0"| /bin/grep -w "$1" | /bin/grep -w "avg_ttl" | awk -F'=|,' '{print $6}'`
235            echo $result
236            ;;
237          *)
238     echo "Usage:$0{db0 keys|db0 expires|db0 avg_ttl}"
239        ;;
240esac
241fi
242
243

1
2
3
1vim /etc/zabbix/zabbix_agentd.d/zbx_redis.conf
2
3

1
2
3
4
5
6
1#监控redis状态,我们可以根据这个参数对应的监控项创建redis状态触发器。
2UserParameter=redis.status,/usr/local/bin/redis-cli -h 10.51.125.88 -p 6379 -a anhuishenghefeishigaoxinjishukaifaqukexuedadao96haohuierpuzhongguo ping |grep -c PONG
3###item参数如何get
4UserParameter=redis_info[*],/etc/zabbix/script/redis/redis.sh $1 $2
5
6

确保在zabbix_agentd.conf里有:


1
2
3
1Include=/etc/zabbix/zabbix_agentd.d/*.conf
2
3

1
2
3
4
5
1chmod +x redis.sh
2cd /etc/zabbix/zabbix_agent.d
3vim zbx_redis.conf
4
5

1
2
3
4
5
6
1#监控redis状态,我们可以根据这个参数对应的监控项创建redis状态触发器。
2UserParameter=redis.status,/usr/local/bin/redis-cli -h 127.0.0.1 -p 6379 -a password ping |grep -c PONG
3###item参数如何get
4UserParameter=redis_info[*],/etc/zabbix/script/redis/redis.sh $1 $2
5
6

重启agent


1
2
3
1service zabbix-agent restart
2
3

测试
在zabbix-server上运行:


1
2
3
1/etc/zabbix/script/redis/zbx_redis_stats.py -p 6379 -a redispass L-192.168.0.3 gcc_version none
2
3

如果返回值,说明测试正确。

本文参考:
http://www.21yunwei.com/archives/4195
https://github.com/adubkov/zbx_redis_template
http://www.cnblogs.com/configure/p/6253590.html

给TA打赏
共{{data.count}}人
人已打赏
安全运维

OpenSSH-8.7p1离线升级修复安全漏洞

2021-10-23 10:13:25

安全运维

设计模式的设计原则

2021-12-12 17:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索