建筑公司网站建设方案,临沂市建设安全管理网站,做网站建设一条龙全包,重庆在线高校文章目录 前言一、概念1.1 发展1.2 时序数据1.3 Metric 二、Prometheus2.1 架构2.2 配置2.3 查询语言PromQL2.4 Exporter 三、Grafana3.1 数据源3.2 权限3.3 面板可视化3.4 仪表盘 四、实战4.1 监控 Windows/Linux4.2 监控 JVM4.3 监控 MySQL4.4 监控 Springboot API 参考 前言… 文章目录 前言一、概念1.1 发展1.2 时序数据1.3 Metric 二、Prometheus2.1 架构2.2 配置2.3 查询语言PromQL2.4 Exporter 三、Grafana3.1 数据源3.2 权限3.3 面板可视化3.4 仪表盘 四、实战4.1 监控 Windows/Linux4.2 监控 JVM4.3 监控 MySQL4.4 监控 Springboot API 参考 前言
Prometheus 是用 GO 语言开发的一个开源的系统监控和告警工具包最初是 2012 年 SoundCloud 发布的后面被很多大公司组织所使用于2016年加入了云原生云计算基金会(Cloud Native Computing FoundationCNCF)并于2018年毕业它现在是一个独立的开源项目并独立于任何公司进行维护。
Prometheus是一个非常优秀的监控工具准确的说更是一套监控方案。Prometheus提供了监控数据收集存储处理可视化和告警的一套完整的监控解决方案。 官方网站https://prometheus.io/ github 地址https://github.com/prometheus/prometheus
Grafana 是一个开源的跨平台的度量分析、可视化工具支持多种数据源如Prometheus、Elasticsearch、InfluxDB等。它提供了丰富的可视化图表和面板可以帮助用户更好地理解和分析监控数据。
文档地址https://grafana.com/docs/grafana/latest/github 地址https://github.com/grafana/grafana
Prometheus 自身携带一个web UI用来显示数据图表但是有点简陋而 Grafana 能够支持精美图表的制作并显示而且自身支持Prometheus, 所以经典的监控方案就是 Prometheus Grafana。
一、概念
1.1 发展
运维监控的发展历程可以追溯到计算机技术的早期阶段。随着计算机技术的不断发展运维监控也在不断地演化和改进。
最初的运维监控主要是通过手动检查系统日志和性能指标来进行的这种方法非常耗时且容易出错。随着计算机技术的不断发展出现了一些基于SNMP 协议的监控工具例如Nagios、Zabbix等等。这些工具可以自动地收集系统日志和性能指标并对其进行分析和处理从而帮助用户更好地了解系统的运行状态。近年来随着云计算和容器技术的不断发展运维监控也在不断地演化和改进。例如 Prometheus就是一种基于云原生技术的开源监控系统它可以帮助用户更好地管理和监控云原生应用程序。
总之随着计算机技术的不断发展运维监控也在不断地演化和改进从最初的手动检查到基于SNMP协议的监控工具再到现在的自动化运维监控工具运维监控为用户提供更好的服务和支持。
1.2 时序数据
时序数据即时间序列数据(Time Series Data)按时间维度顺序记录且索引的数据。像物联网、车联网、工业互联网等领域各种类型的设备都会产生海量的时序数据这些数据将占世界数据总量的90%以上。在监控平台时序数据常常指的是系统的性能指标、日志信息等等带有时间戳的序列数据。
时序数据和传统关系数据的对比时序数据侧重CRUD中的CR没有U
1.3 Metric
metric度量指标一个很重要的概念在运维监控出现的非常高频是指监控系统中的指标例如CPU使用率、内存使用率、网络流量等等在Prometheus 其本质是指存在于数据库的一条记录。
在Prometheus client 中可分为4种类型
Counter一种累积度量它表示一个单调递增的计数器其值只能在重新启动时增加或重置为零。例如可以使用计数器来表示所服务的请求、已完成的任务或错误的数量。Gauge一个可以任意起伏的单个数值。 测量器通常用于测量值比如温度或当前内存使用量但也用于可能上下波动的“计数”比如并发请求的数量。Histogram直方图表示一段时间内的数据采样统计结果通过分桶bucket的方式来统计样本的分布。比如统计接口的耗时多少的请求落在 10ms - 20ms多少的请求落在 20ms - 30ms 等。Summary和Histogram类似根据样本统计出百分位。例如统计链路耗时TP99 是多少TP95 是多少等。
二、Prometheus
2.1 架构 Prometheus Server利用服务发现机制获取需要监控的 target通过Pull方式从target处拉取指标数据根据定义的rule可以提前对指标数据再次进行计算触发报警的发送到 alertmanager组件用于收集和存储时间序列数据。 PushGateway各个目标主机可上报数据到 PushGateway然后Prometheus server统一从pushgateway拉取数据。 Exporters采集已有的第三方服务监控指标并暴露metrics。Prometheus支持多种exporter通过exporter可以采集metrics数据然后发送到Prometheus server 端。 Alertmanager组件根据报警的告警方式发送相应的通知。从Prometheus server端接收到alerts 后会进行去重分组并路由到相应的接收方发出报警常见的接收方式有电子邮件微信钉钉slack等。 Grafana数据可视化组件监控仪表盘通过PromQL从Prometheus Server查询数据进行展示 Prometheus web UI简单的Web控制台,默认端口9090
2.2 配置
Prometheus 可以通过 --config.file 命令选项来加载配置文件。
当启用–web.enable-lifecycle可通过URL /-/reload 发送 POST 请求实现不需要重启Prometheus 加载配置文件
配置文档https://prometheus.io/docs/prometheus/latest/configuration/configuration/下面是4个常用的配置大类 global 配置全局的信息如监控数据的间隔业务的超时时间告警规则执行周期等 scrape_interval 拉取 targets 的默认时间间隔默认1mscrape_timeout 拉取超时时间默认10sevaluation_interval 执行rules间隔时间默认1m rule_files 包含两种规则文件记录规则和告警规则。 记录规则 记录规则允许预先计算经常需要的或计算成本昂贵的表达式并将其结果保存为一组新的时间序列。查询预先计算的结果通常会比每次需要时执行原始表达式快得多。这对于仪表板特别有用它们需要在每次刷新时重复查询相同的表达式。 文档https://prometheus.io/docs/prometheus/latest/configuration/recording_rules/ 告警规则 根据PromQL来定义警报条件并向外部服务发送关于触发警报的通知。 文档https://prometheus.io/docs/prometheus/latest/configuration/alerting_rules/ alerting 配置管理 Alertmanager scrape_configs 配置拉取数据节点job文档https://prometheus.io/docs/prometheus/latest/configuration/configuration/#scrape_config job_name job名字scrape_interval 抓取频率默认globa.scrape_intervalscrape_timeout 抓取超时默认globa.scrape_timeoutmetrics_path 抓取路径, 默认/metricstatic_configs 抓取目标URL地址
2.3 查询语言PromQL
Prometheus 提供了一种名为PromQLPrometheus Query Language的函数式查询语言允许用户实时选择和聚合时间序列数据.文档地址https://prometheus.io/docs/prometheus/latest/querying/basics/ 过滤查询 通过{} 对结果进行过滤内部标签值可使用表示相等! 表示不相等~表示正则匹配!~表示正则不匹配 http_requests_total{methodGET}
http_requests_total{environment~staging|testing|development,method!GET}
http_requests_total{status!~4..}范围时间查询 可通过[时间值]进行选择例如 http_requests_total [5m]偏移时间查询 通过offset 可以允许更改查询中单个即时向量和范围向量的时间偏移量。例如过去5分钟http请求总数 http_requests_total offset 5m定点时间查询 允许更改查询中 单个即时向量和范围向量的计算时间。提供给修改符的时间是一个unix时间戳用浮点数表示 例如返回在2021-01-04T07:40:0000:00时的值 http_requests_total 1609746000聚合查询 Prometheus 提供sum,max,min,avg,count,bottomk,topk等聚合命令查询数据 sum(http_requests_total)
sum by (application, group) (http_requests_total)
topk(5, http_requests_total)函数查询 Prometheus 提供函数参与计算查询数据文档https://prometheus.io/docs/prometheus/latest/querying/functions/ rate(http_requests_total[5m])[30m:1m]2.4 Exporter
Prometheus通过 Exporter 来获取数据可以根据文档中的链接按需下载安装https://prometheus.io/docs/instrumenting/exporters/
三、Grafana
3.1 数据源 3.2 权限
grafana 提供一套权限系统能够让用户根据不同的角色拥有不同的权限如面板的查看编辑等等
权限分为三种admin, viewer, editer
可通过邀请用户并向用户发送链接的操作来添加用户并且可以通过组来通知查看面板的权限控制
3.3 面板可视化
相关文档https://grafana.com/docs/grafana/latest/panels-visualizations/ 面板查询表达式 面板种类 最常见的就是Graph更多的种类可以去官网下载导入注意下版本 面板参数 y轴的参数如果是百分比可以通过如下控制
3.4 仪表盘
上述多个面板整合在一起就是仪表盘了 导入 除了可以自定义面板外还可以使用其他人制作好的面板 https://grafana.com/grafana/dashboards/通过菜单中的Import导入 查看 查看仪表盘可通过url添加参数kiosk 隐藏侧边栏和顶部菜单 匿名访问 修改配置文件conf/defaults.ini [auth.anonymous]
# 设置为true即可匿名访问不用登陆就可以直接访问url
enabled true嵌套允许 修改配置文件conf/defaults.ini # 设置为true即可嵌套
allow_embedding true变量 通过变量可以实现下拉列表选择想要显示的部分文档https://grafana.com/docs/grafana/latest/dashboards/variables/
四、实战
4.1 监控 Windows/Linux
windows: 下载Exporter https://github.com/prometheus-community/windows_exporter/releases
linux: 下载 https://github.com/prometheus/node_exporter/releases
下面以windows 为例
windows_exporter.exe --collectors.enabled [defaults],process,container
windows_exporter.exe --config.file config.yml监控项
监控指标表达式CPU 使用率100 - (avg by (instance,region) (irate(windows_cpu_time_total{mode“idle”}[2m])) * 100)内存 使用率100-(windows_os_physical_memory_free_bytes/windows_cs_physical_memory_bytes)*100磁盘总使用率(sum(windows_logical_disk_size_bytes{volume!~“Harddisk.}) by (instance) - sum(windows_logical_disk_free_bytes{volume!~Harddisk.”}) by (instance)) / sum(windows_logical_disk_size_bytes{volume!~Harddisk.}) by (instance) *100各个磁盘使用率100- 100 * (windows_logical_disk_free_bytes/windows_logical_disk_size_bytes)带宽(sum(irate(windows_net_bytes_total[1m])) 1)* 8系统线程windows_system_threads系统进程windows_os_processes
4.2 监控 JVM
下载Exporterhttps://github.com/prometheus/jmx_exporter/releases
java -javaagent:jmx_prometheus_javaagent-0.18.0.jar12345:config.yml -jar vhr-web-0.0.1-SNAPSHOT.jarrules:
- pattern: .*监控指标表达式jvm 堆内存使用jvm_memory_bytes_used{area“heap”}Eden 区使用jvm_memory_pool_bytes_used{pool“PS Eden Space”}Old 区使用jvm_memory_pool_bytes_used{pool“PS Old Gen”}元空间 使用jvm_memory_pool_bytes_used{pool“Metaspace”}gc时间increase(jvm_gc_collection_seconds_sum[$__interval])gc增长次数increase(jvm_gc_collection_seconds_count[$__interval])
4.3 监控 MySQL
https://github.com/prometheus/mysqld_exporter/releases
mysqld_exporter.exe --config.my-cnf config.cnf --web.listen-addresslocalhost:9104[client]
userroot
password监控指标表达式连接数sum(max_over_time(mysql_global_status_threads_connected[$__interval]))慢查询数sum(rate(mysql_global_status_slow_queries[$__interval]))平均运行线程数sum(avg_over_time(mysql_global_status_threads_running[$__interval]))当前QPSrate(mysql_global_status_queries[$__interval])
4.4 监控 Springboot API
有时候在 Springboot 项目中需要统计 API 接口的调用次数和调用时间可以使用actuatormicrometer 已经内置两个注解实现两者功能因为要使用到aop所以还需导入aop包
文档https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.enabling
dependencygroupIdio.micrometer/groupIdartifactIdmicrometer-registry-prometheus/artifactId
/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId
/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-aop/artifactId
/dependencymanagement:metrics:tags:application: ${spring.application.name}web:server:max-uri-tags: 200endpoints:web:exposure:include: prometheusspring:application:name: prometheus-test-apiBean
public TimedAspect timedAspect(MeterRegistry registry) {return new TimedAspect(registry);
}GetMapping(/test)
Timed(value test_method,description 测试接口耗时)
Counted(value test_method, description 测试接口次数)
public String test() {//try {// Thread.sleep(1000);//} catch (InterruptedException e) {// throw new RuntimeException(e);//}return ok;
}参考
PrometheusGrafana 搭建全方位的监控告警系统