當我們線上上使用了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="activemq_mointer.log",
filemode="a",
format="%(asctime)s %(name)s:%(levelname)s:%(message)s",
datefmt="%Y-%m-%d %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")
使用該指令碼注意事項:
傳入引數只能一個 ,而且只能是
pending
,pending_lists
,queue_sum
,分別代表阻塞訊息數、阻塞訊息佇列名稱、總的訊息數。指令碼有日誌記錄和異常記錄,注意設定 日誌檔案路徑,假設指令碼路徑位於
/opt/scripts/
,我們在該目錄下進行執行指令碼的話,activemq_mointer.log
日誌檔案也就會產生在當前目錄下。我們可以在路徑中通過相對路徑來指定資料夾 形如../..//var/log/activemq_mointer.log
該指令碼是由
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
二 、設定 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)}>=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 的監控項都已經配置好了。