官方文档https://grafana.com/docs/grafana/latest/github 地址https://github.com/grafana/grafana性能监控 netdata / prometheus / grafanahttps://zhuanlan.zhihu.com/p/604041531netdata (开源分布式实时监控系统) 官网https://www.netdata.cloud/influxdb(开源时间序列数据库)https://github.com/influxdata/influxdb1、Grafana 简介Grafana 是一个开源的度量分析与可视化套件。经常被用作基础设施的时间序列数据和应用程序分析的可视化它在其他领域也被广泛的使用包括工业传感器、家庭自动化、天气和过程控制等。Grafana 支持许多不同的数据源。每个数据源都有一个特定的查询编辑器该编辑器定制的特性和功能是公开的特定数据来源。Grafana 的监控是基于数据库的通过插件获取到服务器性能并存储到数据库中然后使用Grafana 连接数据库形成可视化的图表。官方支持以下数据源GraphiteInfluxDBOpenTSDBPrometheusElasticsearchCloudWatch和KairosDB。每个数据源的查询语言和能力都是不同的。你可以把来自多个数据源的数据组合到一个仪表板但每一个面板被绑定到一个特定的数据源它就属于一个特定的组织。Grafana 特性官方在线的 demo可以体验 Grafana 最新特性 http://play.grafana.org/可视化快速灵活的可视化以及多种选择使你可以以任何方式可视化数据。官方库中具有丰富的仪表盘插件比如热图、折线图、图表等多种展示方式。动态仪表盘使用模板变量创建动态且可重复使用的仪表板这些模板变量显示在仪表板顶部。探索指标通过临时查询和动态明细浏览数据。拆分视图并排比较不同的时间范围查询和数据源。探索日志快速搜索所有日志或实时流式传输与 Loki 数据源配合使用效果最佳。警报以可视方式定义最重要指标的警报规则。Grafana 将不断评估并向 SlackPagerDutyVictorOps 和 OpsGenie 等系统发送通知。混合数据源在同一张图中混合使用不同的数据源你可以为每个查询指定数据源适用于自定义数据源。注解使用来自不同数据源的丰富事件注释图形。将鼠标悬停在事件上会显示完整的事件元数据和标签。临时过滤器临时过滤器允许您即时创建新的键/值过滤器这些过滤器会自动应用于使用该数据源的所有查询。在线资源grafana 提供了一些在线资源可以帮助使用者更方便的使用 grafana比如在线 dashboard( https://grafana.net/dashboards ) 可以帮助快速生成一个美观的 dashboard不用自己花心思去布局了在线插件仓库( https://grafana.net/plugins )可以帮助连接其他数据源如 zabbixOpen-Falcon 等或添加其他展示图表如饼图(Pie chat)。合理利用这些在线资源可以让 grafana 更加完善易用。grafana官方文档: http://docs.grafana.org/elasticsearch官方文档: https://www.elastic.co/guide/en/elasticsearch/reference/2.4/index.htmlELK stack 中文指南 之 Grafanahttps://elkguide.elasticsearch.cn/elasticsearch/other/grafana.html下载、安装安装下载https://grafana.com/grafana/download这里以 windows 为例下载完压缩包直接解压执行 grafana-server.exe登录 grafana登录 grafana 界面http://127.0.0.1:3000默认用户和密码均为adminGrafana 安装 插件这里搜索 zabbix、clock 插件并安装插件安装完成后位置D:\Software\grafana-9.3.2\data\plugins注意安装完插件后需要重启 grafana-server插件才能生效。。。clock 插件安装之后直接可以使用无需设置。zabbix 需要设置添加数据源https://blog.csdn.net/weixin_62352862/article/details/1282540502、Grafana 的 Dashboards(看板、仪表盘)创建 DashboardsPrometheus入门教程三Grafana 图表配置快速入门https://mp.weixin.qq.com/s/sA0nYevO8yz6QLRz03qJSwDashboard仪表盘与 Panel面板是 Prometheus 的基本单元所有的监控数据都会通过这两个东西来呈现。Panel 是 Prometheus 中数据呈现的最小单元。一个 Dashboard 中可以有多个 Panel。我们看到的 Dashboard 数据都是由一个个 Panel 构成的。Panel 的设置区域一共可以分为三部分预览区用来预览设置的图表。数据设置区用来设置数据源、数据指标等。图表设置区用来对图表的名称等进行设置。Dashboard 就是一个大的面板里面包含了无数个图表Panel数据设置区可以分为三个设置区域分别是查询设置区设置查询数据的数据源、指标等。转换设置区可以对查询出来的数据进行过滤等。告警设置区可以对设置的指标设置告警规则。手动 创建 Dashboards可以根据自己的需求手动新建一个 Dashboard。除此之外https://grafana.com/grafana/dashboards 上还有很多公共的 Dashboard 可以选用。导入 公共 Dashboards更多的模板我们也可以去 grafana 官网下载监控面板下载网址https://grafana.com/grafana/dashboards?dataSourceprometheus仪表盘 添加 面板(panel)面板(panel) 的 可视化设置 面板的可视化为 clock示例面板可视化 --- 添加 时间 仪表板仪表盘 布局示例面板可视化 --- 创建 CPU、内存使用率 仪表板关键字grafana cpuPrometheus Grafana 监控系统部署https://zhuanlan.zhihu.com/p/425304902主机基础监控(cpu内存磁盘网络)https://grafana.com/grafana/dashboards/9276-1-cpu/clock、zabbix 简单使用https://www.zhangshengrong.com/p/v710KdGrXM/示例微服务监控 - Grafana 使用教程https://makeoptim.com/service-mesh/prometheus-grafana3、Grafana 使用教程https://blog.csdn.net/bbwangj/article/details/811096151、登录grafana 界面2、配置Grafana启用zabbix插件3、配置Zabbix数据源4、添加仪表板(仪表盘的使用)、面板5、创建流量监控图形6、仪表盘模板功能7、查询格式8、变量的使用4、爬虫可视化动态监控grafana influxdb MongoDB爬虫可视化动态监控https://www.jianshu.com/p/9de223e05a5e原理爬虫将抓取的数据写入MongoDBInfluxDB从MongoDB获取数据抓取情况Grafana 从 InfluxDB 中获取爬虫抓取数据情况并做图形化展示。grafana influxdbscrapy、scrapy-redis 使用 grafana、influxdb 可视化监控架构说明Scrapy替换内置STATS_CLASS爬虫运行指标定时推送到InfluxDB 2.x时序库PyPIscrapy‑redis分布式爬虫Redis‑exporter采集 Redis 队列深度待爬 URL 队列长度Grafana对接 InfluxDB 数据源做大盘可视化、告警Scrapy 项目接入 InfluxDBscrapy‑influxdb‑exporter该库替换 Scrapy 内置统计收集器自动把爬虫全部 stats 写入 InfluxDB2兼容 scrapy‑redis 分布式爬虫安装依赖pip install scrapy-influxdb-exporter influxdb-client修改settings.py# InfluxDB2 监控配置 INFLUXDB_HOST http://127.0.0.1:8086 INFLUXDB_ORG crawler-org INFLUXDB_BUCKET scrapy-stats INFLUXDB_TOKEN my‑super‑secret‑token INFLUXDB_MEASUREMENT_NAME scrapy_stats # 核心替换Scrapy内置统计类所有stats自动上报InfluxDB STATS_CLASS scrapy_influxdb_exporter.statscollectors.InfluxDBStatsCollector # 上报间隔单位秒不要太小建议10‑30s STATS_INTERVAL 15上报指标自动包含response_received_count、item_scraped_count、item_dropped_countlog_count/ERROR、downloader/request_count、downloader/response_status_count/403/429/500scrapy‑redis 关键指标scheduler/enqueued/redis、scheduler/dequeued/redisredis 出入队列计数scrapy‑Redis 额外监控Redis 待爬队列深度。crapy‑redis 队列长度待爬 url 数量不会自动上报必须单独采集是分布式爬虫最重要指标。方式 A写简单脚本定时写入 InfluxDB。后台运行脚本就可以采集每个爬虫 redis 待爬队列长度。# redis_queue_monitor.py import time import redis from influxdb_client import InfluxDBClient, Point r redis.Redis(host127.0.0.1, port6379, decode_responsesFalse) client InfluxDBClient(urlhttp://127.0.0.1:8086, tokenmy‑super‑secret‑token, orgcrawler‑org) write_api client.write_api() # 你的spider名字列表 spiders [spider_a,spider_b] while True: for name in spiders: key f{name}:requests queue_len r.llen(key) p Point(scrapy_redis_queue).tag(spider,name).field(queue_length, queue_len) write_api.write(bucketscrapy‑stats, orgcrawler‑org, recordp) time.sleep(15)方式 BPrometheus redis‑exporterscrapy‑prometheus‑exporterScrapy 暴露 Prometheus metrics HTTP 接口redis‑exporter采集 scrapy‑redis Redis 队列指标待爬任务数prometheus拉取指标、存储时序数据。对比 InfluxDBPrometheus 是云原生标准生态成熟告警能力强更适合爬虫生产监控。grafana可视化大盘、告警Grafana 配置 InfluxDB 数据源Grafana → Connections → Data sources → Add data source → 选择InfluxDBQuery language 选择FluxInfluxDB2 必须 Flux不要选 InfluxQLURLhttp://influxdb2:8086Token填入my‑super‑secret‑tokenOrganizationcrawler‑orgBucketscrapy‑stats保存并测试。常用 Flux 查询语句直接复制到 Grafana 面板每秒抓取 Item 速率from(bucket:scrapy-stats) | range(start: v.timeRangeStart, stop:v.timeRangeStop) | filter(fn: (r) r._measurement scrapy_stats and r._field item_scraped_count) | aggregateWindow(every: 10s, fn: difference)HTTP 403/429/500 错误数量from(bucket:scrapy-stats) | range(start: v.timeRangeStart) | filter(fn: (r) r._measurement scrapy_stats and r._field ~ /downloader\/response_status_count\/403|429|500/) | aggregateWindow(every:10s, fn:difference)scrapy‑redis 待爬队列长度from(bucket:scrapy-stats) | range(start: v.timeRangeStart) | filter(fn: (r) r._measurement scrapy_redis_queue and r._field queue_length)ERROR 日志计数from(bucket:scrapy-stats) | range(start: v.timeRangeStart) | filter(fn: (r) r._measurement scrapy_stats and r._field log_count/ERROR) | aggregateWindow(every: 10s, fn: difference)大盘面板建议数字面板总抓取 Item、当前待爬队列长度折线图Item 抓取速率、QPS折线图403/429/500 错误趋势折线图Redis 待爬队列深度告警队列长时间不变爬虫卡死错误率超过阈值触发告警Netdata官网https://www.netdata.cloud/官网有在线演示 demo5、python 进行数据可视化现在市面上可以使用 python 的可视化库多如牛毛各有各的优点。一个经典的可视化库 matplotlib 它是基于 NumPy 的一个数据可视化工具内置了非常多图给我们使用。下载这个库python -m pip install matplotlib示例画 sin 和 cos线import numpy as np import matplotlib.pyplot as plt x np.linspace(-np.pi, np.pi, 256) cos np.cos(x) sin np.sin(x) plt.plot(x, cos, --, linewidth2) plt.plot(x, sin) plt.show()画个饼图画直方图更多关于 matplotlib 的文档可以到以下链接查看https://matplotlib.org/2.0.2/contents.htmlseabornseaborn 是基于 matplotlib 的库所以有更加高级的接口给我们使用相对来说更加简单使用一些画个散点图。安装pip install seaborn数据源https://github.com/mwaskom/seaborn-data下载 seaborn-data然后解压后将文件拖进电脑中 seaborn-data 的空文件夹里即可。import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns sns.set(styledarkgrid) tips sns.load_dataset(tips) sns.relplot(xtotal_bill, ytip, datatips) plt.show()画个折线图fmri sns.load_dataset(fmri)sns.relplot(xtimepoint, ysignal, hueevent, kindline, datafmri);plt.show()画个直方图titanic sns.load_dataset(titanic)sns.catplot(xsex, ysurvived, hueclass, kindbar, datatitanic);plt.show()更多关于 seabornhttps://seaborn.pydata.org/index.html百度开源的数据可视化的 echarts 的库来画个直方图from pyecharts.charts import Bar from pyecharts import options as opts bar ( Bar() .add_xaxis([衬衫, 毛衣, 领带, 裤子, 风衣, 高跟鞋, 袜子]) .add_yaxis(商家A, [114, 55, 27, 101, 125, 27, 105]) .add_yaxis(商家B, [57, 134, 137, 129, 145, 60, 49]) .set_global_opts(title_optsopts.TitleOpts(title某商场销售情况)) ) bar.render()画个饼图def pie_base() - Pie: c ( Pie() .add(, [list(z) for z in zip(Faker.choose(), Faker.values())]) .set_global_opts(title_optsopts.TitleOpts(titlePie-基本示例)) .set_series_opts(label_optsopts.LabelOpts(formatter{b}: {c})) ) return c # 需要安装 snapshot_selenium make_snapshot(driver, pie_base().render(), pie.png)再来画个词云图words [ (Sam S Club, 10000), (Macys, 6181), (Amy Schumer, 4386), (Jurassic World, 4055), (Charter Communications, 2467), (Chick Fil A, 2244), (Planet Fitness, 1868), (Pitch Perfect, 1484), (Express, 1112), (Home, 865), (Johnny Depp, 847), (Lena Dunham, 582), (Lewis Hamilton, 555), (KXAN, 550), (Mary Ellen Mark, 462), (Farrah Abraham, 366), (Rita Ora, 360), (Serena Williams, 282), (NCAA baseball tournament, 273), (Point Break, 265), ] def wordcloud_base() - WordCloud: c ( WordCloud() .add(, words, word_size_range[20, 100]) .set_global_opts(title_optsopts.TitleOpts(titleWordCloud-基本示例)) ) return c # 需要安装 snapshot_selenium make_snapshot(driver, wordcloud_base().render(), WordCloud.png)更多关于 pyechartshttps://pyecharts.org