| Current Path : /var/www/cesa.co.za/python/app/services/__pycache__/ |
| Current File : /var/www/cesa.co.za/python/app/services/__pycache__/server_checks.cpython-311.pyc |
�
<Ri�S � �2 � d Z ddlZddlZddlmZmZmZmZ ddlmZ ddl m
Z
ddlZddlZej
� d e e
e� � j j j � � � � ddlmZ ddlmZ G d � d
� � Zddee deeef fd
�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 �� � e Zd ZdZddee fd�Zdeeef fd�Z deeef fd�Z
deeef fd�Zdeeef fd �Zd
e
deeef fd�Zdeeef fd
�Zdeeef fd�ZdS )�
ServerMonitorzP
Server monitoring class that checks system resources and sends alerts.
N�alert_emailc � � t � � }|p|d | _ t � � | _ |d | _ |d | _ |d | _ t j � � | _ t j
� � | _ 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 �configs �8/var/www/cesa.co.za/python/app/services/server_checks.py�__init__zServerMonitor.__init__ s} � � +�,�,��&�?�&��*?���.�0�0��� %�%5�6��� &�'9� :���#�O�4��� �)�+�+��� �
����
�
�
� �returnc � � g }g }t j � � D �]6} t j |j � � }|j |j z dz }d|z
}|j |j |j t |j dz d� � t |j dz d� � t |j dz d� � t |d� � t |d� � d�}|�
|� � || j k rM|j � d� � s3d|j � d|d�d |d
d�d�}|�
dd
||d�� � ��# t $ r Y ��(t $ r Y ��4w xY wd||t j � � � � � d�S # t $ r@} dt% | � � t j � � � � � d�cY d} ~ S d} ~ ww xY w)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_freez/snapzDISK ALERT: z is �.1fz% full (r( �GB free)�disk�high��type�severity�message�data�success)�status� disk_info�alerts� timestamp�error�r5 r9 r8 N)r �disk_partitions�
disk_usager$ �used�totalr# �fstype�round�free�appendr �
startswith�PermissionError� Exceptionr �now� isoformat�str)
r r6 r7 � partition�usager) r* � disk_data� alert_msg�es
r �check_disk_usagezServerMonitor.check_disk_usage5 s\ � �5 ��I��F�#�3�5�5� #
� #
� �"�"�-�i�.B�C�C�E�$)�J���$<��#C�L�#&��#5�L� #,�"2�&/�&:�&/�&6�$)�%�+��*A�1�$E�$E�#(���w�)?��#C�#C�#(���w�)?��#C�#C�(-�l�A�(>�(>�(-�l�A�(>�(>� !� !�I� �$�$�Y�/�/�/� $�d�&9�9�9�)�BV�Ba�Ba�bi�Bj�Bj�9�C�9�+?� C� C�\�^� C� C� )�)� 4�B�C� C� C� "� �
�
�$*�(.�'0�$-� '� '� � � ��� '� � � ��D� � � � ��D����� $�&� �%�\�^�^�5�5�7�7� � �
�� � � � �!��Q���%�\�^�^�5�5�7�7�� �
�
�
�
�
�
����� ���sM �E; �DD4�2E; �4
E�>E; � E�
E; �
E�,E; �;
G�5G �:G� Gc � � t j � � }t |j dz d� � t |j dz d� � t |j dz d� � t |j d� � t d|j z
d� � d�}g }|d | j k r1d|d d�d|d d�d
�}|� dd||d
�� � d||t j
� � � � � d�S # t $ r@}dt |� � t j
� � � � � d�cY d}~S d}~ww xY w)z
Check memory usage.
Returns:
Dict containing memory usage information and alerts
r! r"