【Prometheus数据采集】基于prometheusAPI 的数据采集


import datetime
import time
import requests
import csv
import json
import datetime
import time
import socket

class promethus():
    """docstring for promethus"""
    def __init__(self):
        self.PROMETHEUS = 'http://192.168.100.209:9090/'
        self.ALERTMANAGER = 'http://192.168.100.209:9093/'
        self.METRICSTYPE = 'ALL' # 'CUSTOMER' OR 'ALL'
        self.METRICSLIST = ['promethes_sd_file_scan_dration_seconds_cont','promethes_sd_file_scan_dration_seconds_sm']

    def getMetricsList(self):
        metricsall=requests.get(self.PROMETHEUS + '/api/v1/label/__name__/values')
        #print(metricsall)
        metricsall=metricsall.json()['data']
        return metricsall

    def getMetricsAll(self):

        times = datetime.datetime.now()
        #print(time)
        time1 = times.strftime("%Y-%m-%d %H:%M:%S")
        time2 = times.strftime("%Y-%m-%d")
        time3 =  int(time.time())

        #metricsall = self.getMetricsList()
        if self.METRICSTYPE == 'CUSTOMER':
            metricsall = self.METRICSLIST
        else:
            metricsall = self.getMetricsList()
        i = len(metricsall)
        print(str(times) + "\t本次采集共" + str(i) + "个指标......")

        for index in range(i):
            a=metricsall[index]
            print(a)
            #response = requests.get(PROMETHEUS + '/api/v1/query', params={'query': 'container_cpu_user_seconds_total{job="algorithm-test168",name="test-kafka"}'})
            response = requests.get(self.PROMETHEUS + '/api/v1/query', params={'query': a})
        #print(response)

            results = response.json()['data']['result']

            for result in results:
                #print(result)
                result1='{metric}: {value[1]}'.format(**result)
                print(result1)
                index01 = result1.find('}:')+1
                index02 = result1.find('}:')+2
                #print(index01)
                Value = result1[index02:].strip()
               # print(Value)
                json01=result1[:index01]
                json01 = result1[:index01]
                print(json01)
                #print(type(json01))
                json01=json01.replace("'", '"')
                json01=eval(json01)
                Name =  json01["__name__"]

                if Name == "node_boot_time_seconds" :
                    Value =  (time3-int(Value))/60/60/24
                    print(Value)
                else :
                   print("keep  origin Value ")

                json01["time"] = time1

                json01["value"] = Value
                json01=str(json01)
                json01 = json01.replace("'", '"')
                #json01 = json01.replace("u","")
                print(json01)

                filename = './logs/promethus-209'+'-'+time2+'.txt'
                f = open(filename, 'a')
                f.write('\n'+str(json01))
                f.close()

    def getAlertList(self):
        ##Get Alert List
        times = datetime.datetime.now()
        time1 = times.strftime("%Y-%m-%d %H:%M:%S")
        time2 = times.strftime("%Y-%m-%d")
        time3 =  int(time.time())
        response = requests.get(self.ALERTMANAGER + '/api/v1/alerts')
        response = response.json()['data']
        return response

    def loopGetData(self):
        while True:
            #metricsall = self.getMetricsList()
            self.getMetricsAll()
            ## Get Alertsdata
            alertList = self.getAlertList()

            for x in alertList:
                x = str(x).replace("\'","\"")
                print("-------" + x)
                #x = json.loads(x)
                if "\"inhibitedBy\": None" in x:
                    #print("inh No Json!")
                    x = x.replace("\"inhibitedBy\": None","\"inhibitedBy\": \"\"")
                    if "\"silencedBy\": None" in x:
                        #print("sil No json")
                        x = x.replace("\"silencedBy\": None","\"silencedBy\": \"\"")
                        print(x)
                        s.sendto(x.encode(),("192.168.100.209",20515))
                    else:
                        print(x)
                        s.sendto(x.encode(),("192.168.100.209",20515))
                else:
                    print(x)
                    s.sendto(x.encode(),("192.168.100.209",20515))
            time.sleep(60)    

prom = promethus()

s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

prom.loopGetData()

Alert部分暂时未完善。