|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import logging |
| 4 | +import time |
| 5 | + |
| 6 | +import psutil |
| 7 | + |
| 8 | + |
| 9 | +def main(): |
| 10 | + setup_logging() |
| 11 | + |
| 12 | + # Track how long memory has been high |
| 13 | + high_memory_min_duration = 120 |
| 14 | + high_memory_threshold = 80 |
| 15 | + |
| 16 | + print("Memory Monitor Started...") |
| 17 | + print("Monitoring for:") |
| 18 | + print( |
| 19 | + f" - Memory usage > {high_memory_threshold}% for {high_memory_min_duration // 60} minutes" |
| 20 | + ) |
| 21 | + print(" - Swap memory usage") |
| 22 | + print(" - OOM kill events") |
| 23 | + |
| 24 | + high_memory_start_time = None |
| 25 | + |
| 26 | + while True: |
| 27 | + try: |
| 28 | + check_memory_usage( |
| 29 | + high_memory_start_time, |
| 30 | + duration=high_memory_min_duration, |
| 31 | + threshold=high_memory_threshold, |
| 32 | + ) |
| 33 | + check_swap_usage() |
| 34 | + check_oom_kills() |
| 35 | + time.sleep(10) # Check every 10 seconds |
| 36 | + except KeyboardInterrupt: |
| 37 | + print("Memory monitoring stopped.") |
| 38 | + break |
| 39 | + except Exception as e: |
| 40 | + logging.error(f"Error during monitoring: {e}") |
| 41 | + |
| 42 | + |
| 43 | +def setup_logging(): |
| 44 | + logging.basicConfig( |
| 45 | + level=logging.INFO, |
| 46 | + format="%(asctime)s - %(levelname)s - %(message)s", |
| 47 | + filename="memory_monitor.log", |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +def check_memory_usage(high_memory_start_time, **kwargs): |
| 52 | + mem = psutil.virtual_memory() |
| 53 | + mem_percent = mem.percent |
| 54 | + duration = kwargs.get("duration", 120) |
| 55 | + threshold = kwargs.get("threshold", 80) |
| 56 | + |
| 57 | + if mem_percent > threshold: |
| 58 | + if high_memory_start_time is None: |
| 59 | + high_memory_start_time = time.time() |
| 60 | + logging.warning(f"Memory usage high: {mem_percent:.1f}%") |
| 61 | + else: |
| 62 | + duration = time.time() - high_memory_start_time |
| 63 | + if duration >= duration: |
| 64 | + logging.critical( |
| 65 | + f"HIGH MEMORY ALERT: {mem_percent:.1f}% for {duration:.0f} seconds" |
| 66 | + ) |
| 67 | + else: |
| 68 | + if high_memory_start_time is not None: |
| 69 | + logging.info(f"Memory usage returned to normal: {mem_percent:.1f}%") |
| 70 | + high_memory_start_time = None |
| 71 | + |
| 72 | + return high_memory_start_time |
| 73 | + |
| 74 | + |
| 75 | +def check_swap_usage(): |
| 76 | + swap = psutil.swap_memory() |
| 77 | + if swap.used > 0: |
| 78 | + swap_percent = swap.percent |
| 79 | + logging.warning( |
| 80 | + f"Swap memory in use: {swap.used / (1024**3):.2f} GB ({swap_percent:.1f}%)" |
| 81 | + ) |
| 82 | + print( |
| 83 | + f"ALERT: Swap memory in use: {swap.used / (1024**3):.2f} GB ({swap_percent:.1f}%)" |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +def check_oom_kills(): |
| 88 | + try: |
| 89 | + with open("/proc/vmstat", "r") as f: |
| 90 | + for line in f: |
| 91 | + if "oom_kill" in line: |
| 92 | + parts = line.strip().split() |
| 93 | + if len(parts) >= 2 and int(parts[1]) > 0: |
| 94 | + logging.critical(f"OOM kill detected: {parts[1]} occurrences") |
| 95 | + except Exception as e: |
| 96 | + logging.error(f"Error checking OOM kills: {e}") |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + main() |
0 commit comments