首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >python度量出口商

python度量出口商
EN

Stack Overflow用户
提问于 2022-04-27 13:38:21
回答 2查看 276关注 0票数 -1

解决了问题--我做了一个python度量导出程序,但是工作不太好,我从metric.txt那里得到了信息

metric.txt

代码语言:javascript
运行
复制
metric_exporter prometheus-1 3 /prometheus
metric_exporter prometheus-2 10 /prometheus
metric_exporter haproxy-1 87 /etc/hosts
metric_exporter haproxy-2 85 /etc/hosts
metric_exporter haproxy-3 68 /etc/hosts
metric_exporter haproxy-4 82 /etc/hosts
metric_exporter haproxy-5 52 /etc/hosts

script.py --这个脚本完全是我自己做的,我遇到的问题是,脚本只是打印出metric.txt的最后一行,而不是所有的行。

代码语言:javascript
运行
复制
import time
import csv
from prometheus_client.core import GaugeMetricFamily, REGISTRY, CounterMetricFamily
from prometheus_client import start_http_server
 
 
with open("metric.txt", "r", newline="") as file:
    reader = csv.reader(file, delimiter=" ")
    for row in reader:
        print(row[1], row[2], row[3])
~                                       
    
class CustomCollector(object):
    def __init__(self):
        pass

    def collect(self):
        g = GaugeMetricFamily("disk_available", 'Disk Available', labels=['pod_name', 'env', 'namespace', 'partition_name'])
        g.add_metric(row[1], "env_prod", "namespace_prod", row[3], row[2])
        yield g


if __name__ == '__main__':
    start_http_server(9800)
    REGISTRY.register(CustomCollector())
    while True:
        time.sleep(1)

python导出程序的输出应该是:

代码语言:javascript
运行
复制
disk_available{env="prod",namespace="sm-prod",pod_name="prometheus-1", partition_name="/prometheus"} 3
disk_available{env="prod",namespace="sm-prod",pod_name="prometheus-2", partition_name="/prometheus"} 10
disk_available{env="prod",namespace="sm-prod",pod_name="haproxy-1", partition_name="/prometheus"} 87
disk_available{env="prod",namespace="sm-prod",pod_name="haproxy-2", partition_name="/prometheus"} 85
disk_available{env="prod",namespace="sm-prod",pod_name="haproxy-3", partition_name="/prometheus"} 68
disk_available{env="prod",namespace="sm-prod",pod_name="haproxy-4", partition_name="/prometheus"} 82
disk_available{env="prod",namespace="sm-prod",pod_name="haproxy-5", partition_name="/prometheus"} 52
EN

回答 2

Stack Overflow用户

发布于 2022-04-27 13:49:16

间接办法:

代码语言:javascript
运行
复制
import pandas as pd
df = pd.read_csv('my_csv_filepath')
my_dict = df.to_dict()

你可能会发现你不需要字典,你可以用熊猫的数据作为任何你想做的事情。

票数 0
EN

Stack Overflow用户

发布于 2022-04-27 14:02:38

您的代码无法工作-您正在覆盖您的行共有的单一键。

创建数据文件:

代码语言:javascript
运行
复制
with open("metric.txt","w") as t:
    t.write("""metric_exporter prometheus-1 3 /prometheus
metric_exporter prometheus-2 10 /prometheus
metric_exporter haproxy-1 87 /etc/hosts
metric_exporter haproxy-2 85 /etc/hosts
metric_exporter haproxy-3 68 /etc/hosts
metric_exporter haproxy-4 82 /etc/hosts
metric_exporter haproxy-5 52 /etc/hosts""")

你的:

data = dict(),以open("metric.txt","r")作为文件:对于文件中的I:# line将始终是"metric_exporter",您只使用一个#键,这样就可以反复重写该键的值,*lines =i.split(metric.txt)= line (*data.items(),sep="\n")

让你:

代码语言:javascript
运行
复制
('metric_exporter', ['haproxy-5', '52', '/etc/hosts'])

你需要使用唯一的键,f.e.:

代码语言:javascript
运行
复制
data.clear()
with open("metric.txt", "r") as file:
    for i in file:
        if not i.strip(): continue  # avoid handling empty lines

        # proxyname seems to be unique - use that as key
        _, proxy_name, some_number, host, *whatever = i.split()

        # the value of _ and *whatever are never used 
        data[proxy_name] =  proxy_name, some_number, host

print(data)
print(*data.items(), sep="\n")

得到:

代码语言:javascript
运行
复制
# the dict
{'prometheus-1': ('prometheus-1', '3',  '/prometheus'), 
 'prometheus-2': ('prometheus-2', '10', '/prometheus'), 
 'haproxy-1':    ('haproxy-1',    '87', '/etc/hosts'), 
 'haproxy-2':    ('haproxy-2',    '85', '/etc/hosts'), 
 'haproxy-3':    ('haproxy-3',    '68', '/etc/hosts'), 
 'haproxy-4':    ('haproxy-4',    '82', '/etc/hosts'), 
 'haproxy-5':    ('haproxy-5',    '52', '/etc/hosts')}

# as items()
('prometheus-1', ('prometheus-1', '3', '/prometheus'))
('prometheus-2', ('prometheus-2', '10', '/prometheus'))
('haproxy-1', ('haproxy-1', '87', '/etc/hosts'))
('haproxy-2', ('haproxy-2', '85', '/etc/hosts'))
('haproxy-3', ('haproxy-3', '68', '/etc/hosts'))
('haproxy-4', ('haproxy-4', '82', '/etc/hosts'))
('haproxy-5', ('haproxy-5', '52', '/etc/hosts'))

您可以使用data.values()获取元组-每个元组的第一个值是host_name,第二个值是数字(作为字符串),第三个值是Uri。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72029814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档