跳转至
首页 解决方案 下载 文档
演示环境

Prometheus API接口

Prometheus的API分为管理API和查询API,管理API主要是Prometheus Server自身的状态和管理操作。查询API,是主要给做二次开发使用的。

管理API

健康检查

GET /-/healthy
[root@linux-node1 ~]# curl -s -u admin:OpsAny@2020 http://192.168.56.11:9090/-/healthy
Prometheus is Healthy.

就绪检查

GET /-/ready
[root@linux-node1 ~]# curl -s -u admin:OpsAny@2020 http://192.168.56.11:9090/-/ready
Prometheus is Ready.

重载

重载是让Prometehus重新加载配置和规则文件。默认该功能是关闭的,需要通过启动参数打开:--web.enable-lifecycle

PUT  /-/reload
POST /-/reload
[root@linux-node1 ~]# curl -X POST -s -u admin:OpsAny@2020 http://192.168.56.11:9090/-/reload
Lifecycle API is not enabled.

查询API

查询API位于/api/v1/下,

Targets(监控目标)

  • 请求地址
GET /api/v1/targets
  • 请求案例
[root@linux-node1 ~]# curl -s -u admin:password http://192.168.56.11:9090/api/v1/targets | python -m json.tool
{
    "data": {
        "activeTargets": [
            {
                "discoveredLabels": {
                    "__address__": "192.168.56.11:9100",
                    "__metrics_path__": "/metrics",
                    "__scheme__": "http",
                    "__scrape_interval__": "15s",
                    "__scrape_timeout__": "10s",
                    "job": "local"
                },
                "globalUrl": "http://192.168.56.11:9100/metrics",
                "health": "up",
                "labels": {
                    "instance": "192.168.56.11:9100",
                    "job": "local"
                },
                "lastError": "",
                "lastScrape": "2022-06-04T16:52:48.970960632+08:00",
                "lastScrapeDuration": 0.0149362,
                "scrapeInterval": "15s",
                "scrapePool": "local",
                "scrapeTimeout": "10s",
                "scrapeUrl": "http://192.168.56.11:9100/metrics"
            },
            {
                "discoveredLabels": {
                    "__address__": "localhost:9090",
                    "__metrics_path__": "/metrics",
                    "__scheme__": "http",
                    "__scrape_interval__": "15s",
                    "__scrape_timeout__": "10s",
                    "job": "prometheus"
                },
                "globalUrl": "http://02def3e91bd9:9090/metrics",
                "health": "up",
                "labels": {
                    "instance": "localhost:9090",
                    "job": "prometheus"
                },
                "lastError": "",
                "lastScrape": "2022-06-04T16:52:50.201297253+08:00",
                "lastScrapeDuration": 0.006224367,
                "scrapeInterval": "15s",
                "scrapePool": "prometheus",
                "scrapeTimeout": "10s",
                "scrapeUrl": "http://localhost:9090/metrics"
            }
        ],
        "droppedTargets": []
    },
    "status": "success"
}
  • 常用参数
state=active, state=dropped, state=any

仅列出正常的监控目标。

[root@linux-node1 ~]# curl -s -u admin:OpsAny@2020 http://192.168.56.11:9090/api/v1/targets?state=active | python -m json.tool
{
    "data": {
        "activeTargets": [
            {
                "discoveredLabels": {
                    "__address__": "192.168.56.11:9100",
                    "__metrics_path__": "/metrics",
                    "__scheme__": "http",
                    "__scrape_interval__": "15s",
                    "__scrape_timeout__": "10s",
                    "job": "local"
                },
                "globalUrl": "http://192.168.56.11:9100/metrics",
                "health": "up",
                "labels": {
                    "instance": "192.168.56.11:9100",
                    "job": "local"
                },
                "lastError": "",
                "lastScrape": "2022-06-04T16:56:18.970617031+08:00",
                "lastScrapeDuration": 0.016422464,
                "scrapeInterval": "15s",
                "scrapePool": "local",
                "scrapeTimeout": "10s",
                "scrapeUrl": "http://192.168.56.11:9100/metrics"
            },
            {
                "discoveredLabels": {
                    "__address__": "localhost:9090",
                    "__metrics_path__": "/metrics",
                    "__scheme__": "http",
                    "__scrape_interval__": "15s",
                    "__scrape_timeout__": "10s",
                    "job": "prometheus"
                },
                "globalUrl": "http://02def3e91bd9:9090/metrics",
                "health": "up",
                "labels": {
                    "instance": "localhost:9090",
                    "job": "prometheus"
                },
                "lastError": "",
                "lastScrape": "2022-06-04T16:56:20.201297253+08:00",
                "lastScrapeDuration": 0.007233097,
                "scrapeInterval": "15s",
                "scrapePool": "prometheus",
                "scrapeTimeout": "10s",
                "scrapeUrl": "http://localhost:9090/metrics"
            }
        ],
        "droppedTargets": []
    },
    "status": "success"
}
Document