?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
啟動node_exporter并設置為開啟自啟動
systemctl enable node_exporter.service
systemctl start node_exporter.service
systemctl status node_exporter.service
啟動成功,測試API訪問
http://192.168.31.80:9100/metrics
能正常返回數據說明node_exporter正常
并將其加入prometheus.yml配置文件中
- job_name: 'CentOS7_VMServer'
static_configs:
- targets: ['192.168.31.80:9100']
然后重啟服務
systemctl restart prometheus.service
這是再登錄prometheus 的Web界面可以看到新增的這個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、配置數據源并添加Dashboard
1)、登錄grafana,默認端口3000,初始賬號/密碼:admin/admin
http://192.168.31.80:3000/
2)添加數據源
示例:Name CentOS7_VM_Prometheus
URL :http://192.168.31.80:9090
3)添加一個DashBoard,Import 例如8919 ID這個
效果如下(點擊圖片查看清晰大圖)
總結:
- Prometheus 屬于一站式監控告警平臺,依賴少,功能齊全。
- Prometheus 支持對云或容器的監控,其他系統主要對主機監控。
- Prometheus 數據查詢語句表現力更強大,內置更強大的統計函數。
- Prometheus 在數據存儲擴展性以及持久性上沒有 InfluxDB,OpenTSDB,Sensu 好
Prometheus vs Zabbix
- Zabbix 使用的是 C 和 php, Prometheus 使用 Golang, 整體而言 Prometheus 運行速度更快一點。
- Zabbix 屬于傳統主機監控,主要用于物理主機,交換機,網絡等監控,Prometheus 不僅適用主機監控,還適用于 Cloud, SaaS, Openstack,Container 監控。
- Zabbix 在傳統主機監控方面,有更豐富的插件。
- Zabbix 可以在 WebGui 中配置很多事情,但是 Prometheus 需要手動修改文件配置。