AspNet Core 下利用普罗米修斯+Grafana构建Metrics和服务器性能的监控

概述

Prometheus是一套开源的监控&报警&时间序列数据库的组合,起始是由SoundCloud公司开发的。该项目有非常活跃的社区和开发人员,目前是独立的开源项目,现在最常见的Kubernetes容器管理系统中,通常也会搭配Prometheus进行监控。

prometheus大多数组件都是用Go编写的,他们可以非常轻松的基于二进制文件部署和构建

Prometheus的主要特点

  • 自定义多维数据模型(时序列数据由metric名和一组key/value标签组成)

  • 内置PromQL是一种灵活的多维度查询语言

  • 不依赖分布式存储; 支持单个服务器节点自治工作

  • 基于HTTP的pull方式采集时序数据

  • 可以通过push gateway进行时序列数据推送(pushing)

  • 可以通过服务发现或者静态配置去获取要采集的目标服务器

  • 内置简单实用的可视化图表及仪表盘

组件

  • (Prometheus server)最主要的就是Prometheus Server,它用来收集和存储时序数据

  • (client libraries) 客户端用来检测被监控的应用程序代码

  • (push gateway) 支持瞬时的网关推送处理

  • (*. exporters)包括对 HAProxy, StatsD, Graphite等的额外支持

  • (alertmanager)监控预警管理、以及各种工具

  • 大量的支持工具

结构图

640?wx_fmt=jpeg

适用场景

Prometheus能够更好的记录数字类型的时序数据,它既可用于对服务器自身性能参数的监视,也适用于高度动态的面向各个服务的监视。在微服务的场景中,它对多维数据收集和查询的支持有很特殊的优势。

为可靠性而设计的Prometheus可以让您在宕机的时候快速诊断分析问题。每个Prometheus服务器都是独立的,不依赖于网络存储或其他远程服务,当基础结构的其他部分损坏时,您可以依赖它,并且不需要设置更多额外的基础结构来使用它。

不适用场景

Prometheus非常重视可靠性,即使在发生故障的情况下,你也可以查看有关系统中的可用统计信息,但是如果你需要百分之百精准统计(如:每次账单请求信息)对Prometheus而言是不适用的,因为收集的数据可能不够详细和完整。在这种情况下,最好使用其他系统来收集和分析数据,并使用Prometheus进行其余额外的监视。

安装node_exporter,系统性能指数收集(收集系统性能情况)

node_exporter 主要用于系统监控, 用 Golang 编写,其默认是9100端口,可以通过/metrics访问

下载文件

cd /tmp
wget https://github.com/prometheus/node_exporter/releases/download/v0.17.0/node_exporter-0.17.0.linux-amd64.tar.gz

解压并复制node_exporter应用程序到/usr/local/bin

tar xvf node_exporter-0.17.0.linux-amd64.tar.gz
sudo cp node_exporter-0.17.0.linux-amd64/node_exporter /usr/local/bin
sudo chown node_exporter:node_exporter /usr/local/bin/node_exporter

清理下载的文件和解压的文件夹

rm -rf node_exporter-0.17.0.linux-amd64.tar.gz node_exporter-0.17.0.linux-amd64

添加自启动服务

添加服务配置文件

sudo vim /etc/systemd/system/node_exporter.service

写入配置内容

[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target

node_exporter收集性能点的配置

默认node_exporter会启动一些收集器,你也可以通过服务配置文件进行配置

比如:

ExecStart=/usr/local/bin/node_exporter --collectors.enabled meminfo,hwmon,entropy

启动服务并配置自启动

sudo systemctl daemon-reload
sudo systemctl enable node_exporter
sudo systemctl start node_exporter
sudo systemctl status node_exporter

prometheus的安装

添加prometheus专用的用户名

sudo useradd --no-create-home --shell /usr/sbin/nologin prometheus
sudo useradd --no-create-home --shell /bin/false node_exporter

添加prometheus的目录

sudo mkdir /etc/prometheus
sudo mkdir /var/lib/prometheus

文件夹授权

sudo chown prometheus:prometheus /etc/prometheus
sudo chown prometheus:prometheus /var/lib/prometheus

下载并解压prometheus

wget https://github.com/prometheus/prometheus/releases/download/v2.8.0/prometheus-2.8.0.linux-amd64.tar.gz
tar xfz prometheus-2.8.0.linux-amd64.tar.gz
cd cd prometheus-2.8.0.linux-amd64

拷贝可执行文件到/usr/local/bin

sudo cp ./prometheus /usr/local/bin/
sudo cp ./promtool /usr/local/bin/

授予文件权限

sudo chown prometheus:prometheus /usr/local/bin/prometheus
sudo chown prometheus:prometheus /usr/local/bin/promtool

拷贝目录

sudo cp -r ./console_libraries /etc/prometheus
sudo cp -r ./consoles /etc/prometheus

授权文件夹

sudo chown -R prometheus:prometheus /etc/prometheus/consoles
sudo chown -R prometheus:prometheus /etc/prometheus/console_libraries

清理无用的下载文件和解压的文件夹

cd .. && rm -rf prometheus-*

创建Prometheus配置文件

sudo vim /etc/prometheus/prometheus.yml
注意YML配置文件的缩进

global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node_exporter'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9100']

配置所有者

sudo chown prometheus:prometheus /etc/prometheus/prometheus.yml

启动prometheus

sudo -u prometheus /usr/local/bin/prometheus --config.file /etc/prometheus/prometheus.yml --storage.tsdb.path /var/lib/prometheus/ --web.console.templates=/etc/prometheus/consoles --web.console.libraries=/etc/prometheus/console_libraries

检测

http://服务器ip:9090

640?wx_fmt=jpeg

配置自启动

sudo vim /etc/systemd/system/prometheus.service

[Unit]
Description=Prometheus Monitoring
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /var/lib/prometheus/ \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

sudo systemctl daemon-reload
sudo systemctl enable prometheus
sudo systemctl start prometheus
sudo systemctl status prometheus

配置安装Grafna

cd /tmp
wget https://dl.grafana.com/oss/release/grafana-6.0.1-1.x86_64.rpm 
sudo yum localinstall grafana-6.0.1-1.x86_64.rpm

配置自启动

sudo systemctl daemon-reload && sudo systemctl enable grafana-server && sudo systemctl start grafana-server

登录

启动grafana后可以通过 http://ip:3000登录
640?wx_fmt=jpeg

初始用户名和密码都是 admin
初次登录后会要求修改密码

配置数据源到之前的Prometheus

640?wx_fmt=jpeg

选择Prometheus数据源

640?wx_fmt=jpeg

640?wx_fmt=jpeg

配置仪表盘

安装饼图插件

grafana-cli plugins install grafana-piechart-panel

导入仪表样本

样本来源,打开浏览器打开如下地址并记录id

https://grafana.com/dashboards/8919

640?wx_fmt=jpeg

640?wx_fmt=jpeg

选择Import

640?wx_fmt=jpeg

然后输入之前记录的id
640?wx_fmt=jpeg

选择prometheus的数据源,之后打开dashboard就可以看到漂亮的仪表盘了

640?wx_fmt=jpeg

AspNet Core App Metrics的监控配置

nuget安装包

App.Metrics.Formatters.Prometheus
App.Metrics.AspNetCore

修改program.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using App.Metrics;
using App.Metrics.AspNetCore;
using App.Metrics.Formatters;
using App.Metrics.Formatters.Prometheus;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace PrometheusAppMetricsDemo
{
public static class Program
{

public static IMetricsRoot Metrics { get; set; }

public static IWebHost BuildWebHost(string[] args)
{
Metrics = AppMetrics.CreateDefaultBuilder()
.OutputMetrics.AsPrometheusPlainText()
.OutputMetrics.AsPrometheusProtobuf()
.Build();

return WebHost.CreateDefaultBuilder(args)
.ConfigureMetrics(Metrics)
.UseMetrics(
options =>
{
options.EndpointOptions = endpointsOptions =>
{
endpointsOptions.MetricsTextEndpointOutputFormatter = Metrics.OutputMetricsFormatters
.GetType<MetricsPrometheusTextOutputFormatter>();
endpointsOptions.MetricsEndpointOutputFormatter = Metrics.OutputMetricsFormatters
.GetType<MetricsPrometheusProtobufOutputFormatter>();
};
})
.UseKestrel(options => options.Listen(IPAddress.Any, 5000))
.UseStartup<Startup>()
.Build();
}

public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
}
}

配置prometheus的job

global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node_exporter'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9100']
- job_name: 'netcoreprometheus'
scrape_interval: 5s
scheme: https
tls_config:
insecure_skip_verify: true
metrics_path: /metrics-text
static_configs:
- targets: ['{demo_ip}:{demoport}']

为什么是metrics-text 参见appmetrics官方解释
/metrics-text will return metrics in Prometheus plain text format and /> metrics in Prometheus protobuf format.

重启prometheus

sudo systemctl restart prometheus
sudo systemctl status prometheus

640?wx_fmt=jpeg

配置Grafana仪表盘

直接可以用的仪表盘地址:

按之前配置的方式配置完成后,就可以看到grafana正常的从prometheus收集数据并展示了
640?wx_fmt=jpeg

原文地址:

https://www.cnblogs.com/linkanyway/p/Configure-a-Prometheus-Monitoring-Server-with-a-Gr.html

.NET社区新闻,深度好文,欢迎访问公众号文章汇总 http://www.csharpkit.com
640?wx_fmt=jpeg


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/316734.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

模板:pb_ds指南

科技改变生活 前言 本来一直被畏于巨长的声明&#xff0c;没有学这个东西… 直到 棘手的操作 这道题&#xff0c;pb_ds模拟实现的两个log的做法不仅好写的一批&#xff0c;连时间竟然也把我单log的左偏树爆踩了&#xff1f;&#xff1f;&#xff1f; … 我选择打不过就加入… …

【学习笔记】多重背包相关优化——二进制优化/单调队列优化

多重背包——二进制优化/单调队列优化二进制优化单调队列优化代码都是 POJ1742 的&#xff0c;注意&#xff0c;那道题二进制优化会超时。 普通的多重背包式子&#xff0c;物品个数限制&#xff1a;c[i]c[i]c[i]&#xff0c;单个物品价值 w[i]w[i]w[i]&#xff0c;每个物品的体…

Game of Swapping Numbers

Game of Swapping Numbers 题意&#xff1a; A&#xff0c;B两个数组&#xff0c;让你对A进行k次操作&#xff0c;每次操作为选两个位置的数&#xff0c;进行交换&#xff0c;求最大化的Σ|Ai-Bi| 题解&#xff1a; 以前有做过最小化的情况&#xff0c;就是把每次交换作定量…

软件工程真的是一门什么用都没有的学科么?

软件工程真的是一门什么用都没有的学科么&#xff1f;-----读《构建之法》有感楔子我很惭愧&#xff0c;构建之法这本书已经出版四五年了&#xff0c;我之前却未曾涉猎&#xff0c;还是在通过组织长沙.net技术社区之后&#xff0c;才因为因缘际遇有幸认识邹欣邹老师之后&#x…

Ball Dropping

Ball Dropping 题意&#xff1a; 求&#xff1f;的具体长度 题解&#xff1a; 算一算就出来了 代码&#xff1a; #include<bits/stdc.h> using namespace std; int main(){double r,a,b,h;cin>>r>>a>>b>>h;if(2*r<b&&2*r<…

[WF2011] MachineWorks(李超树优化dp)

[WF2011]MachineWorksproblem BZOJ3963 solution 来得比较快的是&#xff0c;直接设 dpi,j:dp_{i,j}:dpi,j​: 考虑第 jjj 天换购 iii 机器。 但是马上注意到天数是 1e91e91e9 级别的&#xff0c;而机器是 1e51e51e5 级别。 稍微想想&#xff0c;就能知道&#xff0c;因为…

P3644 [APIO2015]八邻旁之桥(中位数、堆)

前言 卡了很长时间的一个题。 一开始 k1 的关键性质把握就跑偏了&#xff0c;后面基本在硬做… 关键就是一直把每个人当成一条线段作为整体在看&#xff0c;使问题很复杂… 最后用 three-pointers 磕磕绊绊搞出来了。 但是根本不用&#xff01; 解析 这题关键就在于&#xf…

尝试:Script Lab,开发模式之知识储备//SL02

前期00&#xff1a;深度&#xff1a;从 Office 365 新图标来看微软背后的设计新理念前期01&#xff1a;尝试&#xff1a;Script Lab&#xff0c;快速 Office 365 开发工具 //SL01本期02&#xff1a;尝试&#xff1a;Script Lab&#xff0c;开发模式之知识储备 //SL02项目特点适…

【学习笔记】Miller-Rabin(米勒-拉宾)素性测试,附常用表

TOC 素性测试是检验一个给定的整数是否为素数的测试。 最简单的就是用 n\sqrt{n}n​ 以内的数去试除。这是确定性的算法&#xff0c;即能准确知道 nnn 是否为质数。 但今天学习的是一种随机算法。 Fermat 小定理 如果 ppp 是一个质数&#xff0c;且 a%p≠0a\%p≠0a%p​0…

Hash Function

Hash Function 文章目录题意&#xff1a;题解&#xff1a;代码NTT代码FFT代码题意&#xff1a; 给定n个互不相同的数&#xff0c;找一个最小的模域&#xff0c;使得它们在这个模域下互不相同。n<5e5 题解&#xff1a; 考虑两个数a和b&#xff0c;a与b模m余数相同&#xf…

P5321 [BJOI2019]送别(LCT)

Foreword\text{Foreword}Foreword 肝了一下午一晚上的码农题… &#xff08;主要就是在 debug&#xff0c;LCT 太难 de 了…&#xff09; 感谢 M_sea&#xff0c;在调无可调认为LCT会不会不可做时&#xff0c;我看到了他的题解&#xff0c;几乎一样的思路&#xff0c;给了我继…

WebApi网关之Bumblebee和Ocelot性能对比

Bumblebee是基于.net core 2.1开发的WebApi网关组件&#xff0c;由于Bumblebee所追求的轻量化和性能&#xff0c;所以它并没有像Ocelot那样从asp.net core上进行扩展&#xff1b;而是构建在BeetleX.FastHttpApi之上&#xff0c;主要原因BeetleX.FastHttpApi有着更轻量化和高性能…

【无码专区11】异或2(结论 / 推式子 + 哈希hash + 大整数高精度 加减乘除重载考察)

本题已自我实现。但仍归于无码专区 problem 求 ∑i1n−1i⨁(n−i)\sum_{i1}^{n-1}i\bigoplus (n-i)∑i1n−1​i⨁(n−i)。 20%,n≤1e6;;50%,n≤1e9;;70%,n≤1e18;;100%,n≤1050020\%,n\le 1e6;;50\%,n\le 1e9;;70\%,n\le 1e18;;100\%,n\le 10^{500}20%,n≤1e6;;50%,n≤1e9;;7…

模板:常系数齐次线性递推(线性代数、多项式)

所谓常系数齐次线性递推&#xff0c;就是系数为常数的齐次线性递推。 &#xff08;逃&#xff09; 前言 sto Asta orz&#xff01; 又是一个名字高大上&#xff0c;实则小清新的算法&#xff01; 解析 考虑一个 k 次的线性递推&#xff1a; an∑i1kfian−ia_n\sum_{i1}^kf_…

2021牛客暑期多校训练营1

2021牛客暑期多校训练营1 题号题目知识点难度AAlice and Bob博弈论BBall Dropping计算几何签到CCut the TreeDDetermine the Photo Position签到EEscape along Water PipeFFind 3-friendly Integers真签到GGame of Swapping Numbers思维题&#xff0c;推导HHash FunctionFFT&a…

【无码专区12】子集和(背包dp)

此题已自我实现&#xff0c;但仍归于无码专区 本题在考场上就过了&#xff0c;所以难度并不高&#xff0c;发现性质即可。 problem 有 nnn 个正整数 a1,a2,...,ana_1,a_2,...,a_na1​,a2​,...,an​&#xff0c;他们的和为 mmm。你想对于其每一个子集 SSS&#xff0c;求出他…

Penguins

Penguins 题意&#xff1a; 有两个20*20的地图&#xff0c;有障碍物&#xff0c;两个地图各有一个小人&#xff0c;左侧地图的小人要从右下角走到右上角&#xff0c;右侧地图的小人要从左下角走到左上角&#xff0c;这两个小人是镜像移动的&#xff0c; 左侧小人右侧小人左移…

盲盒(随机概率 + 最大公约数)

盲盒problemsolutioncodeproblem 有 2n2n2n 个盲盒&#xff0c;每个盲盒有一个惊喜值 aia_iai​。 打开恰好 nnn 个盲盒&#xff0c;获得的惊喜值为这些盲盒惊喜值的最大公约数。 求能获得的最大惊喜值。 n≤1e5,ai≤1e12n\le 1e5,a_i\le 1e12n≤1e5,ai​≤1e12。 solution…

P5354 [Ynoi2017] 由乃的 OJ(树剖、位运算)

前言 当暴力思路与题解中的“暴力”不同时&#xff0c;继续想优化往往就渐行渐远了… 所以当没有头绪时&#xff0c;要勇于跳出原有的转化&#xff01; 这种位运算类型的优化似乎始终不在我的寄存器中…需要加强&#xff01; 解析 不难想到按位考虑的 O(nklog⁡2n)O(nk\log…

在 .NET Core 中运行 JavaScript

一.前言在 .NET Framework 时&#xff0c;我们可以通过V8.NET等组件来运行 JavaScript&#xff0c;不过目前我看了好几个开源组件包括V8.NET都还不支持 .NET Core &#xff0c;我们如何在 .NET Core 中运行 JavaScript 呢&#xff0c;答案是使用 NodeServices。关于为何有在 .N…