Skip to content

Instantly share code, notes, and snippets.

@Thecarisma
Last active December 12, 2022 15:31
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thecarisma/7f611cbca629c6004295b7c19ee1d4e0 to your computer and use it in GitHub Desktop.
Save Thecarisma/7f611cbca629c6004295b7c19ee1d4e0 to your computer and use it in GitHub Desktop.
A batch script to monitor a kubernetes pod status, it shows windows toast notification when the pod status changes. Change the NAME to your pod name.
@echo off
setlocal enabledelayedexpansion
REM Listen for kubenetes pod status changes,
REM change the value of NAME to your pod name or unique
REM part of the name, if more than one pod has the unique
REM part there will be conflict in the log
REM or you can set the pod name from commandline
REM
REM $ monitorpod podname apodnamespace anotherpodname apodpartname
REM
REM The sample command above monitors four pods
SET NAME=apodnamespace anotherpodname apodpartname
if not "%1"=="" (
SET NAME=%*
)
SET OLD_STATUSES=
for %%a in (!NAME!) do (
if "!OLD_STATUSES!"=="" (
SET OLD_STATUSES=%%a
) else (
SET OLD_STATUSES=!OLD_STATUSES! %%a
)
)
SET CURRENT_STATUS=
SET NOTIFICATOR_PATH=!TMP!\notificator.ps1
call:write_powershell_
call:notify "monitorpod has start monitoring the pod(s) !NAME!"
call:check_pod
exit /b 0
:check_pod
for /L %%n in (1,0,10) do (
for /F "tokens=* USEBACKQ" %%F in (`kubectl get pods ^| findstr "!NAME!"`) do (
SET ___TEMP_VAR=%%F
SET POD_NAME=
SET START_COUNT=
SET CURRENT_STATUS=
SET RESTART_COUNT=
SET SINCE=
SET INDEX=1
for %%a in (!___TEMP_VAR!) do (
if "!INDEX!"=="1" (
SET POD_NAME=%%a
)
if "!INDEX!"=="2" (
SET START_COUNT=%%a
)
if "!INDEX!"=="3" (
SET CURRENT_STATUS=%%a
)
if "!INDEX!"=="4" (
SET RESTART_COUNT=%%a
)
if "!INDEX!"=="5" (
SET SINCE=%%a
)
SET /a INDEX=!INDEX! + 1
)
REM echo Name=!POD_NAME!
REM echo Start Count=!START_COUNT!
REM echo Status=!CURRENT_STATUS!
REM echo Restart Count=!RESTART_COUNT!
REM echo Date=!SINCE!
SET $$$$$=
for %%b in (!NAME!) do (
echo.!POD_NAME! | findstr /C:"%%b" 1>nul
if not errorlevel 1 (
for %%a in (!OLD_STATUSES!) do (
echo.%%a | findstr /C:"%%b" 1>nul
if not errorlevel 1 (
echo.%%a | findstr /C:"!CURRENT_STATUS!" 1>nul
if errorlevel 1 (
SET $$$$$=%%b
SET DATE_TIME=!date! !time!
call:notify "The pod !POD_NAME! status changed to !CURRENT_STATUS! at !DATE_TIME!"
)
)
)
)
)
if not "!$$$$$!"=="" (
SET TMP_OLD_STATUSES=
for %%c in (!OLD_STATUSES!) do (
echo.%%c | findstr /C:"!$$$$$!" 1>nul
if not errorlevel 1 (
if "!TMP_OLD_STATUSES!"=="" (
SET TMP_OLD_STATUSES=!$$$$$!~!CURRENT_STATUS!
) else (
SET TMP_OLD_STATUSES=!TMP_OLD_STATUSES! !$$$$$!~!CURRENT_STATUS!
)
) else (
if "!TMP_OLD_STATUSES!"=="" (
SET TMP_OLD_STATUSES=%%c
) else (
SET TMP_OLD_STATUSES=!TMP_OLD_STATUSES! %%c
)
)
)
SET OLD_STATUSES=!TMP_OLD_STATUSES!
SET CURRENT_STATUS=
)
)
timeout /t 10 > nul 2>&1
)
exit /b 0
:notify
echo %1
powershell -noprofile -executionpolicy bypass -file !NOTIFICATOR_PATH! "monitorpod: Attention^!" %* Info
exit /b 0
:write_powershell_
echo wrote notification script to !NOTIFICATOR_PATH!
echo Param([string]$Title,[string]$Message,[string]$MessageType)> !NOTIFICATOR_PATH!
echo Add-Type -AssemblyName System.Windows.Forms>> !NOTIFICATOR_PATH!
echo $global:balloon = New-Object System.Windows.Forms.NotifyIcon>> !NOTIFICATOR_PATH!
echo Get-Member -InputObject $Global:balloon 2^>^&1 ^| Out-Null>> !NOTIFICATOR_PATH!
echo [void](Register-ObjectEvent -InputObject $balloon -EventName MouseDoubleClick -SourceIdentifier IconClicked -Action {>> !NOTIFICATOR_PATH!
echo $global:balloon.dispose()>> !NOTIFICATOR_PATH!
echo })>> !NOTIFICATOR_PATH!
echo $path = (Get-Process -id $pid).Path>> !NOTIFICATOR_PATH!
echo $balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)>> !NOTIFICATOR_PATH!
echo [System.Windows.Forms.ToolTipIcon] ^| Get-Member -Static -Type Property 2^>^&1 ^| Out-Null>> !NOTIFICATOR_PATH!
echo $balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning>> !NOTIFICATOR_PATH!
echo $balloon.BalloonTipText = $Message>> !NOTIFICATOR_PATH!
echo $balloon.BalloonTipTitle = $Title>> !NOTIFICATOR_PATH!
echo $balloon.Visible = $true>> !NOTIFICATOR_PATH!
echo $balloon.ShowBalloonTip(5000)>> !NOTIFICATOR_PATH!
exit /b 0
:stop
call :__stop 2>nul
:__stop
() creates a syntax error, quits the batch
@Thecarisma
Copy link
Author

Thecarisma commented Feb 29, 2020

Usage

if you have two pod

service-one-5c46bbc9d6-fvz7f       1/1     Running             0          37m
service-two-5667699cd5-8p7bz       1/1     Running             0          2d6h

to monitor the two pods above execute

monitorpod service-one service-two

Or you can open the monitorpod.bat file and set the NAME value to your pods unique name so you can just double click the script to execute instead of sending the pod names as arguments.

SET NAME=service-one service-two

The pod status will be written to the console and display in windows toast notification

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment