簡體   English   中英

使用 python prometheus_client 將 f5sdk 池節點詳細信息推送到 Prometheus

[英]push f5sdk pool node details to Prometheus using python prometheus_client

我在一個循環中的 python 字典中有 f5 池和節點數據詳細信息,用於 10 多個池,我能夠創建摘要指標和標簽並為一個值填充值,但無法在循環中執行pool={'pool_name': 'testing-prom-tool','pool_member_name': 'promtest', 'mem_port': '443', 'mem_address': 'xx.xx.xx.xx', 'mem_state': 'down'}當我使用pool 作為以下代碼中的變量,它在循環中返回值。 這就是我測試並能夠獲得一個值的方式,但我無法獲得一個循環。 它只是給出循環的結束值。 對於循環中的值,我怎樣才能實現這一點?

代碼:

import prometheus_client as prom
import random
import time


pool={'pool_name': 'testing-prom-tool','member_name': 'promtest', 'mem_port': '443', 'mem_address': 'xx.xx.xx.xx', 'mem_state': 'down'}
# Create a metric to track time spent and requests made.
REQUEST_TIME = prom.Summary('request_processing_seconds', 'Time spent processing request')


# Decorate function with metric.
@REQUEST_TIME.time()
def process_request():
    time.sleep(1)


if __name__ == '__main__':
    #                          name                        documentation                            label names ###  I was able to populate all the keys from the dictionary using the pool.keys())
    f5_prom_test = prom.Summary('f5_test','f5_node_status',('pool_name','member_name','mem_port','mem_address','mem_state'))
    prom.start_http_server(1234)
    While True:
        process_request()
        f5_prom_test.labels(pool.get('pool_name'),pool.get('member_name'),pool.get('mem_port'),pool.get('mem_address'),pool.get('mem_state'))
#f5_prom.labels(**pool), this works as well
curl -K http://localhost:1234
f5_test_created{mem_address="xx.xx.xx.xxx",mem_name="test-server",pool_name"=testpool",mem_port="5443",mem_state="down"} 1.658982617711136e+09

我在我的 Prometheus 中使用了 http://localhost。YAML 文件僅顯示 URL 啟動時,並且數據未存儲在 Prometheus tsdb 中。 如何使用 /metrics 將數據保存在 Prometheus tsdb 中

我發現我的 return 語句只打印了最后一個 f5 池詳細信息,而不是 f5 負載均衡器中的所有詳細信息,這就是為什么 http://localhost 中只顯示一個值的原因

如何在腳本中返回字典?

    def f5poolnode_details():
        poolmember_details = {}
        pools = mgmt.tm.ltm.pools.get_collection()
        for pool in pools:
            for member in pool.members_s.get_collection():
                poolmember_details['pool_name'] = pool.name
                poolmember_details['member_name'] = member.name
                poolmember_details['mem_port'] = member.port
                poolmember_details['mem_address'] = member.address
                poolmember_details['mem_state'] = member.state
                print(poolmember_details)##### this gives all the f5 pool details in LB
                return poolmember_details ########### only gives the last f5 pool details in LB


pool={'pool_name': 'testing-prom-tool','member_name': 'promtest', 'mem_port': '443', 'mem_address': 'xx.xx.xx.xx', 'mem_state': 'down'}
# output

如果我理解正確,您需要一個列表,而是立即返回在循環的第一次迭代中創建的 object。 我建議使用嵌套列表理解:

def f5poolnode_details():
    return [
        {
            "pool_name": pool.name,
            "member_name": member.name,
            "mem_port": member.port,
            "mem_address": member.address,
            "mem_state": member.state,
        }
        for member in pool.members_s.get_collection()
        for pool in mgmt.tm.ltm.pools.get_collection()
    ]

這將返回一個對象列表,每個對象都有指定的屬性。

暫無
暫無

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

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