(2) The first step (installation and basic use) | Prometheus (Prometheus)

Welcome to Prometheus! Prometheus is a monitoring platform that collects metrics from monitored targets by scraping metrics HTTP endpoints on targets.

This guide will show you how to install, configure and monitor our first resource with Prometheus.

You will download, install and run Prometheus.

You'll also download and install an exporter, tools that expose time-series data on hosts and services.

Our first exporter will be Prometheus itself, which provides various host-level metrics about memory usage, garbage collection, and more.

Download Prometheus

Download the latest version of Prometheus for your platform , then unzip it:

tar xvfz prometheus-*.tar.gz
cd prometheus-*

Prometheus Server is an  executable named prometheus(or ).prometheus.exe

pass --help可以查看命令帮助.

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

The Prometheus monitoring server

Flags:
  -h, --help                     Show context-sensitive help (also try --help-long and --help-man).
      --version                  Show application version.
      --config.file="prometheus.yml"  
                                 Prometheus configuration file path.
      --web.listen-address="0.0.0.0:9090"  
                                 Address to listen on for UI, API, and telemetry.
      --web.config.file=""       [EXPERIMENTAL] Path to configuration file that can enable TLS or authentication.

. . .

Before starting Prometheus, let's configure it.

Configure Prometheus

Prometheus configuration is in YAML format. prometheus.ymlThe example configuration in the file is a good start.

We removed most of the comments in the example file to make it more concise (a comment is a prefixed #line).

global:
  scrape_interval:     15s
  evaluation_interval: 15s

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

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

There are three configuration blocks in the example configuration file: global, rule_files, and scrape_configs.

globalBlocks control the global configuration of the Prometheus server. We have two options. The first, scrape_intervalcontrols how often Prometheus fetches the target. You can override this for individual targets. In this case, the global setting is to fetch every 15 seconds. This evaluation_intervaloption controls how often Prometheus evaluates rules. Prometheus uses rules to create new time series and generate alerts.

rule_filesblock specifies the location of any rules we want the Prometheus server to load. Currently we don't have any rules.

The last block scrape_configscontrols the resources Prometheus monitors. Since Prometheus also exposes data about itself as an HTTP endpoint, it can scrape and monitor its own health. In the default configuration, there is a prometheusjob called that scrapes the time series data exposed by the Prometheus server. The job contains a statically configured target localhost 的端口 9090. Prometheus expects metrics to be available at path /metrics. So this default job is fetched via the URL: http://localhost:9090/metrics .

The returned time series data will detail the status and performance of the Prometheus server.

See  the configuration documentation for a full specification of configuration options .

Start Prometheus

To start Prometheus with our newly created configuration file, change to the directory containing the Prometheus executable and run:

./prometheus --config.file=prometheus.yml

Prometheus should start successfully. You should also be able to browse to the status page about yourself at http://localhost:9090 . Give it about 30 seconds to collect data about itself from its own HTTP metrics endpoint.

You can also verify that Prometheus is serving metrics about itself by visiting its own metrics endpoint: http://localhost:9090/metrics .

Using the expression browser

Let's try to look at some data that Prometheus collects about itself. To use Prometheus' built-in expression browser, navigate to  http://localhost:9090/graph and select the "Table" view in the "Graph" tab.

As you can browse from http://localhost:9090/metrics , one metric that Prometheus exports about itself is called promhttp_metric_handler_requests_total(Total number of requests served by Prometheus Server /metrics). Go ahead and type this into the expression console:

promhttp_metric_handler_requests_total

This should return a number of different time series (and the latest value for each record), all using the metric name  promhttp_metric_handler_requests_totalbut with different labels. These tags specify different request states.

If we're only interested in requests that produced HTTP codes 200, we can use this query to retrieve that information:

promhttp_metric_handler_requests_total{code="200"}

To count the number of time series returned, you can write:

count(promhttp_metric_handler_requests_total)

For more information on Expression Language, see  the Expression Language documentation .

use GUI

To graph expressions, navigate to http://localhost:9090/graph and use the "Graph" tab.

For example, enter the following expression to plot the rate of HTTP requests returning status code 200 per second from crawling Prometheus:

rate(promhttp_metric_handler_requests_total{code="200"}[1m])

You can experiment with graph range parameters and other settings.

Monitor other targets

Collecting metrics from Prometheus alone is not a good representation of Prometheus' capabilities. To get a better idea of ​​what Prometheus can do, we recommend browsing the documentation on other exporters. The Guide to Monitoring Linux or macOS Host Metrics Using Node Exporter is a good place to start.

Summarize

In this guide, you installed Prometheus, configured a Prometheus instance to monitor resources, and learned some basics of working with time-series data in Prometheus' expression browser. To continue learning about Prometheus, check out the overview for some ideas on what to explore next.

Guess you like

Origin blog.csdn.net/u011936655/article/details/124048248