
Prometheus is an open-source systems monitoring and alerting toolkit originally built at SoundCloud.
1、Features
Prometheus's main features are:
a multi-dimensional data model with time series data identified by metric name and key/value pairs
PromQL, a flexible query language to leverage this dimensionality
no reliance on distributed storage; single server nodes are autonomous
time series collection happens via a pull model over HTTP
pushing time series is supported via an intermediary gateway
targets are discovered via service discovery or static configuration
multiple modes of graphing and dashboarding support
2、Components
The Prometheus ecosystem consists of multiple components, many of which are optional:
the main Prometheus server which scrapes and stores time series data
client libraries for instrumenting application code
a push gateway for supporting short-lived jobs
special-purpose exporters for services like HAProxy, StatsD, Graphite, etc.
an alertmanager to handle alerts
various support tools
Most Prometheus components are written in Go, making them easy to build and deploy as static binaries.
3、Architecture
This diagram illustrates the architecture of Prometheus and some of its ecosystem components:

Prometheus scrapes metrics from instrumented jobs, either directly or via an intermediary push gateway for short-lived jobs. It stores all scraped samples locally and runs rules over this data to either aggregate and record new time series from existing data or generate alerts. Grafana or other API consumers can be used to visualize the collected data.
下面介绍CentOS7下简单搭建Prometheus+Grafana监控系统
1、先官网下载安装包
https://prometheus.io/download/
1)prometheus-2.17.2.linux-amd64.tar.gz
2)node_exporter-0.18.1.linux-amd64.tar.gz

grafana官网下载:https://grafana.com/grafana/download
3)https://dl.grafana.com/oss/release/grafana-6.7.3-1.x86_64.rpm

上传安装包到CentOS7服务器上

2、安装并配置prometheus服务端
groupadd prometheus
useradd -g prometheus -m -d /opt/prometheus/ -s /sbin/nologin prometheus
tar -zxf /data/prometheus-2.17.2.linux-amd64.tar.gz -C /opt
cd /opt/
mv prometheus-2.17.2.linux-amd64/* prometheus
cd prometheus
chown -R prometheus *

prometheus配置文件语法校验方法
./promtool check config prometheus.yml

先采用默认配置启动Prometheus Server

登录Prometheus Server的Web界面http://192.168.31.80:9090可以看到只有一个Targets:http://127.0.0.1:9090/metrics

设置Prometheus为系统服务,并配置为开机自启动
touch /usr/lib/systemd/system/prometheus.service
chown prometheus:prometheus /usr/lib/systemd/system/prometheus.service
vi /usr/lib/systemd/system/prometheus.service
并加入如下配置
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml --web.enable-lifecycle --storage.tsdb.path=/opt/prometheus/data --storage.tsdb.retention=60d
Restart=on-failure
[Install]
WantedBy=multi-user.target

启动参数说明:
--config.file -- 指明prometheus的配置文件路径
--web.enable-lifecycle -- 指明prometheus配置更改后可以进行热加载
--storage.tsdb.path -- 指明监控数据存储路径
--storage.tsdb.retention --指明数据保留时间
启动服务,并设置为开机自启动
systemctl daemon-reload
systemctl enable prometheus.service
systemctl start prometheus.service
systemctl status prometheus.service

可以看到已经报错了,服务未正常启动
排错
/opt/prometheus下data目录属主不对导致
解决办法:chown -R prometheus:prometheus *

3、安装node_exporter
比如就在该服务器上安装node_exporter
cd /data
tar -zxf node_exporter-0.18.1.linux-amd64.tar.gz -C /usr/local/
cd /usr/local/
mv node_exporter-0.18.1.linux-amd64 node_exporter
cd node_exporter/
ll -trh

创建系统服务
touch /usr/lib/systemd/system/node_exporter.service
chown prometheus:prometheus /usr/lib/systemd/system/node_exporter.service
chown -R prometheus:prometheus /usr/local/node_exporter*
vi /usr/lib/systemd/system/node_exporter.service
加入如下行
[Unit]
Description=node_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
