zabbix自动发现服务端口并监控

使用zabbix自动发现功能实现服务端口自动发现及监控

环境:

系统: CentOS 7.6

zabbix server version: 4.0.10

1. 在/etc/zabbix/zabbix_agent.d/下创建userparameter_tcp_port.conf内容如下

  
UserParameter=port.alert,/etc/zabbix/alertscripts/lld_port.py

2. 在/etc/zabbix/alertscripts下创建lld_port.py端口发现脚本

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import json
import socket


def get_host_ip():
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(('114.114.114.114', 80))
        ip = s.getsockname()[0]
    finally:
        s.close()
    return ip

ip = get_host_ip()
os.environ['ip'] = str(ip)

cmd1 = os.popen("""netstat -nlpt | grep -v -w - | grep -v rpc | awk -F "[ :]+" '{if($4 ~ /0.0.0.0/ || $4 ~ /127.0.0.1/ || $4 ~ /'$ip'/) print $5}'""")
cmd2 = os.popen("""netstat -nlpt  | grep -Po ':::\K\d+'""")

ports = []
tmp_ports = []
new_ports = []
for cmd in cmd1,cmd2:
    for port in cmd.readlines():
        tmp_ports.append(port.strip())

for port in tmp_ports:
    if port not in new_ports:
        new_ports.append(port)

for i in new_ports:
    ports += [{'{#PORT}':i}]

print json.dumps({'data': ports}, sort_keys=True, indent=4, separators=(',', ':'))

3. 在zabbix管理页面导入自动发现模板(此模板为主动监控模板)

https://github.com/charnet1019/zabbix/blob/master/port/zbx_active_mode_templates_tcp_port.xml

4. 重启zabbix agent。

5. 主机关联监控模板。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值