簡體   English   中英

Prometheus 導出器 - 讀取 CSV 文件,其中包含過去一天的數據

[英]Prometheus exporter - reading CSV file having data from the past day

我正在編寫一個 Prometheus 導出器,它必須包含不同的 CSV 文件。 它們中的每一個都包含過去一整天的數據(目標是讓導出器每天讀取一個新的 CSV 文件。每天將一個 CSV 文件上傳到服務器,其中包含前一天的數據。

在 CSV 文件中,我每 5 分鍾就有一次相同的指標。 例如:

Date;Time;data
23.03.20;23:55:00;1
23.03.20;23:50:00;50
23.03.20;23:45:00;3

我很難在 Prometheus 中正確添加這些數據。

class CSVCollector(object):
  def collect(self):
    # We list all the min files in the current directory
    list_min = glob.glob("min*.csv")
    metric = GaugeMetricFamily(
                'day_tests_seconds',
                'kw', labels=["jobname"])
    for min in list_min :
      with open(min) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=';')
        line_count = 0
        for row in csv_reader:
            if line_count == 1:
                correct_date_format = row[0][:6] + "20" + row[0][6:]
                datetime_row = correct_date_format + ';' + row[1]
                timestamp = int(time.mktime(datetime.datetime.strptime(datetime_row, "%d.%m.%Y;%H:%M:%S").timetuple()))
                metric.add_metric(str(line_count), int(row[4]), timestamp)
            line_count += 1
    yield metric   
     


if __name__ == '__main__':
  # Usage: json_exporter.py port endpoint
  start_http_server(int(sys.argv[1]))
  REGISTRY.register(CSVCollector())
  while True: time.sleep(1)

Prometheus 只需讀取第一行,將其添加為度量標准,並在每次抓取導出器時再次讀取完全相同的內容。 我究竟做錯了什么? 我覺得這個數據應該是一個 Jauge,因為它會上下波動,但 Prometheus 似乎不希望一次刮取來自同一個Jauge的不同數據?

頁面介紹了一種將歷史數據導入 Prometheus 的方法。

.txt 文件為 OpenMetrics 格式,示例導入命令為: tsdb import rrd_exported_data.txt /var/lib/prometheus/ --max-samples-in-mem=10000

示例文件rrd_exported_data.txt ( [metric]{[labels]} [number value] [timestamp ms] ):

collectd_df_complex{host="myserver.fqdn.com",df="var-log",dimension="free"} 5.8093906125e+10 1582226100000
collectd_varnish_derive{host="myserver.fqdn.com",varnish="request_rate",dimension="MAIN.client_req"} 2.3021666667e+01 1582226100000
collectd_df_complex{host="myserver.fqdn.com",df="var-log",dimension="free"} 5.8093906125e+10 1582226100000
collectd_load{host="myserver.fqdn.com",type="midterm"} 0.0155 1582226100000

也許您可以在 python 代碼中執行該命令。

Prometheus 不支持數據導入(又名推送模型) - 它僅支持數據抓取(又名拉取模型)。 有關詳細信息,請參閱本文 除此之外,Prometheus 不支持存儲歷史數據(也稱為回填)。

如果您需要將 CSV 歷史數據導入到類似 Prometheus 的系統中,請查看 VictoriaMetrics - 它支持導入 CSV 數據支持數據回填

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM