zabbix_telemetry.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python3
  2. #ident "@(#)$Format:LocalFoodAI_lanfr144:zabbix_telemetry.py:%an:%ae:%ad:%cn:%ce:%cd:%H:%D:%N$""
  3. import os
  4. import sys
  5. import pymysql
  6. from dotenv import load_dotenv
  7. def get_db_connection():
  8. current_dir = os.path.dirname(os.path.abspath(__file__))
  9. env_path = os.path.join(current_dir, '.env')
  10. load_dotenv(dotenv_path=env_path)
  11. # Try environment variables from compose or env
  12. db_host = os.environ.get('DB_HOST', '127.0.0.1')
  13. db_user = os.environ.get('DB_USER') or os.environ.get('DB_LOADER_USER') or 'food_loader'
  14. db_pass = os.environ.get('DB_PASS') or os.environ.get('DB_LOADER_PASS') or 'your_db_loader_pass'
  15. # Check if .env config parameters exist
  16. if os.path.exists(env_path):
  17. with open(env_path, 'r') as f:
  18. for line in f:
  19. if line.strip().startswith('MYSQL_ROOT_PASSWORD'):
  20. db_pass = line.split('=')[1].strip()
  21. db_user = 'root'
  22. elif line.strip().startswith('DB_LOADER_PASS') and db_user != 'root':
  23. db_pass = line.split('=')[1].strip()
  24. db_user = 'food_loader'
  25. # Shifted MySQL Port check (if WSL or local offset)
  26. db_port = 3306
  27. if os.environ.get('NETWORK_MODE') == 'local' or os.path.exists(os.path.join(current_dir, 'docker-compose-wsl.yml')):
  28. db_port = 3326
  29. return pymysql.connect(
  30. host=db_host,
  31. user=db_user,
  32. password=db_pass,
  33. database='food_db',
  34. port=db_port,
  35. cursorclass=pymysql.cursors.DictCursor
  36. )
  37. def main():
  38. if len(sys.argv) < 2:
  39. print("Usage: python zabbix_telemetry.py [status|rows|start|end]")
  40. sys.exit(1)
  41. metric = sys.argv[1].lower()
  42. try:
  43. conn = get_db_connection()
  44. except Exception as e:
  45. if metric == 'status':
  46. print("OFFLINE")
  47. elif metric == 'rows':
  48. print("0")
  49. else:
  50. print("N/A")
  51. sys.exit(0)
  52. try:
  53. with conn.cursor() as cursor:
  54. # Query the last ingestion run details
  55. cursor.execute("""
  56. SELECT status, start_time, end_time, rows_loaded
  57. FROM ingestion_status
  58. ORDER BY id DESC LIMIT 1
  59. """)
  60. row = cursor.fetchone()
  61. if not row:
  62. if metric == 'status': print("NO_DATA")
  63. elif metric == 'rows': print("0")
  64. else: print("N/A")
  65. sys.exit(0)
  66. if metric == 'status':
  67. print(row['status'])
  68. elif metric == 'rows':
  69. print(row['rows_loaded'])
  70. elif metric == 'start':
  71. print(row['start_time'].strftime("%Y-%m-%d %H:%M:%S") if row['start_time'] else "N/A")
  72. elif metric == 'end':
  73. print(row['end_time'].strftime("%Y-%m-%d %H:%M:%S") if row['end_time'] else "N/A")
  74. else:
  75. print("INVALID_METRIC")
  76. except Exception as e:
  77. if metric == 'status': print("ERROR")
  78. elif metric == 'rows': print("-1")
  79. else: print(f"Error: {e}")
  80. finally:
  81. conn.close()
  82. if __name__ == "__main__":
  83. main()