diff --git a/collector.py b/collector.py index 896af96..bb581c5 100644 --- a/collector.py +++ b/collector.py @@ -10,6 +10,7 @@ import requests import json import os +import jwt from prometheus_client.core import GaugeMetricFamily, REGISTRY from prometheus_client import start_http_server @@ -29,6 +30,9 @@ def __init__(self, verbose, username, password): self.password = password self.verbose = verbose + + self.previous_limit = None + self.previous_remaining = None def do_verbose(self, text): if self.verbose: @@ -67,6 +71,12 @@ def get_token(self): if not token: raise Exception('Cannot obtain token from Docker Hub. Please try again!') + # extract content from jwt token + jwtContent = jwt.decode(token, options={"verify_signature": False}) + if not self.previous_limit: + self.previous_limit = jwtContent.get('access')[0].get('parameters').get('pull_limit') + self.previous_remaining = self.previous_limit + return token ## Test against registry @@ -102,11 +112,21 @@ def get_registry_limits(self): def collect(self): (limit, remaining, reset) = self.get_registry_limits() + if len(str(remaining)) == 0: + remaining = self.previous_remaining + else: + self.previous_remaining = remaining + gr = GaugeMetricFamily("dockerhub_limit_remaining_requests_total", 'Docker Hub Rate Limit Remaining Requests', labels=['limit']) gr.add_metric(["remaining_requests_total"], remaining) yield gr + if len(str(limit)) == 0: + limit = self.previous_limit + else: + self.previous_limit = limit + gl = GaugeMetricFamily("dockerhub_limit_max_requests_total", 'Docker Hub Rate Limit Maximum Requests', labels=['limit']) gl.add_metric(["max_requests_total"], limit) diff --git a/requirements.txt b/requirements.txt index cea8d88..e7557d6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ requests prometheus_client +pyjwt