最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python如何獲取Prometheus監(jiān)控數(shù)據(jù)

 更新時間:2022年05月16日 11:09:09   作者:Jiang_James_L  
這篇文章主要介紹了python如何獲取Prometheus監(jiān)控數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

獲取Prometheus監(jiān)控數(shù)據(jù)

獲取Prometheus target數(shù)據(jù)

調(diào)用http://<prometheus.address>/api/v1/targets并解析。

def getTargetsStatus(address):
? ? url = address + '/api/v1/targets'
? ? response = requests.request('GET', url)
? ? if response.status_code == 200:
? ? ? ? targets = response.json()['data']['activeTargets']
? ? ? ? aliveNum, totalNum = 0, 0
? ? ? ? downList = []
? ? ? ? for target in targets:
? ? ? ? ? ? totalNum += 1
? ? ? ? ? ? if target['health'] == 'up':
? ? ? ? ? ? ? ? aliveNum += 1
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? downList.append(target['labels']['instance'])
? ? ? ? print('-----------------------TargetsStatus--------------------------')
? ? ? ? print(str(aliveNum) + ' in ' + str(totalNum) + ' Targets are alive !!!')
? ? ? ? print('--------------------------------------------------------------')
? ? ? ? for down in downList:
? ? ? ? ? ? print('\033[31m\033[1m' + down + '\033[0m' + ' down !!!')
? ? ? ? print('-----------------------TargetsStatus--------------------------')
? ? else:
? ? ? ? print('\033[31m\033[1m' + 'Get targets status failed!' + '\033[0m')

獲取Prometheus 監(jiān)控信息(cpu、mem、disks)

調(diào)用http://<prometheus.address>/api/v1/query?query=<expr>并解析,其中expr為prometheus的查詢語句。

### 定義cpu、mem、disks使用率的空字典
diskUsageDict = {}
cpuUsageDict = {}
memUsageDict = {}
### 定義采集時間間隔 s
monitorInterval = 5
### 定義超時告警時間 s
diskAlertTime = 5
cpuAlertTime = 300
memAlertTime = 300
### 定義告警閾值 %
diskThreshold = 80
cpuThreshold = 60
memThreshold = 70
def queryUsage(address, expr):
    url = address + '/api/v1/query?query=' + expr
    try:
        return json.loads(requests.get(url=url).content.decode('utf8', 'ignore'))
    except Exception as e:
        print(e)
        return {}
def orderUsageDict(usageDict, currentTime, monitorInterval):
    '''
    :param usageDict: 資源使用率字典
    :param usageDict: 資源使用率字典
    :param currentTime: 當(dāng)前獲取監(jiān)控數(shù)據(jù)的時間節(jié)點
    :return:
    :description: 剔除字典中不滿足連續(xù)超出閾值的數(shù)據(jù)
    '''
    for key in list(usageDict.keys()):
        if currentTime - usageDict[key][1] >= monitorInterval:
            usageDict.pop(key)
def getCurrentUsageGreater(address, record, threshold, usageDict, monitorInterval):
    '''
    :param address: Prometheus address
    :param record: Prometheus rules record
    :param threshold: 閾值
    :param usageDict: 資源使用率字典
    :param monitorInterval: 監(jiān)控時間間隔
    :return:
    :description: 獲取資源使用率大于閾值的數(shù)據(jù)
    '''
    expr = record + '>=' + str(threshold)
    usage = queryUsage(address=address, expr=expr)
    currentTime = 0
    if 'data' in usage and usage['data']['result']:
        for metric in usage['data']['result']:
            instance = metric['metric']['instance']
            if record == 'node:fs_usage:ratio' or record == 'node:fs_root_usage:ratio':
                metricLabel = instance + ':' + metric['metric']['mountpoint']
            else:
                metricLabel = instance
            utctime = metric['value'][0]
            value = metric['value'][1]
            describe = record.split(':')[1]
            if not metricLabel in usageDict.keys():
                usageDict[metricLabel] = (utctime, utctime, describe, value)
            else:
                startTime = usageDict.get(metricLabel)[0]
                usageDict[metricLabel] = (startTime, utctime, describe, value)
            currentTime = utctime
    orderUsageDict(usageDict=usageDict, currentTime=currentTime, monitorInterval=monitorInterval)
def printUsageDict(usageDict, alertTime):
    '''
    :param usageDict: 資源使用率字典
    :param alertTime: 監(jiān)控告警時間
    :return:
    :description: 打印出超過監(jiān)控告警時間的數(shù)據(jù)
    '''
    for key, value in usageDict.items():
        deltaT = value[1] - value[0]
        if deltaT >= alertTime:
            print(key + ' ----- ' + value[2] + '\033[31m\033[1m ' + str(value[3]) + '\033[0m ----- lasted for\033[31m\033[1m %.2f \033[0mseconds' % deltaT)
def monitorUsageGreater(address):
    '''
    :param address: Prometheus address
    :return:
    :description: 持續(xù)監(jiān)控并輸出數(shù)據(jù)
    '''
    while True:
        getCurrentUsageGreater(address, 'node:fs_usage:ratio', diskThreshold, diskUsageDict, monitorInterval)
        printUsageDict(diskUsageDict, alertTime=diskAlertTime)
        getCurrentUsageGreater(address, 'node:memory_usage:ratio', cpuThreshold, memUsageDict, monitorInterval)
        printUsageDict(memUsageDict, alertTime=memAlertTime)
        getCurrentUsageGreater(address, 'node:cpu_usage:ratio', memThreshold, cpuUsageDict, monitorInterval)
        printUsageDict(cpuUsageDict, alertTime=cpuAlertTime)
        time.sleep(monitorInterval)

其中有一些使用細(xì)節(jié),比如統(tǒng)一資源標(biāo)識符URI的構(gòu)建,將HttpEntity用UTF-8編碼方式轉(zhuǎn)換為字符串再解析為JSON對象,我都寫在注釋里了。

		String paramValue="http_requests_total";
//HTTP客戶端連接工具
        CloseableHttpClient httpClient=HttpClients.createDefault();
        //參數(shù)里有特殊字符,不能直接寫成String(會報Illegal Character錯誤),用URIBuilder構(gòu)造。
        URIBuilder uri=null;
        HttpGet get =null;
        try {
            //一對參數(shù),使用addParameter(param: ,value:)這個方法添加參數(shù)。
            //若多對參數(shù),使用第二種方法(但其實在這里沒有這種情況):uri.addParameters(List<NameValuePair>);
            //這里的ip,port換成你的Prometheus的ip+port。paramValue要自己定義,比如http_request_total
            uri=new URIBuilder("http://ip:port/api/v1/query");
            uri.addParameter("query",paramValue);
            //uri此時是http://ip:port/api/v1/query?query=http_requests_total
            get=new HttpGet(uri.build());
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        JSONObject jsonObject=null;
        CloseableHttpResponse response=null;
        try {
            // 執(zhí)行請求并接收+轉(zhuǎn)換  得到j(luò)sonObject就可以解析了。
            response = httpClient.execute(get);
            String resStr= EntityUtils.toString(response.getEntity(),"UTF-8");
            jsonObject=JSONObject.parseObject(resStr);

通過promsql讀取prometheus內(nèi)的數(shù)據(jù)

需求是python讀取prometheus內(nèi)的數(shù)據(jù),做數(shù)據(jù)處理后入庫到mysql。這里主要說一下,python如何使用官方api通過promsql查詢prom內(nèi)的數(shù)據(jù)。

官方提供的api為:

http://ip:port/api/v1/query?query=

樣例如下:

html = urllib.request.urlopen('http://ip:port/api/v1/query?query=count(node_cpu_seconds_total{job="%s",mode="idle"})' %(s))
data = html.read().decode("utf-8")
json = json.loads(data)

返回值為json類型,如下圖:

在這里插入圖片描述


具體的json各位自己分析,瞬時值為value,值內(nèi)數(shù)據(jù),第一位是時間戳,第二位為查詢的結(jié)果值

區(qū)間向量返回值為values,也比較好理解。

還有個需求需要查詢之前的數(shù)據(jù),比如前一天,月初一周之類的,可以使用如下api:

http://ip:port/api/v1/query_range?query=avg(1-avg(rate(node_cpu_seconds_total{job="%s",mode="idle"}[5m]))by(instance))&start='+start+'&end='+end+'&step=15s

其中start為采集開始時間,end為采集結(jié)束時間,step為步長,即多久設(shè)個采集點。

start和end的格式如下:

2021-11-01T00:00:00Z

獲取方式可以采取以下方式:

獲取每月的第一周數(shù)據(jù),所以從每月一號零點開始到八號的零點

now = datetime.datetime.now()
start = datetime.datetime(now.year, now.month, 1)
end = datetime.datetime(now.year, now.month, 8)
# 格式轉(zhuǎn)換:yyyy-mm-ddThh:MM:ssZ
start_trans = "T".join(str(start).split(" "))+"Z"
end_trans = "T".join(str(end).split(" "))+"Z"

獲取前一周的時間

now_time = datetime.datetime.now()
one_week_ago_time = now_time + datetime.timedelta(days=-7)
# 精確到毫秒
now = now_time.strftime("%Y-%m-%dT%H:%M:%S.%f")
one_week_ago = one_week_ago_time.strftime("%Y-%m-%dT%H:%M:%S.%f")
n = now[0:len(now)-7]+"Z"
one_week = one_week_ago[0:len(one_week_ago)-7]+"Z"

如果獲取時間周期太長,返回數(shù)據(jù)太多會導(dǎo)致報錯,這時候可調(diào)整step大小,或者將時間段分成幾天獲取。

主要還是了解兩個api,其他的都是小問題

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python+selenium實現(xiàn)12306模擬登錄的步驟

    python+selenium實現(xiàn)12306模擬登錄的步驟

    這篇文章主要介紹了python+selenium實現(xiàn)12306模擬登錄的步驟,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2021-01-01
  • Python處理文本文件中控制字符的方法

    Python處理文本文件中控制字符的方法

    最近在使用Python的時候遇到過文檔中出現(xiàn)控制字符報錯的問題。想著總結(jié)一下,方便以后需要或這同樣遇到問題的朋友,下面這篇文章主要介紹了Python處理文本文件中控制字符的解決方法,需要的朋友可以參考借鑒。
    2017-02-02
  • 基于python實現(xiàn)模擬數(shù)據(jù)結(jié)構(gòu)模型

    基于python實現(xiàn)模擬數(shù)據(jù)結(jié)構(gòu)模型

    這篇文章主要介紹了基于python實現(xiàn)模擬數(shù)據(jù)結(jié)構(gòu)模型,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • python實現(xiàn)微信打飛機游戲

    python實現(xiàn)微信打飛機游戲

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)微信打飛機游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • DataFrame 將某列數(shù)據(jù)轉(zhuǎn)為數(shù)組的方法

    DataFrame 將某列數(shù)據(jù)轉(zhuǎn)為數(shù)組的方法

    下面小編就為大家分享一篇DataFrame 將某列數(shù)據(jù)轉(zhuǎn)為數(shù)組的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • 21行Python代碼實現(xiàn)拼寫檢查器

    21行Python代碼實現(xiàn)拼寫檢查器

    21行python代碼實現(xiàn)的一個簡易但是具備完整功能的拼寫檢查器,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Python字符串中的單詞反轉(zhuǎn)的實現(xiàn)示例

    Python字符串中的單詞反轉(zhuǎn)的實現(xiàn)示例

    在Python中,要將字符串中的單詞進行反轉(zhuǎn),本文主要介紹了Python字符串中的單詞反轉(zhuǎn)的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-04-04
  • Python協(xié)程方式的實現(xiàn)及意義筆記分享

    Python協(xié)程方式的實現(xiàn)及意義筆記分享

    協(xié)程也被稱為微線程,是一種用戶態(tài)的上下文切換技術(shù),簡而言之,就是通過一個線程實現(xiàn)代碼互相切換執(zhí)行,本文主要給大家介紹實現(xiàn)協(xié)程的幾種方法
    2021-09-09
  • Pytorch之ToPILImage()不輸出圖片問題及解決

    Pytorch之ToPILImage()不輸出圖片問題及解決

    這篇文章主要介紹了Pytorch之ToPILImage()不輸出圖片問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • python內(nèi)存管理分析

    python內(nèi)存管理分析

    這篇文章主要介紹了python內(nèi)存管理,較為詳細(xì)的分析了Python的內(nèi)存管理機制,需要的朋友可以參考下
    2015-04-04

最新評論

嘉黎县| 南川市| 北川| 荔波县| 苗栗市| 上栗县| 霸州市| 吉隆县| 张家港市| 海南省| 东港市| 玉树县| 全椒县| 铁力市| 龙游县| 喀喇| 本溪市| 来凤县| 汉寿县| 华阴市| 张家界市| 潮州市| 莱州市| 平谷区| 宕昌县| 石台县| 黄梅县| 普安县| 固安县| 临潭县| 湖口县| 仁怀市| 焦作市| 南召县| 外汇| 蕲春县| SHOW| 岳普湖县| 安庆市| 虞城县| 舟曲县|