Centos7匹配Locust+prometheus+grafana性能监控平台

news2024/10/6 22:33:54

一、安装Loust

之前已写过,这里忽略一万字。。。

Locust初次体验【解决webUI没数据】_Vikesgao的博客-CSDN博客

二、安装prometheus

        Prometheus 是一个开源的服务监控系统和时序数据库,其提供了通用的数据模型和快捷数据采集、存储和查询接口。它的核心组件Prometheus server会定期从静态配置的监控目标或者基于服务发现自动配置的自标中进行拉取数据,当新拉取到的数据大于配置的内存缓存区时,数据就会持久化到存储设备当中。

        1.每个被监控的主机都可以通过专用的exporter 程序提供输出监控数据的接口,它会在目标处收集监控数据,并暴露出一个HTTP接口供Prometheus server查询,Prometheus通过基于HTTP的pull的方式来周期性的采集数据。
        2.任何被监控的目标都需要事先纳入到监控系统中才能进行时序数据采集、存储、告警和展示,监控目标可以通过配置信息以静态形式指定,也可以让Prometheus通过服务发现的机制进行动态管理。
        3.Prometheus 能够直接把API Server作为服务发现系统使用,进而动态发现和监控集群中的所有可被监控的对象。

本次是安装到Centos7,所有下载一下Linus版本

下载压缩包
wget http://github.com/prometheus/prometheus/releases/download/v2.44.0/prometheus-2.44.0.linux-amd64.tar.gz

解压
tar -zvxf prometheus-2.44.0.linux-amd64.tar.gz

进入prometheus文件夹
cd prometheus-2.44.0.linux-amd64

启动prometheus
./prometheus --config.file=prometheus.yml &

访问页面-localhost:9090 或 ip:9090,正常访问Prometheus页面表示安装成功

三、添加locust服务到Prometheus

# 编辑prometheus.yml,注意填写服务master IP : port


# my global config
global:
  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).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# 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: ["192.168.24.228:9090"]
        labels:
          instance: prometheus

  - job_name: locust

    metrics_path: '/export/prometheus'
    static_configs:
      - targets: ['192.168.24.228:8089']
        labels:
          instance: locust

编辑配置文件,注意重新启动服务或重启centos7也行

----运行locust----指定运行prometheus_exporter.py

# centos7 作为主控运行刚才的监控文件
locust -f prometheus_exporter.py --master



prometheus_exporter.py
直接复制下面代码进行监察服务


# coding: utf8

import six
from itertools import chain

from flask import request, Response
from locust import stats as locust_stats, runners as locust_runners
from locust import User, task, events
from prometheus_client import Metric, REGISTRY, exposition
import os, sys
from locust import task, tag, TaskSet
from locust import HttpUser
pathS = os.getcwd()
Project_Path = os.path.dirname(os.path.dirname(os.path.dirname(pathS)))
root_Path = os.path.dirname(pathS)
sys.path.append(Project_Path)
sys.path.append(root_Path)

# This locustfile adds an external web endpoint to the locust master, and makes it serve as a prometheus exporter.
# Runs it as a normal locustfile, then points prometheus to it.
# locust -f prometheus_exporter.py --master

# Lots of code taken from [mbolek's locust_exporter](https://github.com/mbolek/locust_exporter), thx mbolek!


class LocustCollector(object):
    registry = REGISTRY

    def __init__(self, environment, runner):
        self.environment = environment
        self.runner = runner

    def collect(self):
        # collect metrics only when locust runner is spawning or running.
        runner = self.runner

        if runner and runner.state in (locust_runners.STATE_SPAWNING, locust_runners.STATE_RUNNING):
            stats = []
            for s in chain(locust_stats.sort_stats(runner.stats.entries), [runner.stats.total]):
                stats.append({
                    "method": s.method,
                    "name": s.name,
                    "num_requests": s.num_requests,
                    "num_failures": s.num_failures,
                    "avg_response_time": s.avg_response_time,
                    "min_response_time": s.min_response_time or 0,
                    "max_response_time": s.max_response_time,
                    "current_rps": s.current_rps,
                    "median_response_time": s.median_response_time,
                    "ninetieth_response_time": s.get_response_time_percentile(0.9),
                    # only total stats can use current_response_time, so sad.
                    # "current_response_time_percentile_95": s.get_current_response_time_percentile(0.95),
                    "avg_content_length": s.avg_content_length,
                    "current_fail_per_sec": s.current_fail_per_sec
                })

            # perhaps StatsError.parse_error in e.to_dict only works in python slave, take notices!
            errors = [e.to_dict() for e in six.itervalues(runner.stats.errors)]

            metric = Metric('locust_user_count', 'Swarmed users', 'gauge')
            metric.add_sample('locust_user_count', value=runner.user_count, labels={})
            yield metric

            metric = Metric('locust_errors', 'Locust requests errors', 'gauge')
            for err in errors:
                metric.add_sample('locust_errors', value=err['occurrences'],
                                  labels={'path': err['name'], 'method': err['method'],
                                          'error': err['error']})
            yield metric

            is_distributed = isinstance(runner, locust_runners.MasterRunner)
            if is_distributed:
                metric = Metric('locust_slave_count', 'Locust number of slaves', 'gauge')
                metric.add_sample('locust_slave_count', value=len(runner.clients.values()), labels={})
                yield metric

            metric = Metric('locust_fail_ratio', 'Locust failure ratio', 'gauge')
            metric.add_sample('locust_fail_ratio', value=runner.stats.total.fail_ratio, labels={})
            yield metric

            metric = Metric('locust_state', 'State of the locust swarm', 'gauge')
            metric.add_sample('locust_state', value=1, labels={'state': runner.state})
            yield metric

            stats_metrics = ['avg_content_length', 'avg_response_time', 'current_rps', 'current_fail_per_sec',
                             'max_response_time', 'ninetieth_response_time', 'median_response_time',
                             'min_response_time',
                             'num_failures', 'num_requests']

            for mtr in stats_metrics:
                mtype = 'gauge'
                if mtr in ['num_requests', 'num_failures']:
                    mtype = 'counter'
                metric = Metric('locust_stats_' + mtr, 'Locust stats ' + mtr, mtype)
                for stat in stats:
                    # Aggregated stat's method label is None, so name it as Aggregated
                    # locust has changed name Total to Aggregated since 0.12.1
                    if 'Aggregated' != stat['name']:
                        metric.add_sample('locust_stats_' + mtr, value=stat[mtr],
                                          labels={'path': stat['name'], 'method': stat['method']})
                    else:
                        metric.add_sample('locust_stats_' + mtr, value=stat[mtr],
                                          labels={'path': stat['name'], 'method': 'Aggregated'})
                yield metric


@events.init.add_listener
def locust_init(environment, runner, **kwargs):
    print("locust init event received")
    if environment.web_ui and runner:
        @environment.web_ui.app.route("/export/prometheus")
        def prometheus_exporter():
            registry = REGISTRY
            encoder, content_type = exposition.choose_encoder(request.headers.get('Accept'))
            if 'name[]' in request.args:
                registry = REGISTRY.restricted_registry(request.args.get('name[]'))
            body = encoder(registry)
            return Response(body, content_type=content_type)

        REGISTRY.register(LocustCollector(environment, runner))


class Dummy(User):
    @task(20)
    def hello(self):
        pass

重启后访问192.168.24.228:8089/export/prometheus,页面显示下截图,表示已经可以成功了

四、安装Grafana

# 下载
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-8.5.3-1.x86_64.rpm
# 安装
yum -y install grafana-enterprise-8.5.3-1.x86_64.rpm

#设置grafana服务开机自启,并启动服务

systemctl daemon-reload

systemctl enable grafana-server.service

systemctl start grafana-server.service

访问IP:3000页面

五、添加prometheus数据源

填写IP、端口即可,然后保存

 再导入需要模版【locust for prometheus】

六、master 及 worker命令启动

# Centos7 master启动
locust -f prometheus_exporter.py --master

# 负载加压设备 worker启动,默认使用5557端口
locust -f locustfile.py --worker --master-host=192.168.24.228 --master-port=5557

待解决问题---【未完】:

1.locust主从执行脚本,General有执行用户数,但没有显示请求数据【已解决】

# locustfile.py



# 【Centos-Jenkins执行路径】/var/lib/jenkins/workspace/GitBlit_pull/other/WB_Locust/my_locustfiles

import os, sys

pathS = os.getcwd()
Project_Path = os.path.dirname(os.path.dirname(os.path.dirname(pathS)))
root_Path = os.path.dirname(pathS)
sys.path.append(Project_Path)
sys.path.append(root_Path)

from locust import HttpUser, FastHttpUser, task, between, User, constant, constant_pacing, constant_throughput, events, LoadTestShape
from other.WB_Locust.my_locustfiles.locustfile_performance import performance_Test
from other.WB_Locust.my_locustfiles.prometheus_exporter import *

class ttt(HttpUser):
    host = 'http://m-test.wbiaotest.cn'
    tasks = [performance_Test]
    wait_time = between(1, 3)
# locust压测文件必须主从设备上都需要有
# prometheus_exporter.py

# coding: utf8

import os, sys

pathS = os.getcwd()
Project_Path = os.path.dirname(os.path.dirname(os.path.dirname(pathS)))
root_Path = os.path.dirname(pathS)
sys.path.append(Project_Path)
sys.path.append(root_Path)

import six
from itertools import chain

from flask import request, Response
from locust import stats as locust_stats, runners as locust_runners
from locust import User, task, events, HttpUser, between
from prometheus_client import Metric, REGISTRY, exposition

from other.WB_Locust.my_locustfiles.locustfile_performance import performance_Test
# This locustfile adds an external web endpoint to the locust master, and makes it serve as a prometheus exporter.
# Runs it as a normal locustfile, then points prometheus to it.
# locust -f prometheus_exporter.py --master

# Lots of code taken from [mbolek's locust_exporter](https://github.com/mbolek/locust_exporter), thx mbolek!


class LocustCollector(object):
    registry = REGISTRY

    def __init__(self, environment, runner):
        self.environment = environment
        self.runner = runner

    def collect(self):
        # collect metrics only when locust runner is spawning or running.
        runner = self.runner

        if runner and runner.state in (locust_runners.STATE_SPAWNING, locust_runners.STATE_RUNNING):
            stats = []
            for s in chain(locust_stats.sort_stats(runner.stats.entries), [runner.stats.total]):
                stats.append({
                    "method": s.method,
                    "name": s.name,
                    "num_requests": s.num_requests,
                    "num_failures": s.num_failures,
                    "avg_response_time": s.avg_response_time,
                    "min_response_time": s.min_response_time or 0,
                    "max_response_time": s.max_response_time,
                    "current_rps": s.current_rps,
                    "median_response_time": s.median_response_time,
                    "ninetieth_response_time": s.get_response_time_percentile(0.9),
                    # only total stats can use current_response_time, so sad.
                    # "current_response_time_percentile_95": s.get_current_response_time_percentile(0.95),
                    "avg_content_length": s.avg_content_length,
                    "current_fail_per_sec": s.current_fail_per_sec
                })

            # perhaps StatsError.parse_error in e.to_dict only works in python slave, take notices!
            errors = [e.to_dict() for e in six.itervalues(runner.stats.errors)]

            metric = Metric('locust_user_count', 'Swarmed users', 'gauge')
            metric.add_sample('locust_user_count', value=runner.user_count, labels={})
            yield metric

            metric = Metric('locust_errors', 'Locust requests errors', 'gauge')
            for err in errors:
                metric.add_sample('locust_errors', value=err['occurrences'],
                                  labels={'path': err['name'], 'method': err['method'],
                                          'error': err['error']})
            yield metric

            is_distributed = isinstance(runner, locust_runners.MasterRunner)
            if is_distributed:
                metric = Metric('locust_slave_count', 'Locust number of slaves', 'gauge')
                metric.add_sample('locust_slave_count', value=len(runner.clients.values()), labels={})
                yield metric

            metric = Metric('locust_fail_ratio', 'Locust failure ratio', 'gauge')
            metric.add_sample('locust_fail_ratio', value=runner.stats.total.fail_ratio, labels={})
            yield metric

            metric = Metric('locust_state', 'State of the locust swarm', 'gauge')
            metric.add_sample('locust_state', value=1, labels={'state': runner.state})
            yield metric

            stats_metrics = ['avg_content_length', 'avg_response_time', 'current_rps', 'current_fail_per_sec',
                             'max_response_time', 'ninetieth_response_time', 'median_response_time',
                             'min_response_time',
                             'num_failures', 'num_requests']

            for mtr in stats_metrics:
                mtype = 'gauge'
                if mtr in ['num_requests', 'num_failures']:
                    mtype = 'counter'
                metric = Metric('locust_stats_' + mtr, 'Locust stats ' + mtr, mtype)
                for stat in stats:
                    # Aggregated stat's method label is None, so name it as Aggregated
                    # locust has changed name Total to Aggregated since 0.12.1
                    if 'Aggregated' != stat['name']:
                        metric.add_sample('locust_stats_' + mtr, value=stat[mtr],
                                          labels={'path': stat['name'], 'method': stat['method']})
                    else:
                        metric.add_sample('locust_stats_' + mtr, value=stat[mtr],
                                          labels={'path': stat['name'], 'method': 'Aggregated'})
                yield metric


@events.init.add_listener
def locust_init(environment, runner, **kwargs):
    print("locust init event received")
    if environment.web_ui and runner:
        @environment.web_ui.app.route("/export/prometheus")
        def prometheus_exporter():
            registry = REGISTRY
            encoder, content_type = exposition.choose_encoder(request.headers.get('Accept'))
            if 'name[]' in request.args:
                registry = REGISTRY.restricted_registry(request.args.get('name[]'))
            body = encoder(registry)
            return Response(body, content_type=content_type)

        REGISTRY.register(LocustCollector(environment, runner))


# class Dummy(User):
#     @task(20)
#     def hello(self):
#         pass
# 调用压测类
class ttt(HttpUser):
    host = 'http://m-test.wbiaotest.cn'
    tasks = [performance_Test]
    wait_time = between(1, 3)

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

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

相关文章

LLM:LLaMA模型和微调的Alpaca模型

待写 LLaMA模型 论文原文:https://arxiv.org/abs/2302.13971v1 预训练数据 模型架构 模型就是用的transformer的decoder,所以在结构上它与GPT是非常类似的,只是有一些细节需要注意一下。 1、RMS Pre-Norm 2、SwiGLU激活函数 3、RoPE旋转位置编码 Alpaca模型 [Stanford …

cuda编程学习——CUDA全局内存性能优化(八)

前言 参考资料&#xff1a; 高升博客 《CUDA C编程权威指南》 以及 CUDA官方文档 CUDA编程&#xff1a;基础与实践 樊哲勇 文章所有代码可在我的GitHub获得&#xff0c;后续会慢慢更新 文章、讲解视频同步更新公众《AI知识物语》&#xff0c;B站&#xff1a;出门吃三碗饭 …

Python3数据分析与挖掘建模(8)检验

1. 假设检验 1.1 概述 假设检验是一种统计推断方法&#xff0c;用于对一个或多个总体参数提出关于其取值的假设&#xff0c;并根据样本数据对这些假设进行检验。假设检验的目的是根据样本数据提供统计上的证据&#xff0c;以便对总体参数的假设进行接受或拒绝。 在假设检验中…

JAVA基础 - 如何使用ClassLoader?

1. CLASSLOADER是什么 ClassLoader&#xff0c;类加载器。用于将CLASS文件动态加载到JVM中去&#xff0c;是所有类加载器的基类(Bootstrap ClassLoader不继承自ClassLoader)&#xff0c;所有继承自抽象的ClassLoader的加载器&#xff0c;都会优先判断是否被父类加载器加载过&a…

C++数据结构:二叉树之二(二叉搜索树)

文章目录 前言一、二叉搜索树的概念二、代码详解1、构建节点2、构建二叉树类3、插入方法4、删除方法5、四种遍历方法6、测试代码 总结 前言 前文已经讲了二叉树概念&#xff0c;并搞出一个数组存储的没写具体实用意义的二叉树&#xff0c;这篇文章将讲解二叉树的另一种存储方式…

限量内测名额释放:微信云开发管理工具新功能

我们一直收到大家关于云数据库管理、快速搭建内部工具等诉求&#xff0c;为了给大家提供更好的开发体验&#xff0c;结合大家的诉求&#xff0c;云开发团队现推出新功能「管理工具」&#xff0c;现已启动内测&#xff0c;诚邀各位开发者参与内测体验。 什么是「管理工具」 管…

当节点内存管理遇上 Kubernetes:自动调度与控制

原理 在现代的容器化环境中&#xff0c;节点资源的管理是一个重要的任务。特别是对于内存资源的管理&#xff0c;它直接影响着容器应用的性能和可用性。在 Kubernetes 中&#xff0c;我们可以利用自动调度和控制的机制来实现对节点内存的有效管理。本文将介绍一种基于 Bash 脚…

EM中等效原理

EM中等效原理 一、基本简介 电磁等效定理对于简化许多问题的解是有用的。此外&#xff0c;它们还提供了对麦克斯韦系统电磁场行为的物理见解。它们与唯一性定理和惠更斯原理密切相关。一个应用是它们在研究来自孔径天线或来自激光腔输出的辐射中的用途。 等效源原理&#xf…

3.2 掌握RDD算子

一、准备工作 &#xff08;一&#xff09;准备文件 1、准备本地系统文件 2、把文件上传到HDFS &#xff08;二&#xff09;启动Spark Shell 1、启动HDFS服务 2、启动Spark服务 3、启动Spark Shell 二、掌握转换算子 &#xff08;一&#xff09;映射算子 - map() …

Sketch在线版免费使用,Windows也能用的Sketch!

Sketch 的最大缺点是它对 Windows/PC 用户不友好。它是一款 Mac 工具&#xff0c;无法在浏览器中运行。此外&#xff0c;使用 Sketch 需要安装其他插件才能获得更多响应式设计工具。然而&#xff0c;现在有了 Sketch 网页版工具即时设计替代即时设计&#xff01; 即时设计几乎…

通达信凹口平量柱选股公式,倍量柱之后调整再上升

凹口平量柱是一组量柱形态&#xff0c;表现为量柱两边高、中间扁平或圆底的形态。如下图所示&#xff0c;左右各有一根高度持平的高量柱&#xff0c;中间夹杂着三五根甚至更多根低量柱。 凹口平量柱选股公式需要结合量柱以及K线&#xff0c;主要考虑以下三点&#xff1a; 1、倍…

git各阶段代码修改回退撤销操作

git push origin master 的含义是将本地当前分支的提交推送到名为 origin 的远程仓库的 master 分支上。 各阶段代码修改回退撤销的操作 case1 git checkout -- . 修改了文件内容但没还有git add 或git commit时撤销当前目录下所有文件的修改 case2 当完成了git add 之后&a…

项目管理:面对未知的挑战时,如何获取和使用信息?

一项实验展示了人们在面对未知的挑战时&#xff0c;对信息的获取和使用的影响。在下面的实验中&#xff0c;三组人被要求步行到十公里外的三个村庄。 第一组人没有任何信息&#xff0c;只跟着向导走。他们在走了短短的两三公里后就开始抱怨和情绪低落&#xff0c;同时感到疲惫…

2022年天府杯全国大学生数学建模竞赛E题地铁线路的运营与规划解题全过程文档及程序

2022年天府杯全国大学生数学建模竞赛 E题 地铁线路的运营与规划 原题再现&#xff1a; 地铁是一种非常绿色快捷的交通出行方式&#xff0c;全国各大城市也都在如火如荼地进行地铁线路建设与规划。但乘坐地铁有时候会感觉特别拥挤&#xff0c;这一时期我们称为高峰期。如何合理…

sqlserver中的merge into语句

merge into语句是用来合并两张表的数据的&#xff0c;比如我们想把一张表的数据批量更新到另外一张表&#xff0c;就可以用merge into语句。具体有哪些业务场景呢&#xff1f; 1.数据同步 2.数据转换 3.基于源表对目标表进行增&#xff0c;删&#xff0c;改的操作。 实践步骤…

JavaScript了解调用带参函数,无参函数的代码

以下为JavaScript了解调用带参函数&#xff0c;无参函数的程序代码和运行截图 目录 前言 一、带参函数 1.1 运行流程及思想 1.2 代码段 1.3 JavaScript语句代码 1.4 运行截图 二、无参函数 2.1 运行流程及思想 2.2 代码段 2.3 JavaScript语句代码 2.4 运行截图 前言…

让代码创造童话,共建快乐世界:六一儿童节特辑

让代码创造童话&#xff0c;共建快乐世界&#xff1a;六一儿童节特辑 六一儿童节即将来临&#xff0c;这是一个属于孩子们的快乐节日。为了让这个节日更加有趣&#xff0c;我们发起了“让代码创造童话&#xff0c;共建快乐世界”六一活动。在这个活动中&#xff0c;我们邀请您…

使用Tensorrt对YOLOv5目标检测的代码进行加速

文章目录 1. 前言2. 官网3. 安装依赖3.1. 安装OpenCV3.1.1. 安装3.1.2. 添加环境变量3.1.3. 查看版本 3.2. 安装TensorRT3.2.1. 下载3.2.2. 安装3.2.3. 添加环境变量 4. 下载项目5. 生成WTS模型6. cmake6.1. 生成Makefile6.1.1. 配置CMakeLists.txt6.1.1.1. 修改编译依赖的路径…

通过python采集1688商品评论数据封装接口、1688评论数据接口

1688商品评论数据是指在1688网站上对商品的评价和评论信息。这些信息包括买家对商品的使用、品质、包装、服务等方面的评价和意见&#xff0c;可以帮助其他用户更好地了解商品的优缺点和性能&#xff0c;从而做出更明智的购买决策。 1688网站是中国最大的B2B电子商务网站之一&…

RK3566调试EC20

参考博客&#xff1a;RK3568开发笔记-buildroot移远EC20模块调试记录 一、内核配置 cd 到kernel目录&#xff0c;执行make ARCHarm64 menuconfig&#xff0c; Device Drivers >USB support > USB Serial Converter support 选中 USB driver for GSM and CDMA modems选…