【工作笔记】python+influxdb+grafana监控云行情

最近供应商提供的上海阿里云行情站点比较卡,为此用python+influxdb+grafana监控服务器在开市期间与休市期间请求应答耗时,代码如下:

#!/usr/bin/env python3
# -*-coding:utf-8-*-
# author by Michael Ho
# contact:rui.he@geekthings.com.cn
# description:moniter Shanghai AliCloud

import requests, time
from influxdb import InfluxDBClient

# connect to influxdb
client = InfluxDBClient('localhost', 8086, 'root', 'xxxxxx', 'hq_moniter')

while True:
    url = "http://172.50.1.151:3006/reqxml?"
    params = {
        "action": "60",
        "mobilecode": "13988888888",
        "StartPos": "1",
        "StockIndex": "1",
        "maxcount": "3",
        "AccountIndex": "6",
        "Grid": "00600|4353,000001|4609,600000|4353"
    }
    r = requests.get(url=url, params=params)
    request_time = r.elapsed.total_seconds()

    json_body = [
        {
            "measurement": "hq_server",
            "tags": {
                "host": "Shanghai_AliCloud",
            },
            "fields": {
                "request_time": request_time
            }
        }
    ]
    client.write_points(json_body)
    time.sleep(10)

守护进程脚本,代码如下:

#!/bin/bash
# description : moniter Shanghai AliCloud Service
# author by : Michael Ho
# contact : rui.he@geekthings.com.cn

service="/usr/bin/env python3 /opt/scripts/moniter_zzCloudHq/moniter_shAliCloud.py"
pid_file=shAliCloud.pid

start() {
  ${service} &
  # shellcheck disable=SC2181
  if [[ $? -eq 0 ]]; then
    echo $! > ${pid_file}
  else
    exit 1
  fi
}

stop() {
    # shellcheck disable=SC2046
    kill -9 $(cat ${pid_file})
    # shellcheck disable=SC2181
    if [[ $? -eq 0 ]]; then
        rm -f ${pid_file}
    else
      exit 1
    fi
}

case $1 in
  start )
    echo "moniter Shanghai AliCloud Service is start ..."
    start
    ;;

  stop )
    echo "moniter Shanghai AliCloud Service is stop ..."
    stop
    ;;

  * )
    echo "Please input start | stop ..."
    ;;
esac
原文地址:https://www.cnblogs.com/herui1991/p/12508081.html