#! /usr/bin/env python#-*- coding:utf-8 -*- import osimport arrowimport timefrom time import sleepfrom 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)
然后配置图形,这个就简单了,只要你数据没写错,基本上grafana都能采集到,这里忽略配置数据源创建dashboard和表格等乱七八糟的,直接上配置的sql图形,大致就是这样吧