From 6a45c379c23dc209e9a5cce0d2c75f814821204a Mon Sep 17 00:00:00 2001 From: Amin Ben Romdhane Date: Mon, 18 Nov 2024 11:09:16 +0100 Subject: [PATCH] sysmngr: Added support for DeviceInfo.ProcessStatus.CPU. Object --- sysmngr/Makefile | 7 +- sysmngr/files/etc/config/sysmngr | 12 +++ sysmngr/files/etc/init.d/sysmngr | 9 ++- .../etc/sysmngr/critical_state_logger.sh | 78 +++++++++++++++++++ 4 files changed, 102 insertions(+), 4 deletions(-) create mode 100755 sysmngr/files/etc/sysmngr/critical_state_logger.sh diff --git a/sysmngr/Makefile b/sysmngr/Makefile index e843fa106..caaaec076 100644 --- a/sysmngr/Makefile +++ b/sysmngr/Makefile @@ -5,13 +5,13 @@ include $(TOPDIR)/rules.mk PKG_NAME:=sysmngr -PKG_VERSION:=1.0.4 +PKG_VERSION:=1.0.5 LOCAL_DEV:=0 ifneq ($(LOCAL_DEV),1) PKG_SOURCE_PROTO:=git PKG_SOURCE_URL:=https://dev.iopsys.eu/system/sysmngr.git -PKG_SOURCE_VERSION:=ac570cb938211b131d44b70e551213f34278946f +PKG_SOURCE_VERSION:=ff129f75b9266e2b3cdc6bb319b12378f9ec5511 PKG_SOURCE:=$(PKG_NAME)-$(PKG_SOURCE_VERSION).tar.gz PKG_MIRROR_HASH:=skip endif @@ -92,6 +92,9 @@ define Package/$(PKG_NAME)/install $(INSTALL_DIR) $(1)/etc/init.d $(INSTALL_BIN) ./files/etc/init.d/sysmngr $(1)/etc/init.d/sysmngr + $(INSTALL_DIR) $(1)/etc/sysmngr + $(INSTALL_BIN) ./files/etc/sysmngr/critical_state_logger.sh $(1)/etc/sysmngr/critical_state_logger.sh + $(INSTALL_DIR) $(1)/usr/share/bbfdm/scripts $(CP) $(PKG_BUILD_DIR)/src/files/usr/share/bbfdm/scripts/bbf_activate_handler.sh $(1)/usr/share/bbfdm/scripts/ $(CP) $(PKG_BUILD_DIR)/src/files/usr/share/bbfdm/scripts/bbf_check_idle.sh $(1)/usr/share/bbfdm/scripts/ diff --git a/sysmngr/files/etc/config/sysmngr b/sysmngr/files/etc/config/sysmngr index 6c0c0a4a5..65bc35ff5 100644 --- a/sysmngr/files/etc/config/sysmngr +++ b/sysmngr/files/etc/config/sysmngr @@ -1,4 +1,7 @@ +config globals 'globals' + option log_level '3' + config reboots 'reboots' option max_reboot_entries '3' @@ -13,3 +16,12 @@ config memory_status 'memory' option enable_critical_log '0' option file_path '/var/log/critical_memory.log' +config cpu_status 'cpu' + option enable '0' + option poll_interval '5' + option num_samples '30' + option critical_rise_threshold '80' + option critical_fall_threshold '60' + option enable_critical_log '0' + option file_path '/var/log/critical_cpu.log' + diff --git a/sysmngr/files/etc/init.d/sysmngr b/sysmngr/files/etc/init.d/sysmngr index c10d5bb91..59af1556e 100644 --- a/sysmngr/files/etc/init.d/sysmngr +++ b/sysmngr/files/etc/init.d/sysmngr @@ -1,6 +1,6 @@ #!/bin/sh /etc/rc.common -START=60 +START=15 STOP=8 USE_PROCD=1 @@ -8,9 +8,14 @@ PROG=/usr/sbin/sysmngr start_service() { + local log_level + + config_load sysmngr + config_get log_level "globals" log_level 4 + procd_open_instance "sysmngr" procd_set_param command ${PROG} - #procd_append_param command -ddd + procd_append_param command -l "${log_level}" procd_set_param respawn procd_close_instance "sysmngr" } diff --git a/sysmngr/files/etc/sysmngr/critical_state_logger.sh b/sysmngr/files/etc/sysmngr/critical_state_logger.sh new file mode 100755 index 000000000..cdea58fc5 --- /dev/null +++ b/sysmngr/files/etc/sysmngr/critical_state_logger.sh @@ -0,0 +1,78 @@ +#!/bin/sh + +# Function to execute commands and append outputs to the log file +execute_commands() { + log_path="$1" # Log file path + cmd="$2" # Command + + echo "Output of command: $cmd" >> "$log_path" + $cmd >> "$log_path" 2>&1 + echo "" >> "$log_path" +} + +# Function to generate a critical state log +generate_critical_log() { + log_path="$1" # Path to the log file + log_name="$2" # Name of the critical state (CPU or Memory) + critical_state="$3" # Boolean-like string to indicate critical state ("true" or "false") + + # Get the current time in a formatted way + log_time=$(date "+%Y-%m-%d %H:%M:%S") + + # Determine critical state description + if [ "$critical_state" = "true" ]; then + state_desc="Reached" + else + state_desc="No Longer Present" + fi + + # Write the log header with a timestamp + echo "=== $log_name Critical State $state_desc at $log_time ===" >> "$log_path" + echo "Running diagnostic commands..." >> "$log_path" + + # Common commands for CPU and Memory + execute_commands "$log_path" "top -b -n 1" + execute_commands "$log_path" "free" + + # Add specific commands based on the critical state type + if [ "$log_name" = "CPU" ]; then + execute_commands "$log_path" "ps" + elif [ "$log_name" = "Memory" ]; then + execute_commands "$log_path" "df -h" + fi + + # End of log entry + echo "=== End of Critical $log_name Log ===" >> "$log_path" + echo "" >> "$log_path" + + # Echo a success message to indicate script completion + echo "Success: Log generation completed successfully." +} + +# Main script logic +if [ "$#" -ne 3 ]; then + echo "Usage: $0 " + exit 1 +fi + +type="$1" # First argument: type of critical state (CPU or Memory) +log_file_path="$2" # Second argument: path to log file +critical_state="$3" # Third argument: critical state indicator ("true" or "false") + +# Validate the critical state type +if [ "$type" != "CPU" ] && [ "$type" != "Memory" ]; then + echo "Invalid type: $type. Must be 'CPU' or 'Memory'." + exit 1 +fi + +# Validate the log file path before proceeding +if [ ! -f "$log_file_path" ]; then + echo "Error: File $log_file_path does not exist" + exit 1 +fi + +# Generate the log +generate_critical_log "$log_file_path" "$type" "$critical_state" + +# Exit with success code +exit 0