Prometheus初步搭建和配置(Windows)

时间:2025-05-13 07:37:27

官网:Overview | Prometheus

启动:

执行,Prometheus启动时增加---lifecycle;可以使用 http://localhost:9090/-/reload 来重新加载;

默认地址为:http://localhost:9090/graph  地址http://localhost:9090/targets可查看当前节点状态

启动:

执行默认地址为:http://localhost:3000/

3.节点启动:

不同exporter提供不同的监听服务window系统可使用windows_exporter-0.17.;

相关下载地址:Download | Prometheus

4.配置Prometheus服务发现:

Prometheus会通过读取配置文件,来确定其服务发现机制,可同时配置多种服务发现,通过配置服务发现,可以使得再不重启Prometheus服务的情况下更新exporter。

配置文件示例:

global:
  scrape_interval:     15s
  evaluation_interval: 15s

rule_files:
  # - ""
  # - ""

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ['localhost:9090']
  - job_name: "node"
    file_sd_configs:
    - refresh_interval: 1m
      files: 
      - "D:\\Prometheus\\prometheus-2.32.-amd64\\nodeyml\\node*.json"
  - job_name: 'http-other'
    http_sd_configs:
     - url: http://localhost:8080/hosts

  (1)基于网络的服务发现:可以发现通过向consul中特定服务加入节点, 就可以在prometheus动态变动采集的实例地址。(需要额外部署consul)
consul启动命令:consul agent -dev -ui -node=cy

(2)基于文件的服务发现提供了一种更通用的方式来配置静态目标并用作插入自定义服务发现机制的接口。
它读取一组包含零个或多个 <static_config>s 列表的文件。通过磁盘监视检测所有定义文件的更改并立即应用。文件可能以 YAML 或 JSON 格式提供。仅应用导致结构良好的目标组的更改。
文件必须包含静态配置列表,使用以下格式:
JSON json [ { "targets": [ "<host>", ... ], "labels": { "<labelname>": "<labelvalue>", ... } }, ... ]
YAML yaml - targets: [ - '<host>' ] labels: [ <labelname>: <labelvalue> ... ]
作为回退,文件内容也会以指定的刷新间隔定期重新读取。
每个目标__meta_filepath在 重新标记阶段都有一个元标签。它的值设置为从中提取目标的文件路径。
有一个 与此发现机制的集成列表。
# Patterns for files from which target groups are extracted.
files:
  [ - <filename_pattern> ... ]
# Refresh interval to re-read the files.
[ refresh_interval: <duration> | default = 5m ]
其中<filename_pattern>可能是以.json,.yml或结尾的路径.yaml。最后一个路径段可能包含一个*匹配任何字符序列的单个,例如my/path/tg_*.json.

(3) 基于http的服务发现:
配置文件更改,使用url返回:
  - job_name: 'http-other'
    http_sd_configs:
     - url: http://my:8080/api/hosts

示例:

    /**
     * 返回host内容
     * Prometheus使用基于http的服务发现时,会持续访问此接口
     *
     * @return json
     */
    @RequestMapping("/hosts")
    @ResponseBody
    public String getHost() {
        StringBuilder jsonstr = new StringBuilder();
        ("[");
        ResultTO<List<Node>> listResultTO = ();
        if (()) {
            List<Node> nodeList = ();
            for (int i = 0; i < (); i++) {
                Node node = (i);
                ("{");
                ("\"targets\": [\"" + () + ":" + () + "\"],");
                (" \"labels\": {");
                (" \"id\": \"" + () + "\",");
                //判断是主机还是数据库
                if (null == () || ().isEmpty()) {
                    (" \"job\": \"os\"");
                } else {
                    (" \"job\": \"db\"");
                }
                ("}");
                ("}");
                if (i != () - 1) {
                    (",");
                }
            }
        }
        ("]");
        (());
        return ();
    }

其他服务发现方式可查看官网,Promethues配置:
Configuration | Prometheus