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

基于Zabbix添加host与trigger

189次阅读
没有评论

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

在配置 Zabbix 的过程中或者通过 Zabbix 获取数据是,总是需要获取各个组建的 id 号,有时候需要添加几十上百个 item 或者 trigger。所以就自己写了下面的类。如果以后有需要会再完善。不过估计是不需要了,因为做了一个月监控,终于做完了。代码如下,已经给出简单的注释。有问题可留言。

#!/usr/bin/Python
#-*- coding:utf8 -*-
from zabbix_api import ZabbixAPI
class zabbix_tools:
    ”’
        从 zabbixweb 端获取,Item,host,等列表并添加 host 及 trigger。
    ”’
    def __init__(self,server,username,password):
        self.server=server
        self.zapi = ZabbixAPI(server=self.server, path=””, log_level=0)
        self.zapi.login(username,password)
        self.item_dict={}
        self.app_dict={}
        self.host_dict={}
        self.group_dict={}
       
    def get_item_list(self,hostname):
        #获取指定 host 的 item 列表
        search={“filter”:{“host”:[hostname]}}
        for application_id in [‘2311′,’2312’]:
            search[“applicationids”]=application_id
            for item_info in self.zapi.item.get(search):
                self.item_dict[item_info[“name”]]=[item_info[“itemid”],item_info[“key_”]]
        return self.item_dict
       
    def get_host_list(self):
        #获取整个 zabbix 的 host 列表
        for host in zapi.host.get({}):
            self.host_dict[host[‘name’]]=host[‘hostid’]
        return self.host_dict
       
    def get_group_list(self):
        #获取整个 zabbix 的 hostgroup 列表
        for group in zapi.hostgroup.get({}):
            self.group_dict[group[‘name’]]=group[‘groupid’]
        return self.group_dict
       
    def get_host_info(self,hostname):
        #获取指定 host 的信息
        host_info = self.zapi.host.get({“selectInterfaces”:[“interfaceid”],”filter”:{“host”:[hostname]}})
        hostid = host_info[0][‘hostid’]
        interfaceid =  host_info[0][‘interfaces’][0][‘interfaceid’]
        return (hostid,interfaceid)
       
    def get_aplication_list(self,hostname):
        #获取指定 host 的 application 列表
        search={“filter”:{“host”:[hostname]}}
        for app in self.zapi.application.get(search):
            self.app_dict[app[‘name’]]=app[‘applicationid’]
        return self.app_dict
       
    def create_item(name,key,hostname,appname):
        #在指定 host 下添加一个 item 并指定 appname。
        a = self.get_host_info(hostname)
        hostid = a[0]
        interfaceid = a[1]
        applicationid=self.get_aplication_list[hostname][appname]
        create_item=self.zapi.item.create(
            {
                “name”:name,
                “key_”:key,
                “hostid”:hostid,
                “type”:0,
                “value_type”:0,
                “interfaceid”:interfaceid,
                “date_type”:0,
                “delay”:60,
                “history”:7,
                “trends”:90,
                “status”:0,
                “applications”:[
                    applicationid
                ]
            }
        )
        return “item create success”
       
    def create_trigger(self,expression,description,hostname):
        #为某个 host 添加 trigger
        a = self.get_host_info(hostname)
        hostid = a[0]
        self.zapi.trigger.create({“description”:description,”expression”:expression,”hostid”:hostid,’priority’:2})
        print “trigger create success”
       
if __name__ == ‘__main__’:
    server = “zabbix web server url”
    username = “youusername”
    password = “yourpassword”
    app=zabbix_tools(server,username,password)
    print app.get_group_list()

一些 Zabbix 相关教程集合

Ubuntu 14.04 下 Zabbix2.4.5 源码编译安装  http://www.linuxidc.com/Linux/2015-05/117657.htm

CentOS 7 LNMP 环境搭建 Zabbix3.0  http://www.linuxidc.com/Linux/2017-02/140134.htm

Ubuntu 16.04 安装部署监控系统 Zabbix2.4  http://www.linuxidc.com/Linux/2017-03/141436.htm

Zabbix 监控安装部署及警报配置  http://www.linuxidc.com/Linux/2017-03/141611.htm

Ubuntu 16.04 下安装部署 Zabbix3.0  http://www.linuxidc.com/Linux/2017-02/140395.htm

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

CentOS 7 下 Zabbix 3.0 安装详解 http://www.linuxidc.com/Linux/2017-03/141716.htm

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

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

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-03/141918.htm

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