Two methods of Zabbix monitoring Redis database state

Source: Internet
Author: User
Tags json memory usage redis

Method One, Zabbix monitor Redis

1, Import Template

Again, I have uploaded the template to the GitHub project. Download directly, import the template through the following steps: Click configuration----> Templates------> Import-------> Import downloaded XML file.

2. Client key Configuration

On the monitored host, open the/etc/zabbix/zabbix_agentd.conf configuration file and add on the last line:

Userparameter=redis_stats[*],redis-cli-h 127.0.0.1-p $ info|grep $2|cut-d:-f2
After the configuration is complete, restart the service through the/etc/init.d/zabbix_agentd restart and apply the latest configuration file.

Note: Also confirm the Unsafeuserparameters=1 in the file.

3. Service-Side verification

Verification method or through the Zabbix_get program to obtain data, the normal access to data, proof of normal communication.

The code is as follows Copy Code
# zabbix_get-s 172.20.0.20-k redis_stats[6379,total_connections_received]

The above normal from the monitoring of the host data obtained, C/s communication is normal.

4. Add Host association Template

Create a new host through the configuration------> hosts------> Creation Host, find the Templates redis_6379 template in the templates item, and link the template. You can get the data normally. Similarly, validation data uses the monitoring-----lastest data----hits the host just added to see if there is data.

5, view the monitoring chart

With monitoring-----Graphs can view the monitor map of the monitor item just added, which only truncates the memory usage graph.

Method two, using Zabbix Low level discovery Automatic discovery monitoring Redis

First, server configuration

1 configures regular expressions, which are used when you add Discovery rules later
#这里是个坑, what I'm matching here is the port number, just beginning to write is "d+" test also passed, all configured well, in the server through the Zabbix_get can also be normal access to the value of the client, helpless is not automatically reported, check a noon, and then asked a few friends, Finally found the reason, is to use ^ (8899|10000) $ similar to this format, said more than tears
Finally, check the official document, the original Zabbix is not clear, it is the report of the results of the two processing, in fact, if the report is accurate, do not have to do two times filtration, the final use of a regular blank, do not do two times matching processing
2) Create template

Click Discovery Rules to create a custom rule
Click Item prototypes to add a monitor item
I need to monitor 4 metrics here, build 4 Item

The key value is defined on the client

Second, the client configuration:

First look at the zabbix_agentd.conf configuration file

The code is as follows Copy Code
Unsafeuserparameters=1 #允许zabbix用户运行root命令
userparameter=redis.discovery,/usr/local/zabbix/bin/redis_port.py #获取json格式数据, on the mouth of the newspapers
Userparameter=redis[*],/usr/local/zabbix/bin/redis_stats.sh $ #自定义Key值

Requires two scripts:

  code is as follows copy code
ll/usr/local/zabbix/bin/
Total 516
-rwxr-xr-x 1 root root 342 Oct 16:56 redis_port.py
-rwxr-xr-x 1 root root 773 Oct 14:59 redis_stats.sh
-rwxr-xr-x 1 root root 236737 June Zabbix_get
-rwxr-xr-x 1 root root 278582 June Zabbix_sender
JSON format script
[root@ ~]# cat/usr/local/zabbix/bin/redis_port.py # #json串
#!/usr/bin/env python
Import OS
Import JSON
T=os.popen ("" "Netstat-natp|awk-f: '/redis-server/&&/listen/{print $} ' |awk ' {print $} '" ")
Ports = []
For port in T.readlines ():
R = Os.path.basename (Port.strip ())
Ports + = [{' {#REDISPORT} ': R}]
Print json.dumps ({' Data ':p orts},sort_keys=true,indent=4,separators= (', ', ': '))

To get the port data status script:

The code is as follows Copy Code

[root@ ~]# cat/usr/local/zabbix/bin/redis_stats.sh # #获取状态

#!/bin/bash
Metric= "$"
hostname=127.0.0.1
Port= "${2:-6379}"
Cache_file= "/tmp/redis_$port.cache"

(Echo-en "Inforn"; sleep 1;) | NC $HOSTNAME $PORT > $CACHE _file 2>/dev/null | | Exit 1

grep "^ $METRIC:" $CACHE _file |awk-f ': |, ' {print $} ' |sed ' s/[^0-9]//g '


The above is all configured complete, down to test

Test on the Zabbix_server and execute the following command:

The code is as follows Copy Code

Zabbix_get-s 192.168.250.206-k Redis.discovery

The following results are obtained:
{
"Data": [
{
' {#REDISPORT} ': ' 10000 '
},
{
' {#REDISPORT} ': ' 8899 '
}
]
}
OK, automatic discovery Port success!

Zabbix_get-s 192.168.250.206-k redis[used_memory,8899]

4834072
OK, get the value you need!

And then add the template to the machine you need to monitor, which is not on the diagram.
After you finish adding the template, view the data:

In the process of doing the following problems encountered:
1. With regard to the question of the regular
A regular match is different from a regular one, and a match between multiple matches is used ^ (8899|10000) $ similar to this format
Or just leave it blank
2. Permissions issues, ZABBIX_AGENTD is Zabbix user-initiated, the default can not perform netstat-p, and other commands, resulting from the server to the automatic discovery script is empty

The code is as follows Copy Code
(Not all processes could is identified, non-owned process info
Won't is shown, you would have to is root to the it all.)
Workaround:
chmod +s/bin/netstat

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.