阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Zabbix 自定义LLD

196次阅读
没有评论

共计 3159 个字符,预计需要花费 8 分钟才能阅读完成。

线上部分实时 job 是用 storm 开发的,为了监控数据的延迟,在 storm 处理日志的时候会把日志的时间插入到 Redis 中,然后通过 zabbix 做延迟的监控。由于经常有新的 job 上线,手动配置监控项就变得比较麻烦,为了解放生产力,还是需要搞成自动化。

之前添加网卡和分区监控的时候用了 LLD 的功能,并用了其内置的宏变量,新版本的 zabbix 是支持 custom LLD 的,实现步骤如下:

1. 在模板中设置一个 discovery rule (UserParameter  Key),调用脚本,返回 zabbix 规定的 json 数据(返回自定义的宏变量),并正确设置的 discovery(比如 filter 等)

这里通过官方文档并结合线上的 agent 日志,可以看到 zabbix 规定的数据格式

143085:20141127:000548.967 Requested [vfs.fs.discovery]
143085:20141127:000548.967 Sending back [{
        “data”:[
                {
                        “{#FSNAME}”:”\/”,
                        “{#FSTYPE}”:”rootfs”},
                {
                        “{#FSNAME}”:”\/proc\/sys\/fs\/binfmt_misc”,
                        “{#FSTYPE}”:”binfmt_misc”},
                {
                        “{#FSNAME}”:”\/data”,
                        “{#FSTYPE}”:”ext4″}]}]

比如线上返回 json 数据的 key:

UserParameter=storm.delay.discovery,Python2.6 /apps/sh/zabbix_scripts/storm/storm_delay_discovery.py

并通过

zabbix_get  -s 127.0.0.1 -k storm.delay.discovery

验证返回数据的准确性

storm_delay_discovery.py 内容如下:

#!/usr/bin/python
import sys
import redis
import exceptions
import traceback
_hashtables = []
_continue = True
_alldict = {}
_alllist = []
class RedisException(Exception):
    def __init__(self, errorlog):
        self.errorlog = errorlog
    def __str__(self):
            return “error log is %s” % (self.errorlog)
           
def scan_one(cursor,conn):
    try:
        cursor_v =  conn.scan(cursor)
        cursor_next = cursor_v[0]
        cursor_value = cursor_v[1]
        for line in cursor_value:
            if (line.startswith(“com-vip-storm”) or line.startswith(“stormdelay_”)) and str(line) != “stormdelay_riskcontroll”:
                _hashtables.append(line)
            else:
                pass
        return cursor_next
    except Exception,e:
        raise RedisException(str(e))
       
def scan_all(conn):
    try:
        cursor1 = scan_one(‘0’,conn)
        global _continue
        while _continue:
            cursor2 = scan_one(cursor1,conn)
            if int(cursor2) == 0:
                _continue = False
            else:
                cursor1 = cursor2
                _continue = True
    except Exception,e:
        raise RedisException(str(e))
def hget_fields(conn,hashname):
        onedict = {}
        fields = conn.hkeys(hashname)
        for field in fields:
            onedict[“{#STORMHASHNAME}”] = hashname
            onedict[“{#STORMHASHFIELD}”] = field
        _alllist.append(onedict)
if __name__ == ‘__main__’:
    try:
        r=redis.StrictRedis(host=’xxxx’, port=xxx, db=0)
        scan_all(r)
        for hashtable in _hashtables:
                hget_fields(r,hashtable)
        _alldict[“data”] = _alllist
        print str(_alldict).replace(“‘”,'”‘)
    except Exception,e:
        print  -1

2. 设置 item/graph/trigger prototypes:

这里以 item 为例,定义 item prototypes (同样需要定义 key),key 的参数为宏变量

比如 Free inodes on {#FSNAME} (percentage)—>vfs.fs.inode[{#FSNAME},pfree]

本例中,在 item 中使用上面返回的宏变量即可,

storm_delay[hget,{#STORMHASHNAME},{#STORMHASHFIELD}]

最后,把包含 LLD 的 template 链接到 host 上即可。

最后再配合 screen.create/screenitem.update api 就可以实现监控添加 /screen 添加, 更新的自动化了。

一些 Zabbix 相关教程集合

安装部署分布式监控系统 Zabbix 2.06 http://www.linuxidc.com/Linux/2013-07/86942.htm

《安装部署分布式监控系统 Zabbix 2.06》http://www.linuxidc.com/Linux/2013-07/86942.htm

CentOS 6.3 下 Zabbix 安装部署 http://www.linuxidc.com/Linux/2013-05/83786.htm

Zabbix 分布式监控系统实践 http://www.linuxidc.com/Linux/2013-06/85758.htm

CentOS 6.3 下 Zabbix 监控 apache server-status http://www.linuxidc.com/Linux/2013-05/84740.htm

CentOS 6.3 下 Zabbix 监控 MySQL 数据库参数 http://www.linuxidc.com/Linux/2013-05/84800.htm

64 位 CentOS 6.2 下安装 Zabbix 2.0.6   http://www.linuxidc.com/Linux/2014-11/109541.htm

ZABBIX 的详细介绍 :请点这里
ZABBIX 的下载地址 :请点这里

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计3159字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中