From dead6b4197ebae6ead51e01b07188ede333b475c Mon Sep 17 00:00:00 2001 From: lTimej Date: Fri, 5 Jul 2024 11:35:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=B3=BB=E7=BB=9F=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E7=8A=B6=E6=80=81=E8=87=AA=E7=9B=91=E6=8E=A7?= =?UTF-8?q?=E8=83=BD=E5=8A=9B=E6=94=AF=E6=8C=81=20#7457?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gcloud/contrib/monitor/urls.py | 2 ++ gcloud/contrib/monitor/views.py | 45 +++++++++++++++++++++++++++++++++ requirements.txt | 2 ++ 3 files changed, 49 insertions(+) diff --git a/gcloud/contrib/monitor/urls.py b/gcloud/contrib/monitor/urls.py index 4ebebef3fe..493ba34bf3 100644 --- a/gcloud/contrib/monitor/urls.py +++ b/gcloud/contrib/monitor/urls.py @@ -19,4 +19,6 @@ url(r"^get_failed_task/$", views.get_failed_task), url(r"^get_executing_task/$", views.get_executing_task), url(r"^get_schedule_times/$", views.get_schedule_times), + url(r"^get_mq_overview/$", views.get_mq_overview), + url(r"^get_mq_data/$", views.get_mq_data), ] diff --git a/gcloud/contrib/monitor/views.py b/gcloud/contrib/monitor/views.py index 06f5379dc8..66d3d7fb63 100644 --- a/gcloud/contrib/monitor/views.py +++ b/gcloud/contrib/monitor/views.py @@ -11,6 +11,7 @@ specific language governing permissions and limitations under the License. """ +import pyrabbit2 from django.db.models import Q from django.http import JsonResponse from django.utils import timezone @@ -151,3 +152,47 @@ def get_schedule_times(request): for task in tasks ] return JsonResponse({"result": True, "data": schedule_times}) + + +@require_GET +@iam_intercept(StatisticsViewInpterceptor()) +def get_mq_overview(request): + """ + 获取mq总览 + """ + data = {} + cl = pyrabbit2.Client("localhost:15672", "guest", "guest") + overview = cl.get_overview() + data = { + "totals": { + "ready": overview["queue_totals"]["messages_ready"], + "unacked": overview["queue_totals"]["messages_unacknowledged"], + "total": overview["queue_totals"]["messages"], + }, + "global_totals": overview["object_totals"], + "nodes": cl.get_nodes(), + } + return JsonResponse({"result": True, "data": data}) + + +@require_GET +@iam_intercept(StatisticsViewInpterceptor()) +def get_mq_data(request): + """ + 获取mq数据 + """ + cl = pyrabbit2.Client("localhost:15672", "guest", "guest") + data = { + vhost: [ + { + "vhost": vhost, + "queue_name": queue["name"], + "message_count": queue["messages"], + "queue_state": queue["state"], + "messages": cl.get_messages(vhost, queue["name"], count=queue["messages"], requeue=True), + } + for queue in cl.get_queues(vhost=vhost) + ] + for vhost in cl.get_vhost_names() + } + return JsonResponse({"result": True, "data": data}) diff --git a/requirements.txt b/requirements.txt index 7f482bc6eb..47a0f41419 100644 --- a/requirements.txt +++ b/requirements.txt @@ -36,6 +36,8 @@ bkstorages==1.0.1 ujson==4.1.0 django-dbconn-retry==0.1.5 pydantic==1.9.1 +pyrabbit2==1.0.7 + # monitor django-prometheus==2.1.0