解决了问题--我做了一个python度量导出程序,但是工作不太好,我从metric.txt那里得到了信息
metric.txt
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的最后一行,而不是所有的行。
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导出程序的输出应该是:
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
发布于 2022-04-27 13:49:16
间接办法:
import pandas as pd
df = pd.read_csv('my_csv_filepath')
my_dict = df.to_dict()
你可能会发现你不需要字典,你可以用熊猫的数据作为任何你想做的事情。
发布于 2022-04-27 14:02:38
您的代码无法工作-您正在覆盖您的行共有的单一键。
创建数据文件:
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")
让你:
('metric_exporter', ['haproxy-5', '52', '/etc/hosts'])
你需要使用唯一的键,f.e.:
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")
得到:
# 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。
https://stackoverflow.com/questions/72029814
复制相似问题