PyTelekomPass/PyPass.py

69 lines
2 KiB
Python
Raw Normal View History

2018-12-08 13:59:05 +01:00
#!/usr/bin/env python3
2018-12-09 14:32:19 +01:00
# Python Telekom Data Usage Logger
# Copyright (c) 2018 Nis Wechselberg <enbewe@enbewe.de>
#
# This work is licensed under MIT license. You should have
# received a copy of the MIT license legalcode along with this
# work. If not, see <https://opensource.org/licenses/MIT>.
2018-12-08 13:59:05 +01:00
# pylint: disable=C0103
"""
2018-12-09 14:32:19 +01:00
Python Telekom Data Usage Logger
2018-12-08 13:59:05 +01:00
Small script to request data from the portal pass.telekom.de and
submit it to InfluxDB
"""
2018-12-09 16:30:01 +01:00
import argparse
2018-12-08 13:59:05 +01:00
import requests
from influxdb import InfluxDBClient
REQUEST_API_URL = 'http://pass.telekom.de/api/service/generic/v1/status'
REQUEST_HEADERS = {'User-Agent': 'Mozilla/4.0'}
2018-12-09 16:30:01 +01:00
parser = argparse.ArgumentParser()
parser.add_argument("hostname", help="Hostname of this machine")
parser.add_argument("influxHost", help="Hostname/IP of the InfluxDB machine")
parser.add_argument("database", help="Name of the database to write to")
args = parser.parse_args()
2018-12-08 13:59:05 +01:00
# Send http request to telekom page and extract json response
response = requests.get(REQUEST_API_URL, headers=REQUEST_HEADERS)
respJson = response.json()
# Prepare timedata for database
2019-04-30 18:32:31 +02:00
if respJson['usedPercentage'] < 100:
measurement = [
{
"measurement": "pass_telekom_de",
"tags": {
"host": args.hostname
},
"fields": {
"initialVolume": respJson['initialVolume'],
"usedVolume": respJson['usedVolume'],
"remainingSeconds": respJson['remainingSeconds'],
"usedAtMillis": respJson['usedAt']
}
2018-12-08 13:59:05 +01:00
}
2019-04-30 18:32:31 +02:00
]
else:
measurement = [
{
"measurement": "pass_telekom_de",
"tags": {
"host": args.hostname
},
"fields": {
"remainingSeconds": respJson['remainingSeconds'],
"usedAtMillis": respJson['usedAt']
}
}
]
2018-12-08 13:59:05 +01:00
# Send data to InfluxDB
2018-12-09 16:30:01 +01:00
influx = InfluxDBClient(host=args.influxHost, database=args.database)
2018-12-08 13:59:05 +01:00
influx.write_points(measurement)