1.1、编写redis_status.sh脚本
[fieldyang@centos7-135 sbin]$ mkdir -p /usr/lib/zabbix/scripts/
[fieldyang@centos7-135 sbin]$ cd /usr/lib/zabbix/scripts/
[fieldyang@centos7-135 scripts]$ cat >redis_status.sh
#!/bin/bash
#author:Fieldyang
#description:此脚本用于监控redis集群
REDISCLI='/app/redis/bin/redis-cli'
HOST='192.168.28.135'
PORT=7001
PASS='123456'
if [[ $# == 1 ]];then
case $1 in
cluster_state)
result=`$REDISCLI -c -h $HOST -p $PORT -a $PASS cluster info 2>/dev/null | grep -w "cluster_state" | awk -F':' '{print $2}'| grep -c ok`
echo $result
;;
cluster_slots_assigned)
result=`$REDISCLI -c -h $HOST -p $PORT -a $PASS cluster info 2>/dev/null | grep -w "cluster_slots_assigned" | awk -F':' '{print $2}'`
echo $result
;;
cluster_slots_ok)
result=`$REDISCLI -c -h $HOST -p $PORT -a $PASS cluster info 2>/dev/null | grep -w "cluster_slots_ok" | awk -F':' '{print $2}'`
echo $result
;;
cluster_slots_pfail)
result=`$REDISCLI -c -h $HOST -p $PORT -a $PASS cluster info 2>/dev/null | grep -w "cluster_slots_pfail" | awk -F':' '{print $2}'`
echo $result
;;
cluster_slots_fail)
result=`$REDISCLI -c -h $HOST -p $PORT -a $PASS cluster info 2>/dev/null | grep -w "cluster_slots_fail" | awk -F':' '{print $2}'`
echo $result
;;
cluster_known_nodes)
result=`$REDISCLI -c -h $HOST -p $PORT -a $PASS cluster info 2>/dev/null | grep -w "cluster_known_nodes" | awk -F':' '{print $2}'`
echo $result
;;
cluster_size)
result=`$REDISCLI -c -h $HOST -p $PORT -a $PASS cluster info 2>/dev/null | grep -w "cluster_size" | awk -F':' '{print $2}'`
echo $result
;;
cluster_current_epoch)
result=`$REDISCLI -c -h $HOST -p $PORT -a $PASS cluster info 2>/dev/null | grep -w "cluster_current_epoch" | awk -F':' '{print $2}'`
echo $result
;;
cluster_my_epoch)
result=`$REDISCLI -c -h $HOST -p $PORT -a $PASS cluster info 2>/dev/null | grep -w "cluster_my_epoch" | awk -F':' '{print $2}'`
echo $result
;;
cluster_stats_messages_ping_sent)
result=`$REDISCLI -c -h $HOST -p $PORT -a $PASS cluster info 2>/dev/null | grep -w "cluster_stats_messages_ping_sent" | awk -F':' '{print $2}'`
echo $result
;;
cluster_stats_messages_pong_sent)
result=`$REDISCLI -c -h $HOST -p $PORT -a $PASS cluster info 2>/dev/null | grep -w "cluster_stats_messages_pong_sent" | awk -F':' '{print $2}'`
echo $result
;;
cluster_stats_messages_sent)
result=`$REDISCLI -c -h $HOST -p $PORT -a $PASS cluster info 2>/dev/null | grep -w "cluster_stats_messages_sent" | awk -F':' '{print $2}'`
echo $result
;;
cluster_stats_messages_ping_received)
result=`$REDISCLI -c -h $HOST -p $PORT -a $PASS cluster info 2>/dev/null | grep -w "cluster_stats_messages_ping_received" | awk -F':' '{print $2}'`
echo $result
;;
cluster_stats_messages_pong_received)
result=`$REDISCLI -c -h $HOST -p $PORT -a $PASS cluster info 2>/dev/null | grep -w "cluster_stats_messages_pong_received" | awk -F':' '{print $2}'`
echo $result
;;
cluster_stats_messages_received)
result=`$REDISCLI -c -h $HOST -p $PORT -a $PASS cluster info 2>/dev/null | grep -w "cluster_stats_messages_received" | awk -F':' '{print $2}'`
echo $result
;;
fail_node)
result=`$REDISCLI -c -h $HOST -p $PORT -a $PASS cluster nodes 2>/dev/null | awk -F ',' '{print $2}' | grep -c 'fail'`
echo $result
;;
*)
echo -e "\033[33mUsage: $0 {cluster_state|cluster_slots_assigned|cluster_slots_ok|cluster_slots_pfail|cluster_slots_fail|cluster_known_nodes|cluster_size|cluster_current_epoch|cluster_my_epoch|cluster_stats_messages_ping_sent|cluster_stats_messages_pong_sent|cluster_stats_messages_sent|cluster_stats_messages_ping_received|cluster_stats_messages_pong_received|cluster_stats_messages_received|fail_node}\033[0m"
;;
esac
fi
[fieldyang@centos7-135 scripts]$ chmod +x redis_status.sh
1.2、修改agent配置并重启
[fieldyang@centos7-135 scripts]$ grep ^UserParameter /etc/zabbix/zabbix-agent/conf/zabbix_agentd.conf
UserParameter=redis.status[*],/usr/lib/zabbix/scripts/redis_status.sh $1
[fieldyang@centos7-135 scripts]$
[fieldyang@centos7-135 sbin]$ ps -ef|grep zabbix_agentd |grep -v grep|awk '{print $2}'|xargs kill -9
[fieldyang@centos7-135 sbin]$ ./zabbix_agentd -c /etc/zabbix/zabbix-agent/conf/zabbix_agentd.conf
zabbix_agentd [31217]: Warning: EnableRemoteCommands parameter is deprecated, use AllowKey=system.run[*] or DenyKey=system.run[*] instead
[fieldyang@centos7-135 sbin]$
1.3、测试验证
[fieldyang@www ~]$ echo "cluster_state|cluster_slots_assigned|cluster_slots_ok|cluster_slots_pfail|cluster_slots_fail|cluster_known_nodes|cluster_size|cluster_current_epoch|cluster_my_epoch|cluster_stats_messages_ping_sent|cluster_stats_messages_pong_sent|cluster_stats_messages_sent|cluster_stats_messages_ping_received|cluster_stats_messages_pong_received|cluster_stats_messages_received|fail_node"|sed "s/|/\n/g"|while read i
> do
> zabbix_get -s 192.168.28.135 -k redis.status[$i] |xargs |xargs echo "${i}:"
> done
cluster_state: 1
cluster_slots_assigned: 16384
cluster_slots_ok: 16384
cluster_slots_pfail: 0
cluster_slots_fail: 0
cluster_known_nodes: 10
cluster_size: 5
cluster_current_epoch: 15
cluster_my_epoch: 11
cluster_stats_messages_ping_sent: 15397
cluster_stats_messages_pong_sent: 15516
cluster_stats_messages_sent: 30913
cluster_stats_messages_ping_received: 15516
cluster_stats_messages_pong_received: 15394
cluster_stats_messages_received: 30910
fail_node: 0
[fieldyang@www ~]$
转换一下,方便后面使用python脚本自动添加item项目
echo "cluster_state|cluster_slots_assigned|cluster_slots_ok|cluster_slots_pfail|cluster_slots_fail|cluster_known_nodes|cluster_size|cluster_current_epoch|cluster_my_epoch|cluster_stats_messages_ping_sent|cluster_stats_messages_pong_sent|cluster_stats_messages_sent|cluster_stats_messages_ping_received|cluster_stats_messages_pong_received|cluster_stats_messages_received|fail_node"|sed -e "s/|/\",\"/g" -e "s/^/\"/g" -e "s/$/\"/g"
"cluster_state","cluster_slots_assigned","cluster_slots_ok","cluster_slots_pfail","cluster_slots_fail","cluster_known_nodes","cluster_size","cluster_current_epoch","cluster_my_epoch","cluster_stats_messages_ping_sent","cluster_stats_messages_pong_sent","cluster_stats_messages_sent","cluster_stats_messages_ping_received","cluster_stats_messages_pong_received","cluster_stats_messages_received","fail_node"
2.1、编写python脚本:自动添加应用集,批量添加监控项、触发器
相关api参考官方文档
#!/usr/bin/env python
"""
# author: Fieldyang
# version 1.0
# description:此脚本用于自动创建应用集,批量生成监控项,并创建触发器,可用于脚本自定义的监控项的添加
"""
from urllib import request, parse
import json
# 定义通过HTTP方式访问API地址的函数,后面每次请求API的各个方法都会调用这个函数
def requestJson(url, values):
data = json.dumps(values)
# json.dumps() 将python数据结构转化为Json字符串,即dict类型转换为str类型
# json.dump() 编码,用于将dict类型的数据转换为str类型,并写入到json文件
data = bytes(data, 'utf8')
# 必须做转换,否则会返回 TypeError: POST data should be bytes, an iterable of bytes,
# or a file object. It cannot be of type str.
req = request.Request(url, data, {'Content-Type': 'application/json-rpc'})
response = request.urlopen(req, data)
output = json.loads(response.read())
# json.loads() 将json编码的字符串转换回python数据结构,即str类型转换成dict类型
# json.load() 解码,用于从json文件中读取数据。
try:
message = output['result']
except:
message = output['error']['data']
print(message)
quit()
return output['result']
# API接口认证的函数,登录成功会返回一个Token
def authenticate(url, username, password):
pdata = {}
pdata['method'] = 'user.login' # 方法
pdata['id'] = '0' # 用户 id
pdata['jsonrpc'] = "2.0" # api 版本
pdata['params'] = {"user": username, "password": password} # 用户账号密码
#此种定义方式方便json转换,其它方法直接从官方拷贝json
token = requestJson(url, pdata)
return token
#定义获取hostid函数
def gethostid(auth, ip):
pdata = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": [
"hostid",
"host"
],
"selectInterfaces": [
"interfaceid",
"ip"
],
'filter': {
'ip': ip
},
},
"id": 2,
"auth": auth
}
output = requestJson(url, pdata)
return output
#定义获取应用集函数
def getapp(hostid,appname,auth):
values = {
"jsonrpc": "2.0",
"method": "application.get",
"params": {
"output": "extend",
"hostids": hostid,
"sortfield": "name",
'filter': {
'name': appname
}
},
"auth":auth,
"id": 1
}
output = requestJson(url,values)
return output
#定义创建应用集函数
def createapp(hostid,appname,auth):
values = {
"jsonrpc": "2.0",
"method": "application.create",
"params": {
"name": appname,
"hostid": hostid,
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
# 定义maintenance维修模式host函数,本文不涉及,可忽略
def maintenancecreate(maintenancename,active_since,active_till,hostid,period,auth):
values = {
'jsonrpc': '2.0',
'method': 'maintenance.create',
'params': {
"name": maintenancename,
"active_since": active_since,
"active_till": active_till,
"hostids": [
hostid
],
"timeperiods": [{
"timeperiod_type": 0,
"every": 1,
"dayofweek": 64,
"start_time": 64800,
"period": period
}]
},
'auth': auth,
'id': '8'
}
output = requestJson(url,values)
return output
#定义创建监控项函数
def createitem(name, key, hostid, interfaceid,applicationid, auth):
values = {
"jsonrpc": "2.0",
"method": "item.create",
"params": {
"name": name,
"key_": key,
"hostid": hostid,
"type": 0,
"value_type": 3,
"interfaceid": interfaceid,
"applications": [applicationid],
"delay": "30s"
},
"auth": auth,
"id": 1
}
output = requestJson(url, values)
return output
#定义创建触发器函数
def createtrigger(description, expression,auth):
values = {
"jsonrpc": "2.0",
"method": "trigger.create",
"params": [
{
"description": description,
"expression": expression,
},
],
"auth": auth,
"id": 4
}
output = requestJson(url, values)
return output
#登出函数
def logout(auth):
pdata = {}
pdata['method'] = 'user.logout'
# 方法
pdata['id'] = 3 # 用户 id
pdata['jsonrpc'] = "2.0" # api 版本
pdata['params'] = []
pdata['auth'] = auth # auth 串
output = requestJson(url, pdata)
return output
# 返回信息
if __name__ == '__main__':
# 定义URL账户密码
url = 'http://192.168.28.132/zabbix/api_jsonrpc.php'
username = 'fieldyang'
password = '8357690'
ip = '192.168.28.135'
#定义items条目
item_list = ["cluster_state", "cluster_slots_assigned", "cluster_slots_ok", "cluster_slots_pfail",
"cluster_slots_fail",
"cluster_known_nodes", "cluster_current_epoch",
"cluster_size", "cluster_my_epoch", "cluster_stats_messages_ping_sent",
"cluster_stats_messages_pong_sent", "cluster_stats_messages_sent",
"cluster_stats_messages_ping_received", "cluster_stats_messages_pong_received",
"cluster_stats_messages_received", "fail_node"]
auth = authenticate(url, username, password)
if auth:
host = gethostid(auth, ip)
hostid = host[0]['hostid']
interfaceid = host[0]['interfaces'][0]['interfaceid']
#print(host)
#print(interfaceid)
if hostid:
for item in item_list:
name = 'redis.cluster_status.%s' %(item)
key = 'redis.status[%s]' %(item)
#print(name,key)
appname = 'redis cluster'
#定义应用集名称
try:
#添加应用集
application = createapp(hostid,appname,auth)
print("添加 应用集 %s 成功!" %application)
if application:
applicationid=application[0]['interfaceid']
#print(applicationid)
#创建监控项
itemids = createitem(name, key, hostid, interfaceid,applicationid, auth)
itemid = itemids['itemids'][0]
#print("添加 应用集 %s 成功!" %application)
print("添加 名称 %s 键值 %s 键id %s 成功!" % (name,key,itemid))
else:
pass
except:
# 检索应用集
application=getapp(hostid, appname, auth)
applicationid=application[0]['applicationid']
#print(application)
#print(applicationid)
# 创建监控项
itemids = createitem(name, key, hostid, interfaceid, applicationid, auth)
itemid = itemids['itemids'][0]
print("添加 名称 %s 键值 %s 键id %s 成功!" % (name,key,itemid))
description = 'Redis cluster fail node >=1'
expression = '{centos7-135:redis.status[fail_node].last()}>=1'
triggerids=createtrigger(description, expression, auth)
if triggerids:
triggerid=triggerids['triggerids'][0]
print("添加触发器 %s 触发器id %s 成功!" % (description,triggerid))
else:
pass
else:
pass
logout(auth)
2.2、运行脚本创建 应用集、监控项、触发器
2.3、zabbix web端确认是否已经创建成功
2.4、确认最新数据
2.5、创建图形(略)