原理就不多說了,直接上操作過程吧
Prometheus Push Gateway 參考: https://github.com/prometheus/pushgateway
Prometheus Server 參考: https://github.com/prometheus/prometheus#install
Grafana 參考: http://docs.grafana.org
安裝
tar -zxvf prometheus-2.8.1.linux-amd64.tar.gz -C /usr/local/
cd /usr/local
mv prometheus-2.8.1.linux-amd64/ prometheus
cd prometheus/
./prometheus --version
修改prometheus.yml文件,確定啟動ip
修改prometheus.yml文件,確定啟動ip
完成修改后,可以直接啟動
./prometheus
# 添加用戶,后期用此賬號啟動服務
[root@localhost prometheus]# groupadd prometheus
[root@localhost prometheus]# useradd -g prometheus -s /sbin/nologin prometheus
# 賦權和創建prometheus運行數據目錄
[root@localhost prometheus]# cd ~
[root@localhost ~]# chown -R prometheus:prometheus /usr/local/prometheus/
[root@localhost ~]# mkdir -p /home/software/prometheus-data
[root@localhost ~]# chown -R prometheus:prometheus /home/software/prometheus-data
設置開機啟動
[root@localhost ~]# touch /usr/lib/systemd/system/prometheus.service
[root@localhost ~]# chown prometheus:prometheus /usr/lib/systemd/system/prometheus.service
[root@localhost ~]# vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
# Type設置為notify時,服務會不斷重啟
Type=simple
User=prometheus
# --storage.tsdb.path是可選項,默認數據目錄在運行目錄的./dada目錄中
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/home/software/prometheus-data
Restart=on-failure
[Install]
WantedBy=multi-user.target
修改配置文件設置開機啟動
再次修改配置文件
vim prometheus.yml
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
scrape_interval: 5s
static_configs:
- targets: ['192.168.33.134:9090']
labels:
instance: prometheus
- job_name: 'centos7-machine1'
scrape_interval: 10s
static_configs:
- targets: ['192.168.33.131:9100']
labels:
instance: node1
- job_name: 'centos7-machine2'
scrape_interval: 10s
static_configs:
- targets: ['192.168.33.132:9100']
labels:
instance: node2
- job_name: 'centos7-machine3'
scrape_interval: 10s
static_configs:
- targets: ['192.168.33.132:9100']
labels:
instance: node3
- job_name: 'centos7-machine4'
scrape_interval: 10s
static_configs:
- targets: ['192.168.33.134:9100']
labels:
instance: node4
- job_name: 'win2012-machine5'
scrape_interval: 10s
static_configs:
- targets: ['192.168.33.135:9182']
labels:
instance: node5
[root@prometheus ~]# systemctl enable prometheus
[root@prometheus ~]# systemctl start prometheus
設置iptables或者firewalld(測試環境可以直接關閉)
啟動並驗證
1)查看服務狀態
[root@prometheus ~]# systemctl status prometheus
2)web ui
Prometheus自帶有簡單的UI
在Status菜單下,Configuration,Rule,Targets等,
Statu-->Configuration展示prometheus.yml的配置,如下
Statu-->Targets展示監控具體的監控目標
centos還沒有監控到,一會安裝插件
繪圖
訪問http://192.168.33.134:9090/metrics 查看從exporter具體能抓到的數據,如下
三.部署node_exporter
Node_exporter收集機器的系統數據,這里采用prometheus官方提供的exporter,除node_exporter外,官方還提供consul,memcached,haproxy,mysqld等exporter,具體可查看官網。
這里在prometheus服務和 node節點部署相關服務。
tar -zxvf node_exporter-0.17.0-rc.0.linux-amd64.tar.gz -C /usr/local/
cd /usr/local/
mv node_exporter-0.17.0-rc.0.linux-amd64 node_exporter
設置用戶(節點)
groupadd prometheus
useradd -g prometheus -s /sbin/nologin prometheus
chown -R prometheus:prometheus /usr/local/node_exporter
[root@node1 ~]# vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
[root@node1 ~]# systemctl enable node_exporter
[root@node1 ~]# systemctl start node_exporter
grafana安裝
wget https://dl.grafana.com/oss/release/grafana-6.1.3-1.x86_64.rpm
yum -y localinstall grafana-6.1.3-1.x86_64.rpm
配置文件
配置文件位於/etc/grafana/grafana.ini,這里暫時保持默認配置即可
設置開機啟動
systemctl enable grafana-server
systemctl start grafana-server
防火牆管理(測試環境直接關閉)
添加數據
訪問:http://192.168.33.134:3000,默認賬號/密碼:admin/admin
默認登錄后,要求改密碼,可以選擇先跳過
添加數據源
在登陸首頁,點擊"Add data source"按鈕,跳轉到添加數據源頁面,配置如下:
Name: prometheus
Type: prometheus
Access: proxy
取消Default的勾選,其余默認,點擊"Add",如下:
在"Dashboards"頁簽下"import"自帶的模版,如下:
發現是空的
導入dashboard
從grafana官網下載相關dashboaed到本地,如:https://grafana.com/dashboards/405
Grafana首頁-->左上角圖標-->Dashboard-->import
加入1-node-exporter-0-16-0-17-for-prometheus_rev7.json這個插件
如果出現grafana-piechart-panel這個報錯
1.安裝pie插件
官網:https://grafana.net/plugins/grafana-piechart-panel
grafana-cli plugins install grafana-piechart-panel
2.重啟grafana-server
即可恢復,效果如下
現在監控正常了,我們開始把服務加入這個項目中,現在各個節點安裝node_exporter-0.17.0-rc.0.linux-amd64.tar.gz
部署
[root@node1 src]# tar -zxvf node_exporter-0.17.0-rc.0.linux-amd64.tar.gz -C /usr/local/
[root@node1 src]# cd /usr/local/
[root@node1 local]# mv node_exporter-0.17.0-rc.0.linux-amd64/ node_exporter/
2. 設置用戶
groupadd prometheus
useradd -g prometheus -s /sbin/nologin prometheus
chown -R prometheus:prometheus /usr/local/node_exporter/
3. 設置開機啟動
[root@node1 ~]# vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
[root@node1 ~]# systemctl enable node_exporter
[root@node1 ~]# systemctl start node_exporter
監控win主機
安裝wmi_exporter-0.3.3-amd64.msi
從https://github.com/martinlindhe/wmi_exporter/releases/download/v0.3.3/wmi_exporter-0.3.3-amd64.msi
下載后,安裝插件
訪問http://ip:9182/metrics后,如圖
說明安裝成功
telegram報警設置
在 telegram中@BotFather,並加為好友,然后,創建你的機器人
填入紅色塗鴉的地方
https://api.telegram.org/bot{$token}/getUpdates
把token換成你自己剛剛注冊到的那個紅色標注的字符串
后連接 變成為https://api.telegram.org/bot690994839:AAGejCWBwy4gKun9B6q7-Il2uooz0OYXlOU/getMe
返回如下則成功
驗證完畢,我們刪掉這個token,重新申請一個來解決
打開這個選項
然后,將這個機器人拉入報警群
和它進行交流后,在此刷新下面的命令
https://api.telegram.org/bot887549729:AAFyS949ryyYxbJI3mRS6ScW8qXbd7Yc0Og/getUpdates
然后發現找到了chat的id