一直都觉的有个仪表监控盘很屌,以前就听说过这个软件组合。Prometheus+grafana 显示超炫的仪表界面
一直没动手,直到最近。尝试了监控服务器,监控数据库,问题微信(企业微信)报警
公司业务代码有bug反馈有延迟性,我就尝试着搭建这个监控,这次就是一些摸索过程的记录。
安装环境 Ubuntu14.04
1 prometheus 需要golang的支持。先安装golang语言
安装golang
https://golang.org/dl/
wget https://dl.google.com/go/go1.12.9.linux-amd64.tar.gz
tar -xvf go1.12.9.darwin-amd64.tar.gz
添加一些信息
export GOROOT=/home/vagrant/golang/go
export GOPATH=/home/vagrant/golang/gopath
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
source ~/.bashrc
2 安装prometheus 端口号9090
解压
tar -xvf prometheus-2.12.0.linux-amd64.tar.gz
/home/vagrant/golang/go/src/github.com/prometheus-2.12.0.linux-amd64
启动
./prometheus --config.file=prometheus.yml
注意看版本 amd64 我开始下载错了一直报错。后来从新下载了
这个时候可以在显示器里看到 prometheus的页面了
3 监控nginx ,需要下载nginx-vts-exporter 端口号 9913
wget https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.10.3/nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
解压之后 启动
./nginx-vts-exporter -nginx.scrape_uri=http://127.0.0.1/shows/format/json
4 为了更好看的界面,安装grafana 端口号是3000
sudo wget https://dl.grafana.com/oss/release/grafana_6.3.3_amd64.deb
sudo dpkg -i grafana_6.3.3_amd64.deb
它自己会启动
service grafana-server status 看下状态
这里我的机器上有ntop,端口冲突了 我就把ntop关掉了
127.0.0.1:3000 可以看到 grafana界面了,输入账号和密码就能进去了
5 想要看到nginx数据仪表,在grafana里面
点击左上角+号,点击import 输入2949 id 这个id号是官方的一个模板号
输入之后点load 就变成了另外一个界面,然后选择prometheus 数据源 就ok了,我这边是这样子。
6 服务器监控 下载node_exporter (node 节点)
解压
tar -xvf node_exporter-0.17.0.linux-amd64.tar.gz -C /usr/local
启动
./node_exporter &
7 配置grafana 回到第5步这次导入的模板id是10262 配置完成之后 可以看到如下界面:哈哈 是不是很酷
8 配置mysql监控节点
下载mysqld_exporter
解压
tar -xvf mysqld_exporter-0.12.1.linux-amd64.tar.gz
需要给这个mysqld_exporter创建一个账号,安全起见
vim my_exporter.cnf
写入
[client]
user=mysql_exporter
password=mysql_exporter
然后进入mysql 创建账号
GRANT REPLICATION CLIENT,PROCESS ON *.* TO 'mysql_exporter'@'localhost' identified by 'mysql_exporter';
GRANT SELECT ON *.* TO 'mysql_exporter'@'localhost'; -- 得有查的权限
flush privileges;
然后启动监控节点
cd mysqld_exporter-0.12.1.linux-amd64
/mysqld_exporter --config.my-cnf=my_exporter.cnf
9 配置 prometheus.yml
alerting:
alertmanagers:
- static_configs:
- targets: ['127.0.0.1:9093']
# - alertmanager: 9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
- "first_rules.yml"
- "http_request_rules.yml"
- "cpu_idle.yml"
- "mysql_rule.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'.
static_configs:
- targets: ['127.0.0.1:9090']
- job_name: 'nginx'
static_configs:
- targets: ['127.0.0.1:9913']
- job_name: 'node-exporter'
static_configs:
- targets: ['127.0.0.1:9100']
- job_name: 'mysqld_exporter'
static_configs:
- targets: ['127.0.0.1:9104']
重启prometheus
service prometheus restart
10 监控完成之后,需要报警,问题要及时的传达给相关人员。比解决问题更重要的是及时发现问题。报警用alertmananger.
Alertmanager作为一个独立的组件,负责接收并处理来自Prometheus Server(也可以是其它的客户端程序)的告警信息。
Alertmanager可以对这些告警信息进行进一步的处理,比如当接收到大量重复告警时能够消除重复的告警信息,同时对告警信息进行分组并且路由到正确的通知方,
Prometheus内置了对邮件,Slack,企业微信等多种通知方式的支持,同时还支持与Webhook的集成,以支持更多定制化的场景。
例如,目前Alertmanager还不支持钉钉,那用户完全可以通过Webhook与钉钉机器人进行集成,从而通过钉钉接收告警信息。同时AlertManager还提供了静默和告警抑制机制来对告警通知行为进行优化。
安装alertmanager 端口 9093
wget https://github.com/prometheus/alertmanager/releases/download/v0.18.0/alertmanager-0.18.0.linux-amd64.tar.gz
启动
./alertmanager --config.file=alertmanager.yml
http://192.168.22.87:9093/#/alerts
关联prometheus 和alertmananger
配置prometheus.yml 文件
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets: ['127.0.0.1:9093']
# - alertmanager: 9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
- "first_rules.yml"
- "http_request_rules.yml"
- "cpu_idle.yml"
- "mysql_rule.yml"
其中rule_files 是报警规则文件 这些文件的位置跟prometheus.yml 平行就好
11 配置alertmanager.yml
这里我配置了两种接收报警消息,分别是邮件和企业微信
1 邮件接收报警
global:
resolve_timeout: 5m
smtp_smarthost: "smtp.qq.com:465"
smtp_from: "1243079@qq.com" # 发送者邮箱
smtp_auth_username: '124307'
smtp_auth_password: 'qaoiesoyuxro' #这个是授权码
smtp_require_tls: false
route:
group_by: [page]
group_wait: 3s
group_interval: 3s
repeat_interval: 1h
receiver: 'email' 邮件接收
receivers:
- name: 'email'
email_configs:
- to: 'jack@mol.com' #接受者邮箱
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']
说明 这里smtp 需要去邮箱里去开启,如何开启很简单,在设置里面点击一下即可。可能还需要发条短信。
授权码不是qq邮箱密码,怎么获取qq邮箱授权码
效果图
2 企业微信配置
route:
group_by: [page]
group_wait: 3s
group_interval: 3s
repeat_interval: 1h
receiver: 'wechat'
receivers:
- name: 'wechat'
wechat_configs:
- corp_id: 'wwaf239d8d' #公司的码可以在企业微信后台拿到
to_party: '1' 1值是组的ID 你可通过链接去定制报警信息接收人或者组
agent_id: '10002'
api_secret: 'FkcczdxAPiswj83DYtxxrHzlowVEjMsUY'
send_resolved: true # 告警解除发送通知 true 发送
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']
这里企业微信配置也很简单,如果没有企业微信,可以自己申请一个,自己玩玩就可以了。
我刚开始看到这么麻烦我也想退却。
百度一下,有相关文章教学,为之则易,不为则难。 十分钟最多就配置完毕了。
企业微信推送,用自己的微信加入到自己的企业微信(一人可以加入多个企业的企业微信)
这样就能在微信看到推送了,至于怎么直接在微信收到推送,上文的webhook 应该可以的,我没有去尝试。
12 告警的模板配置
在alertmanager文件中新建wechat.tmpl,写入以下
{{ define "wechat.default.message" }}
{{ if gt (len .Alerts.Firing) 0 -}}
Alerts Firing:
{{ range .Alerts }}
告警级别:{{ .Labels.severity }}
告警类型:{{ .Labels.alertname }}
故障主机: {{ .Labels.instance }}
告警主题: {{ .Annotations.summary }}
告警详情: {{ .Annotations.description }}
触发时间: {{ .StartsAt.Format "2006-01-02 15:04:05" }}
{{- end }}
{{- end }}
{{ if gt (len .Alerts.Resolved) 0 -}}
Alerts Resolved:
{{ range .Alerts }}
告警级别:{{ .Labels.severity }}
告警类型:{{ .Labels.alertname }}
故障主机: {{ .Labels.instance }}
告警主题: {{ .Annotations.summary }}
触发时间: {{ .StartsAt.Format "2006-01-02 15:04:05" }}
恢复时间: {{ .EndsAt.Format "2006-01-02 15:04:05" }}
{{- end }}
{{- end }}
告警链接:
{{ template "__alertmanagerURL" . }}
{{- end }}
然后在alertmanager.yml 中receivers 下面加入配置
receivers:
- name: 'wechat'
wechat_configs:
- corp_id: 'wwaf239d8d' #公司的码可以在企业微信后台拿到
to_party: '1' 1值是组的ID 你可通过链接去定制报警信息接收人或者组
agent_id: '10002'
api_secret: 'FkcczdxAPiswj83DYtxxrHzlowVEjMsUY'
send_resolved: true # 告警解除发送通知 true 发送
templates:
- '/home/vagrant/alertmaneger/alertmanager-0.18.0.linux-amd64/wechat.tmpl'
这样就能用自定义的报警模板了