使用Prometheus+grafana打造高逼格监控平台(赠书)

2,594 阅读9分钟
原文链接: mp.weixin.qq.com

点击上方“民工哥技术之路 ”选择“星标”

每天10点 为你分享不一样的干货

  读者福利!多达 2048G 各种资源免费赠送

作者:又耳笔记

原文:https://blog.51cto.com/youerning/2050543

前言:

笔者看来, 监控不应该只是监控,除了及时有效的报警,更应该”好看”,因为视觉上的感受更能给我们直观的感受,更能从绚丽的走势中发现异常, 如果你觉得监控就应该像老牌监控nagios,cacti一样,我想也没什么不对的,因为也许那是你们最适合的,但,你还是可以瞧瞧这个监控能给你带来什么。

效果图

为了你能有更多的动力看下去,这里放一部分通过Prometheus + grafana打造出来的监控平台,效果图如下。

如果你觉得不错可以继续看下去,上面主要是kvm宿主机, ceph集群, 物理机监控,以及ping, 最后一张的监控图没有展开是为了让你可以瞥一眼所能监控的指标条目。

Prometheus架构图

参考:https://prometheus.io/docs/introduction/overview/

如果你对Prometheus没有接触过,也许会看不懂上面说什么,但是没关系,如果你看完之后,在回过头来瞧瞧,也许就了解这个架构了,也会对Prometheus有一个更深的认识。

这里简单说一下Prometheus的各个部分。 Prometheus Server: Prometheus服务端,由于存储及收集数据,提供相关api对外查询用。 Exporter: 类似传统意义上的被监控端的agent,有区别的是,它不会主动推送监控数据到server端,而是等待server端定时来手机数据,即所谓的主动监控。 Pushagateway: 用于网络不可直达而居于exporter与server端的中转站。 Alertmanager: 报警组件,将报警的功能单独剥离出来放在alertmanager。 Web UI: Prometheus的web接口,可用于简单可视化,及语句执行或者服务状态监控。

安装

由于Prometheus是go语言写的,所以不需要编译,安装的过程非常简单,仅需要解压然后运行。 Prometheus官方下载地址:https://prometheus.io/download/

注:为了演示方便,这里node_exporter, Prometheus server, grafana都安装再同一台机器,系统环境Ubuntu14.04

安装Prometheus server

解压tar xf prometheus-2.0.0-rc.2.linux-amd64.tar.gz运行cd prometheus-2.0.0-rc.2.linux-amd64./prometheus --config.file=prometheus.yml

然后我们可以访问 http://<服务器IP地址>:9090,验证Prometheus是否已安装成功,web显示应该如下

通过点击下拉栏选取指标,点击”Excute” 我们能够看到Prometheus的性能指标。

点击”status”可以查看相关状态。

但是光安装Prometheus server意义不大,下面我们再安装node_exporter以及grafana

node_exporter安装

解压tar xf node_exporter-0.15.0.linux-amd64.tar.gz运行cd node_exporter-0.15.0.linux-amd64./node_exporter

验证node_exporter是否安装成功

curl 127.0.0.1:9100

curl 127.0.0.1:9100/metrics

返回一大堆性能指标。

grafana安装

下载deb安装wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana_4.5.2_amd64.debdpkg -i grafana_4.5.2_amd64.deb安装依赖sudo apt-get install -y adduser libfontconfig启动grafanasudo service grafana-server start

加入自启动

sudo update-rc.d grafana-server defaults

注:其他系统安装参考:http://docs.grafana.org/installation/

启动grafana并查看状态

systemctl daemon-reloadsystemctl start grafana-serversystemctl status grafana-server

访问grafana, http://<服务器IP>:3000 默认用户名密码:admin/admin

为grafana添加Prometheus数据源

至此所有安装已完成 但是还存在以下问题 一:Prometheus server并没有配置被监控端的IP地址,即没有取指定的机器取数据 二:启动的方式太不人性化了,没有启动脚本。 三:grafana没有可用的dashboard用于展示 这些问题我们放在下面的配置,可视化段落处理。

配置

关闭之前之间运行的node_exporter及prometheus

增加一个被监控端配置项

创建目录/etc/prometheus/mkdir /etc/prometheus/创建配置文件vi /etc/prometheus/prometheus.yml修改如下(在有配置文件基础上增加红色区域)# my global configglobal:  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).  # Attach these labels to any time series or alerts when communicating with  # external systems (federation, remote storage, Alertmanager).  external_labels:      monitor: 'codelab-monitor'# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.rule_files: # - "first.rules"  # - "second.rules"# 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: ['localhost:9090']

注意:缩进是必须的

添加启动脚本 下载地址:https://github.com/youerning/blog/tree/master/prometheus

cp node-exporter.service /etc/init.d/node-exportercp prometheus.service /etc/init.d/prometheuschmod +x /etc/init.d/node-exporterchmod +x /etc/init.d/prometheus

将上面的可执行二进制文件移到/usr/local/bin

cp prometheus-2.0.0-rc.2.linux-amd64/prometheus /usr/local/bin/prometheusmv node_exporter-0.15.0.linux-amd64/node_exporter /usr/local/bin/node_exporter

然后启动Prometheus,node-exporter 创建工作目录(Prometheus的数据会存在这,启动脚本里面我写的是/data)

mkdir /dataservice prometheus startservice node-exporter start

在Prometheus的web页面能看到被监控端

然后grafana导入dashboard 下载地址:https://grafana.com/dashboards/1860

注: https://grafana.com/dashboards还有很多的dashboard可以下载

按照以下步骤导入

点击import以后grafana就会多一个dashboard

至此一个系统层面性能指标监控已经全部完成。

可视化自定义 由于grafana的界面配置都是页面点击,需要截图标注,如果截太多图就文章太冗长了,这里就不进一步说明了,相关配置参考 http://docs.grafana.org/features/panels/ 通过上面的安装配置发现,其实整个监控的流程还缺少了报警的环节,如果不能及时通报异常情况再好看也白搭。

报警

解压tar xf alertmanager-0.11.0.linux-amd64.tar.gz规则配置cat /etc/prometheus/alert.rules

groups:

  • name: uptime rules:

    Alert for any instance that is unreachable for >1 minutes.

  • - alert: InstanceDown expr: up == 0for: 1m labels: severity: pageannotations: summary: "Instance {{ $labels.instance }} down" description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes."

prometheus.yml增加以下内容

rule_files:   - "/etc/prometheus/alert.rules" alerting:  alertmanagers:  - scheme: http    static_configs:    - targets:      - "localhost:9093"

详细配置参考: https://github.com/youerning/blog/blob/master/prometheus/prometheus-alertmanager.yml

启动alertmanager

./alertmanager --config.file=/etc/prometheus/alertmanager.yml

查看监控状态

所以需要邮件报警的话有两种方法,一:再报警服务器里面植入自己的证书, 参考:http://blog.amigapallo.org/2016/04/14/alertmanager-docker-container-self-signed-smtp-server-certificate/ 二:允许smtp不使用tls

其实上面两种方法都不太优雅,观法推荐的是使用web_hook 但是又得保证web_hook的服务是运行的,这就很扯淡了,不过,如果是全部跑在docker管理平台,如k8s,倒是不错的。

下面是一个简单的实现。

from __future__ import print_functionimport falconfrom wsgiref import simple_serverfrom email.mime.text import MIMETextimport smtplibimport jsonsmtpServer = "mx.example.com"smtpUser = "sender@example.com"smtpPass = "password"sender = "sender@example.com"reciver = "reciver@example.com"tpl = """status: {status}alerts: {alerts}"""def sendMail(reciver, subject, message):    server = smtplib.SMTP(smtpServer, 587)    server.ehlo()    server.starttls()    server.ehlo()    server.login(smtpUser, smtpPass)    server.set_debuglevel(1)    msg = MIMEText(message, "plain", "utf8")    msg["Subject"] = subject    server.sendmail(sender, [reciver], msg.as_string())    server.quit()class WebHook(object):    def on_post(self, req, resp):        """Handles GET requests"""        body = req.stream.read()        postData = json.loads(body.decode('utf-8'))        msg = tpl.format(**postData)        print(msg)        sendMail(reciver, "alert", msg)        resp.status = falcon.HTTP_200 # This is the default status        resp.body = "OK"app = falcon.API()app.add_route('/', WebHook())if __name__ == '__main__':    httpd = simple_server.make_server('0.0.0.0', 80, app)    httpd.serve_forever()

源码:https://github.com/youerning/blog/blob/master/prometheus/webhookmail.py

注意:有falcon的依赖,需要pip install falcon

效果如下

注:由于我没有进一步处理post过来的json数据,所以显得不是很直观,大家可以根据自己的需要编排数据

福利时间

中秋节福利:本次【 民工哥技术之路】联合【机械工业出版社华章公司 】为大家送上7本正版 《Prometheus监控实战》图书。祝大家中秋节快乐,阖家团圆!

推荐语:

(1)Docker公司前服务与支持副总裁、Kickstarter前首席技术官、Empatico首席技术官撰写

(2)第一本国外权威技术人员的Prometheus译著,体系结构完整,非简单的资料整合

(3)覆盖了目前的一些主流云技术(比如AWS),但同样适用于各种其他环境和技术栈。

通过阅读本书,你将学到以下内容:

1)·Prometheus监控:使用Prometheus来监控主机、应用程序和服务,内容涉及安装、基本监视、服务发现、警报、日志监控、伸缩和可视化。

2) ·监控与度量:监控基础、方法和途径,介绍如何在以度量为中心的环境中进行监控,包括构建动态阈值、基本异常检测以及监控聚合与联合,研究如何应用现代模式,如谷歌的四个黄金指标、USE方法和RED方法。

3) ·Kubernetes与容器:对Kubernetes、Docker和容器进行监控,专注于监控高度动态、瞬息万变的环境和新架构(如微服务)的特殊挑战。

4) ·云:在云中进行监控,包括服务发现和云平台的监控。

送书规则:

1、仅限公众号读者参与,活动截止时间 2019年9月17日12:00 。

2、留言谈谈你在学习、处理与prometheus监控当中相关的经验、踩过的坑等,精选留 言点赞前7名 (短时间内赞数爆增或其它刷赞行为均视为无效),各送出此书一本。

3、获得者需于一个工作日内联系民工哥发送详细收货地址,逾期则视为自动放弃,名额转移至其他读者。

4、没有获得的读者,也可以直接点击下面的图片购买此书:

关注  民工哥技术之路 微信公众号对话框回复关键字: 1024 可以获取一份最新整理的技术干货。

这个截图神器,能轻松碾压QQ和微信。。。

Python 2大限来了!113天后官方不再维护 | 附升级指南

我在网盘中看到了无数人泄露的私密文件

sonar+Jenkins 构建代码质量自动化分析平台

警惕!又一疑似东南亚“黑砖窑”人才输送基地

不想起标题,只想发红包~|读者福利

15 个超赞超牛逼的 Linux 工具,提高效率的同时增加乐趣!

点击【 阅读原文】和民工哥一起聊技术、搞事情~~

不管怎样

点“在看”一定不能放弃啊!