agentctl 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #!/bin/bash
  2. #== 脚本执行中遇到不存在的变量就报错
  3. set -o nounset
  4. #== 脚本执行中报错即刻退出
  5. set -o errexit
  6. #== 脚本执行只要一个子命令失败,整个管道命令就失败
  7. set -o pipefail
  8. readonly BRAND_FORMAL_NAME="Cloudwise"
  9. readonly BRAND_PRODUCT_NAME="euspace"
  10. readonly BRAND_PARENT_PRODUCT_NAME="omniagent"
  11. readonly BRAND_AGENT_PRODUCT_NAME="${BRAND_FORMAL_NAME} ${BRAND_PRODUCT_NAME}"
  12. readonly BRAND_FORMAL_NAME_LOWER="cloudwise"
  13. readonly BRAND_PRODUCT_NAME_LOWER="euspace"
  14. #== **********************************************************
  15. #== 配置目录
  16. #== **********************************************************
  17. readonly INSTALL_BASE=/opt
  18. #== /opt/cloudwise/omniagent/agents 目录
  19. readonly BASE_INSTALL_DIR=${INSTALL_BASE}/${BRAND_FORMAL_NAME_LOWER}/${BRAND_PARENT_PRODUCT_NAME}/agents
  20. #== cloudwise/omniagent/agents/agent
  21. readonly INSTALL_FOLDER=${BRAND_FORMAL_NAME_LOWER}/${BRAND_PARENT_PRODUCT_NAME}/agents/${BRAND_PRODUCT_NAME_LOWER}
  22. #== cloudwise/omniagent/agents/agent/version
  23. readonly INSTALL_VERSION_FOLDER=${INSTALL_FOLDER}/current
  24. #== /opt/cloudwise/omniagent/agents/agent/version
  25. readonly INSTALL_DIR=${INSTALL_BASE}/${INSTALL_VERSION_FOLDER}
  26. #== /opt/cloudwise/omniagent/agents/agent
  27. readonly AGENT_BASE_DIR=${INSTALL_BASE}/${INSTALL_FOLDER}
  28. readonly AGENT_BIN_DIR="${INSTALL_DIR}/bin"
  29. readonly AGENT_LOG_DIR="${INSTALL_DIR}/logs"
  30. #readonly AGENT_CONF_DIR="${INSTALL_DIR}/conf"
  31. #readonly MAIN_CONF_FILE="${AGENT_CONF_DIR}/${AGENT_MAIN_CONF}"
  32. readonly EXIT_CODE_OK=0
  33. readonly EXIT_CODE_GENERIC_ERROR=1
  34. #== **********************************************************
  35. #== 配置重要文件名称
  36. #== **********************************************************
  37. readonly AGENT_PROC="euspace"
  38. readonly AGENT_CONFIG_PATH=""
  39. readonly AGENT_PID_FILE="${INSTALL_DIR}/bin/${AGENT_PROC}.pid"
  40. getDaemonPid(){
  41. local agent="${1}"
  42. #== 约定pid文件
  43. local pidFilePath="${AGENT_PID_FILE}"
  44. #== 二进制文件绝对路径
  45. # shellcheck disable=SC2155
  46. local procPath="$(readlink -m "${AGENT_BIN_DIR}/${agent}" 2>&1)"
  47. local cmdline
  48. local pid
  49. #== 查询pid文件
  50. if [ -s "${pidFilePath}" ] ; then
  51. pid="$(cat "${pidFilePath}" 2>&1)"
  52. cmdline="$(cat /proc/${pid}/cmdline 2>&1 | xargs -0 echo -n)"
  53. if [ -d "/proc/${pid}" ] && ( [[ ${cmdline} =~ ${procPath} ]] || [[ ${cmdline} =~ ${AGENT_BIN_DIR}/${agent} ]] ); then
  54. # toLogInfo "[${agent}] Find pid by ${pidFilePath} [content:${pid}][cmdline:${cmdline}]"
  55. printf '%s' ${pid}
  56. return 0
  57. else
  58. #== 进程绝对路径查询
  59. # toLogWarning "[${agent}] Pidfile exist [content:${pid}]. But process not find or not myself. [cmdline:${cmdline}]"
  60. if ! pid="$(listProcesses "${agent} pid" "${procPath}|${AGENT_BIN_DIR}/${agent}" "sudo|tail")"; then
  61. # toLogWarning "[${agent}] Not find process grep '${procPath}'"
  62. return 1
  63. fi
  64. cmdline="$(cat /proc/${pid}/cmdline 2>&1 | xargs -0 echo -n)"
  65. if ! [[ ${cmdline} =~ ${procPath} || ${cmdline} =~ ${AGENT_BIN_DIR}/${agent} ]]; then
  66. # toLogWarning "[${agent}] Find process success. But process not myself. [cmdline:${cmdline}]"
  67. return 1
  68. fi
  69. # toLogInfo "[${agent}] Find pid by process name [${procPath}|${AGENT_BIN_DIR}/${agent}]"
  70. fi
  71. else
  72. #== 进程绝对路径查询
  73. # toLogWarning "[${agent}] Pidfile [${pidFilePath}] not find. Use abs path queries."
  74. if ! pid="$(listProcesses "${agent} pid" "${procPath}|${AGENT_BIN_DIR}/${agent}" "sudo|tail")"; then
  75. # toLogWarning "[${agent}] Not find process by grep '${procPath}'"
  76. return 1
  77. fi
  78. cmdline="$(cat /proc/${pid}/cmdline 2>&1 | xargs -0 echo -n)"
  79. if ! [[ ${cmdline} =~ ${procPath} || ${cmdline} =~ ${AGENT_BIN_DIR}/${agent} ]]; then
  80. # toLogWarning "[${agent}] Find process success. But process not myself. [cmdline:${cmdline}]"
  81. return 1
  82. fi
  83. # toLogInfo "[${agent}] Find pid by process name [${procPath}|${AGENT_BIN_DIR}/${agent}]."
  84. fi
  85. printf '%s' ${pid}
  86. return 0
  87. }
  88. listProcesses() {
  89. local errorMessage="${1}"
  90. local includeRegex="${2}"
  91. local excludeRegex=" grep"
  92. if [ "${3}" ]; then
  93. excludeRegex="grep|${3}"
  94. fi
  95. # toLogInfo "#DEBUG== listProcesses command: ps -e -o \"pid,args\" 2>&1 | grep -E "${includeRegex}" | awk '{{ print \$1,\$2 }}' | grep -vE \"${excludeRegex}\""
  96. local output
  97. if ! output="$(ps -e -o "pid,args" 2>&1 | grep -E "${includeRegex}" | awk '{{ print $1,$2 }}')"; then
  98. # toLogError "Failed to get ${errorMessage}, output: ${output}"
  99. return 1
  100. fi
  101. local foundProcesses="$(printf '%s' "${output}" | grep -vE "${excludeRegex}")"
  102. if [ ! "${foundProcesses}" ]; then
  103. return 1
  104. fi
  105. printf '%s' "${foundProcesses}" | awk '{{ print $1 }}'
  106. return 0
  107. }
  108. getAgentPid() {
  109. local DAEMONPID="$(getDaemonPid "${AGENT_PROC}")"
  110. if [ -z ${DAEMONPID} ]; then
  111. return 1
  112. fi
  113. printf '%s' "${DAEMONPID}"
  114. }
  115. startAgent() {
  116. local pid
  117. if [ -f "${AGENT_BIN_DIR}/${AGENT_PROC}" ]; then
  118. export DISABLE_E2E_TRACING=false
  119. export DISABLE_STACK_TRACING=true
  120. export SEND=1
  121. local params="--listen=0.0.0.0:8123"
  122. # if [ -f "/etc/chaosd/pki/ca.crt" ] && [ -f "/etc/chaosd/pki/chaosd.crt" ] && [ -f "/etc/chaosd/pki/chaosd.key" ]; then
  123. # params="server --https-port 31768 --CA=/etc/chaosd/pki/ca.crt --cert=/etc/chaosd/pki/chaosd.crt --key=/etc/chaosd/pki/chaosd.key"
  124. # fi
  125. pid=$(nohup ${AGENT_BIN_DIR}/${AGENT_PROC} ${params}>>"${AGENT_LOG_DIR}/service.log" 2>&1 & echo $!)
  126. echo ${pid} > "${AGENT_PID_FILE}"
  127. else
  128. echo "${AGENT_BIN_DIR}/${AGENT_PROC} not exist."
  129. exit "${EXIT_CODE_GENERIC_ERROR}"
  130. fi
  131. }
  132. agentStart() {
  133. local agentPid
  134. if agentPid="$(getAgentPid)"; then
  135. echo "${BRAND_AGENT_PRODUCT_NAME} is running. Agent pid: ${agentPid}."
  136. return
  137. fi
  138. startAgent
  139. local exitCode=$?
  140. if [ "${exitCode}" -ne 0 ]; then
  141. echo "Failed to start ${BRAND_AGENT_PRODUCT_NAME} service."
  142. exit "${EXIT_CODE_GENERIC_ERROR}"
  143. fi
  144. if agentPid="$(getAgentPid)"; then
  145. echo "${BRAND_AGENT_PRODUCT_NAME} service started. Agent pid: ${agentPid}."
  146. else
  147. echo "${BRAND_AGENT_PRODUCT_NAME} not start!"
  148. exit "${EXIT_CODE_GENERIC_ERROR}"
  149. fi
  150. }
  151. agentStop() {
  152. local agentPid
  153. if agentPid="$(getAgentPid)"; then
  154. # ${AGENT_BIN_DIR}/stop.sh ${agentPid}
  155. kill ${agentPid}
  156. echo "${BRAND_AGENT_PRODUCT_NAME} service stopped success."
  157. cat /dev/null > "${AGENT_PID_FILE}"
  158. else
  159. echo "${BRAND_AGENT_PRODUCT_NAME} is stopped. service stopped success."
  160. fi
  161. }
  162. agentRestart(){
  163. agentStop
  164. sleep 1
  165. agentStart
  166. }
  167. agentStatus() {
  168. local agentPid
  169. local statusOutput
  170. if agentPid="$(getAgentPid)"; then
  171. statusOutput="${BRAND_AGENT_PRODUCT_NAME} is running. ${BRAND_AGENT_PRODUCT_NAME} pid: ${agentPid}"
  172. else
  173. statusOutput="${BRAND_AGENT_PRODUCT_NAME} is not running. ${BRAND_AGENT_PRODUCT_NAME} process not found or stop."
  174. fi
  175. echo "${statusOutput}"
  176. }
  177. info() {
  178. local agentPid="$(getAgentPid)"
  179. local version=$(cat "${INSTALL_DIR}/installer.version" | tr -d '\n')
  180. printf '{"pid":%d,"version":"%s","agent_id":"%s","config":"%s","pipe": true}' "${agentPid}" "${version}" "${BRAND_PRODUCT_NAME_LOWER}" "${AGENT_CONFIG_PATH}"
  181. }
  182. ################################################################################
  183. #
  184. # Script start
  185. #
  186. ################################################################################
  187. main() {
  188. case "$1" in
  189. start)
  190. agentStart
  191. ;;
  192. stop)
  193. agentStop
  194. ;;
  195. restart)
  196. agentRestart
  197. ;;
  198. status)
  199. agentStatus
  200. ;;
  201. info)
  202. info
  203. ;;
  204. *)
  205. toConsoleInfo "usage: $0 {start|stop|restart|status}"
  206. ;;
  207. esac
  208. exit "${EXIT_CODE_OK}"
  209. }
  210. main "$@"