l#g fdZddlZejdddgZGddZeZdS)aThis module implements health status reporting for watchdog operation. Module receive important health metrics and exports its status of overall health assessment. This health assessment can be used by external watchdog scripts to initiate agent restart. Process is considered "healthy" if: * it is being shut down and shutdown timeout has not elapsed -> HEALTHY * it is not registered -> HEALTHY * process was started more than 6 hours ago and no data was sent to server within last 6 hours -> FAULTY * process was started more than 18 hours ago and no data was received from server within last 18 hours -> FAULTY Otherwise process is considered HEALTHY. As agent exports this information through RPC interface there is an additional implicit "health" requirement that: * it responds to RPC requests. This implicit requirement considered valid because UI fully depends on RPC so it does not make health assessment any worse than it should.N HealthStatushealthywhyceZdZdZdZdZdZdZdeddfd Z deddfd Z deddfd Z deddfd Z dd Z ddZdedefdZdS) HealthSensoraHealthSensor receives events about agent operation and provides information about overall status. Initially, new HealthSensor object assumes: * process was started long ago; * process is not being shut down; * data from server has been received long ago; * data to server was sent long ago; * agent is registered (license is valid). So, initial health status is False (faulty).i i`TiXcLd|_d|_d|_d|_d|_dS)NgT) _started_at _shutdown_at_last_received _last_sent_is_registeredselfs O/opt/imunify360/venv/lib/python3.11/site-packages/defence360agent/api/health.py__init__zHealthSensor.__init__1s/!"whenreturnNc||_dS)z!Records a moment of agent startupN)r rrs rstartingzHealthSensor.starting8src||_dS)z7Records a moment of externally initiated agent shutdownN)r rs r shutting_downzHealthSensor.shutting_down<s rc||_dS)z3Records a moment when data was received from serverN)r rs rserver_data_receivedz!HealthSensor.server_data_received@"rc||_dS)z-Records a moment when data was sent to serverN)r rs rserver_data_sentzHealthSensor.server_data_sentDs rcd|_dS)zMarks agent as being registeredTNr rs r registeredzHealthSensor.registeredHrrcd|_dS)z#Marks agent as being not registeredFNr rs r unregisteredzHealthSensor.unregisteredLs#rnowc|jdkr3||jz |jkrtddStddS|jstddS||jz |jkr#||jz |jkrtddS||jz |jkr#||jz |jkrtddStdd S) NrFzstuck at shutdownTzshutdown is in progressznot registeredzno data received from serverzno data sent to serverz all is ok) r SHUTDOWN_TIMEOUTrr r RECEIVE_WINDOWr SEND_WINDOWr )rr$s rstatuszHealthSensor.statusPs  q T&&$*???#E+>???&?@@ @" 8&677 7 $" "d&9 9 9d))T-@@@'EFF F $" "d&6 6 6do%)999'?@@ @D+...r)rN)__name__ __module__ __qualname____doc__r'r(r&rfloatrrrrr!r#rr)rrrrs 4 4NK### U t    !%!D!!!!##4####Ut####$$$$/%/L//////rr)r- collections namedtuplerrsensorr/rrr3szCC2%{%ny%6HII B/B/B/B/B/B/B/B/J r