Giter VIP home page Giter VIP logo

Comments (2)

nodiaque avatar nodiaque commented on August 29, 2024

Here's the code. I also fix a bug in mtr-exporter.sh where $CYCLE isn't defined. Cycle represent the number of time the MTR run against a single host. Code exemple have it to 2. Interval is the wait time between each run of MTR.

mtr-exporter.sh

#!/bin/bash

INTERVAL=60
CYCLE=2
function monitor_mtr() {
  for MTR_HOST in $(cat ./list-ip-dest); do
    ( mtr --report --tcp --port=443 --json --report-cycles $CYCLE $MTR_HOST | /usr/bin/python3 ./save_data.py ) &
  done
}

which mtr &>/dev/null
if [ $? -eq 1 ]; then
  echo "mtr not found, please install mtr "
  exit 1
else
echo "collecting data..."
fi 
while true; do
  monitor_mtr
  sleep $INTERVAL
done

save_data.py

#!/usr/bin/env python3
import json
import sys
import datetime as dt
import logging

import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS


logging.basicConfig(level=logging.INFO)


bucket = "bucketname"
org = "orgname"
token = "apitoken"
url="url_to_influxdb"


def main():
    client = influxdb_client.InfluxDBClient(
        url=url,
        token=token,
        org=org
    )
    write_api = client.write_api(write_options=SYNCHRONOUS)


    mtr_result = json.load(sys.stdin)
    # ping destination
    destination = mtr_result['report']['mtr']['dst']
    report_time = dt.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
    for hub in mtr_result['report']['hubs']:
        # persist the hub entry
        # Modifying the data if needed so that is can be easily sorted in the event of more than 9 hops.
        if len(str(hub['count'])) < 2:
            hop = "0" + str(hub['count']) + "-" + hub['host']
        else:
            hop = str(hub['count']) + "-" + hub['host']
         
        p = influxdb_client.Point("mtr").tag("destination",destination).tag("hop",hop).field("loss",hub['Loss%']).field("snt",hub['Snt']).field("last",hub['Last']).field("avg", hub['Avg']).field("best",hub['Best']).field("wrst",hub['Wrst']).field("stdev",hub['StDev']).field("time",report_time)
        
        write_api.write(bucket=bucket, org=org, record=p)


if __name__ == '__main__':
    main()

from mtr-monitor.

nodiaque avatar nodiaque commented on August 29, 2024

oh btw, this code wasn't tested with the whole solution here. By using this 2 script with the ip list file, this will run mtr and send it to a influxdb. I have created this code myself from this git so I can have a mtr docker running that does only that, no grafana and other dependency like that. All I need now is to learn git so I can upload this code somewhere do docker can be dynamic

from mtr-monitor.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.