Prometheus

Prometheus是一个监控平台,通过抓取目标上和metric相关的HTTP endpoint,收集被监控目标的metrics。本文会指导你怎么安装、配置prometheus,并通过prometheus监控资源。你将会下载、安装、运行prometheus;下载、安装exporter or tools(输出主机或服务的时间序列数据)。我们介绍的第一个exporter是Node Exporter(输出主机级别的metrics,比如CPU、内存、磁盘)。

下载Prometheus

下载使用你平台的最新版本的Prometheus,然后解压: 

curl -LO https://github.com/prometheus/prometheus/releases/download/v2.0.0/prometheus-2.0.0.linux-amd64.tar.gz
tar xvfz prometheus-*.tar.gz
cd prometheus-*

  

prometheus服务是一个单一的二进制文件。我们运行这个二进制文件,通过可选择项--help寻求帮助

./prometheus --help
usage: prometheus [<flags>]

The Prometheus monitoring server

. . .

  

配置Prometheus

prometheus配置文件是yaml文件。prometheus安装包里面有一个简单的配置文件模版叫prometheus.yml,我们从这里开始。

为了直观,下面的prometheus.yml内容省去了大多注释的内容(通过#可以增加注释)。

global:
  scrape_interval:     15s
  evaluation_interval: 15s

rule_files:
  # - "first.rules"
  # - "second.rules"

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ['localhost:9090']
 

  

在上述配置文件中有三个区块:globalrule_files and scrape_configs

global控制prometheus服务的全局配置。当前配置文件配置了两项。scrape_interval控制prometheus抓取目标metrics的频率(每15s采集一次目标metrics),当然对个别的目标可以覆盖该全局配置。evaluation_interval控制评估规则的频率,prometheus使用规则产生新的时间序列数据或者产生警报。

rule_files块制定了规则所在的位置,prometheus可以根据这个配置加载规则,当前我们还没有配置任何规则。

scrape_configs控制prometheus监控哪些资源。由于prometheus通过HTTP endpoint暴露的它本身的监控数据,prometheus也能够监控本身的健康情况。在默认的配置里有一个单独的job,叫做prometheus,它采集prometheus服务本身输出的时间序列数据。这个job包含了一个单独的、静态配置的目标:在端口9090上的localhost。prometheus默认会通过目标的/metrics路径采集metrics。所以,默认的job通过URL:http://localhost:9090/metrics采集metrics。收集到的时间序列会详述prometheus服务的状态和性能。

For a complete specification of configuration options, see the configuration documentation.

eg: 

global:
  evaluation_interval: 10s
  scrape_interval: 10s
  scrape_timeout: 5s
rule_files:
- /usr/local/zstack/prometheus/rules/hosts/collectd.rule
scrape_configs:
- file_sd_configs:
  - files:
    - /usr/local/zstack/prometheus/discovery/hosts/*.json
    refresh_interval: 10s
  job_name: collectd
  scrape_interval: 10s
  scrape_timeout: 5s

  

开始运行Prometheus

使用我们的配置文件运行prometheus,cd到prometheus目录,运行prometheus:

./prometheus --config.file=prometheus.yml
运行prometheus之后,你可以通过http://localhost:9090访问prometheus的状态页面。你也可以通过本地访问metrics endpoint:http://localhost:9090/metrics确认prometheus在工作,并在产生它自己的metrics。

使用表达式浏览器

我们首先展示prometheus自己采集自己的数据,prometheus自带展示界面。打开浏览器,访问http://localhost:9090/graph,点击Console按钮

其中一个prometheus输出的metric是http_requests_total(prometheus服务接收的http请求总数)。在浏览器输入http_requests_total,会返回许多不同的时间序列(每个序列按照时间排序)。所有的序列有着同一个metric namehttp_requests_total,不同的标签;这些标签标明不同类型的请求。如果我们仅仅对状态码200的请求感兴趣,可以使用下面的查询表达式查询相应的信息:

http_requests_total{code="200"}

为了计算序列的数量,可以使用如下查询表达式:

count(http_requests_total)

  

For more about the expression language, see the expression language documentation.

使用绘图界面

访问http://localhost:9090/graph,点击Graph按钮。

输入如下表达式,绘制每秒HTTP请求率:

rare(http_requests_total[1m])

  

安装Node Exporter

收集本身的metrics不能很好的表现Prometheus的能力。所以,接下来我们使用Node Exporter监控我们第一个资源Linux Host,这个样例是监控prometheus所在的host,不过你可以监控任意一个host(只要prometheus能够访问)。你也可以使用VMI Exporter采集windows主机。

Download the latest release of the Node Exporter of Prometheus for your platform, then extract it:

curl -LO https://github.com/prometheus/node_exporter/releases/download/v0.15.2/node_exporter-0.15.2.linux-amd64.tar.gz
tar xvfz node_exporter-*.tar.gz
cd node_exporter-*

  

 Node Exporter是一个单独的二进制文件,node_exporter,有一组可配置的收集器用于手机各种基于主机的metrics。默认,收集器会收集cpu、内存、磁盘和其它metrics,暴露,使prometheus能够抓取监控数据。
启动Node Exporter:

./node_exporter

  

Node Exporter的metrics可以通过访问9100端口的/metrics路径获取。本例中,访问地址:http://localhost:9100/metrics

现在我们需要配置Prometheus,使Prometheus能够发现这个exporter。

配置prometheus监控Host

 我们将会配置prometheus抓取新的目标metrics。我们需要在prometheus.yml配置的scrape_configs区增加一个job。

- job_name: node
  static_configs:
    - targets: ['localhost:9100']

  

我们新的job叫做“node”。它抓取一个静态的目标,在端口9100上的loclahost。你可以用hostname或者IP地址替换localhost。

现在我们需要重新加载Prometheus配置来激活这个新的job。reload配置有两种方式:

  • send SIGHUP signal
kill -HUP <pid>

  

  • send a HTTP POST to the Prometheus web server
使用API来reload prometheus需要在启动prometheus时,开启web.enable-lifecycle配置参数
--web.enable-lifecycle Enable shutdown and reload via HTTP request.
/prometheus --config.file=prometheus.yml --web.enable-lifecycle

  

curl -X POST http://localhost:9090/-/reload

  

访问prometheus,在“Execute”按钮旁边有一个下拉框,在下拉框中可以看到prometheus采集的指标列表。在这个列表中能够看到以node_开头metrics,这些metrics就是Node Exporter收集的。比如通过node_cpu查看节点CPU使用率。

一个很有用的metric是up metric。up metric能够标识目标的状态。如果值是1,那么能够成功的从目标抓取metrics;如果值是0,那么则标明从该目标抓取metrics失败。在一定程度上能够通过改指标判断目标的状态。当前只能看到两个up metric,一个是关于Node Exporter的,一个是关于prometheus的。

总结

本文简单介绍了如何安装、配置prometheus来监控资源信息。同时,也安装了第一个exporter,介绍了时间序列的基本查询方法。
You can find more documentation and guides to help you continue to learn more about Prometheus.


杂记: 
 

Storage

自身包含了一个基于本地磁盘的时间序列数据库,但是也是支持其他远程的存储系统。

Local storage
  • --storage.tsdb.path 数据库位置,默认 data/
  • --storage.tsdb.retention 数据保留时间,默认15d
     sudo /usr/local/zstack/apache-tomcat/webapps/zstack/WEB-INF/classes/tools/prometheus -config.file /usr/local/zstack/prometheus/conf.yaml -storage.local.path /var/lib/zstack/prometheus/data -storage.local.retention 720h0m0s -query.timeout 2m0s -web.listen-address 192.168.211.5:9090 -query.max-concurrency 20 -alertmanager.url http://192.168.211.5:8080/zstack/prometheus
    

     命令行启动存储位置,存储天数以及报警位置等

猜你喜欢

转载自www.cnblogs.com/zqyanywn/p/9828782.html