| Current Path : /home/otto/python/app/services/__pycache__/ |
| Current File : //home/otto/python/app/services/__pycache__/server_checks.cpython-310.pyc |
o
�ױh�N � @ s� d Z ddlZddlZddlmZmZmZmZ ddlmZ ddl m
Z
ddlZddlZej
�dee
e�jjj�� ddlmZ ddlmZ G d d
� d
�Zddee deeef fd
d�ZdS )z�
Server Monitoring Service for IGA Core
This module provides server health monitoring including:
- Memory usage monitoring
- Disk space monitoring
- CPU load monitoring
- Email alerts when thresholds are exceeded
� N)�Dict�Any�Optional�Tuple)�datetime)�Path)�get_server_monitor_config� )�SymfonyMailServicec @ s� e Zd ZdZddee fdd�Zdeeef fdd�Z deeef fd d
�Z
deeef fdd�Zdeeef fd
d�Zde
deeef fdd�Zdeeef fdd�Zdeeef fdd�ZdS )�
ServerMonitorzP
Server monitoring class that checks system resources and sends alerts.
N�alert_emailc C sR t � }|p|d | _t� | _|d | _|d | _|d | _t�� | _t �
� | _dS )z�
Initialize the server monitor.
Args:
alert_email: Email address to send alerts to (optional, uses config default if not provided)
r �disk_threshold�memory_threshold�
cpu_thresholdN)r r r
�mail_servicer
r r �psutil� cpu_count�platform�node�hostname)�selfr �config� r �^/home/otto/wwwroots/BitBucket/iga-core-symfony/iga-core-5/python/app/services/server_checks.py�__init__ s
zServerMonitor.__init__�returnc
C sL z�g }g }t �� D ]r}z_t �|j�}|j|j d }d| }|j|j|jt|jd d�t|jd d�t|j d d�t|d�t|d�d�}|�
|� || jkrid|j� d|d�d|d d�d
�}|�
dd||d
�� W q tys Y q t
y{ Y q w d||t�� �� d�W S t
y� } zdt| �t�� �� d�W Y d} ~ S d} ~ ww )z�
Check disk usage for all mounted filesystems.
Returns:
Dict containing disk usage information and alerts
�d � @� )�device�
mountpoint�
filesystem�total_gb�used_gb�free_gb�percent_used�percent_freezDISK ALERT: z is �.1fz% full (r$ �GB free)�disk�high��type�severity�message�data�success)�status� disk_info�alerts� timestamp�error�r1 r5 r4 N)r �disk_partitions�
disk_usager �used�totalr �fstype�round�free�appendr
�PermissionError� Exceptionr �now� isoformat�str)
r r2 r3 � partition�usager% r&