from prometheus_client import CollectorRegistry, Gauge, push_to_gateway import sys, random import subprocess from pyroute2 import netns, NSPopen import socket import time registry = CollectorRegistry() g_count = Gauge('test_nf_conntrack_count', 'nf_conntrack_count inside given namespace', ['host','namespace'],registry=registry ) g_max = Gauge('test_nf_conntrack_max', 'nf_conntrack_max inside given namespace', ['host','namespace'],registry=registry ) hostname=socket.gethostname() while True: for _item in netns.listnetns(): if str(_item).startswith("qrouter"): nsp = NSPopen(_item, ['cat', '/proc/sys/net/netfilter/nf_conntrack_count'], stdout=subprocess.PIPE) result=nsp.communicate() nsresult_count=(result[0].decode('ascii').strip()) nsp = NSPopen(_item, ['cat', '/proc/sys/net/netfilter/nf_conntrack_max'], stdout=subprocess.PIPE) result=nsp.communicate() nsresult_max=(result[0].decode('ascii').strip()) print(_item + " " + nsresult_count + " " + nsresult_max) nsp.wait() nsp.release() g_count.labels(hostname,_item).set(nsresult_count) g_max.labels(hostname,_item).set(nsresult_max) # exit() time.sleep(30) push_to_gateway('10.10.110.250:9091', job='cory_test_job2', registry=registry) # print("Done") # # curl -X GET http://10.10.110.250:9091/api/v1/metrics | jq