首页 > 编程知识 正文

python远程windows执行命令,python脚本编写

时间:2023-05-05 03:01:33 阅读:13015 作者:2319

要使用python创建Prometheus监视,必须首先打开Prometheus群集。 可以参考//www.jb51.net/article/148895.htm进行安装。 在python上实现服务器端。 在Prometheus中设置请求网址后,Prometheus会定期向该网址发出请求,获取想要返回的数据。

使用Python和Flask创建Prometheus监视

安装

pip安装闪存

pip install prometheus_client

测量结果

Prometheus有四种类型的度量: counter、Gauge、Summary和Histogram

柜台

计数器增加,程序重启时复位为0,常用于任务数、总处理时间、错误数等不增加的指标。

导入Prometheus _ client

from Prometheus _ clientimportcounter

from Prometheus _ client.coreimportcollectorregistry

from flask导入响应,flask

APP=flask(_name__ )

requests _ total=counter (' request _ count ',' Total request cout of the host ' ) )

@app.route(/metrics ) ) )。

def requests_count () :

requests_total.inc (

#requests_total.Inc(2)

返回响应(Prometheus _ client.generate _ latest ) requests_total )、

mimetype='text/plain ' )

@app.route('/' ) )。

defindex(: )

requests_total.inc (

return 'Hello World '

if __name__=='__main__':

App.run(host='0.0.0' ) )

运行此脚本以访问youhost:5000/metrics

# help request _ counttotalrequestcoutofthehost

# TYPE request_count counter

request_count 3.0

高加坡

Gauge与Counter类似,唯一的区别是可以减少Gauge的数值,常用于温度、利用率等指标。

导入随机

导入Prometheus _ client

from Prometheus _ clientimportgauge

from flask导入响应,flask

APP=flask(_name__ )

random _ value=gauge (' random _ value ',' Random value of the request ' ) )

@app.route(/metrics ) ) )。

def r_value () :

random _ value.set (random.randint (0,10 ) )

返回响应(Prometheus _ client.generate _ latest ) random_value )、

mimetype='text/plain ' )

if __name__=='__main__':

App.run(host='0.0.0' ) )

运行此脚本以访问youhost:5000/metrics

# help random _ valuerandomvalueoftherequest

# TYPE random_value gauge

random_value 3.0

摘要/历史记录

摘要/历史记录的概念很复杂,在一般的导出器中很难使用,暂且不谈。

标签

用labels区分公制特征

from Prometheus _ clientimportcounter

c=counter('requests_total ',' HTTP requests total ',['method ',' clientip']

c.labels('get ',' 127.0.0.1 ' ).inc ) )。

c.Labels('post ',' 192.168.0.1 ' (.Inc ) )3) ) ) ) ) )。

c.labels (方法=' get ',客户端=' 192.168.0.1 ' ).inc ) ) ) ) ) ) ) ) )。

使用Python和asyncio创建Prometheus监视

from Prometheus _ clientimportcounter,Gauge

from Prometheus _ client.coreimportcollectorregistry

注册=收集器注册(auto _ describe=false )。

requests _ total=counter (request _ count )、) Total request cout of the host )、registry=REGISTRY ) )。

random _ value=gauge (random _ value )、Random value of the request )、registry=REGISTRY ) ) )。

导入Prometheus _ client

from Prometheus _ clientimportcounter,Gauge

from Prometheus _ client.coreimportcollectorregistry

from AIO http导入web

import aiohttp

import asyncio

导入uv loop

导入随机、日志、时间、日期

async io.set _ event _ loop _ policy (uv loop.eventlooppolicy ()

routes=web.RouteTableDef (

#度量包括

requests _ total=counter (request _ count ),Total request cout of the host ) )的值只会增加

random _ value=gauge (random _ value,) Random value of the request )数值可以小于jmdyl

@routes.get(/metrics ) ) )。

异步测量(请求) :

requests_total.Inc(#计数器的自增加

#requests_total.Inc(2)

data=Prometheus _ client.generate _ latest (requests _ total )

returnweb.response(body=data,content_type='text/plain ' )返回计数器的值

@routes.get(/metrics2) ) )。

asyncdefmetrics2(请求) :

random _ value.set (random.randint (0,10 ) )设置可以是任意值,但必须是整数或浮点数

returnweb.response (body=Prometheus _ client.generate _ latest (random _ value ),content_type='text/plain

@routes.get('/' ) ) )。

asyncdefhello (请求) :

returnweb.response % ef % BC % 88 text % 3d % 27 hello % ef % BC % 8c world % 27 % ef % BC % 89 % ef % BC % 89 % ef % BC % 89 % 89 f % BC % 89 % E3 % 80 % 82 % 0a labels % E3 % 82 % 92 % E4 % BD % E3 % 81 % a3 % E3 % 81 % a6公制% E3 % 81 % AE % E7 % er % ef % BC % 88 requests _ total % ef % BC % 89 % E3 % 80 % 81 % ef % BC % 89 http % 20 requests % 20 total % ef % BC % 89 % E3 7 % 5d % ef % BC % 89 % ef % BC % 89 lable % E3 % 81 % a ekey % E3 % 82 % E8 % BF % E5 % 8a % A0 % E3 % 81 % 97 % E3 % 9 % ef % BC % 89 % E3 % 80 % 81 Inc % ef % BC % 89 % 89 % 23 % E3 % 81 % af % E7 % 95 % B0 % E3 % 81 % aa % E3 % 82 % 81 % A8 % E3 % 82 % 8b % 0ac.labels % ef % BC % 88 post % ef % BC % 89 % E3 % 80 % 81 % ef % BC % 89192.168.0.1 % ef % 1 % aa % E3 % 82 % 8b label % E3 % 81 % ab % E3 % 81 % a4 % E3 % 84 % E3 % 81 % a6 % b5 % B1 % E8 % A8 % 88 % E3 % 82 tip % 3d % 27192.168.0.1 % 27 % ef % BC % 89.Inc % ef % BC % 89 % 23 % E3 % 81 % af % E7 % 95 % B0 % E3 8 % E3 % 82 % 92 % E3 % 81 % A8 % E3 % 82 % 8b % 0ag % 3d gauge % ef % BC % 88my _ in progress _ requests % ef % BC % 89 % E3 标签名称% ef % BC % 89 % ef % BC % 89 % 29 % 0ag.labels % ef % BC % 88my标签名称% 3d % 27 str % 27 % ef % BC % 89.set % % E5 % AE % 9a % E7 % be % a9 % E3 % 81 % 97 % E3 % 81 % be % E3 % 81 % E3 % 81 % 80 % 81 % E5 % BF % 85 % E3 % 8f % E6 % 95 % B0 % E7 % 82 % B9 % E6 % 95 % B0 % E3 % 81 % ab % E3 % 81 % E3 % 81 % a6 % E3 % 81 % 8f % E3 % 81 % A0 % E0 nfo % ef % BC % 88 serverstart % 3a % 25s % ef % BC % 89 % datetime.datetime.now % ef % BC % 89 % ef % BC % 89 % 29 % 0a app c % 89 % 2a 1024 % 2a %2a2 % ef % BC % 89 % 23 % E4 % BD % 9c % E6 % 88 % 90 app % E3 % 81 % af % E3 % 80 % 81 % E6 % 9c % 80 % E3 % 82 % 922 m % E3 % 81 % ab % E8 % A8 % ad % E5 % AE % 9a % E3 % 81 % 99 % E3 % 82 % 8b % 0a app.add _ routes % ef % BC % BF % BD % E5 % 8a % A0 % E3 % 81 % 99 % E3 % 82 % 8b % 0a web.run _ app % ef % BC % 88 app % ef % BC %8chost % 3d % 270.0 info % ef % BC % 88服务器关闭% 3a % 25s % ef % BC % 89 % datetime.datetime.now % ef % BC % 89 % ef % BC % 89 % 29 % 0a % 3 % E3 % 81 % AE % E6 % 96 % 87 % E7 % ab % A0 % E3 % 81 % AE % 81 % E3 % 81 % B9 % E3 % 81 % a6 % E3 % 81 % AE % E5 % A0 % E3 % 81 % AE % E5 % 86 % 85 % E5 % AE % B9 % E3 % 81 % 8c % E7 % 9a % 86 % E3 % 81 % E3 % 82 % 93 % E3 % 81 % AE % 81 % AE % E5 % 8f % 82 % E8 % 80 % 83 % E5 % ad % a6 % E7 % E2 % E4 % be % a1 % E5 % 80 % a4 % E3 % 82 % E6 % 8c % 88 3 % 81 % a3 % E3 % 81 % a6 % E3 % 81 % 84 % E3 % 81 % E3 % 81 % 99 % E3 % 80 % 82 % E8 % 84 % 9a % E6 % 9c % AC % E5 % AE % E3 % 81 % 97 % E3 % 81 % be % E3 % 81 % 99 % E3 % 80 % 82 % 20 % E8 % a9 % B3 % B4 % E3 % 81 % ab % E3 % 81 % a4 % 83 % aa % E3 % 83 % B3 % E3 % 82 % af % E3 % 82 % E5 % 82 % E7 % 85 % a7 % E3 % 81 % E3 % 81 % a6 % E3 % 81 % 81

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。