首页 > 其他分享 >zabbix基于api方式批量创建监控项和触发器示例

zabbix基于api方式批量创建监控项和触发器示例

时间:2023-05-07 14:01:29浏览次数:50  
标签:name 示例 req zabbix port host api id

基于python3 zabbix api方式,达到批量创建指定机器业务端口的监控项和触发器的创建的效果,目前不支持并发处理,只支持串行处理

zabbix-net-server.py 内容 ,放到zabbix服务器上,然后执行python3 zabbix-net-server.py 启动socket监听

#!/usr/bin/python3
import socket, sys, time
import json
import requests
import pymysql
#当前master监听
host = 'xxxx'
port = 2443
 
#自定义监控项所在的应用集名称
zabbix_app = "netstat"
 
#禁止的监控的服务列表,比如sshd,master,ntpd
deny_server = ["sshd", "hblog", "zabbix_agentd", "master", "staragent-core", "DragoonAgent", "filebeat", "agent"]
deny_port = ["32000"]
 
#触发器告警级别,(0:未分类; 1:信息; 2:警告; 3:一般严重 ...)
trigger_pri = 4
 
#zabbix服务器的IP地址
zabbix_ip = "10.99.1.240"
 
#zabbix的用户名
zabbix_user = "xxxx"
 
#zabbix的密码
zabbix_pass = "xxxx"
 
#zabbix api接口地址
url = "http://" + zabbix_ip + "/api_jsonrpc.php"
#zabbix api定义的访问头部信息
post_header = {'Content-Type': 'application/json'}
 
 
#调用rds查询
def get_hostname(host_ip):
    con = pymysql.connect(host='xxxxx.mysql.rds.aliyuncs.com', user='xxxx', password='xxxxxxx', db='heroes',
                          # 需要这台负载均衡上开下3306透传,访问控制白名单先去掉
                          charset='utf8')
    CURSOR = con.cursor()
    sql = "SELECT host_name FROM hosts where host_ip='%s'; " % host_ip
    CURSOR.execute(sql)
    result = CURSOR.fetchone()[0]
    return result
 
#调用zabbix api需要身份令牌auth
def get_auth():
    post_data = {
        "jsonrpc": "2.0",
        "method": "user.login",
        "params": {
            "user": zabbix_user,
            "password": zabbix_pass
        },
        "id": "1"
    }
 
    ret = requests.post(url, data=json.dumps(post_data), headers=post_header)
    zabbix_ret = json.loads(ret.text)
    if 'result' not in zabbix_ret:
        print('login error')
    else:
        return zabbix_ret.get('result')
 
#用于给api提交并提交
def post_info(values, id_name):
    zabbix_req = requests.post(url, data=json.dumps(values), headers=post_header)
    zabbix_req = zabbix_req.json()
    try:
        req_error = zabbix_req['error']['data'] #如果获取不到就说明成功了,则返回信息
    except:
        req_info = zabbix_req['result'][id_name]
        return req_info
 
#以hostname信息获取主机id
def get_hostid(auth, host_ip):
    values = {
        "jsonrpc": "2.0",
        "method": "host.get",
        "params": {
            "output": "extend",
            "filter": {
                "host": [host_ip]
            }
        },
        "auth": auth,
        "id": 2,
    }
    zabbix_req = requests.post(url, data=json.dumps(values), headers=post_header)
    zabbix_req = zabbix_req.json()
    zabbix_req = zabbix_req['result']
    try:
        host_id = zabbix_req[0]['hostid']
        return host_id
    except:
        return 1
 
def get_interfaces(auth,host_id):
    values = {
        "jsonrpc":"2.0",
        "method":"host.get",
        "params":{
            "output":['name','host_id'],
            "filter":{
                'hostids':[host_id]
            },
            "selectInterfaces":[
                "interfaceid",
                "ip"
            ],
        },
        "auth":auth,
        "id":1
    }
    zabbix_req = requests.post(url, data=json.dumps(values), headers=post_header)
    zabbix_req = zabbix_req.json()
    zabbix_req = zabbix_req['result']
    for i in range(len(zabbix_req)):
        if zabbix_req[i]['hostid'] == host_id:
            return zabbix_req[i]['interfaces'][0]['interfaceid']
 
#根据主机id和interfaceid定位,传入端口号来添加监控端口,成功返回监控项id
def post_item(auth,host_id,host_interfaceid,server_name,host_port,app_id):
    item_name = server_name + "的" + host_port + "端口-gd"
    item_key = 'net.tcp.listen[' + host_port + ']'
    values = {
        "jsonrpc":"2.0",
        "method":"item.create",
        "params": {
            "name": item_name,
            "key_": item_key,
            "hostid": host_id,
            "interfaceid": host_interfaceid,
            "applications": [app_id],
            "type": 0,
            "value_type": 3,
            "delay": "60s"
        },
        "auth": auth,
        "id": 1
    }
    zabbix_req = requests.post(url,data=json.dumps(values),headers=post_header)
    zabbix_req = zabbix_req.json()
    print(zabbix_req)
    return zabbix_req['result']['itemids'][0]
 
#根据主机ip来定位,传入端口号和服务名来添加触发器,成功返回触发器id
def post_trigger(auth, host_name, server_name, host_port):
    description = server_name + "的" + host_port + "端口关闭"
    expression = '{' + host_name + ':net.tcp.listen[' + host_port + '].last()}<>1'
    values = {
        "jsonrpc": "2.0",
        "method": "trigger.create",
        "params": {
            "description": description,
            "expression": expression,
            "priority": trigger_pri
            },
        "auth": auth,
        "id": 4
    }
    return post_info(values, 'triggerids')
 
#创建主机的应用集,成功返回应用集id
def post_app(auth,zabbix_app,host_id):
    values = {
        "jsonrpc": "2.0",
        "method": "application.create",
        "params": {
            "name": zabbix_app,
            "hostid": host_id
        },
        "auth": auth,
        "id": 1
    }
    return post_info(values, 'applicationids')
 
#根据host_id返回应用集列表,
def get_appid(auth, zabbix_app, host_id):
    values = {
        "jsonrpc": "2.0",
        "method": "application.get",
        "params": {
            "output": "extend",
            "hostids": host_id,
            "sortfield": "name"
        },
        "auth": auth,
        "id": 1
    }
    zabbix_req = requests.post(url, data=json.dumps(values), headers=post_header)
    zabbix_req = zabbix_req.json()
    zabbix_req = zabbix_req['result']
    for i in range(len(zabbix_req)):
        if zabbix_req[i]['name'] == zabbix_app:
            return zabbix_req[i]['applicationid']
 
#正式步骤
def post_all(host_ip,dict_info):
    host_name = get_hostname(host_ip)
    print(host_name + "开始添加监控项和对应触发器,并加入到应用集" + zabbix_app)
    auth = get_auth()
    host_id = get_hostid(auth,host_name)
    host_interfaceid = get_interfaces(auth,host_id)
    #先创建应用集,如果已经存在则从循环中里找出id
    app_id = post_app(auth,zabbix_app,host_id)
    if app_id is None:
        app_id = get_appid(auth,zabbix_app,host_id)
    print(app_id)
    for k,v in dict_info.items():
        if v[1] in deny_server:
            continue
        elif v[0] in deny_port:
            continue
        else:
            item_id = post_item(auth, host_id, host_interfaceid, v[1], v[0], app_id)
            trigger_id = post_trigger(auth, host_name, v[1], v[0])
            print(item_id,trigger_id)
        print(str(item_id))
        if item_id:
            print("添加" + v[1] + "的" + v[0] + "端口监控完成")
        else:
            print("跳过" + v[1] + "的" + v[0] + "监控添加")
    print(" ")
 
#持续监听端口,对客户端传入的数据进行监控项和触发器的添加
def main(host,port):
    serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serversocket.bind((host, port))
    serversocket.listen(200)  # 最大连接数
    while True:
        print('服务器启动,监听客户端链接')
        clientsocket, addr = serversocket.accept()
        host_ip = addr[0]
        data = clientsocket.recv(1024)
        try:
            dict_info = eval(data.decode('utf-8'))  # 变成字典
        except Exception as e:
            print(e)
            exit(1) #直接退出
        post_all(host_ip, dict_info)
        clientsocket.close()
    serversocket.close()
if __name__ == '__main__':
    main(host,port)


zabbix-net-agent.py,发放文件到指定需要被监控的服务器上

#!/usr/bin/python
#coding=utf-8
#通过netstat -unltp命令收集监听端口,需要客户端安装net-tools
#发送的字典结构:{'序号',['端口号', '进程名']},pid号主要用于去重,因为一个进程可能监控多个端口
#本脚本适用于单区服id下的服务器范围内执行agent脚本
 
import json, subprocess, string
import socket, sys
import json
import requests
#填写服务端地址和端口
host = "xxxx"
port = 2443
 
# slsk一组区服部署了2个gw的情况,gw的serverid对应slb健康检查的端口
slsk_gw_port_map = {'10111': '22111', '10112': '22121', '10121': '22211', '10122': '22221'}
#qiuqiu一组区服部署1个gw的情况,gw的serverid对应slb健康检查的端口
qiuqiu_gw_port_map = {'10011':'22111','10012':'22121'}
# slsk一组区服部署了2个bt的情况,bt的serverid对应slb健康检查的端口
slsk_bt_port_map = {'1010001': '20011', '1010002': '20021'}
#qiuqiu一组区服部署了1个bt的情况,bt的serverid对应slb健康检查的端口
qiuqiu_bt_port_map = {'1000001':'20011'}
 
# #常用端口列表,防止临时端口
# eternal_port = ["8889","8120"]
def get_gw_bt_map(port_list,s,project):
    num = 0
    if s == "GW":
        print("GW类型")
        gwinfo = subprocess.getstatusoutput("ls -l /data/app/ |grep GW |awk '{print $9}' |awk -F '_' '{print $2}' ")[1].split('\n')
        for gw in gwinfo:
            num += 1
            proc_name = "GW_" + gw
            if project == "slsk":
                port = slsk_gw_port_map.get(gw,'')
            elif project == "qiuqiu":
                port = qiuqiu_gw_port_map.get(gw,'')
            if port:
                port_list[num] = [port, proc_name]
 
    if s == "BT":
        print("BT类型")
        btinfo = subprocess.getstatusoutput("ls -l /data/app/ |grep BT |awk '{print $9}' |awk -F '_' '{print $2}'")[1].split('\n')
        for bt in btinfo:
            num += 1
            proc_name = "BT_" + bt
            if project == "slsk":
                port = slsk_bt_port_map.get(bt, '')
            elif project == "qiuqiu":
                port = qiuqiu_bt_port_map.get(bt,'')
            if port:
                port_list[num] = [port, proc_name]
 
#返回当前机器 {'序号',['端口号', '进程名']}
def get_port():
    n = 0  # type: int
    i = 0
    port_list = {}  # 最终返回结果
    #获取内网ip
    # try:
    #     ipstr = subprocess.getstatusoutput("ip a |grep eth0 |grep inet |awk '{print $2}'|awk -F '/' '{print $1}'")[1]
    # except:
    #     ipstr = ''
    # if not ipstr:
    #     raise SystemExit(1)  #异常退出
    # con = pymysql.connect(host='47.114.80.9', user='heroes', password='GtspI0KxouLvHcb3', db='heroes', #需要这台负载均衡上开下3306透传,访问控制白名单先去掉
    #                       charset='utf8')
    # CURSOR = con.cursor()
    # sql = "SELECT host_name FROM hosts where host_ip='%s'; "%ipstr
    # CURSOR.execute(sql)
    # try:
    #     result = CURSOR.fetchone()[0]
    # except:
    #     raise SystemExit(1) #异常退出
    hostnameinfo = requests.get("http://xxxx:5000/query/hostname").text
    hostnameinfo = json.loads(hostnameinfo)
    statu = hostnameinfo['success']
    if statu:
        result = hostnameinfo['msg'][0]['host_name']
    else:
        raise SystemExit(1) #异常退出
    project = result.split('_')[0].strip()         #获取项目
    server_type = result.split('_')[4].strip().split('-') #获取服务类型
    #server_num = CURSOR.fetchone()[0].split('_')[5].strip()           #获取服务num数
    if project == "qiuqiu":
        for s in server_type:
            if s == "AU":
                n += 1
                proc = "AC" + str(n)
                port_list[n] = ['9522',proc]      #保证key的唯一性就行
            elif s == "CR":
                n += 1
                proc = "CR" + str(n)
                port_list[n] = ['9523',proc]
            get_gw_bt_map(port_list,s,project)
 
    if project == "slsk":
        for s in server_type:
            if s == "AC":
                p_list = ['9522','9523']
                for j in range(0,len(p_list)):
                    i+=1
                    proc = "AC"+str(i)
                    port_list[i] = [p_list[j],proc]
            get_gw_bt_map(port_list,s,project)
    return port_list
 
 
 
# #返回当前机器 {'pid号',['端口号', '进程名']}
# def get_port():
#     prostr = subprocess.getstatusoutput('netstat -unltp')
#     prostr = str(prostr)
#     prolist = prostr.split('\\n') #用两个\才行
#     del prolist[0]
#     del prolist[0]
#     prolist = prolist[::-1]
#     port_list = {}
#     lock_pid = [] #用于锁定pid
#
#     for info in prolist:
#         port_info = info.split()
#         if len(port_info) >= 7:
#             tmp_port = port_info[3].split(':')
#             tmp_port = tmp_port.pop()
#             tmp_info = port_info[6].split('/')
#             tmp_pid = tmp_info[0]
#             try:
#                 tmp_name = tmp_info[1]
#             except:
#                 continue
#
#             if tmp_pid in lock_pid: #如果端口是常用端口组里,就跳过这个,只监控常用端口
#                 continue
#
#             if tmp_port in eternal_port: #如果在表里就把pid锁定了,加入到锁定数组里
#                 lock_pid = lock_pid + [tmp_pid]
#
#             port_list[tmp_pid] = [tmp_port, tmp_name]
#     return port_list
 
#向master程序提交信息
def post_port(host, port):
    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    host=host
    port=port
    s.connect((host,port))
 
    data = get_port()
    if data != {}:
        data = str(data)
        print(data)
        s.send(data.encode('utf-8'))
        s.close()
 
if __name__ == "__main__":
    post_port(host, port)
    #print(get_port())


最后在跳板机上批量串行执行被监控机器上的zabbix-net-agent.py脚本

标签:name,示例,req,zabbix,port,host,api,id
From: https://blog.51cto.com/u_15703497/6251924

相关文章

  • 解决微信小程序请"注意游客模式下,调用 wx.login 是受限的, API 的返回是工具的模拟返
    新建一个微信小程序的项目,导入了代码后,出现微信小程序请"注意游客模式下,调用wx.login是受限的,API的返回是工具的模拟返回"这是因为,微信开发者工具没有配置小程序的appid下面添加上就可以了 ......
  • Entity Framework使用DBContext实现增删改查示例
    导读这篇文章介绍了EntityFramework使用DBContext实现增删改查的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下有一段时间没有更新博客了,赶上今天外面下雨,而且没人约球,打算把最近对EntityFrameworkDBContext使用......
  • Zabbix 3.0 发布:加强了云端运行监视的安全性
    拉脱维亚的Zabbix于2016年2月16日发布开源系统运用监视软件最新版本「Zabbix3.0」。改善了通信加密和预测检测功能实现等100个以上的项目。Zabbix是用于监视服务器或网络设备等的软件。支持广泛的架构,能监视数十万台以上的设备状况和性能。因为是作为GPL(GNUGeneralPublicLic......
  • 字面量,Object新增api,面向过程和面向对象,类构造函数
    /*字面量速写:如果属性名和形参名一致,可以直接写形参名方法速写:省略冒号和functionsayHello(){}//实际上是sayHello:function(){}计算属性名通过[]计算出来*///方法速写//字面量速写://functionsan(a,b,c,d){//constsayHello=function(){//......
  • Intersection Observer API 实现图片懒加载
    1,为需要延迟加载的图片设置data-src属性。<imgsrc=""data-src="image.jpg"alt="图片">2,使用IntersectionObserverAPI监听可视区域内的元素变化,并将其data-src属性值赋给src属性,显示图片。constlazyLoadImg=newIntersectionObserver((entries,observer)=>{en......
  • 如何通过代码接入手机在网状态 API
    引言在许多场景下,手机号码是一种常用的身份验证信息。而使用手机在网状态API可以判断出手机号码是否有效,在一定程度上提高了身份验证的准确性和安全性,它的出现和广泛应用,为各行各业提供了更为便利和高效的解决方案。本文将探讨该API的使用场景,使用思路以及如何通过代码接入......
  • zabbix通过api方式批量创建和删除主机
    zabbix库连接信息,文件名为Conn.pyimportpymysql"""获取后台数据库连接"""defCON():con=pymysql.connect(host='rm-xxx.xxxxx.rds.aliyuncs.com',user='xxx',password='xxxx',db=�......
  • Java获取pdd详情api接口、商品详情、商品描述、宝贝链接获取展示示例
    ​拼多多商品详情就是对拼多多商城中的宝贝的描述了,消费者们在进入到宝贝的详情页面后,可以通过这些描述去了解该款宝贝。其实这样也可以大大的增加商品的转化率。那么它的作用有什么呢?1.突出商品卖点:把商品的特色和突出点写表现出来,很更好的吸引到顾客,让顾客有点击和购买的欲......
  • Net Core Web Api 配置Swagger
    一、创建NETCoreAPI项目NETCore版本:NETCore2.21.创建coreweb应用程序2.选择API3.下图为生成后的项目二、安装Swagger1.打开NuGet包管理器2.搜索Swashbuckle.AspNetCore安装在项目上3.点击我接受三、配置Swagger对于ConfigureServices和Configure的配置点击可参考1.ConfigureS......
  • Springboot 系列 (30) - Springboot+HBase 大数据存储(八)| Springboot Client/Server
    Kerberos(SecureNetworkAuthenticationSystem,网络安全认证系统),是一种网络认证协议,其设计目标是通过密钥系统为Client/Server提供强大的认证服务。该认证过程的实现不依赖于主机操作系统的认证,无需基于的信任,不要求网络上所有主机的物理安全,并假定网络上传送的数据包可以被......