diff --git a/agents/plugins/curl.ps1 b/agents/plugins/curl.ps1 deleted file mode 100755 index 04727a27a02e41f4304b787fb6459f022c0a0e5e..0000000000000000000000000000000000000000 --- a/agents/plugins/curl.ps1 +++ /dev/null @@ -1,218 +0,0 @@ -<# -License: GNU General Public License v2 - -Author: thl-cmk[at]outlook[dot]com -URL : https://thl-cmk.hopto.org -Date : 2022-02-10 - -based on the work by Christian Wirtz doc[at]snowheaven[dot]de and Ingo Hambrock - -Wrapper around: https://curl.se/ - -2022-02-10: rewritten by thl-cmk[at]outlook.com to output json string -2022-02-16: added file checks for CURL_EXECUTABLE and CURL_FORMAT -2022-02-17: added ERRORLEVEL -2022-02-21: added curl_option to the output -2022-02-22: fixed search for curl.exe -2022-02-23: fixed handling of options from curl.cfg -2022-02-24: removed curl.format, replaced by -w %{json} -2022-02-25: removed separate url and error_level from output -2022-02-27: rewritten for powershell (from cmd) -2022-02-28: added support to search for strings in cURL output -2022-03-05: added support for search in header strings -2022-03-06: added support for ssl/tls session info -2022-03-11: added --verbose --stderr $TEMP_DIR\curl_session to CURL_OPTIONS -2022-03-12: fixed escapes for json format in TLS_INFO -2022-03-13: moved curl_item files to curl sub directory under $MK_CONFDIR -2022-03-15: moved curl options from curl.cfg to curl_item_#.options -2022-03-15: added regex pattern match -#> - -$TEMP_DIR="c:\windows\temp" - -# config file directory -$MK_CONFDIR = $env:MK_CONFDIR - -# Fallback if no MK_CONFDIR is set -if (!$MK_CONFDIR) { - $MK_CONFDIR= "$env:ProgramData\checkmk\agent\config" -} - -Write-Output "<<<curl:sep(0)>>>" - -if (Test-Path -Path $MK_CONFDIR\curl.cfg -PathType Leaf) { - $CURL_CONFIG="$MK_CONFDIR\curl.cfg" -} else { - Write-Output '{"ERROR":"config file $MK_CONFDIR\curl.cfg does not exist"}' - exit 0 -} - -# first check for curl.exe in cmk agent directory -if (Test-Path -Path "C:\ProgramData\checkmk\agent\bin\curl.exe" -PathType Leaf) { - $CURL_EXECUTABLE="C:\ProgramData\checkmk\agent\bin\curl.exe" - # if not found look for system provided file -} elseif (Test-Path -Path "C:\Windows\System32\curl.exe" -PathType Leaf) { - $CURL_EXECUTABLE="C:\Windows\System32\curl.exe" -} else { - Write-Output '{"ERROR":"executable file curl.exe not found"}' - exit 0 -} - -$CURL_OPTIONS="--disable --write-out %{json} --silent --verbose --include --stderr $TEMP_DIR\curl_session" -$CURL_OPTIONS=$CURL_OPTIONS.Substring(0,$CURL_OPTIONS.Length -0) -$CURL_RUN="FIRST" -$CURL_RESULT="" -$CURL_OUTPUT="$TEMP_DIR\curl_output" -$CURL_HEADER="$TEMP_DIR\curl_header" -$CURL_SESSION_FILE="$TEMP_DIR\curl_session" - -function Find-CurlStrings { - param ( - [string]$SEARCH_FILE, - [string]$SEARCH_EXTENSION, - [string]$JSON_KEY - ) - if (Test-Path -Path "$SEARCH_FILE" -PathType Leaf) { - if (Test-Path -Path "$MK_CONFDIR/curl/$CURL_ITEM.$SEARCH_EXTENSION" -PathType Leaf) { - $ROUND="FIRST" - $CURL_FILE=Get-Content $SEARCH_FILE -Raw - $CURL_RESULT="$CURL_RESULT{""$SERVICE_NAME"":{""$JSON_KEY"":[" - foreach($LINE in Get-Content $MK_CONFDIR/curl/$CURL_ITEM.$SEARCH_EXTENSION) { - if ( $ROUND -eq "SECOND" ) { - $CURL_RESULT="$CURL_RESULT," - } else { - $ROUND="SECOND" - } - $CURL_SEARCH=$CURL_FILE.IndexOf($LINE) - # change to match grep return codes - if ($CURL_SEARCH -ne -1){ - $CURL_SEARCH = 0 - } else { - $CURL_SEARCH = 1 - } - $CURL_RESULT="$CURL_RESULT[""$LINE"", $CURL_SEARCH]" - } - $CURL_RESULT="$CURL_RESULT]}}" - } - } - return $CURL_RESULT -} - -function Find-CurlSessionInfo { - param ( - [string]$SEARCH_MARKER, - [string]$JSON_KEY - ) - if (Test-Path -Path "$CURL_SESSION_FILE" -PathType Leaf) { - $CURL_RESULT = "$CURL_RESULT{""$SERVICE_NAME"":{""$JSON_KEY"":[" - $ROUND="FIRST" - foreach($LINE in Get-Content "$CURL_SESSION_FILE") { - if ($LINE[0] -eq "$SEARCH_MARKER") { - $LINE = $LINE.Substring(1, $LINE.Length -1).trim() -replace """", "\""" - if ( $ROUND -eq "SECOND" ) { - $CURL_RESULT="$CURL_RESULT," - } else { - $ROUND="SECOND" - } - $CURL_RESULT="$CURL_RESULT""$LINE""" - } - } - $CURL_RESULT="$CURL_RESULT]}}" - } - # return $CURL_RESULT even if it not altered - return $CURL_RESULT -} - -function Find-CurlRegex { - if (Test-Path -Path "$CURL_OUTPUT" -PathType Leaf) { - if (Test-Path -Path "$MK_CONFDIR/curl/$CURL_ITEM.regex" -PathType Leaf) { - if ($CURL_REG_MULTI -eq "multiline") { - $CURL_REG_MULTI = "(?sm)" - } else { - $CURL_REG_MULTI = "" - } - $CURL_FILE=Get-Content "$CURL_OUTPUT" -Raw - foreach($LINE in Get-Content "$MK_CONFDIR/curl/$CURL_ITEM.regex") { - if ($CURL_REG_CASE -eq "case") { - $CURL_REG_RESULT= $CURL_FILE -cmatch "$CURL_REG_MULTI$LINE" - } else { - $CURL_REG_RESULT= $CURL_FILE -match "$CURL_REG_MULTI$LINE" - } - } - if ("$CURL_REG_RESULT" -eq "True") { - $CURL_REG_RESULT = 0 - } else { - $CURL_REG_RESULT = 1 - } - $CURL_RESULT = "$CURL_RESULT{""$SERVICE_NAME"":{""regex"":$CURL_REG_RESULT}}" - } - } - return $CURL_RESULT -} - -function Cleanup { - if (Test-Path -Path $CURL_OUTPUT -PathType Leaf) { - Remove-Item -Path $CURL_OUTPUT - } - if (Test-Path -Path $CURL_HEADER -PathType Leaf) { - Remove-Item -Path $CURL_HEADER - } - if (Test-Path -Path $CURL_SESSION_FILE -PathType Leaf) { - Remove-Item -Path $CURL_SESSION_FILE - } -} - - -Write-Host -NoNewline "{" - -foreach($LINE in Get-Content $CURL_CONFIG) { - $SERVICE_NAME=$LINE.split("|")[0] - $CURL_ITEM=$LINE.split("|")[1] - $CURL_REGEX=$LINE.split("|")[2] - $CURL_REG_CASE=$CURL_REGEX.split("_")[0] - $CURL_REG_MULTI=$CURL_REGEX.split("_")[1] - - if ( $CURL_RUN -eq "SECOND") { - Write-Host -NoNewline "," - } else { - $CURL_RUN="SECOND" - } - - Cleanup - - Write-Host -NoNewline """$SERVICE_NAME"":{""data"":" - # $RUN="$CURL_EXECUTABLE $CURL_OPTIONS --url $URL $OPTIONS" - $RUN="$CURL_EXECUTABLE $CURL_OPTIONS --config $MK_CONFDIR/curl/$CURL_ITEM.options" - $RESULT = . cmd /c "$RUN" - Write-Host -NoNewline $RESULT - # Write-Host -NoNewline ",""curl_options"":""$CURL_OPTIONS $OPTIONS""" - Write-Host -NoNewline "}" - - # check for expected strings in response - $CURL_RESULT=Find-CurlStrings -SEARCH_FILE "$CURL_OUTPUT" -SEARCH_EXTENSION "search_response" -JSON_KEY "expected_response" - - # check for expected strings in headers - $CURL_RESULT=Find-CurlStrings -SEARCH_FILE "$CURL_HEADER" -SEARCH_EXTENSION "search_header" -JSON_KEY "expected_header" - - # collect for tls/ss session info - $CURL_RESULT=Find-CurlSessionInfo -SEARCH_MARKER "*" -JSON_KEY "TLS_INFO" - - # collect request header - $CURL_RESULT=Find-CurlSessionInfo -SEARCH_MARKER ">" -JSON_KEY "REQUEST_HEADER" - - # collect response header - $CURL_RESULT=Find-CurlSessionInfo -SEARCH_MARKER "<" -JSON_KEY "RESPONSE_HEADER" - - # check for regex match - $CURL_RESULT=Find-CurlRegex - -} - -Write-Output "}" - -Write-Output $CURL_RESULT - -# clean up -Cleanup - -exit 0