diff --git a/agent_ssllabs.mkp b/agent_ssllabs.mkp index 8c0e9d0c4171153e6c1fdf91d3e07cbd29bc98f7..eadc333bcafc203183b78d15dee5c9f2b2fbf0f1 100644 Binary files a/agent_ssllabs.mkp and b/agent_ssllabs.mkp differ diff --git a/lib/check_mk/special_agent/agent_ssllabs.py b/lib/check_mk/special_agent/agent_ssllabs.py index 726bc389e5f4195bc9c063fb153696cd73613709..a9bee538f97a0c86ffd4ae85d21ebf1ce6e8f8eb 100755 --- a/lib/check_mk/special_agent/agent_ssllabs.py +++ b/lib/check_mk/special_agent/agent_ssllabs.py @@ -30,8 +30,6 @@ from typing import Optional, Sequence from cmk.utils.paths import tmp_dir -# import datetime as datetime - def parse_arguments(argv: Sequence[str]) -> argparse.Namespace: ''''Parse arguments needed to construct an URL and for connection conditions''' parser = argparse.ArgumentParser() @@ -44,18 +42,11 @@ def parse_arguments(argv: Sequence[str]) -> argparse.Namespace: return parser.parse_args(argv) -def print_check_header(): - print('<<<ssllabs_grade:sep(0)>>>') - - -def connect_ssllabs_api(ssl_host_address: str, host_cache: str, - host_state_cache: str, grade_cache: str, args: argparse.Namespace, ): +def connect_ssllabs_api(ssl_host_address: str, host_cache: str, args: argparse.Namespace, ): server = 'api.ssllabs.com' uri = 'api/v3/analyze' maxAge = args.maxage # default 167 (1 week minus 1 hour) publish = args.publish # default off - all = 'done' - startNew = 'off' fromCache = 'on' now = time.time() @@ -66,7 +57,6 @@ def connect_ssllabs_api(ssl_host_address: str, host_cache: str, proxies = {'https': args.proxy.split('/')[-1]} # remove 'https://' from proxy string try: - response = requests.get( url=url, timeout=args.timeout, @@ -77,49 +67,32 @@ def connect_ssllabs_api(ssl_host_address: str, host_cache: str, except Exception as err: sys.stdout.write(f'{ssl_host_address} Connection error: {err} on {url}') - print(f'{ssl_host_address};{err};{now};2') + # print(f'{ssl_host_address};{err};{now};2') return + print(response.text) + try: - starttime = jsonData.get('startTime') - statusmessage = jsonData.get('statusMessage') if jsonData['status'] == 'READY': - grade = jsonData['endpoints'][0]['grade'] - testtime = jsonData['testTime'] - print(f'{ssl_host_address};{grade};{testtime};0;{grade}') # if test finish and json data ok --> write data in cache file with open(host_cache, 'w') as outfile: json.dump(jsonData, outfile) - with open(host_state_cache, 'w') as f: - f.write(grade) - elif jsonData['status'] == 'ERROR': - print(f'{ssl_host_address};{statusmessage};{starttime};2;{grade_cache}') - elif jsonData['status'] == 'IN_PROGRESS': - statusdetails = jsonData['endpoints'][0]['statusDetails'] - print(f'{ssl_host_address};Testing, step: {statusdetails};{starttime};1;{grade_cache}') - elif jsonData['status'] == 'DNS': - print(f'{ssl_host_address};{statusmessage};{starttime};1;{grade_cache}') - else: - print(f'{ssl_host_address};status unknown;{now};3;{grade_cache}') except (ValueError, KeyError, TypeError): - print(f'{ssl_host_address};request JSON format error;{now};2;{grade_cache}') + print(f'{ssl_host_address};request JSON format error;{now};2') # ;{grade_cache} -def read_cache(ssl_host_address, host_cache, grade_cache): - now = time.time() - +def read_cache(host_cache: str): # read cache file with open(host_cache) as json_file: # check if cache file contains valid json data try: jsonData = json.load(json_file) if jsonData['status'] == 'READY': - grade = jsonData['endpoints'][0]['grade'] - testtime = jsonData['testTime'] - print(f'{ssl_host_address};{grade};{testtime};0;{grade}') + print(json.dumps(jsonData)) except (ValueError, KeyError, TypeError): - print(f'{ssl_host_address};cache JSON format error;{now};2;{grade_cache}') + # print(f'{ssl_host_address};cache JSON format error;{now};2;{grade_cache}') + return def main(argv: Optional[Sequence[str]] = None) -> None: @@ -135,50 +108,36 @@ def main(argv: Optional[Sequence[str]] = None) -> None: sys.stdout.write('<<<check_mk>>>\n') sys.stdout.write('Version: %s\n' % VERSION) sys.stdout.write('AgentOS: linux\n') - # sys.stdout.write('<<<systemtime>>>\n') - # sys.stdout.write(time.strftime('%s\n')) # create cache directory, if not exists if not os.path.exists(cache_dir): os.makedirs(cache_dir) - print_check_header() + print('<<<ssllabs_grade:sep(0)>>>') for ssl_host_address in ssl_hosts: # changed cache file form host_address to ssl_host_address host_cache = '%s/%s' % (cache_dir, ssl_host_address) - host_state_cache = '%s/%s_state' % (cache_dir, ssl_host_address) - - # state cache form cache file - if os.path.exists(host_state_cache): - with open(host_state_cache) as cache_file: - grade_cache = cache_file.read() - else: - grade_cache = 'A' # check if cache file exists and is not older as cache_age if not os.path.exists(host_cache): connect_ssllabs_api( ssl_host_address=ssl_host_address, host_cache=host_cache, - host_state_cache=host_state_cache, - grade_cache=grade_cache, args=args) else: json_cache_file_age = os.path.getmtime(host_cache) cache_age_sec = now - json_cache_file_age if cache_age_sec < cache_age: read_cache( - ssl_host_address=ssl_host_address, host_cache=host_cache, - grade_cache=grade_cache) + ) else: connect_ssllabs_api( ssl_host_address=ssl_host_address, host_cache=host_cache, - host_state_cache=host_state_cache, - grade_cache=grade_cache, - args=args) + args=args + ) if __name__ == '__main__':