#!/usr/bin/env python3 import requests from requests.auth import HTTPDigestAuth import argparse import sys OK = 0 WARNING = 1 CRITICAL = 2 UNKNOWN = 3 def parse_args(): parser = argparse.ArgumentParser(description="TVHeadend DVB Tuner Check") parser.add_argument("--host", default="localhost", help="TVHeadend host") parser.add_argument("--port", default="9981", help="TVHeadend port") parser.add_argument("--user", required=True, help="API username") parser.add_argument("--password", required=True, help="API password") parser.add_argument("--timeout", type=int, default=10, help="Request timeout") parser.add_argument("--debug", action="store_true", help="Enable debug mode") return parser.parse_args() def check_api_status(args): base_url = f"http://{args.host}:{args.port}" auth = HTTPDigestAuth(args.user, args.password) try: # First check basic API accessibility status_url = f"{base_url}/api/status" if args.debug: print(f"DEBUG: Checking API status at {status_url}") response = requests.get(status_url, auth=auth, timeout=args.timeout) if response.status_code == 401: raise Exception("Authentication failed - check credentials") response.raise_for_status() if args.debug: print(f"DEBUG: API status response: {response.json()}") return True except Exception as e: if args.debug: print(f"DEBUG: API check failed: {str(e)}") raise Exception(f"Cannot access TVHeadend API: {str(e)}") def main(): args = parse_args() try: # Verify API is accessible check_api_status(args) # If we got here, API is accessible but we couldn't find specific endpoints msg = "TVHeadend API is accessible but required endpoints not found" if args.debug: print(f"DEBUG: {msg}") print("DEBUG: Try checking available endpoints with:") print(f"DEBUG: curl -u '{args.user}:{args.password}' http://{args.host}:{args.port}/api/help") print(msg) sys.exit(UNKNOWN) except Exception as e: print(str(e)) sys.exit(CRITICAL) if __name__ == "__main__": main()