【MySQL】企業級監控解決方案Promethus+Grafana
Promethus用作監控數據采集與處理,而Grafana只是用作數據展示
一、Promethus簡介
Prometheus(普羅米修斯)是一套開源的監控&報警&時間序列數據庫的組合,起始是由SoundCloud公司開發的。隨着發展,越來越多公司和組織接受采用Prometheus,社區也十分活躍,他們便將它獨立成開源項目,並且有公司來運作。Google SRE的書內也曾提到跟他們BorgMon監控系統相似的實現是Prometheus。現在最常見的Kubernetes容器管理系統中,通常會搭配Prometheus進行監控。
Prometheus基本原理是通過HTTP協議周期性抓取被監控組件的狀態,這樣做的好處是任意組件只要提供HTTP接口就可以接入監控系統,不需要任何SDK或者其他的集成過程。這樣做非常適合虛擬化環境比如VM或者Docker 。
Prometheus應該是為數不多的適合Docker、Mesos、Kubernetes環境的監控系統之一。
輸出被監控組件信息的HTTP接口被叫做exporter 。目前互聯網公司常用的組件大部分都有exporter可以直接使用,比如Varnish、Haproxy、Nginx、MySQL、Linux 系統信息 (包括磁盤、內存、CPU、網絡等等),具體支持的源看:https://github.com/prometheus
與其他監控系統相比,Prometheus的主要特點是:
一個多維數據模型(時間序列由指標名稱定義和設置鍵/值尺寸)。
非常高效的存儲,平均一個采樣數據占~3.5bytes左右,320萬的時間序列,每30秒采樣,保持60天,消耗磁盤大概228G。
一種靈活的查詢語言。
不依賴分布式存儲,單個服務器節點。
時間集合通過HTTP上的PULL模型進行。
通過中間網關支持推送時間。
通過服務發現或靜態配置發現目標。
多種模式的圖形和儀表板支持。
二、Promethus原理
基本原理就是這樣:
exporter作為數據采集器部署在被監控端,暴露一個http接口給server端抓取數據
Promethus Server通過接口去每個exporter上抓取監控數據
實際上也可以通過pushgateway將監控數據主動推送給服務端
服務端存入監控數據,以K/V形式存儲,並將報警推送給Alertmanager實現報警
三、安裝Promethus
服務端安裝:
1、安裝go環境
配置阿里yum源
[root@mydb1 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@mydb1 ~]# yum install -y go
2、安裝prometheus
[root@mydb1 soft]# tar xvf prometheus-2.3.2.linux-amd64.tar.gz
[root@mydb1 soft]# mv prometheus-2.3.2.linux-amd64 /usr/local/prometheus
3、編輯配置文件
[root@mydb1 ~]# cat /usr/local/prometheus/prometheus.yml
# my global config
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['192.168.1.101:9090']
labels:
instance: prometheus
- job_name: Linux-Server
static_configs:
- targets: ['192.168.1.101:9100']
labels:
instance: mydb1
- targets: ['192.168.1.102:9100']
labels:
instance: mydb2
- targets: ['192.168.1.103:9100']
labels:
instance: mydb3
- job_name: Mysql-Server
static_configs:
- targets: ['192.168.1.101:9104']
labels:
instance: mysql1
- targets: ['192.168.1.102:9104']
labels:
instance: mysql2
- targets: ['192.168.1.103:9104']
labels:
instance: mysql3
4、啟動promethus
[root@mydb1 ~]# /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path="/data/prometheus" --storage.tsdb.retention=30d &
config.file:指定配置文件
storage.tsdb.path:指定監控數據存放目錄
storage.tsdb.retention:指定保留30天監控數據
5、查看狀態
登陸ip:9090可以看到promethus自帶的web界面,可以自己寫類SQL查詢數據,點擊status可以看到節點的exporter的狀態
客戶端安裝
1、安裝node_exporter和mysqld_exporter
[root@mydb1 soft]# tar xvf node_exporter-0.16.0.linux-amd64.tar.gz
[root@mydb1 soft]# mv node_exporter-0.16.0.linux-amd64 /usr/local/node_exporter
[root@mydb1 soft]# tar xvf mysqld_exporter-0.11.0.linux-amd64.tar.gz
[root@mydb1 soft]# mv mysqld_exporter-0.11.0.linux-amd64 /usr/local/mysqld_exporter
[root@mydb2 soft]# tar xvf node_exporter-0.16.0.linux-amd64.tar.gz
[root@mydb2 soft]# mv node_exporter-0.16.0.linux-amd64 /usr/local/node_exporter
[root@mydb2 soft]# tar xvf mysqld_exporter-0.11.0.linux-amd64.tar.gz
[root@mydb2 soft]# mv mysqld_exporter-0.11.0.linux-amd64 /usr/local/mysqld_exporter
[root@mydb3 soft]# tar xvf node_exporter-0.16.0.linux-amd64.tar.gz
[root@mydb3 soft]# mv node_exporter-0.16.0.linux-amd64 /usr/local/node_exporter
[root@mydb3 soft]# tar xvf mysqld_exporter-0.11.0.linux-amd64.tar.gz
[root@mydb3 soft]# mv mysqld_exporter-0.11.0.linux-amd64 /usr/local/mysqld_exporter
2、啟動node_exporter
主要采集系統層面的監控數據
[root@mydb1 ~]# /usr/local/node_exporter/node_exporter &
[root@mydb2 ~]# /usr/local/node_exporter/node_exporter &
[root@mydb3 ~]# /usr/local/node_exporter/node_exporter &
3、啟動mysqld_exporter
主要采集mysql層的監控數據
mydb1,mydb2,mydb3都執行
創建監控賬號
GRANT REPLICATION CLIENT,PROCESS,SELECT ON *.* TO 'mysql_monitor'@'192.168.1.%' identified by 'mysql_monitor';
創建配置文件
[root@mydb1 ~]# cat /usr/local/mysqld_exporter/.my.cnf
[client]
host=192.168.1.101
user=mysql_monitor
password=mysql_monitor
[root@mydb2 ~]# cat /usr/local/mysqld_exporter/.my.cnf
[client]
host=192.168.1.102
user=mysql_monitor
password=mysql_monitor
[root@mydb2 ~]# cat /usr/local/mysqld_exporter/.my.cnf
[client]
host=192.168.1.103
user=mysql_monitor
password=mysql_monitor
[root@mydb1 ~]# /usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf="/usr/local/mysqld_exporter/.my.cnf" &
[root@mydb2 ~]# /usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf="/usr/local/mysqld_exporter/.my.cnf" &
[root@mydb3 ~]# /usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf="/usr/local/mysqld_exporter/.my.cnf" &
5、web界面測試數據獲取
http://localhost:9090
四、安裝Grafana
1、上傳rpm包,直接安裝即可
[root@mydb1 soft]# rpm -ivh grafana-5.2.1-1.x86_64.rpm
[root@mydb1 ~]# systemctl start grafana-server.service
2、登陸web界面
登陸ip:3000,首次登陸初始賬號密碼都是admin,會提示修改密碼。修改后admin/hgc3177678
3、配置數據源
4、導入模板
解壓grafana-dashboards包,該包中提供了大量的json格式文件的grafana dashboards,根據需要自行選擇,https://github.com/percona/grafana-dashboards
linux_overview-1531814759712.json
MySQL_Overview-1531814739662.json
MySQL_InnoDB_Metrics-1531814772539.json
MySQL_Replication-1531814749684.json
效果圖如下
【MySQL】Promethus監控報警-AlertManager
Promethus通過AlertManager實現報警
一、安裝AlertManager
AlertMnager安裝方式基本同exporter,解壓,修改配置文件,啟動即可,下載地址:https://github.com/prometheus/alertmanager/releases/download/v0.15.1/alertmanager-0.15.1.linux-amd64.tar.gz
[root@mydb1 soft]# tar xvf alertmanager-0.15.1.linux-amd64.tar.gz
[root@mydb1 soft]# mv alertmanager-0.15.1.linux-amd64 /usr/local/alertmanager
二、配置文件
[root@mydb1 ~]# cat /usr/local/alertmanager/alertmanager.yml
global:
smtp_smarthost: 'smtp.163.com:25'
smtp_from: 'hugaochao320@163.com'
smtp_auth_username: 'hugaochao320@163.com'
smtp_auth_password: 'hgc3177678'
smtp_require_tls: false
templates:
- '/usr/local/alertmanager/template/*.tmpl'
route:
group_by: ['alertname']
group_wait: 10s
group_interval: 10s
repeat_interval: 1m
receiver: 'hugc'
receivers:
- name: 'hugc'
email_configs:
- to: 'hugc@knowbox.cn'
html: '{{ template "alert.html" . }}'
headers: { Subject: " {{ .CommonAnnotations.summary }}" }
三、告警規則
[root@mydb1 ~]# cat /usr/local/prometheus/rules/memory_over.yml
groups:
- name: NodeMemoryUsage
rules:
- alert: NodeMemoryUsage
expr: round((node_memory_MemTotal_bytes - (node_memory_MemFree_bytes+node_memory_Buffers_bytes+node_memory_Cached_bytes )) / node_memory_MemTotal_bytes * 100) > 80
for: 1m
labels:
user: xucl
annotations:
summary: "{{$labels.instance}}: High Memory usage detected"
description: "{{$labels.instance}}: Memory usage is above 80% (current value is:{{ $value }})"
value: "{{ $value }}"
[root@mydb1 ~]# cat /usr/local/prometheus/rules/node_down.yml
groups:
- name: InstanceDown
rules:
- alert: InstanceDown
expr: up == 0
for: 1m
labels:
user: xucl
annotations:
summary: "Instance {{ $labels.instance }} down"
description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 1 minutes."
四、修改promethus配置文件
在原先的promethus.yml最后增加
[root@mydb1 ~]# cat /usr/local/prometheus/prometheus.yml
# my global config
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['192.168.1.101:9090']
labels:
instance: prometheus
- job_name: Linux-Server
static_configs:
- targets: ['192.168.1.101:9100']
labels:
instance: mydb1
- targets: ['192.168.1.102:9100']
labels:
instance: mydb2
- targets: ['192.168.1.103:9100']
labels:
instance: mydb3
- job_name: Mysql-Server
static_configs:
- targets: ['192.168.1.101:9104']
labels:
instance: mysql1
- targets: ['192.168.1.102:9104']
labels:
instance: mysql2
- targets: ['192.168.1.103:9104']
labels:
instance: mysql3
alerting:
alertmanagers:
- static_configs:
- targets: ["localhost:9093"]
rule_files:
- "rules/memory_over.yml"
- "rules/node_down.yml"
五、模板文件
[root@mydb1 ~]# cat /usr/local/alertmanager/template/alert.tmpl
{{ define "alert.html" }}
<style type="text/css">
table
{
border-collapse: collapse;
margin: 0 auto;
text-align: center;
}
table td, table th
{
border: 1px solid #cad9ea;
color: #666;
height: 30px;
}
table thead th
{
width: 100px;
}
table tr:nth-child(odd)
{
background: #fff;
}
table tr:nth-child(even)
{
background: #F5FAFA;
}
</style>
<table width="90%" class="table">
<tr><td>報警項</td>
<td>主機</td>
<td>報警閥值</td>
<td>開始時間</td>
</tr>
{{ range $i, $alert := .Alerts }}
<tr><td>{{ index $alert.Labels "alertname" }}</td>
<td>{{ index $alert.Labels "instance" }}</td>
<td>{{ index $alert.Annotations "value" }}</td>
<td>{{ $alert.StartsAt }}</td>
</tr>
{{ end }}
</table>
{{ end }}
六、啟動測試
首先啟動AlertManager
[root@mydb1 ~]# /usr/local/alertmanager/alertmanager --config.file="/usr/local/alertmanager/alertmanager.yml" --log.level=debug &
然后啟動Promethus
[root@mydb1 ~]# /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path="/data/prometheus" --storage.tsdb.retention=30d &
登陸平台查看
報警規則已經生效
再看報警
調低閾值,查看是否收到報警郵件
https://juejin.im/post/5b8a659bf265da431e7b947e
prometheus 監控學習
https://songjiayang.gitbooks.io/prometheus/content/
Prometheus 實戰
https://www.cnblogs.com/xiaobaozi-95/p/10684524.html
prometheus數據上報方式-pushgateway
https://blog.csdn.net/wuxiaobingandbob/article/details/78954406
使用Prometheus+Grafana監控MySQL實踐