diff --git a/scripts/check-all-by-signal.sh b/scripts/check-all-by-signal.sh index db03e60fa..8149392e4 100644 --- a/scripts/check-all-by-signal.sh +++ b/scripts/check-all-by-signal.sh @@ -42,10 +42,10 @@ trap handle_error ERR openim::util::check_ports_by_signal ${OPENIM_SERVER_PORT_LISTARIES[@]} if [[ $? -ne 0 ]]; then - echo "++++ All openim service ports stop successfully !" + openim::log::success "++++ All openim service ports stop successfully !" else echo "+++ cat openim log file >>> ${LOG_FILE}" - openim::log::error_exit "The service does not stop properly, there are still processes running, please check!" + openim::log::error "The service does not stop properly, there are still processes running, please check!" fi diff --git a/scripts/check-all-by-signal1.sh b/scripts/check-all-by-signal1.sh new file mode 100644 index 000000000..7db4e22ff --- /dev/null +++ b/scripts/check-all-by-signal1.sh @@ -0,0 +1,106 @@ +#!/usr/bin/env bash +# Copyright © 2023 OpenIM. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This script is check openim service is running normally +# +# Usage: `scripts/check-all.sh`. +# Encapsulated as: `make check`. +# READ: https://github.com/openimsdk/open-im-server/tree/main/scripts/install/environment.sh + +set -o errexit +set -o nounset +set -o pipefail + +OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. +source "${OPENIM_ROOT}/scripts/install/common.sh" + +OPENIM_VERBOSE=4 + +openim::log::info "\n# Begin to check all openim service" + +openim::log::status "Check all dependent service ports" +# Elegant printing function +# Elegant printing function +print_services_and_ports() { + local service_names=("$@") + local half_length=$((${#service_names[@]} / 2)) + local service_ports=("${service_names[@]:half_length}") + + echo "+-------------------------+----------+" + echo "| Service Name | Port |" + echo "+-------------------------+----------+" + + for ((index=0; index < half_length; index++)); do + printf "| %-23s | %-8s |\n" "${service_names[$index]}" "${service_ports[$index]}" + done + + echo "+-------------------------+----------+" +} + +handle_error() { + echo "An error occurred. Printing ${STDERR_LOG_FILE} contents:" + cat "${STDERR_LOG_FILE}" + exit 1 +} + +trap handle_error ERR + +# Assuming OPENIM_SERVER_NAME_TARGETS and OPENIM_SERVER_PORT_TARGETS are defined +# Similarly for OPENIM_DEPENDENCY_TARGETS and OPENIM_DEPENDENCY_PORT_TARGETS + +# Print out services and their ports +print_services_and_ports "${OPENIM_SERVER_NAME_TARGETS[@]}" "${OPENIM_SERVER_PORT_TARGETS[@]}" + +# Print out dependencies and their ports +print_services_and_ports "${OPENIM_DEPENDENCY_TARGETS[@]}" "${OPENIM_DEPENDENCY_PORT_TARGETS[@]}" + +# OpenIM check +echo "++ The port being checked: ${OPENIM_SERVER_PORT_LISTARIES[@]}" +openim::log::info "\n## Check all dependent service ports" +echo "++ The port being checked: ${OPENIM_DEPENDENCY_PORT_LISTARIES[@]}" + +set +e + +# Later, after discarding Docker, the Docker keyword is unreliable, and Kubepods is used +if grep -qE 'docker|kubepods' /proc/1/cgroup || [ -f /.dockerenv ]; then + openim::color::echo ${COLOR_CYAN} "Environment in the interior of the container" +else + openim::color::echo ${COLOR_CYAN} "The environment is outside the container" + openim::util::check_ports ${OPENIM_DEPENDENCY_PORT_LISTARIES[@]} || return 0 +fi + +if [[ $? -ne 0 ]]; then + openim::log::error_exit "The service does not start properly, please check the port, query variable definition!" + echo "+++ https://github.com/openimsdk/open-im-server/tree/main/scripts/install/environment.sh +++" +else + echo "++++ Check all dependent service ports successfully !" +fi + +openim::log::info "\n## Check OpenIM service name" +. $(dirname ${BASH_SOURCE})/install/openim-msgtransfer.sh openim::msgtransfer::check_by_signal1 + +openim::log::info "\n## Check all OpenIM service ports" +echo "+++ The port being checked: ${OPENIM_SERVER_PORT_LISTARIES[@]}" +openim::util::check_ports_by_signal1 ${OPENIM_SERVER_PORT_LISTARIES[@]} +if [[ $? -ne 0 ]]; then + echo "++++ All openim service ports stop successfully !" +else + echo "+++ cat openim log file >>> ${LOG_FILE}" + openim::log::error_exit "The service does not stop properly, please check!" +fi + +set -e + +trap - ERR \ No newline at end of file diff --git a/scripts/install/openim-msgtransfer.sh b/scripts/install/openim-msgtransfer.sh index 09f054da5..584d9624e 100755 --- a/scripts/install/openim-msgtransfer.sh +++ b/scripts/install/openim-msgtransfer.sh @@ -109,6 +109,29 @@ function openim::msgtransfer::check_by_signal() { fi } +function openim::msgtransfer::check_by_signal1() { + PIDS=$(pgrep -f "${OPENIM_OUTPUT_HOSTBIN}/openim-msgtransfer") + + NUM_PROCESSES=$(echo "$PIDS" | wc -l) + + if [ "$NUM_PROCESSES" -eq "$OPENIM_MSGGATEWAY_NUM" ]; then + openim::log::info "Found $OPENIM_MSGGATEWAY_NUM processes named $OPENIM_OUTPUT_HOSTBIN" + for PID in $PIDS; do + if [[ "$OSTYPE" == "linux-gnu"* ]]; then + ps -p $PID -o pid,cmd + elif [[ "$OSTYPE" == "darwin"* ]]; then + ps -p $PID -o pid,comm + else + openim::log::error "Unsupported OS type: $OSTYPE" + fi + done + openim::log::error "Processes have not been stopped properly." + else +# openim::log::error_exit "Expected $OPENIM_MSGGATEWAY_NUM openim msgtransfer processes, but found $NUM_PROCESSES msgtransfer processes." + openim::log::success "All openim-msgtransfer processes have been stopped properly." + fi +} + ###################################### Linux Systemd ###################################### SYSTEM_FILE_PATH="/etc/systemd/system/${SERVER_NAME}.service" diff --git a/scripts/lib/util.sh b/scripts/lib/util.sh index 74d8d977c..8e8db94c3 100755 --- a/scripts/lib/util.sh +++ b/scripts/lib/util.sh @@ -451,6 +451,86 @@ openim::util::check_ports_by_signal() { fi } +openim::util::check_ports_by_signal1() { + # An array to collect ports of processes that are not running. + local not_started=() + + # An array to collect information about processes that are running. + local started=() + + openim::log::info "Checking ports: $*" + # Iterate over each given port. + for port in "$@"; do + # Initialize variables + # Check the OS and use the appropriate command + if [[ "$OSTYPE" == "linux-gnu"* ]]; then + if command -v ss > /dev/null 2>&1; then + info=$(ss -ltnp | grep ":$port" || true) + else + info=$(netstat -ltnp | grep ":$port" || true) + fi + elif [[ "$OSTYPE" == "darwin"* ]]; then + # For macOS, use lsof + info=$(lsof -P -i:"$port" | grep "LISTEN" || true) + fi + + # Check if any process is using the port + if [[ -z $info ]]; then + not_started+=($port) + else + if [[ "$OSTYPE" == "linux-gnu"* ]]; then + # Extract relevant details for Linux: Process Name, PID, and FD. + details=$(echo $info | sed -n 's/.*users:(("\([^"]*\)",pid=\([^,]*\),fd=\([^)]*\))).*/\1 \2 \3/p') + command=$(echo $details | awk '{print $1}') + pid=$(echo $details | awk '{print $2}') + fd=$(echo $details | awk '{print $3}') + elif [[ "$OSTYPE" == "darwin"* ]]; then + # Handle extraction for macOS + pid=$(echo $info | awk '{print $2}' | cut -d'/' -f1) + command=$(ps -p $pid -o comm= | xargs basename) + fd=$(echo $info | awk '{print $4}' | cut -d'/' -f1) + fi + + # Get the start time of the process using the PID + if [[ -z $pid ]]; then + start_time="N/A" + else + start_time=$(ps -p $pid -o lstart=) + fi + + started+=("Port $port - Command: $command, PID: $pid, FD: $fd, Started: $start_time") + fi + done + + # Print information about ports whose processes are not running. + if [[ ${#not_started[@]} -ne 0 ]]; then + openim::log::info "\n### Not started ports:" + for port in "${not_started[@]}"; do + openim::log::error "Port $port is not started." + done + fi + + # Print information about ports whose processes are running. + if [[ ${#started[@]} -ne 0 ]]; then + openim::log::info "\n### Started ports:" + for info in "${started[@]}"; do + openim::log::info "$info" + done + fi + + # If any of the processes is not running, return a status of 1. + if [[ ${#not_started[@]} -ne 0 ]]; then + openim::log::success "All specified processes are stop." + return 1 + else + openim::color::echo $COLOR_RED " OpenIM Stdout Log >> cat ${LOG_FILE}" + openim::color::echo $COLOR_RED " OpenIM Stderr Log >> cat ${STDERR_LOG_FILE}" + cat "$TMP_LOG_FILE" | awk '{print "\033[31m" $0 "\033[0m"}' + openim::log::success "Have processes no stop." + return 0 + fi +} + # set +o errexit # Sample call for testing: