diff --git a/build/windows/Makefile.ps1 b/build/windows/Makefile.ps1 index b9bd1f3e4..9f3c438b0 100644 --- a/build/windows/Makefile.ps1 +++ b/build/windows/Makefile.ps1 @@ -3,6 +3,7 @@ # 1. Builds the certificate generator code in .NET and copy the binaries in zip file to ..\..\kubernetes\windows\omsagentwindows # 2. Builds the out_oms plugin code in go lang into the shared object(.so) file and copy the out_oms.so file to ..\..\kubernetes\windows\omsagentwindows # 3. copy the files under installer directory to ..\..\kubernetes\windows\omsagentwindows +# 4. Builds the livenessprobe cpp and copy the executable to the under directory ..\..\kubernetes\windows\omsagentwindows $dotnetcoreframework = "netcoreapp3.1" @@ -157,7 +158,7 @@ if ($isCDPxEnvironment) { Write-Host("getting latest go modules ...") go get - Write-Host("successfyullt got latest go modules") -ForegroundColor Green + Write-Host("successfully got latest go modules") -ForegroundColor Green go build -ldflags "-X 'main.revision=$buildVersionString' -X 'main.builddate=$buildVersionDate'" -buildmode=c-shared -o out_oms.so . } @@ -167,16 +168,28 @@ Write-Host("copying out_oms.so file to : $publishdir") Copy-Item -Path (Join-path -Path $outomsgoplugindir -ChildPath "out_oms.so") -Destination $publishdir -Force Write-Host("successfully copied out_oms.so file to : $publishdir") -ForegroundColor Green +# compile and build the liveness probe cpp code +Write-Host("Start:build livenessprobe cpp code") +$livenessprobesrcpath = Join-Path -Path $builddir -ChildPath "windows\installer\livenessprobe\livenessprobe.cpp" +$livenessprobeexepath = Join-Path -Path $builddir -ChildPath "windows\installer\livenessprobe\livenessprobe.exe" +g++ $livenessprobesrcpath -o $livenessprobeexepath -municode +Write-Host("End:build livenessprobe cpp code") +if (Test-Path -Path $livenessprobeexepath){ + Write-Host("livenessprobe.exe exists which indicates cpp build step succeeded") -ForegroundColor Green +} else { + Write-Host("livenessprobe.exe doesnt exist which indicates cpp build step failed") -ForegroundColor Red + exit +} $installerdir = Join-Path -Path $builddir -ChildPath "common\installer" Write-Host("copying common installer files conf and scripts from :" + $installerdir + " to :" + $publishdir + " ...") -$exclude = @('*.cs','*.csproj') +$exclude = @('*.cs','*.csproj', '*.cpp') Copy-Item -Path $installerdir -Destination $publishdir -Recurse -Force -Exclude $exclude Write-Host("successfully copied installer files conf and scripts from :" + $installerdir + " to :" + $publishdir + " ") -ForegroundColor Green $installerdir = Join-Path -Path $builddir -ChildPath "windows\installer" Write-Host("copying installer files conf and scripts from :" + $installerdir + " to :" + $publishdir + " ...") -$exclude = @('*.cs','*.csproj') +$exclude = @('*.cs','*.csproj', '*.cpp') Copy-Item -Path $installerdir -Destination $publishdir -Recurse -Force -Exclude $exclude Write-Host("successfully copied installer files conf and scripts from :" + $installerdir + " to :" + $publishdir + " ") -ForegroundColor Green diff --git a/build/windows/installer/livenessprobe/livenessprobe.cpp b/build/windows/installer/livenessprobe/livenessprobe.cpp new file mode 100644 index 000000000..eea792686 --- /dev/null +++ b/build/windows/installer/livenessprobe/livenessprobe.cpp @@ -0,0 +1,137 @@ +#ifndef UNICODE +#define UNICODE +#endif + +#ifndef _UNICODE +#define _UNICODE +#endif + +#include +#include +#include + +#define SUCCESS 0x00000000 +#define NO_FLUENT_BIT_PROCESS 0x00000001 +#define FILESYSTEM_WATCHER_FILE_EXISTS 0x00000002 +#define CERTIFICATE_RENEWAL_REQUIRED 0x00000003 +#define FLUENTDWINAKS_SERVICE_NOT_RUNNING 0x00000004 +#define UNEXPECTED_ERROR 0xFFFFFFFF + +/* + check if the process running or not for given exe file name +*/ +bool IsProcessRunning(const wchar_t *const executableName) +{ + PROCESSENTRY32 entry; + entry.dwSize = sizeof(PROCESSENTRY32); + + const auto snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL); + + if (!Process32First(snapshot, &entry)) + { + CloseHandle(snapshot); + wprintf_s(L"ERROR:IsProcessRunning::Process32First failed"); + return false; + } + + do + { + if (!_wcsicmp(entry.szExeFile, executableName)) + { + CloseHandle(snapshot); + return true; + } + } while (Process32Next(snapshot, &entry)); + + CloseHandle(snapshot); + return false; +} + +/* + check if the file exists +*/ +bool IsFileExists(const wchar_t *const fileName) +{ + DWORD dwAttrib = GetFileAttributes(fileName); + return dwAttrib != INVALID_FILE_SIZE; +} + +/* + Get the status of the service for given service name +*/ +int GetServiceStatus(const wchar_t *const serivceName) +{ + SC_HANDLE theService, scm; + SERVICE_STATUS_PROCESS ssStatus; + DWORD dwBytesNeeded; + + scm = OpenSCManager(nullptr, nullptr, SC_MANAGER_ENUMERATE_SERVICE); + if (!scm) + { + wprintf_s(L"ERROR:GetServiceStatus::OpenSCManager failed"); + return UNEXPECTED_ERROR; + } + + theService = OpenService(scm, serivceName, SERVICE_QUERY_STATUS); + if (!theService) + { + CloseServiceHandle(scm); + wprintf_s(L"ERROR:GetServiceStatus::OpenService failed"); + return UNEXPECTED_ERROR; + } + + auto result = QueryServiceStatusEx(theService, SC_STATUS_PROCESS_INFO, + reinterpret_cast(&ssStatus), sizeof(SERVICE_STATUS_PROCESS), + &dwBytesNeeded); + + CloseServiceHandle(theService); + CloseServiceHandle(scm); + + if (result == 0) + { + wprintf_s(L"ERROR:GetServiceStatus:QueryServiceStatusEx failed"); + return UNEXPECTED_ERROR; + } + + return ssStatus.dwCurrentState; +} + +/** + +**/ +int _tmain(int argc, wchar_t *argv[]) +{ + if (argc < 5) + { + wprintf_s(L"ERROR:unexpected number arguments and expected is 5"); + return UNEXPECTED_ERROR; + } + + if (!IsProcessRunning(argv[1])) + { + wprintf_s(L"ERROR:Process:%s is not running\n", argv[1]); + return NO_FLUENT_BIT_PROCESS; + } + + DWORD dwStatus = GetServiceStatus(argv[2]); + + if (dwStatus != SERVICE_RUNNING) + { + wprintf_s(L"ERROR:Service:%s is not running\n", argv[2]); + return FLUENTDWINAKS_SERVICE_NOT_RUNNING; + } + + if (IsFileExists(argv[3])) + { + wprintf_s(L"INFO:File:%s exists indicates Config Map Updated since agent started.\n", argv[3]); + return FILESYSTEM_WATCHER_FILE_EXISTS; + } + + if (IsFileExists(argv[4])) + { + wprintf_s(L"INFO:File:%s exists indicates Certificate needs to be renewed.\n", argv[4]); + return CERTIFICATE_RENEWAL_REQUIRED; + } + + return SUCCESS; +} diff --git a/build/windows/installer/scripts/livenessprobe.cmd b/build/windows/installer/scripts/livenessprobe.cmd deleted file mode 100644 index 19d0b69d7..000000000 --- a/build/windows/installer/scripts/livenessprobe.cmd +++ /dev/null @@ -1,36 +0,0 @@ -REM "Checking if fluent-bit is running" - -tasklist /fi "imagename eq fluent-bit.exe" /fo "table" | findstr fluent-bit - -IF ERRORLEVEL 1 ( - echo "Fluent-Bit is not running" - exit /b 1 -) - -REM "Checking if config map has been updated since agent start" - -IF EXIST C:\etc\omsagentwindows\filesystemwatcher.txt ( - echo "Config Map Updated since agent started" - exit /b 1 -) - -REM "Checking if certificate needs to be renewed (aka agent restart required)" - -IF EXIST C:\etc\omsagentwindows\renewcertificate.txt ( - echo "Certificate needs to be renewed" - exit /b 1 -) - -REM "Checking if fluentd service is running" -sc query fluentdwinaks | findstr /i STATE | findstr RUNNING - -IF ERRORLEVEL 1 ( - echo "Fluentd Service is NOT Running" - exit /b 1 -) - -exit /b 0 - - - - diff --git a/kubernetes/omsagent.yaml b/kubernetes/omsagent.yaml index d84e46701..98621b5f0 100644 --- a/kubernetes/omsagent.yaml +++ b/kubernetes/omsagent.yaml @@ -833,7 +833,11 @@ spec: command: - cmd - /c - - C:\opt\omsagentwindows\scripts\cmd\livenessProbe.cmd + - C:\opt\omsagentwindows\scripts\cmd\livenessprobe.exe + - fluent-bit.exe + - fluentdwinaks + - "C:\\etc\\omsagentwindows\\filesystemwatcher.txt" + - "C:\\etc\\omsagentwindows\\renewcertificate.txt" periodSeconds: 60 initialDelaySeconds: 180 timeoutSeconds: 15 diff --git a/kubernetes/windows/Dockerfile b/kubernetes/windows/Dockerfile index 290deef40..aa756b8b8 100644 --- a/kubernetes/windows/Dockerfile +++ b/kubernetes/windows/Dockerfile @@ -46,7 +46,7 @@ RUN ./setup.ps1 COPY main.ps1 /opt/omsagentwindows/scripts/powershell COPY ./omsagentwindows/installer/scripts/filesystemwatcher.ps1 /opt/omsagentwindows/scripts/powershell -COPY ./omsagentwindows/installer/scripts/livenessprobe.cmd /opt/omsagentwindows/scripts/cmd/ +COPY ./omsagentwindows/installer/livenessprobe/livenessprobe.exe /opt/omsagentwindows/scripts/cmd/ COPY setdefaulttelegrafenvvariables.ps1 /opt/omsagentwindows/scripts/powershell # copy ruby scripts to /opt folder