go

grafana + influxdb 简单配置示例

2017-09-20  本文已影响0人  站长_郭生

官方示例地址:http://docs.grafana.org/features/datasources/influxdb/

  # coding=utf-8

import requests
import simplejson as json
from influxdb import InfluxDBClient
import time


def get_json(url):
    resp = requests.get(url)
    if resp.status_code == 200:
        return json.loads(resp.content)


def main(dst_host, dst_port, dst_database, hosts):
    influx = InfluxDBClient(host=dst_host, port=dst_port, database=dst_database)
    path = "http://{0}:{1}{2}"

    for host in hosts:
        result = get_json(path.format(host.get("host"), host.get("port"), host.get("uri")))
        if result:
            data = result.get("data")
            uri = data.get("uri")
            for key, value in uri.items():
                info = {
                    "measurement": "table",
                    "tags": {
                        "host": host.get("host"),
                        "domain": key
                    },
                    "fields": {
                        "requests": value.get("request"),
                        "avg_time": value.get("avg_time"),
                        # "status_2xx": value.get("status", {}).get("200", 0),
                        # "status_4xx": value.get("status", {}).get("400", 0),
                        # "status_5xx": value.get("status", {}).get("500", 0)
                    }
                }
                status_all = {"status_2xx" :0,"status_3xx":0,"status_4xx":0,"status_5xx":0}
                for k, v in value.get("status").items():
                    k = int(k)
                    if k <= 200:
                        status_all["status_2xx"] = status_all["status_2xx"] + v
                    elif k <= 300:
                        status_all["status_3xx"] = status_all["status_3xx"] + v
                    elif k <= 400:
                        status_all["status_4xx"] = status_all["status_4xx"] + v
                    else:
                        status_all["status_5xx"] = status_all["status_5xx"] + v
                for k1, v1 in status_all.items():
                    info["fields"][k1] = v1
                influx.write_points([info])


hosts = [
    {
        "host": "127.0.0.1",
        "port": 80,
        "uri": "/count/uri"
    },
]
influxdb_host = "127.0.0.1"
influxdb_port = 8086
influxdb_database = 'count_status'

main(influxdb_host, influxdb_port, influxdb_database, hosts)

上一篇下一篇

猜你喜欢

热点阅读