Prometheus+Grafana+Altermanager钉钉报警

一.prometheus介绍node

Prometheus是一个开源的系统监控和报警的工具包,最初由SoundCloud发布。mysql

特色:linux

  • 多维数据模型(有metric名称和键值对肯定的时间序列)
  • 灵活的查询语言
  • 不依赖分布式存储
  • 经过pull方式采集时间序列,经过http协议传输
  • 支持经过中介网关的push时间序列的方式
  • 监控数据经过服务或者静态配置来发现
  • 支持图表和dashboard等多种方式 

组件: git

  • Prometheus :主程序,Prometheus服务端,因为存储及收集数据,提供相关api对外查询用,主要是负责存储、抓取、聚合、查询方面。
  • Alertmanager:程序,主要是负责实现报警功能。
  • Pushgateway :程序,主要是实现接收由Client push过来的指标数据,在指定的时间间隔,由主程序来抓取。
  • *_exporter :相似传统意义上的被监控端的agent,有区别的是,它不会主动推送监控数据到server端,而是等待server端定时来收集数据,即所谓的主动监控。

架构:github

二.prometheus部署

Prometheus官网下载:https://prometheus.io/download/golang

 

1. 下载&部署

 

# 下载 [root@prometheus src]# cd /usr/local/src/ [root@prometheus src]# wget https://github.com/prometheus/prometheus/releases/download/v2.3.2/prometheus-2.3.2.linux-amd64.tar.gz # 部署到/usr/local/目录 # promethus不用编译安装,解压目录中有配置文件与启动文件

[root@prometheus src]#tar zxf prometheus-2.3.2.linux-amd64.tar.gz -C /usr/local/
[root@prometheus src]# cd /usr/local/ 
[root@prometheus local]# mv prometheus-2.0.0.linux-amd64/ prometheus/
# 验证
[root@prometheus local]# cd prometheus/
[root@prometheus prometheus]# ./prometheus --version

 

2. 配置文件

# 解压目录中的prometheus.yml # 简单验证,主要配置采用默认文件配置,有修改/新增处用红色标示 [root@prometheus prometheus]# vim prometheus.yml # 全局配置 global: scrape_interval: 15s # 设置抓取(pull)时间间隔,默认是1m evaluation_interval: 15s # 设置rules评估时间间隔,默认是1m # scrape_timeout is set to the global default (10s). # 告警管理配置,暂未使用,默认配置 alerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093 # 加载rules,并根据设置的时间间隔按期评估,暂未使用,默认配置 rule_files: # - "first_rules.yml" # - "second_rules.yml" # 抓取(pull),即监控目标配置 # 默认只有主机自己的监控配置 scrape_configs: # 监控目标的label(这里的监控目标只是一个metric,而不是指某特定主机,能够在特定主机取多个监控目标),在抓取的每条时间序列表中都会添加此label - job_name: 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. # 可覆盖全局配置设置的抓取间隔,由15秒重写成5秒。 scrape_interval: 5s # 静态指定监控目标,暂不涉及使用一些服务发现机制发现目标 static_configs: - targets: ['localhost:9090'] # (opentional)再添加一个label,标识了监控目标的主机 labels: instance: prometheus - job_name: 'linux' scrape_interval: 10s static_configs: # 采用node_exporter默认开放的端口 - targets: ['172.20.1.212:9100','192.168.233.131:9100'] labels: instance: node1

3. 设置用户

# 添加用户,后期用此帐号启动服务 [root@prometheus prom etheus]# groupadd prometheus [root@prometheus prometheus]# useradd -g prometheus -s /sbin/nologin prometheus # 赋权 [root@prometheus prometheus]# cd ~ [root@prometheus ~]# chown -R prometheus:prometheus /usr/local/prometheus/ # 建立prometheus运行数据目录 [root@prometheus ~]# mkdir -p /var/lib/prometheus [root@prometheus ~]# chown -R prometheus:prometheus /var/lib/prometheus/

4. 设置开机启动




[root@prometheus ~]# touch /usr/lib/systemd/system/prometheus.service [root@prometheus ~]# chown prometheus:prometheus /usr/lib/systemd/system/prometheus.service [root@prometheus ~]# 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=/var/lib/prometheus Restart=on-failure [Install] WantedBy=multi-user.target # 设置开机启动 [root@prometheus ~]# systemctl enable Prometheus [root@prometheus ~]# systemctl start prometheus

5. 启动并验证

1)查看服务状态

[root@prometheus ~]# systemctl status prometheus

[root@prometheus ~]# netstat -tunlp | grep 9090

2)web ui

Prometheus自带有简单的UI,http://localhost:9090web

在Status菜单下,Configuration,Rule,Targets等,sql

Statu-->Configuration展现prometheus.yml的配置,json

三.部署node_exporter

Node_exporter收集机器的系统数据,这里采用prometheus官方提供的exporter,除node_exporter外,官方还提供consul,memcached,haproxy,mysqld等exporter,具体可查看官网或去github下载(官网没有的github有可能有如:windows的exporter)。vim

这里在prometheus node节点部署相关服务。

1. 下载&部署

# 下载 [root@node1 ~]# cd /usr/local/src/ [root@node1 src]# wget https://github.com/prometheus/node_exporter/releases/download/v0.16.0/node_exporter-0.16.0.linux-amd64.tar.gz # 部署 [root@node1 src]# tar -zxvf node_exporter-0.16.0.linux-amd64.tar.gz-C /usr/local/ [root@node1 src]# cd /usr/local/ [root@node1 local]# mv node_exporter-0.16.0.linux-amd64/ node_exporter/

2. 设置用户

[root@node1 ~]# groupadd prometheus [root@node1 ~]# useradd -g prometheus -s /sbin/nologin prometheus [root@node1 ~]# 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

4. 验证

访问prometheus,查看node1主机已经可被监控

 

5. 绘图

 

访问:http://192.168.233.131:9100/metrics,查看从exporter具体能抓到的数据,以下:

 

 

访问:prometheus,在输入框中任意输入1个exporter能抓取得值,点击"Execute"与"Execute"按钮,便可见相应抓取数据的图形,同时可对时间与unit作调整,

 

四.部署grafana

在prometheus& grafana server节点部署grafana服务。

1. 下载&安装

# 下载 [root@prometheus ~]# cd /usr/local/src/
[root@prometheus src]# wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.2.2-1.x86_64.rpm
 
 
# 安装 
sudo yum localinstall grafana-5.2.2-1.x86_64.rpm

2. 配置文件

配置文件位于/etc/grafana/grafana.ini,这里暂时保持默认配置便可。

3. 设置开机启动

[root@prometheus src]# systemctl enable grafana-server [root@prometheus src]# systemctl start grafana-server

5. 添加数据源 

1)登录

访问:http://localhost:3000,默认帐号/密码:admin/admin

 

2)添加数据源

在登录首页,点击"Configuration-Data Sources"按钮,跳转到添加数据源页面,配置以下:

Name: prometheus

Type: prometheus

URL: http://localhost:9090/

Access: Server

取消Default的勾选,其他默认,点击"Add",以下:

在"Dashboards"页签下"import"自带的模版,以下:

 

 

6. 导入dashboard

从grafana官网下载相关dashboard到本地,如:https://grafana.com/dashboards/1860

Upload已下载至本地的json文件

Grafana.com Dashboard输入grafana官网的Dashboard连接(如:https://grafana.com/dashboards/1860)

能够下载使用upload上传,也可不下载直接复制连接

7. 查看dashboard

Grafana首页-->Dashboard-->Home,Home下拉列表中可见有已添加的两个dashboard,"Prometheus Stats"与"Node Exporter Full",选择1个便可

补充

grafana官网若是没有你想要的dashboard,你可去github上看看。

 

大部分的dashborad是没法直接使用,它们呈现不出图像显示“no data”或者显示的图像和原本的图像不符合,好比你要显示磁盘剩余但他显示的是磁盘已使用多少。这就很尴尬了。能够经过修改Metrics的计算公式来是之有效。

选中一个很差使的图标点击Edit

Add Query添加一个监控值

 

五.部署Alertmanager 钉钉报警

虽说grafana也有报警可是使用事后感受不太好用,grafana报警没法使用模板变量而且报警规则比较繁琐,而后从新比对决定使用Alertmanager的钉钉报警。可是alertmanager不止是钉钉报警,还有微信,邮件等。

1. 下载&安装

[root@localhost src]# wget https://github.com/prometheus/alertmanager/releases/download/v0.15.2/alertmanager-0.15.2.linux-amd64.tar.gz [root@localhost src]# tar zxf alertmanager-0.15.2.linux-amd64.tar.gz

2.配置文件

alertmanager的webhook集成了钉钉报警,因此他不是原本就有的。钉钉对格式要求很严格,一会还须要使用插件进行格式转换。

cat alertmanager.yml
global: resolve_timeout: 5m route: receiver: webhook group_wait: 30s group_interval: 5m repeat_interval: 4h group_by: [alertname] routes: - receiver: webhook group_wait: 10s match: team: node receivers: - name: webhook webhook_configs: - url: http://localhost:8060/dingtalk/ops_dingding/send send_resolved: true

3.启动alertmanager

nohup ./alertmanager --config.file=alertmanager.yml 2>&1 1>altermanager.log &
#查看端口
netstat -anpt | grep 9093

4.报警规则

监控主机是否存活

cd /usr/local/prometheus
cat rules.yml
groups: - name: test-rule rules: - alert: 主机状态 expr: up == 0 for: 2m labels: status: warning annotations: summary: "{{$labels.instance}}:服务器关闭" description: "{{$labels.instance}}:服务器关闭"

5.修改prometheus配置文件

修改alerting和rule_file

rule_files能够指定多个规则

# Alertmanager configuration alerting: alertmanagers: - static_configs: - targets: ["localhost:9093"] # - alertmanager:9093 # Load rules once and periodically evaluate them according to the global 'evaluation_interval'. rule_files: - "rules.yml" # - "second_rules.yml"

重启

6.将钉钉接入 Prometheus AlertManager WebHook

参考文档:http://theo.im/blog/2017/10/16/release-prometheus-alertmanager-webhook-for-dingtalk/

插件下载地址:https://github.com/timonwong/prometheus-webhook-dingtalk

安装

先安装go环境

建议把主机名改为主机ip,方便报警时提供url或者改为域名也能够

cd /root/go/src/github.com/timonwong/
git clone https://github.com/timonwong/prometheus-webhook-dingtalk.git
cd prometheus-webhook-dingtalk make(出错不要管他)
若是没有生成prometheus-webhook-dingtalk,建立新目录,进入目录git clone软件从新编译
mkdir -p /usr/lib/golang/src/github.com/timonwong/
 

启动

不会加机器人的去网上搜
ding.profile是钉钉机器人的webhook
nohup ./prometheus-webhook-dingtalk --ding.profile="ops_dingding=https://oapi.dingtalk.com/robot/send?access_token=xxx" 2>&1 1>dingding.log & netstat -anpt | grep 8060

7.测试

把监控主机的exporter关闭或者关闭主机

 

再启动exporter,已经恢复

相关文章
相关标签/搜索