?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下簡(jiǎn)單搭建Prometheus+Grafana監(jiān)控系統(tǒng)
1、先官網(wǎng)下載安裝包
https://prometheus.io/download/
1)prometheus-2.17.2.linux-amd64.tar.gz
2)node_exporter-0.18.1.linux-amd64.tar.gz


grafana官網(wǎng)下載
:https://grafana.com/grafana/download
3)https://dl.grafana.com/oss/release/grafana-6.7.3-1.x86_64.rpm

上傳安裝包到CentOS7服務(wù)器上

2、安裝并配置prometheus服務(wù)端
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配置文件語(yǔ)法校驗(yàn)方法
./promtool check config prometheus.yml

先采用默認(rèn)配置啟動(dòng)Prometheus Server

登錄Prometheus Server的Web界面http://192.168.31.80:9090可以看到只有一個(gè)
Targets:http://127.0.0.1:9090/metrics

設(shè)置Prometheus為系統(tǒng)服務(wù),并配置為開(kāi)機(jī)自啟動(dòng)
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

啟動(dòng)參數(shù)說(shuō)明:
--config.file -- 指明prometheus的配置文件路徑
--web.enable-lifecycle -- 指明prometheus配置更改后可以進(jìn)行熱加載
--storage.tsdb.path -- 指明監(jiān)控?cái)?shù)據(jù)存儲(chǔ)路徑
--storage.tsdb.retention --指明數(shù)據(jù)保留時(shí)間
啟動(dòng)服務(wù),并設(shè)置為開(kāi)機(jī)自啟動(dòng)
systemctl daemon-reload
systemctl enable prometheus.service
systemctl start prometheus.service
systemctl status prometheus.service

可以看到已經(jīng)報(bào)錯(cuò)了,服務(wù)未正常啟動(dòng)
排錯(cuò)
/opt/prometheus下data目錄屬主不對(duì)導(dǎo)致
解決辦法:chown -R prometheus:prometheus *

3、安裝node_exporter
比如就在該服務(wù)器上安裝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

創(chuàng)建系統(tǒng)服務(wù)
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

啟動(dòng)node_exporter并設(shè)置為開(kāi)啟自啟動(dòng)
systemctl enable node_exporter.service
systemctl start node_exporter.service
systemctl status node_exporter.service

啟動(dòng)成功,測(cè)試API訪問(wèn)
http://192.168.31.80:9100/metrics
能正常返回?cái)?shù)據(jù)說(shuō)明node_exporter正常

并將其加入prometheus.yml配置文件中
- job_name: 'CentOS7_VMServer'
static_configs:
- targets: ['192.168.31.80:9100']
然后重啟服務(wù)
systemctl restart prometheus.service

這是再登錄prometheus 的Web界面可以看到新增的這個(gè)Target
http://192.168.31.80:9090/targets

4、安裝Grafana圖形化工具包

缺少組件包的情況,使用
yum localinstall grafana-6.7.3-1.x86_64.rpm
systemctl enable grafana-server.service
systemctl start grafana-server.service
systemctl status grafana-server.service

5、配置數(shù)據(jù)源并添加Dashboard
1)、登錄grafana,默認(rèn)端口3000,初始賬號(hào)/密碼:admin/admin
http://192.168.31.80:3000/


2)添加數(shù)據(jù)源

示例:Name CentOS7_VM_Prometheus
URL :http://192.168.31.80:9090

3)添加一個(gè)DashBoard,Import 例如8919 ID這個(gè)




效果如下(點(diǎn)擊圖片查看清晰大圖)

總結(jié):
- Prometheus 屬于一站式監(jiān)控告警平臺(tái),依賴少,功能齊全。
- Prometheus 支持對(duì)云或容器的監(jiān)控,其他系統(tǒng)主要對(duì)主機(jī)監(jiān)控。
- Prometheus 數(shù)據(jù)查詢語(yǔ)句表現(xiàn)力更強(qiáng)大,內(nèi)置更強(qiáng)大的統(tǒng)計(jì)函數(shù)。
- Prometheus 在數(shù)據(jù)存儲(chǔ)擴(kuò)展性以及持久性上沒(méi)有 InfluxDB,OpenTSDB,Sensu 好
Prometheus vs Zabbix
- Zabbix 使用的是 C 和 php, Prometheus 使用 Golang, 整體而言 Prometheus 運(yùn)行速度更快一點(diǎn)。
- Zabbix 屬于傳統(tǒng)主機(jī)監(jiān)控,主要用于物理主機(jī),交換機(jī),網(wǎng)絡(luò)等監(jiān)控,Prometheus 不僅適用主機(jī)監(jiān)控,還適用于 Cloud, SaaS, Openstack,Container 監(jiān)控。
- Zabbix 在傳統(tǒng)主機(jī)監(jiān)控方面,有更豐富的插件。
- Zabbix 可以在 WebGui 中配置很多事情,但是 Prometheus 需要手動(dòng)修改文件配置。