惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
T
Tenable Blog
Webroot Blog
Webroot Blog
L
Lohrmann on Cybersecurity
S
Securelist
S
Schneier on Security
NISL@THU
NISL@THU
Know Your Adversary
Know Your Adversary
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
L
LINUX DO - 热门话题
C
CXSECURITY Database RSS Feed - CXSecurity.com
O
OpenAI News
I
Intezer
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
TaoSecurity Blog
TaoSecurity Blog
S
Secure Thoughts
Application and Cybersecurity Blog
Application and Cybersecurity Blog
P
Privacy International News Feed
H
Hacker News: Front Page
N
Netflix TechBlog - Medium
M
MIT News - Artificial intelligence
博客园 - Franky
PCI Perspectives
PCI Perspectives
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Azure Blog
Microsoft Azure Blog
MongoDB | Blog
MongoDB | Blog
L
LangChain Blog
P
Proofpoint News Feed
S
Security Affairs
WordPress大学
WordPress大学
The Last Watchdog
The Last Watchdog
S
SegmentFault 最新的问题
小众软件
小众软件
F
Full Disclosure
博客园 - 叶小钗
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
The Blog of Author Tim Ferriss
Simon Willison's Weblog
Simon Willison's Weblog
P
Palo Alto Networks Blog
Security Latest
Security Latest
P
Proofpoint News Feed
月光博客
月光博客
T
Tailwind CSS Blog
Scott Helme
Scott Helme
Hacker News - Newest:
Hacker News - Newest: "LLM"
Google Online Security Blog
Google Online Security Blog
T
Threat Research - Cisco Blogs
Help Net Security
Help Net Security
Project Zero
Project Zero

博客园 - py哥

rabbitmq集群docker部署 MongoDB副本集docker部署 MySQL8.0单实例部署 Redis管理平台 k8s网络与本地开发环境网络互通方案 CentOS7 FTP结合ssl/tls实现加密通信安装与配置 数据库运维平台 基于Docker构建Jenkins CI平台 KeepLived + nginx 高可用 k8s-1.16 二进制安装 Ansible自动化部署K8S集群 Kubernetes1.16下部署Prometheus+node-exporter+Grafana+AlertManager 监控系统 在Kubernetes下部署Prometheus docker部署coredns kubeadm部署多master节点高可用k8s1.16.2 二进制搭建一个完整的K8S集群部署文档 kubeadm部署k8s集群 Keepalived+LVS+nginx搭建nginx高可用集群 centos7 dns(bind)安装配置
prometheus 监控 eureka 里面的服务状态
py哥 · 2025-03-10 · via 博客园 - py哥

1.用python写个eureka_exporter.py

pip install flask requests prometheus_client
from flask import Flask, Response
import requests
from prometheus_client import Gauge, generate_latest, REGISTRY
import prometheus_client
from prometheus_client import Counter, Gauge, generate_latest
from prometheus_client.core import CollectorRegistry

app = Flask(__name__)



# 设置 metrics
registry = CollectorRegistry(auto_describe=False)
downgrade = Gauge('eureka', 'eureka', ['instanceId','hostName', 'app', 'status', 'port'], registry=registry)

@app.route("/metrics")
def metrics():
    downgrade.clear()
    # 假设 Eureka 地址为 http://localhost:8761/eureka/apps/
    eureka_url = "http://10.20.11.138:9500/eureka/apps/"

    up_count = 0

    try:
        response = requests.get(eureka_url,headers = {"Content-Type": "application/json", "Accept": "application/json"})

        if response.status_code == 200:
            apps = response.json()['applications']['application']

            # 遍历所有应用并设置其状态
            for app in apps:
                app_name = app['name']
                for instance in app['instance']:
                    status = instance['status']
                    # print(instance)
                    if status == 'UP':
                        downgrade.labels(instanceId=instance['instanceId'], hostName=instance['hostName'], app=app_name, status=status, port=instance['port']['$']).set(1)
                    else:
                        downgrade.labels(instanceId=instance['instanceId'], hostName=instance['hostName'], app=app_name, status=status, port=instance['port']['$']).set(0)




        else:
            print(f"Failed to fetch data from Eureka. Status code: {response.status_code}")
    except Exception as e:
        print(f"Error fetching data from Eureka: {e}")

    metrics_page = prometheus_client.generate_latest(registry)
    return Response(metrics_page, mimetype=prometheus_client.CONTENT_TYPE_LATEST)


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)

2.配置Prometheus:
修改prometheus.yml文件,添加针对Eureka Server的scrape_configs。这里需要指定job_name、scrape_interval、metrics_path以及targets等参数。例如,如果你的Eureka Server运行在10.124.129.42:8000,并且暴露了/metrics端点用于指标收集,那么你的配置可能如下所示:

- job_name: 'eureka_exporter'
  scrape_interval: 10s
  metrics_path: '/metrics'
  static_configs:
    - targets: ['10.124.129.42:8000']

3.告警配置

- name: Eureka_Rules
  rules:

  # Eureka里面的 服务down告警
  - alert: EurekaDown
    expr: eureka==0
    for: 5s  
    labels:
      severity: critical
    annotations:
      summary: "{{ $labels.app }} instance down on {{ $labels.hostName }}"
      description: "{{ $labels.app }} instance {{ $labels.hostName }} has been down for more than 1 minute."
  - alert: 服务数量小于2
    expr: count by (app) (eureka{job="eureka_exporter"}) < 2
    for: 5s  
    labels:
      severity: critical
    annotations:
      summary: "{{ $labels.app }} 数量小于2"
      description: "{{ $labels.app }} 数量小于2 "