python度量导出器



已解决我制作了一个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

一种间接方法:

import pandas as pd
df = pd.read_csv('my_csv_filepath')
my_dict = df.to_dict()

你可能会发现你不需要字典,可以使用pandas数据帧来做任何你想做的事情

您的代码无法工作-您正在覆盖行中的公共键。

创建数据文件:

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()
with open("metric.txt", "r") as file:
for i in file:
# line will ALWAYS be "metric_exporter", you use only one 
# key so you overwrite the values of that key over and over
line, *lines = i.split() 
data[line] = lines
print(*data.items(), sep="n")

让你:

('metric_exporter', ['haproxy-5', '52', '/etc/hosts'])

你需要使用唯一的密钥,例如:

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。

最新更新