#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright 2024 Inc. # All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. # NOTE(mmagr): This script will be usable when following patch will be merged # and included in release -> # https://review.opendev.org/c/openstack/ceilometer/+/924205 import datetime import socket import sys HEARTBEAT_SOCKET = "/tmp/ceilometer-compute.socket" POLL_SPREAD = datetime.timedelta(hours=1) def check_pollsters(socket_path: str) -> (int, str): """ Returns 0 if socket_path content contains all records of polling no older than POLL_SPREAD, otherwise returns 1. """ s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.connect(socket_path) report = "" try: while True: data = s.recv(2048) if data: report += data.decode('utf-8') else: break finally: s.close() limit = datetime.datetime.today() - POLL_SPREAD for line in report.split('\n'): pollster, timestr = line.split() timestamp = datetime.datetime.fromisoformat(timestr) if timestamp < limit: return 1, f"{pollster}'s timestamp is out of limit" return 0, "" if __name__ == "__main__": if len(sys.argv) > 1: pth = sys.argv[1] else: pth = HEARTBEAT_SOCKET rc, reason = check_pollsters(pth) if rc != 0: print(reason) sys.exit(rc)