Files
DNS-AppNeta/RestfulClient.py
T
2025-08-03 21:24:38 +00:00

74 lines
2.8 KiB
Python

# https://app-13.pm.appneta.com/api/v3/dns/webPath/data?from=1557257776&to=1557258076&api_key=v3
#Python 2.7.6
#RestfulClient.py
import requests
from requests.auth import HTTPBasicAuth
import json
from credentials import username, password, apm_server
from influxdb import InfluxDBClient
import argparse
import time
from datetime import datetime, timedelta
datafrom = 1557257776
datato = 1557258076
# Replace with the correct URL
url = "https://"+apm_server+"/api/v3/dns/webPath/data?from="+str(datafrom)+"&to="+str(datato)+"&api_key=v3"
# It is a good practice not to hardcode the credentials. So ask the user to enter credentials at runtime
#myResponse = requests.get(url,auth=HTTPDigestAuth(username, password), verify=True)
myResponse = requests.get(url,auth=HTTPBasicAuth(username, password), verify=True)
#print (myResponse.status_code)
def main(influxhost, influxport):
# For successful API call, response code will be 200 (OK)
if(myResponse.ok):
# Loading the response data into a dict variable
# json.loads takes in only binary or string variables so using content to fetch binary content
# Loads (Load String) takes a Json file and converts into python data structure (dict or list, depending on JSON)
jData = json.loads(myResponse.content)
print("The response contains {0} properties".format(len(jData)))
print("\n")
print jData
# for key in jData:
# print (key , " : " , jData[key])
else:
# If response code is not ok (200), print the resulting http error code with description
myResponse.raise_for_status()
# labels, tags, time, count
def writeinflux(measurement,tags,time,fields,influxhost,influxport):
influxuser = 'CMWrite'
influxpass = 'CMWrite'
databasename = 'help'
client = InfluxDBClient(host=influxhost, port=influxport, username=influxuser, password=influxpass, database=databasename)
json_body = [
{
"measurement": measurement,
"tags": tags,
"time": time,
"fields": fields
}
]
print("Write points: {0}".format(json_body))
client.write_points(json_body)
# Allow command line overrides for host and port influxDB
def parse_args():
parser = argparse.ArgumentParser(
description='Sync Jira items with specific tag into InfluxDB database')
parser.add_argument('--host', type=str, required=False, default='itmetricsdb.mathworks.com',
help='hostname of InfluxDB http API')
parser.add_argument('--port', type=int, required=False, default=8086,
help='port of InfluxDB http API')
return parser.parse_args()
# grab arguments
if __name__ == '__main__':
args = parse_args()
main(influxhost=args.host, influxport=args.port)