天天看點

influx+grafana自定義python采集資料和一些坑的總結

版權聲明:本文可能為部落客原創文章,若标明出處可随便轉載。 https://blog.csdn.net/Jailman/article/details/78913824

先上網卡資料采集腳本,這個基本上是最大的坑,因為一些資料的類型不正确會導緻no datapoint的錯誤,真是令人抓狂,注意其中幾個key的值必須是int或者float類型,如果你不慎寫成了string,那就麻煩了,其他的tag是string類型。

另外資料采集時間間隔一般就是10秒,這是潛規則,大家都懂。

官方參考位址:

官參 有圖有真相

#! /usr/bin/env python
#-*- coding:utf-8 -*-

import os
import arrow
import time
from time import sleep
from influxdb import InfluxDBClient

client = InfluxDBClient('localhost', 8086, 'root', '', 'telegraf') 

while True:
    if int(time.time())%10 == 0:
        cmd = 'cat /proc/net/dev|grep "ens4"'
        rawline = os.popen(cmd).read().strip()
        rxbytes = int(rawline.split()[1])
        txbytes = int(rawline.split()[9])
        rxpks = int(rawline.split()[2])
        txpks = int(rawline.split()[10])
        now = str(arrow.now()).split('.')[0] + 'Z'

        print time.time(), rxbytes,txbytes,rxpks,txpks   

        json_body = [
            {
                "measurement": "network",
                "tags": {
                    "host": "gc-u16",
                    "nio": "ens4"
                },
                #"time": now,
                "fields": {
                    "rxbytes": rxbytes,
                    "txbytes": txbytes,
                    "rxpks": rxpks,
                    "txpks": txpks
                }
            }
        ]

        client.write_points(json_body)
    sleep(1)           

運作腳本,檢視influxdb資料,至于背景+獨立線程這些東西就見仁見智了

然後配置圖形,這個就簡單了,隻要你資料沒寫錯,基本上grafana都能采集到,這裡忽略配置資料源建立dashboard和表格等亂七八糟的,直接上配置的sql圖形,大緻就是這樣吧