Benutzer-Werkzeuge

Webseiten-Werkzeuge


monitoring:icinga2:windows:checks:check_filecontent

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen gezeigt.

Link zu der Vergleichsansicht

monitoring:icinga2:windows:checks:check_filecontent [2017/09/09 20:54] (aktuell)
rafaelvoss angelegt
Zeile 1: Zeile 1:
 +====== Check_filecontent ======
  
 +
 +<sxh bash>
 +<#
 +V0.3
 +
 +Simple script to search for a string in a textfile or folder. iT also can check for fileage
 +usage: -path '​folderOrFilePath"​ -string "​text"​ -minutes '​minutes'​ # -path '​c:​\logfiles\logfile.log'​ -string "​error","​fehler"​ -minutes 3600
 +if path is a folder, the newest file in the fodler will be used.
 + 
 +Known errors: you need at least Powershell 3.0. Its in the Windows Management Framework > https://​www.microsoft.com/​de-DE/​download/​details.aspx?​id=40855 ​
 +#>
 +  ​
 +Param(
 +    [string]$path,​
 +    [string[]]$string,​
 +    [string]$minutes
 +)
 +
 +try {
 +    $item = get-item $path -ErrorAction SilentlyContinue
 +} catch {
 +    write-host "​UNKNOWN - Item not found or no permission to access"​
 +    exit 3 
 +}
 +$state = "​UNKNOWN"​
 +#Check if path is a folder
 + if ($item.Attributes -eq "​Directory"​) ​   {
 +     $item = (Get-ChildItem $item | sort LastWriteTime -Descending | select -First 1)
 +     ​$itemdate = $item.LastWriteTime ​   ​
 + } else {
 +    $item = (Get-Item $item | sort LastWriteTime -Descending | select -First 1)
 +     ​$itemdate = $item.LastWriteTime ​   ​
 + }
 +
 +#Exit if file is too old
 +if ($minutes) {
 +  if (($item.LastWriteTime) -lt ((Get-Date).AddMinutes(-$minutes))) {
 +    write-host "​CRITICAL - Item $item is older than $minutes minutes"​
 +    exit 2
 +  }
 +}
 +
 +$filecontent = Get-Content $item.Fullname
 +#echo $filecontent
 +ForEach ($line in $filecontent) { 
 +    ForEach ($singleString in $string) {
 +      if ($line -match $singleString) {
 +         ​$state = "​CRITICAL"​
 +         ​write-host "​CRITICAL - `"​$singleString`"​ found in Line: `"​$line`" ​ - last modified: $itemdate"​
 +       } else {
 +         if ($state -ne "​CRITICAL"​) { $state = "​OK"​ }
 +       }
 +            ​
 +      ​
 +    } 
 +}  ​
 + 
 +switch ($state) {
 +    "​OK"​ { write-host "OK - `"​$string`"​ not found - last modified: $itemdate"​ ; exit 0 }
 +    "​WARNING" ​ { exit 1 }
 +    "​CRITICAL" ​ { exit 2 }
 +    "​UNKNOWN" ​ { exit 3 }
 +    default {exit 3 }
 +}    ​
 +</​sxh>​
monitoring/icinga2/windows/checks/check_filecontent.txt · Zuletzt geändert: 2017/09/09 20:54 von rafaelvoss