Zabbix monitoring Redis Python3 scripts

Source: Internet
Author: User

One: Install the Redis-python module

WGE thttps://pypi.python.org/packages/source/r/redis/redis-2.9.1.tar.gz

Tar XF redis-2.9.1.tar.gz

CD redis-2.9.1

Python setup.py Install

Two: Configuration Zabbix

(1) Put the zabbix-redis.py into the/usr/local/zabbix/etc/scripts directory

(2) Change zabbix_agentd.conf Include${dir}/zabbix. Userparameter

(3) Restart Zabbix_agentd

Killall Zabbix_agentd

/usr/local/zabbix/sbin/zabbix_agentd-c/usr/local/zabbix/etc/zabbix_agentd.conf

Second: The main monitoring projects include

The client queries the key value hits and misses to calculate the hit ratio

Number of client connections for the current Redis instance

Number of clients currently in a blocked state

How often the client executes commands per second

Number of connections from the library

Memory usage Status

Three: Specific parameters explained

Keyspace_misses//indicates number of misses

Keyspace_hits//= number of Hits

Keyspace_hits_rate = keyspace_hits/(keyspace_hits + keyspace_misses)

Number of connected_clients//client connections

Blocked_clients//Client blocking number

Connected_slaves//From library number

INSTANTANEOUS_OPS_PER_SEC//Client executes command frequency per second

USED_MEMORY_RSS//The memory assigned to Redis by the operating system

Used_memory//redis Allocator Allocated memory

Mem_fragmentation_ratio//Memory fragmentation ratio in ideal cases,

The value of the Used_memory_rss should be only slightly higher than the used_memory. When RSS > used, and the values of the two differ significantly, there is a memory fragment (internal or external). When used > rss, some of the memory of Redis is swapped out to swap space by the operating system, in which case the operation can have a noticeable delay.

Four. Monitoring scripts

1. Python2 Get Reis Script

#!/bin/python#-*-coding:utf-8-*-#author Sunkedong Mail: [email protected] qq:512378103ImportRedisImportSYS"""explanation of each parameter: keyspace_misses//Indicates the number of misses keyspace_hits//indicates the number of hits keyspace_hits_rate = Keyspace_hits/(keyspace_hits + keyspace_misses) connected_clients//Client connections Blocked_clients//client blocking number connected_slaves//from the number of libraries Instantaneous_ops_per_sec The client executes the command frequency Used_memory_rss//per second//operating system assigned to Redis memory used_memory//redis Allocator Allocated memory Mem_fragmentation_ratio//memory Fragmentation scale"""#define the parameter as a listKeyIndex = ['used_memory','Used_memory_rss','Mem_fragmentation_ratio','blocked_clients','connected_clients',            'connected_slaves',            'instantaneous_ops_per_sec','keyspace_hits','keyspace_misses','Keypace_query_total_count',            'keyspace_hits_rate','Status']returnval=Nonedefzabbix_faild ():Print "zbx_notsupported"Sys.exit (2)ifLen (SYS.ARGV)! = 2:#need to have a parameter, plus the program itself is two parameters. So, if there are no parameters, direct prompt    PrintLen (SYS.ARGV) zabbix_faild ()Try: Conn=redis. Redis (host='172.16.17.40', port='6379', password="')exceptexception,e:Printe zabbix_faild ()#The following parameters are judged and evaluated, and the final return status is added to the Zabbix .ifSYS.ARGV[1]inchKeyIndex:ifSYS.ARGV[1] = ='Status':#If the parameter is status, Ping is true, and a return value of 1 in 1,zabbix is normal.         Try: conn.ping () ReturnVal= 1exceptException,e:returnval=0elifSYS.ARGV[1] = ='keyspace_hits_rate': Merit=conn.info () Keyspace_hits_count= Float (merit['keyspace_hits']) Keyspace_misses_count= Float (merit['keyspace_misses']) Keyspace_hits_rate= Keyspace_hits_count/(Keyspace_hits_count + keyspace_misses_count) * 100ReturnVal=keyspace_hits_rateelifSYS.ARGV[1] = ='Keypace_query_total_count': Merit=conn.info () Keyspace_hits_count= merit['keyspace_hits'] Keyspace_misses_count= merit['keyspace_misses'] Keypace_query_total_count= Keyspace_hits_count +Keyspace_misses_count returnval=Keypace_query_total_countElse: Merit=Conn.info ()Try: ReturnVal= Merit[unicode (sys.argv[1])]        exceptexception,e:Pass#functions that determine the status of a return valuedefret_status ():ifReturnVal = =None:zabbix_faild ()Else:        Printreturnvalret_status ()

2. Python3 Get Redis Script

#!/usr/bin/python#-*-coding:utf-8-*-ImportRedisImportSYS"""explanation of each parameter: keyspace_misses//Indicates the number of misses keyspace_hits//indicates the number of hits keyspace_hits_rate = Keyspace_hits/(keyspace_hits + keyspace_misses) connected_clients//Client connections Blocked_clients//client blocking number connected_slaves//from the number of libraries Instantaneous_ops_per_sec The client executes the command frequency Used_memory_rss//per second//operating system assigned to Redis memory used_memory//redis Allocator Allocated memory Mem_fragmentation_ratio//memory Fragmentation scale"""#define the parameter as a listKeyIndex = ['used_memory','Used_memory_rss','Mem_fragmentation_ratio','blocked_clients','connected_clients',            'connected_slaves',            'instantaneous_ops_per_sec','keyspace_hits','keyspace_misses','Keypace_query_total_count',            'keyspace_hits_rate','Status']returnval=Nonedefzabbix_faild ():Print("zbx_notsupported") Sys.exit (2)ifLen (SYS.ARGV)! = 2:#need to have a parameter, plus the program itself is two parameters. So, if there are no parameters, direct promptZabbix_faild ()Try: Conn=redis. Redis (host='192.168.40.36', port='6379', password='123456')#This is a python syntax, and it's a little different from Python3.#except Exception,e:#Zabbix_faild ()exceptException as E:zabbix_faild ()#The following parameters are judged and evaluated, and the final return status is added to the Zabbix .ifSYS.ARGV[1]inchKeyIndex:ifSYS.ARGV[1] = ='Status':#If the parameter is status, Ping is true, and a return value of 1 in 1,zabbix is normal.         Try: conn.ping () ReturnVal= 1exceptException as E:returnval=0elifSYS.ARGV[1] = ='keyspace_hits_rate': Merit=conn.info () Keyspace_hits_count= Float (merit['keyspace_hits']) Keyspace_misses_count= Float (merit['keyspace_misses']) Keyspace_hits_rate= Keyspace_hits_count/(Keyspace_hits_count + keyspace_misses_count) * 100ReturnVal=keyspace_hits_rateelifSYS.ARGV[1] = ='Keypace_query_total_count': Merit=conn.info () Keyspace_hits_count= merit['keyspace_hits'] Keyspace_misses_count= merit['keyspace_misses'] Keypace_query_total_count= Keyspace_hits_count +Keyspace_misses_count returnval=Keypace_query_total_count#Python3 does not require Unicode    #Else:    #merit = Conn.info ()    #Try:    #returnval = Merit[unicode (sys.argv[1])    #except Exception as E:    #Pass    Else: Merit=Conn.info ()Try: ReturnVal= merit[(sys.argv[1])]        exceptException as E:Pass#functions that determine the status of a return valuedefret_status ():ifReturnVal = =None:zabbix_faild ()Else:        Print(ReturnVal) ret_status ()

Zabbix monitoring Redis Python3 scripts

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.