fix a few issues with check_media_cdn

This commit is contained in:
Cyberes 2024-02-18 14:54:33 -07:00
parent 458865e1e6
commit 9f81effbcf
2 changed files with 48 additions and 30 deletions

View File

@ -18,30 +18,6 @@ from urllib3.exceptions import InsecureRequestWarning
from checker import nagios
from checker.synapse_client import send_image, write_login_details_to_disk
parser = argparse.ArgumentParser(description='')
parser.add_argument('--user', required=True, help='User ID for the bot.')
parser.add_argument('--pw', required=True, help='Password for the bot.')
parser.add_argument('--hs', required=True, help='Homeserver of the bot.')
parser.add_argument('--admin-endpoint', required=True, help='Admin endpoint that will be called to purge media for this user.')
parser.add_argument('--room', required=True, help='The room the bot should send its test messages in.')
parser.add_argument('--check-domain', required=True, help='The domain that should be present.')
parser.add_argument('--media-cdn-redirect', default='true', help='If set, the server must respond with a redirect to the media CDN domain.')
parser.add_argument('--required-headers', nargs='*', help="If these headers aren't set to the correct value, critical. Use the format 'key=value")
parser.add_argument('--auth-file', help="File to cache the bot's login details to.")
parser.add_argument('--retries', type=int, default=11, help="It may take a few seconds for Synapse to send the uploaded file to S3. Retry this many times with 1 second interval between.")
parser.add_argument('--timeout', type=float, default=90, help='Request timeout limit.')
parser.add_argument('--warn', type=float, default=2.0, help='Manually set warn level.')
parser.add_argument('--crit', type=float, default=2.5, help='Manually set critical level.')
args = parser.parse_args()
if args.media_cdn_redirect == 'true':
args.media_cdn_redirect = True
elif args.media_cdn_redirect == 'false':
args.media_cdn_redirect = False
else:
print('UNKNOWN: could not parse the value for --media-cdn-redirect')
sys.exit(nagios.UNKNOWN)
def verify_media_header(header: str, header_dict: dict, good_value: str = None, warn_value: str = None, critical_value: str = None):
"""
@ -70,7 +46,7 @@ def verify_media_header(header: str, header_dict: dict, good_value: str = None,
return f'OK: {header} is present', nagios.OK # with value "{header_value}"'
async def main() -> None:
async def main(args) -> None:
exit_code = nagios.OK
async def cleanup(client, test_image_path, image_event_id=None):
@ -165,14 +141,19 @@ async def main() -> None:
retried += 1
end = time.time()
if args.ignore_first_attempt and retried > 0:
retried -= 1
exit_code = nagios.STATE_OK
prints = []
if r.status_code != 200 and not args.media_cdn_redirect:
await cleanup(client, test_image_path, image_event_id=image_event_id)
prints.append(f'CRITICAL: status code is "{r.status_code}"')
print(f'CRITICAL: status code for CDN image is "{r.status_code}"')
print(f'Hosted URL is {r.url}')
sys.exit(nagios.CRITICAL)
else:
prints.append(f'OK: status code is "{r.status_code}"')
prints.append(f'OK: status code for CDN image is "{r.status_code}"')
# Check domain
if args.media_cdn_redirect:
@ -228,6 +209,9 @@ async def main() -> None:
print('WARNING: media CDN is bad.')
elif exit_code == nagios.CRITICAL:
print('CRITICAL: media CDN is bad.')
else:
raise Exception('No exit code matched')
for msg in prints:
print(msg)
@ -235,15 +219,40 @@ async def main() -> None:
print(clean_msg)
if exit_code == nagios.STATE_OK:
print(f'Took {int(end - start)} seconds to with {retried} retries. | retries={retried}')
print(f'Took {int(end - start)} seconds to fetch the uploaded image with {retried} retries. | retries={retried}')
sys.exit(exit_code)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='')
parser.add_argument('--user', required=True, help='User ID for the bot.')
parser.add_argument('--pw', required=True, help='Password for the bot.')
parser.add_argument('--hs', required=True, help='Homeserver of the bot.')
parser.add_argument('--admin-endpoint', required=True, help='Admin endpoint that will be called to purge media for this user.')
parser.add_argument('--room', required=True, help='The room the bot should send its test messages in.')
parser.add_argument('--check-domain', required=True, help='The domain that should be present.')
parser.add_argument('--media-cdn-redirect', default='true', help='If set, the server must respond with a redirect to the media CDN domain.')
parser.add_argument('--required-headers', nargs='*', help="If these headers aren't set to the correct value, critical. Use the format 'key=value")
parser.add_argument('--auth-file', help="File to cache the bot's login details to.")
parser.add_argument('--retries', type=int, default=11, help="It may take a few seconds for Synapse to send the uploaded file to S3. Retry this many times with 1 second interval between.")
parser.add_argument('--timeout', type=float, default=90, help='Request timeout limit.')
parser.add_argument('--warn', type=float, default=2.0, help='Manually set warn level.')
parser.add_argument('--crit', type=float, default=2.5, help='Manually set critical level.')
parser.add_argument('--ignore-first-attempt', action='store_true', help='Ignore the first attempt at fetching the image from the media CDN because the server may not have cached the file if it has never been requested before.')
args = parser.parse_args()
if args.media_cdn_redirect == 'true':
args.media_cdn_redirect = True
elif args.media_cdn_redirect == 'false':
args.media_cdn_redirect = False
else:
print('UNKNOWN: could not parse the value for --media-cdn-redirect')
sys.exit(nagios.UNKNOWN)
try:
asyncio.run(main())
asyncio.run(main(args))
except Exception as e:
print(f'UNKNOWN: exception\n{e}')
print(traceback.format_exc())
traceback.print_exc()
sys.exit(nagios.UNKNOWN)

View File

@ -9,6 +9,15 @@ from checker import nagios, print_icinga2_check_status
from checker.types import try_int
"""
## MySQL User Setup ##
Attention: The DB-user you type in must have CLIENT REPLICATION rights on the DB-server. Example:
GRANT REPLICATION CLIENT on *.* TO 'nagios'@'monitoringhost' IDENTIFIED BY 'secret';
If you use MariaDB 10.5 or newer, the DB user must have REPLICA MONITOR rights:
GRANT REPLICA MONITOR ON *.* TO 'nagios'@'monitoringhost' IDENTIFIED BY 'secret';
## Usage example ##
Target delay = 300 seconds