Agent の起動監視および自動起動

Agent が予期せず停止した際に自動的に起動する仕組みを、以下のサンプルスクリプトを用いて作成できます。

サンプルスクリプトをスケジューラに登録して一定間隔で実行することで、Agent の起動監視および自動起動を行うことが可能です。

サンプルスクリプトは、Agent の起動方法に応じたものを選択してください。

注意

サンプルスクリプトの内容については、改善のため予告なしに変更される事があります。

また、サンプルスクリプトはサポートサービス対象外となりますのでご注意ください。

@echo off
setlocal enabledelayedexpansion

REM --------------------------------------- 
REM  設定 
REM --------------------------------------- 
REM Agentの導入ディレクトリ 
set "AGENT_PATH=C:\webconnect-agent\bin"
REM サービス名 
set SERVICE_NAME=webconnect-agent
REM ステータスチェックのリトライ最大回数 
set /a MAX_RETRY=10 

REM --------------------------------------- 
REM  初期処理 
REM --------------------------------------- 
set "CURRENT_DIR=%~dp0"
cd /d "%AGENT_PATH%"

REM check directory 
if %ERRORLEVEL% neq 0 (
  echo Cannot cd to !AGENT_PATH! 
  goto :ERROR_END
)

set DO_NET_STOP=0 

REM リトライのカウンター 
set /a COUNTER=1 

REM --------------------------------------- 
REM  配信Agentのステータス確認 
REM --------------------------------------- 
:CHECK_SENDING-AGENT_STATUS
echo CHECK_SENDING-AGENT_STATUS
FOR /f "DELIMS=" %%i IN ('agentctl.bat -s status') DO SET SENDING-AGENT_STATUS=%%i
echo RESULT: %SENDING-AGENT_STATUS% 

echo %SENDING-AGENT_STATUS% | findstr "001" 1>nul 
if %ERRORLEVEL%==0 (
  echo Waiting for starting the sending-side Agent.
  call :SLEEP
  goto :CHECK_SENDING-AGENT_STATUS
)

echo %SENDING-AGENT_STATUS% | findstr "000" 1>nul 
if %ERRORLEVEL%==0 (
  echo Starting the sending-side Agent.
  set DO_NET_STOP=1 
  goto :START_SERVICE
)

echo %SENDING-AGENT_STATUS% | findstr "100" 1>nul 
if %ERRORLEVEL%==0 (
  echo Starting the sending-side Agent.
  set DO_NET_STOP=1 
  goto :START_SERVICE
)

echo %SENDING-AGENT_STATUS% | findstr "255" 1>nul 
if %ERRORLEVEL%==0 (
  echo Starting the sending-side Agent.
  set DO_NET_STOP=1 
  goto :START_SERVICE
)

REM --------------------------------------- 
REM  集信Agentのステータス確認 
REM --------------------------------------- 
:CHECK_RECEIVING-AGENT_STATUS
echo CHECK_RECEIVING-AGENT_STATUS
FOR /f "DELIMS=" %%i IN ('agentctl.bat -r status') DO SET RECEIVING-AGENT_STATUS=%%i 
echo RESULT: %RECEIVING-AGENT_STATUS% 

echo %RECEIVING-AGENT_STATUS% | findstr "001" 1>nul 
if %ERRORLEVEL%==0 (
  echo Waiting for starting the receiving-side Agent.
  call :SLEEP
  goto :CHECK_RECEIVING-AGENT_STATUS
)

echo %RECEIVING-AGENT_STATUS% | findstr "000" 1>nul 
if %ERRORLEVEL%==0 (
  echo Starting the receiving-side Agent.
  set DO_NET_STOP=1 
  goto :START_SERVICE
)

echo %RECEIVING-AGENT_STATUS% | findstr "100" 1>nul 
if %ERRORLEVEL%==0 (
  echo Starting the receiving-side Agent.
  set DO_NET_STOP=1 
  goto :START_SERVICE
)

echo %RECEIVING-AGENT_STATUS% | findstr "255" 1>nul 
if %ERRORLEVEL%==0 (
  echo Starting the receiving-side Agent.
  set DO_NET_STOP=1 
  goto :START_SERVICE
)

goto :END

:START_SERVICE

if !COUNTER! GTR !MAX_RETRY! (
  goto :ERROR_END
)

set /a COUNTER+=1 
sc query %SERVICE_NAME% | findstr STATE | findstr STOPPED > nul 
if !ERRORLEVEL! neq 0 (
  if %DO_NET_STOP%==1 (
    call net stop "!SERVICE_NAME!"
    call :SLEEP
    call :WAIT_TO_STOP_BOTH_AGENTS
    goto :START_SERVICE
  )
)

call net start "%SERVICE_NAME%"
call :SLEEP
goto :CHECK_SENDING-AGENT_STATUS


:SLEEP
ping -n 10 localhost > nul 
exit /B 0 

:WAIT_TO_STOP_BOTH_AGENTS
FOR /f "DELIMS=" %%i IN ('agentctl.bat -r status') DO SET RECEIVING-AGENT_STATUS=%%i 
echo %RECEIVING-AGENT_STATUS% | findstr "100" 1>nul 
if %ERRORLEVEL% neq 0 (
  call :SLEEP
  goto :WAIT_TO_STOP_BOTH_AGENTS
)
exit /B 0 

:END
cd /d %CURRENT_DIR% 
exit /B

:ERROR_END
echo Failed to start Agent.
exit /b -1 

@echo off
setlocal enabledelayedexpansion

REM --------------------------------------- 
REM  設定 
REM --------------------------------------- 
REM Agentの導入ディレクトリ 
set "AGENT_PATH=C:\webconnect-agent\bin"
REM ステータスチェックのリトライ最大回数 
set /a MAX_RETRY=10 

REM --------------------------------------- 
REM  初期処理 
REM --------------------------------------- 
set "CURRENT_DIR=%~dp0"
cd /d "%AGENT_PATH%"

REM check directory 
if %ERRORLEVEL% neq 0 (
  echo Cannot cd to !AGENT_PATH! 
  goto :ERROR_END
)

REM --------------------------------------- 
REM  配信Agentのステータス確認 
REM --------------------------------------- 
set /a COUNTER=1
:CHECK_SENDING-AGENT_STATUS
echo CHECK_SENDING-AGENT_STATUS

FOR /f "DELIMS=" %%i IN ('agentctl.bat -s status') DO SET SENDING-AGENT_STATUS=%%i 
echo RESULT: %SENDING-AGENT_STATUS% 

echo %SENDING-AGENT_STATUS% | findstr "000" 1>nul 
if %ERRORLEVEL%==0 (
  echo Starting the sending-side Agent.
  goto :START_SENDING-AGENT
)

echo %SENDING-AGENT_STATUS% | findstr "100" 1>nul 
if %ERRORLEVEL%==0 (
  echo Starting the sending-side Agent.
  goto :START_SENDING-AGENT
)

echo %SENDING-AGENT_STATUS% | findstr "255" 1>nul 
if %ERRORLEVEL%==0 (
  echo Starting the sending-side Agent.
  goto :START_SENDING-AGENT
)

goto :CHECK_RECEIVING-AGENT_STATUS


:START_SENDING-AGENT
call agentctl.bat -s start 
call :SLEEP
if !COUNTER! LSS !MAX_RETRY! (
  set /a COUNTER+=1 
  goto :CHECK_SENDING-AGENT_STATUS
) else (
  goto :ERROR_END
)


REM --------------------------------------- 
REM  集信Agentのステータス確認 
REM --------------------------------------- 
set /a COUNTER=1
:CHECK_RECEIVING-AGENT_STATUS
echo CHECK_RECEIVING-AGENT_STATUS

FOR /f "DELIMS=" %%i IN ('agentctl.bat -r status') DO SET RECEIVING-AGENT_STATUS=%%i 
echo RESULT: %RECEIVING-AGENT_STATUS% 

echo %RECEIVING-AGENT_STATUS% | findstr "000" 1>nul 
if %ERRORLEVEL%==0 (
  echo Starting the receiving-side Agent.
  goto :START_RECEIVING-AGENT
)

echo %RECEIVING-AGENT_STATUS% | findstr "100" 1>nul 
if %ERRORLEVEL%==0  (
  echo Starting the receiving-side Agent.
  goto :START_RECEIVING-AGENT
)

echo %RECEIVING-AGENT_STATUS% | findstr "255" 1>nul 
if %ERRORLEVEL%==0 (
  echo Starting the receiving-side Agent.
  goto :START_RECEIVING-AGENT
)

goto :NORMAL_END

:START_RECEIVING-AGENT
call agentctl.bat -r start 
call :SLEEP
if !COUNTER! LSS !MAX_RETRY! (
  set /a COUNTER+=1
  goto CHECK_RECEIVING-AGENT_STATUS
) else (
  goto :ERROR_END
)

:NORMAL_END
cd /d %CURRENT_DIR% 
exit /B

:ERROR_END
echo Failed to start Agent.
exit /b -1

:SLEEP
ping -n 10 localhost > nul 
exit /B 0

#!/bin/bash 

# --------------------------------------- 
#  設定 
# --------------------------------------- 
# Agentの導入ディレクトリ 
AGENT_PATH=~/webconnect-agent/bin
# ステータスチェックのリトライ最大回数
MAX_RETRY=10
# 待機時間
SLEEP_SEC=10

CURRENT_DIR=`pwd`
# --------------------------------------- 
#  functions 
# --------------------------------------- 
function error_end () {
  cd $CURRENT_DIR 
  echo Failed to start Agent.
}

# --------------------------------------- 
#  初期処理 
# --------------------------------------- 
# check directory 
if [ ! -e $AGENT_PATH ]; then 
  echo Cannot cd to $AGENT_PATH 
  error_end
  exit 1
fi 

cd $AGENT_PATH 
# --------------------------------------- 
#  配信Agentのステータス確認 
# --------------------------------------- 
COUNTER=1

function checkSendingAgentStatus () {
  echo checkSendingAgentStatus

  sendingAgentStatus=`./agentctl -s status`
  echo RESULT: $sendingAgentStatus 

  echo $sendingAgentStatus | grep -e 000 -e 100 -e 255 > /dev/null
  if [ $? -eq 0 ]; then 
    echo Starting the sending-side Agent.
    return 1
  fi 
  return 0
}

checkSendingAgentStatus
if [ $? -ne 0 ]; then 
  while [ $COUNTER -lt $MAX_RETRY ]
  do
    ./agentctl -s start
    sleep $SLEEP_SEC
    checkSendingAgentStatus
    if [ $? -eq 0 ]; then 
      break 
    fi 
    COUNTER=$((COUNTER + 1))
  done 

  if [ $COUNTER -ge $MAX_RETRY ]; then
    error_end
    exit 1
  fi 
fi 

# --------------------------------------- 
#  集信Agentのステータス確認 
# --------------------------------------- 
COUNTER=1

function checkReceivingAgentStatus () {
  echo checkReceivingAgentStatus

  receivingAgentStatus=`./agentctl -r status`
  echo RESULT: $receivingAgentStatus 

  echo $receivingAgentStatus | grep -e 000 -e 100 -e 255 > /dev/null
  if [ $? -eq 0 ]; then 
    echo Starting the receiving-side Agent.
    return 1
  fi 
  return 0
}

checkReceivingAgentStatus
if [ $? -ne 0 ]; then 
  while [ $COUNTER -lt $MAX_RETRY ]
  do 
    ./agentctl -r start
    sleep $SLEEP_SEC
    checkReceivingAgentStatus
    if [ $? -eq 0 ]; then 
      break 
    fi 
    COUNTER=$((COUNTER + 1))
  done 
  
  if [ $COUNTER -ge $MAX_RETRY ]; then
    error_end
    exit 1
  fi 
fi 

cd $CURRENT_DIR