Zabbix监控ActiveMQ

news2024/9/24 7:20:45

当我们在线上使用了ActiveMQ 后,我们需要对一些参数进行监控,比如 消息是否有阻塞,哪个消息队列阻塞了,总的消息数是多少等等。下面我们就通过 Zabbix 结合 Python 脚本来实现对 ActiveMQ的监控。

一、创建 Activemq Python 监控脚本

因为 CentOS 系统默认安装的是 Python2.7,为了避免麻烦,我们这里的脚本也是对应的 Python2

Python2 监控脚本

# -*- coding: utf-8 -*-
# @Time    : 2019/6/25 9:26
# @Author  : djx
# @Email   : djxlsp@163.com
# @File    : mointer_mq_python2.py
# @Software: PyCharm
# @Python_version: python2.7

import base64
import urllib2
import json
import logging
import sys


def activemq_mointer(userinfo_encode):
    # 总的消息阻塞数
    pending_queue_sum = 0
    # 阻塞消息的队列名称
    pending_queue_lists = ''
    # 总的消息数
    mq_sum = 0
    headers = {
        'Authorization': 'Basic {}'.format(userinfo_encode),
        'ua': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36'
    }
    url = 'http://' + ip + ':' + port + \
          '/api/jolokia/read/org.apache.activemq:type=Broker,brokerName=localhost/Queues/'
    request = urllib2.Request(url=url, headers=headers)
    try:
        response = urllib2.urlopen(request)
    except Exception as e:
        logging.error(e)
        return {'pending_queue_sum': 110, 'pending_queue_lists': '110', 'mq_sum': 0}  # 当服务不可用时,返回预警数字,用于预警。
    activemq_info = response.read()
    activemq_info_json = json.loads(activemq_info)
    activemq_queues = activemq_info_json['value']
    for i in activemq_queues:
        queue_url = 'http://' + ip + ':' + port + \
            '/api/jolokia/read/' + i['objectName']
        queue_request = urllib2.Request(url=queue_url, headers=headers)
        try:
            queue_response = urllib2.urlopen(queue_request)
        except Exception as e:
            logging.error(e)
            return {'pending_queue_sum': 110, 'pending_queue_lists': '110', 'mq_sum': 0}
        queue_info = queue_response.read()
        info_dict = json.loads(queue_info)
        mq_sum += info_dict['value']['EnqueueCount']
        if int(info_dict['value']['QueueSize']
               ) > 0:  # 取值 QueueSize ,就是未消费的消息数量
            pending_queue_sum += info_dict['value']['QueueSize']
            pending_queue_lists += info_dict['value']['Name']
            pending_queue_lists += ' and '
            logging.info(
                "消息队列--{}--有阻塞消息--{} 条".format(
                    info_dict['value']['Name'],
                    info_dict['value']['QueueSize']))
    return {'pending_queue_sum': pending_queue_sum, 'pending_queue_lists': pending_queue_lists, 'mq_sum': mq_sum}


if __name__ == '__main__':
    # ActiveMQ 服务器信息
    username = 'admin'
    password = 'admin'
    ip = '127.0.0.1'
    port = '8161'
    userinfo = username + ':' + password
    userinfo_encode = base64.b64encode(userinfo.encode('utf8'))
    # 日志配置,注意下面日志文件的路径是采用相对路径的。
    logging.basicConfig(
        filename="/var/log/activemq_mointer.log",
        filemode="a",
        format="%(asctime)s %(name)s:%(levelname)s:%(message)s",
        datefmt="%d-%M-%Y %H:%M:%S",
        level=logging.DEBUG)
    if len(sys.argv) == 2:
        mointer_argv = sys.argv[1]
        if mointer_argv in ('pending', 'pending_lists', 'queue_sum'):
            mq_re = activemq_mointer(userinfo_encode)
            if mointer_argv == 'pending':
                print(mq_re['pending_queue_sum'])
            elif mointer_argv == 'pending_lists':
                print(mq_re['pending_queue_lists'])
            else:
                print(mq_re['mq_sum'])
        else:
            # 错误提示
            print("Please enter the correct parameters pending|pending_lists|queue_sum")
    else:
        # 错误提示
        print("Please enter the correct parameters pending|pending_lists|queue_sum")

使用该脚本注意事项:

  1. 传入参数只能一个 ,而且只能是 pending, pending_lists, queue_sum ,分别代表阻塞消息数、阻塞消息队列名称、总的消息数。

  2. 脚本有日志记录和异常记录,注意设置 日志文件路径,假设脚本路径位于 /opt/scripts/,我们在该目录下进行执行脚本的话,activemq_mointer.log 日志文件也就会产生在当前目录下。我们可以在路径中通过绝对路径来指定文件夹 形如 /var/log/activemq_mointer.log

  3. 该脚本是由 zabbix agent 进行使用 ,所以我们需要设置该 脚本的权限,以及保证该脚本的用户有创建日志文件的权限(或者我们先前创建好对应权限日志文件)

    sudo chown  zabbix:zabbix  mointer_mq_python2.py
    sudo  chmod 744 mointer_mq_python2.py
    sudo  touch /var/log/activemq_mointer.log
    sudo chown  zabbix:zabbix  /var/log/activemq_mointer.log
    

Python3 脚本

# -*- coding: utf-8 -*-
# @Time    : 2019/6/25 9:20
# @Author  : djx
# @Email   : djxlsp@163.com
# @File    : mointer_mq.py.py
# @Software: PyCharm
# @Python_version: python3

import  base64
import  requests
import  logging
import  sys

def activemq_mointer(userinfo_encode):
    # 总的消息阻塞数
    pending_queue_sum = 0
    # 阻塞消息的队列名称
    pending_queue_lists = ''
    # 总的消息数
    mq_sum = 0
    headers = {
        'Authorization': 'Basic {}'.format(str(userinfo_encode,'utf-8')),
        'ua': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36'
    }
    url = 'http://' + ip + ':' + port + '/api/jolokia/read/org.apache.activemq:type=Broker,brokerName=localhost/Queues/'
    try:
        response = requests.get(url=url, headers=headers)
    except Exception as e:
        logging.error(e)
        return {'pending_queue_sum': 110, 'pending_queue_lists': '110', 'mq_sum': 0}  # 当服务不可用时,返回预警数字,用于预警。
    activemq_info = response.json()
    activemq_queues = activemq_info['value']
    for i in activemq_queues:
        queue_url = 'http://' + ip + ':' + port + '/api/jolokia/read/' + i['objectName']
        try:
            queue_info = requests.get(url=queue_url, headers=headers)
        except Exception as e:
            logging.error(e)
            return {'pending_queue_sum': 110, 'pending_queue_lists': '110', 'mq_sum': 0}
        info_dict = queue_info.json()
        mq_sum += info_dict['value']['EnqueueCount']
        if int(info_dict['value']['QueueSize']) > 0:  # 取值 QueueSize ,就是未消费的消息数量
            pending_queue_sum += info_dict['value']['QueueSize']
            pending_queue_lists += info_dict['value']['Name']
            pending_queue_lists += ' and '
            logging.info(
                "Queues--{}--peding msg --{}".format(
                    info_dict['value']['Name'],
                    info_dict['value']['QueueSize']))
    return {'pending_queue_sum': pending_queue_sum, 'pending_queue_lists': pending_queue_lists, 'mq_sum': mq_sum}

if __name__ == '__main__':
    # ActiveMQ 服务器信息
    username = 'admin'
    password = 'admin'
    ip = '127.0.0.1'
    port = '8161'
    userinfo = username + ':' + password
    userinfo_encode = base64.b64encode(userinfo.encode('utf8'))
    # 日志配置
    logging.basicConfig(
        filename="/var/log/activemq_mointer.log",
        filemode="a",
        format="%(asctime)s %(name)s:%(levelname)s:%(message)s",
        datefmt="%Y-%m-%d %H:%M:%S",
        level=logging.INFO)
    if len(sys.argv) == 2:
        mointer_argv = sys.argv[1]
        if mointer_argv in ('pending', 'pending_lists', 'queue_sum'):
            mq_re = activemq_mointer(userinfo_encode)
            if mointer_argv == 'pending':
                print(mq_re['pending_queue_sum'])
            elif mointer_argv == 'pending_lists':
                print(mq_re['pending_queue_lists'])
            else:
                print(mq_re['mq_sum'])
        else:
            # 错误提示
            print("Please enter the correct parameters pending|pending_lists|queue_sum")
    else:
        # 错误提示
        print("Please enter the correct parameters pending|pending_lists|queue_sum")

二 、设置 zabbix agent

设置 zabbix agent

# 将监控项配置写入配置文件
sudo echo "UserParameter=activemq.mointer[*],python /opt/scripts/mointer_mq_python2.py \$1 " >> /opt/zabbix-agent/etc/zabbix_agentd.conf
# 重启zabbix agent
sudo systemctl restart  zabbix-agent 

三、导入监控项:

监控模板 xml 文件。(该监控模板包含三个监控项,一个触发器)

<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
    <version>4.0</version>
    <date>2019-06-26T03:49:47Z</date>
    <groups>
        <group>
            <name>AWS-1688</name>
        </group>
        <group>
            <name>Fy-hbg</name>
        </group>
    </groups>
    <templates>
        <template>
            <template>Template App ActiveMQ</template>
            <name>Template App ActiveMQ</name>
            <description/>
            <groups>
                <group>
                    <name>AWS-1688</name>
                </group>
                <group>
                    <name>Fy-hbg</name>
                </group>
            </groups>
            <applications>
                <application>
                    <name>ActiveMQ</name>
                </application>
            </applications>
            <items>
                <item>
                    <name>activemq pending amount</name>
                    <type>0</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>activemq.mointer[pending]</key>
                    <delay>1m</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units></units>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description/>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>ActiveMQ</name>
                        </application>
                    </applications>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <timeout>3s</timeout>
                    <url/>
                    <query_fields/>
                    <posts/>
                    <status_codes>200</status_codes>
                    <follow_redirects>1</follow_redirects>
                    <post_type>0</post_type>
                    <http_proxy/>
                    <headers/>
                    <retrieve_mode>0</retrieve_mode>
                    <request_method>0</request_method>
                    <output_format>0</output_format>
                    <allow_traps>0</allow_traps>
                    <ssl_cert_file/>
                    <ssl_key_file/>
                    <ssl_key_password/>
                    <verify_peer>0</verify_peer>
                    <verify_host>0</verify_host>
                    <master_item/>
                </item>
                <item>
                    <name>activemq pending queue name</name>
                    <type>0</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>activemq.mointer[pending_lists]</key>
                    <delay>1m</delay>
                    <history>90d</history>
                    <trends>0</trends>
                    <status>0</status>
                    <value_type>1</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description/>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>ActiveMQ</name>
                        </application>
                    </applications>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <timeout>3s</timeout>
                    <url/>
                    <query_fields/>
                    <posts/>
                    <status_codes>200</status_codes>
                    <follow_redirects>1</follow_redirects>
                    <post_type>0</post_type>
                    <http_proxy/>
                    <headers/>
                    <retrieve_mode>0</retrieve_mode>
                    <request_method>0</request_method>
                    <output_format>0</output_format>
                    <allow_traps>0</allow_traps>
                    <ssl_cert_file/>
                    <ssl_key_file/>
                    <ssl_key_password/>
                    <verify_peer>0</verify_peer>
                    <verify_host>0</verify_host>
                    <master_item/>
                </item>
                <item>
                    <name>Total number of  activemq msg</name>
                    <type>0</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>activemq.mointer[queue_sum]</key>
                    <delay>1m</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units></units>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description/>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>ActiveMQ</name>
                        </application>
                    </applications>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <timeout>3s</timeout>
                    <url/>
                    <query_fields/>
                    <posts/>
                    <status_codes>200</status_codes>
                    <follow_redirects>1</follow_redirects>
                    <post_type>0</post_type>
                    <http_proxy/>
                    <headers/>
                    <retrieve_mode>0</retrieve_mode>
                    <request_method>0</request_method>
                    <output_format>0</output_format>
                    <allow_traps>0</allow_traps>
                    <ssl_cert_file/>
                    <ssl_key_file/>
                    <ssl_key_password/>
                    <verify_peer>0</verify_peer>
                    <verify_host>0</verify_host>
                    <master_item/>
                </item>
            </items>
            <discovery_rules/>
            <httptests/>
            <macros/>
            <templates/>
            <screens/>
        </template>
    </templates>
    <triggers>
        <trigger>
            <expression>{Template App ActiveMQ:activemq.mointer[pending].avg(10m)}&gt;=5</expression>
            <recovery_mode>1</recovery_mode>
            <recovery_expression>{Template App ActiveMQ:activemq.mointer[pending].avg(5m)}=0</recovery_expression>
            <name>activemq queue  pending on {HOST.NAME}</name>
            <correlation_mode>0</correlation_mode>
            <correlation_tag/>
            <url/>
            <status>0</status>
            <priority>3</priority>
            <description>activemq 消息发生阻塞,10分钟内平均阻塞消息数超过5条</description>
            <type>0</type>
            <manual_close>0</manual_close>
            <dependencies/>
            <tags/>
        </trigger>
    </triggers>
</zabbix_export>

将该监控模板链接到对应的主机。

我们可以看到我们监控的数据了。

至此,ActiveMQ 的监控项都已经配置好了。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/810732.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

springboot创建并配置环境(四) - 配置扩展属性(下集)

文章目录 一、介绍二、对contributors中的配置属性进行处理三、处理contributors中的配置属性1. 解析配置文件位置和资源2. 加载配置属性 四、确定当前运行环境激活的profile1. 获取附加的addtionalProfiles2. 获取spring.profiles.include定义的配置3. 获取spring.profiles.ac…

互斥量 的初识

Q: 什么是互斥量&#xff1f; A: 在多数情况下&#xff0c;互斥型信号量和二值型信号量非常相似&#xff0c;但是从功能上二值型信号量用于同步&#xff0c; 而互斥型信号量用于资源保护。 互斥型信号量和二值型信号量还有一个最大的区别&#xff0c;互斥型信号量可以有效解决…

CAD .NET 15.0 企业版 Crack

CAD .NET 15.0 企业版 企业版 企业版 企业版 企业版 Updated: June 14, 2023 | Version 15.0 NEW CAD .NET is a library for developing solutions in .NET environment. It supports AutoCAD DWG/ DXF, PLT and other CAD formats. The library can be used in a wide rang…

SpringBoot百货超市商城系统 附带详细运行指导视频

文章目录 一、项目演示二、项目介绍三、运行截图四、主要代码 一、项目演示 项目演示地址&#xff1a; 视频地址 二、项目介绍 项目描述&#xff1a;这是一个基于SpringBoot框架开发的百货超市系统。首先&#xff0c;这是一个很适合SpringBoot初学者学习的项目&#xff0c;代…

在linux上面部署activemq

1、下载 网址&#xff1a;ActiveMQ 注意&#xff1a;新版本5.17起 要求jdk11, 5.16兼容jdk8, 所以&#xff0c;确保已经安装 java11 或以上的版本 这里安装较新版&#xff1a;5.18.2&#xff0c;已经安装了java17 如何安装jdk17,请详见我的另一篇文章&#xff1a;linux…

mermaid使用记录

记录mermaid可使用到场景&#xff0c;部分关键使用过程备忘 markdown idea开启mermaid预览 grafana 参考插件 Diagram https://grafana.com/grafana/plugins/jdbranham-diagram-panel/ 结合监控数据&#xff0c;可以展示某个处理流程中&#xff0c;各个中间环节的处理指标及…

巧用NGINX配置解决跨域问题

页面nginx配置 1&#xff0c;前端页面放在域名根目录&#xff0c;比如&#xff0c;http://www.xuecheng.com/ &#xff0c;对应的nginx配置&#xff1a; #门户location / {alias D:/Z_lhy/SpringCloud/xuecheng_online/www/xc-ui-pc-static-portal/;index index.html;} 页…

VMPWN的入门系列-2

温馨提示&#xff1a; 文章有点长&#xff0c;图片比较多&#xff0c;请耐心阅读 实验四 VMPWN4 题目简介 这道题应该算是虚拟机保护的一个变种&#xff0c;是一个解释器类型的程序&#xff0c;何为解释器&#xff1f;解释器是一种计算机程序&#xff0c;用于解释和执行源代码。…

quarkus核心编程笔记

此篇只做总结&#xff0c;有大佬做的更详细 大佬quarkus笔记 依赖注入 在应用中&#xff0c;一个接口有多个实现是很常见的&#xff0c;那么依赖注入时&#xff0c;如果类型是接口&#xff0c;如何准确选择实现呢&#xff1f; 修饰符匹配Named注解属性匹配根据优先级选择写…

小红书推广 方法总结

大家好&#xff0c;我是网媒智星&#xff0c;今天跟大家分享一下小红书的推广方法和经验。 一、平台简介 1、什么是小红书&#xff1f; 小红书是一个消费决策/生活方式平台&#xff0c;用户可以通过图片、文案、视频等方式分享美好生活。 2、用户画像 - 2亿月活跃…

better scoll的使用以及注意事项以及左联右

下载better scoll的核心 在你要使用的页面引入 在data里面定义一个对象 然后在createad里面放一个nexttick异步操作。 上面是获取这个left-box节点是父节点 记住里面只能有一个子节点如果循环了 就要再包一个div就是一个子节点 左联右 首先也要获取 右边的 父节点 然后配…

RPC与REST有什么区别?

背景 好多开发的同学在工作中&#xff0c;经常分不清RPC和REST的区别&#xff0c;导致经常沟通不在一个层次上。甚至有些同学把这两个当成同一个东西。 RPC与REST的区别&#xff1f; 对比名称rpcrest备注架构风格RPC是基于过程调用的架构风格&#xff0c;它将远程方法调用封装为…

深度学习技巧应用24-深度学习手撕代码与训练流程的联系记忆方法

大家好,我是微学AI,今天给大家介绍一下深度学习技巧应用24-深度学习手撕代码与训练流程的联系记忆方法,大家都知道深度学习模型训练过程是个复杂的过程,这个过程包括数据的收集,数据的处理,模型的搭建,优化器的选择,损失函数的选择,模型训练,模型评估等步骤,其中缺少…

gitee使用参考

Git代码托管服务 2.1 常用的Git代码托管服务 gitHub&#xff08; 地址&#xff1a;https://github.com/ &#xff09;是一个面向开源及私有软件项目的托管平台&#xff0c;因为只支持Git 作为唯一的版本库格式进行托管&#xff0c;故名gitHub码云&#xff08;地址&#xff1a;…

shell脚本:数据库的分库分表

#!/bin/bash ######################### #File name:db_fen.sh #Version:v1.0 #Email:admintest.com #Created time:2023-07-29 09:18:52 #Description: ########################## MySQL连接信息 db_user"root" db_password"RedHat123" db_cmd"-u${…

ROS暑期学校分享-2023

云课的优势 https://gitcode.net/ZhangRelay/cocubesim 网络编程和单机编程 网络编程和单机编程是两种不同的编程方式&#xff0c;它们的主要区别在于其应用场景和实现技术上。 1 应用场景 网络编程主要用于构建基于互联网的应用程序&#xff0c;例如Web应用程序、网上购物…

安装typora

1、下载压缩包 链接&#xff1a;https://pan.baidu.com/s/1nFvk3hAyXNbvKPJnu9ipIA 提取码&#xff1a;sdyy 2、安装typora 3、打开Crack 4、将这个dll文件复制粘贴到typora的安装路径里

Linux--进程的新建状态

新建状态&#xff1a; 操作系统创建了进程的内核数据结构&#xff08;task_struct、mm_struct、页表&#xff09;&#xff0c;但是页表没有创建映射关系&#xff0c;而且磁盘里的程序的代码和数据未加载到物理内存

Spring注解系列——@PropertySource

在Spring框架中PropertySource注解是非常常用的一个注解&#xff0c;其主要作用是将外部化配置解析成key-value键值对"存入"Spring容器的Environment环境中&#xff0c;以便在Spring应用中可以通过Value或者占位符${key}的形式来使用这些配置。 使用案列 // Propert…

React 路由使用-详细介绍

路由初使用 抽象路由模块 src\page\Article\index.js const Article () > {return (<div><p>文章页</p></div>); };export default Article;src\router\index.js // 导入页面 import Article from "../page/Article"; import Login fr…