Commit 863e1087 by syed-awais-ali

fixes

parent cc986977
"""
CloudFlare API
https://api.cloudflare.com/#zone-analytics-dashboard
"""
import requests import requests
import argparse import argparse
import sys
CLOUDFLARE_API_ENDPOINT = "https://api.cloudflare.com/client/v4/" CLOUDFLARE_API_ENDPOINT = "https://api.cloudflare.com/client/v4/"
def calcualte_cache_hit_rate(zone_id, auth_key, email): def calcualte_cache_hit_rate(zone_id, auth_key, email, threshold):
HEADERS = {"Accept": "application/json", HEADERS = {"Accept": "application/json",
"X-Auth-Key": auth_key, "X-Auth-Key": auth_key,
"X-Auth-Email": email} "X-Auth-Email": email}
# for last 7 hours, -419 indicates minutes, we can go
# beyond that as well, for example for last 15
# hours it will be -899
PARAMS = {"since": "-419", "continuous": "true"}
res = requests.get(CLOUDFLARE_API_ENDPOINT + "zones/" + zone_id res = requests.get(CLOUDFLARE_API_ENDPOINT + "zones/" + zone_id
+ "/analytics/dashboard?since=-419&continuous=true", + "/analytics/dashboard", headers=HEADERS,
headers=HEADERS) params=PARAMS)
data = res.json() try:
all_req = float(data["result"]["timeseries"][0]["requests"]["all"]) data = res.json()
cached_req = float(data["result"]["timeseries"][0]["requests"]["cached"]) all_req = float(data["result"]["timeseries"][0]["requests"]["all"])
threshold_limit = cached_req * 100.0 / all_req cached_req = float(data["result"]["timeseries"][0]["requests"]["cached"])
current_cache_hit_rate = cached_req / all_req * 100
if current_cache_hit_rate < threshold:
sys.exit(1)
except Exception as error:
print "JSON Error: {}".format(error)
# Add threshold for alerts/alarms
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
...@@ -26,6 +43,8 @@ if __name__ == "__main__": ...@@ -26,6 +43,8 @@ if __name__ == "__main__":
help="Authentication Key") help="Authentication Key")
parser.add_argument('-e', '--email', required=True, parser.add_argument('-e', '--email', required=True,
help="email to use for authentication for CloudFlare API") help="email to use for authentication for CloudFlare API")
parser.add_argument('-t', '--threshold', required=True,
help="Threshold limit to be passed to check against it")
args = parser.parse_args() args = parser.parse_args()
calcualte_cache_hit_rate(args.zone, args.auth_key, args.email) calcualte_cache_hit_rate(args.zone, args.auth_key, args.email, args.threshold)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment