init ready for pord

This commit is contained in:
Florian Späth
2025-08-08 18:26:32 +02:00
commit 161a98a690
3 changed files with 324 additions and 0 deletions

9
list.csv Normal file
View File

@@ -0,0 +1,9 @@
Url
https://spaeth-bayern.de
https://gitlab.spaeth-bayern.de
https://bookshelf.spaeth-bayern.de
http://192.168.178.201
https://ai.spaeth-bayern.de
https://home.spaeth-bayern.de
https://ntfy.spaeth-bayern.de
http://192.168.178.3
1 Url
2 https://spaeth-bayern.de
3 https://gitlab.spaeth-bayern.de
4 https://bookshelf.spaeth-bayern.de
5 http://192.168.178.201
6 https://ai.spaeth-bayern.de
7 https://home.spaeth-bayern.de
8 https://ntfy.spaeth-bayern.de
9 http://192.168.178.3

269
log.psm1 Normal file
View File

@@ -0,0 +1,269 @@
#_ __ __ _ _
#| | | \/ | | | | |
#| | ___ __ _ | \ / | ___ __| |_ _| | ___
#| | / _ \ / _` | | |\/| |/ _ \ / _` | | | | |/ _ \
#| |___| (_) | (_| | | | | | (_) | (_| | |_| | | __/
#|______\___/ \__, | |_| |_|\___/ \__,_|\__,_|_|\___|
# __/ |
# |___/
#OVERRIDE: Gestolen vom BR, jetzt für PRIVAT benutzt
#
#### Vorwort:
# Dieses Modul wurde entwickelt, um das Schreiben und Löschen von Logs zu standardisieren.
# Entwickelt von der Fachgruppe Hardware und Datenspeicherung des Bayerischen Rundfunks, steht dieses Modul dem gesamten BR zur Verfügung.
#
# Entwickler: Florian Späth, Johannes Krenig
#
### Programmablauf:
# Bei der Initialisierung der Funktion wird ein Log-Ordner anhand des Produktnamens erstellt. Dieser wird in den darauffolgenden Läufen beim Start überprüft, ob vorhandene Logs die definierte Lebensdauer überschreiten, und ggf gelöscht.
# Mit dem RollingInterval wird festgelegt, wie viele Logs erstellt werden sollen. Infinite bedeutet in diesem Fall: Jeder Start des Skripts erzeugt ein neues Log. Month hingegen erstellt ein Log für den jeweiligen Kalendermonat.
# Der GUI-Parameter wurde für die FG HDS implementiert, um bei einer Log-Aktualisierung die vorhandene PowerShell-Oberfläche zu triggern.
# Für das Protokollieren von Fehlern kann Write-Warning oder Write-Error verwendet werden. Für andere Anwendungsfälle kann Write-Log mit einem Fehlerlevel (0 = Info, 1 = Warning, 2 = Error) aufgerufen werden.
#
### Fehlerverhalten:
# Bei Fehlern während der Initialisierung werden die Folgefunktionen nicht aktiviert. Bei unerwarteten Fehlern im Modul wird sowohl eine Meldung in der Konsole ausgegeben als auch eine Mail an die hinterlegte Adresse (Standard: hdsdev@br.de) gesendet.
# Schlägt das Versenden der Mail in den ausführenden Funktionen fehl oder treten unerwartete Fehler auf, beendet das Modul den Vorgang mit EXIT 1.
#
### Einbindung:
# Import-Module $PSScriptRoot\log.psm1
#
#
### LOG_MODULE:
## Parameter:
# $productname - Name der Anwendung, es ist eine UID für die Log Dateien
# $lifetime - Wie viele Tage wird das Log aufgehoben, danach gelöscht
# $GUI - Parameter für das Ausführende Script, wenn gesetzt wird String an HDS Admin Panel Log geschickt
# $level - Manuelle wählen des Error levels (0 = Info, 1 = Warning, 2 = Error)
# $mail - Booleran ($true/$false) eine Mail mit gesamten Log wird versandt
# $rollingInterval - Logs werden in zusammengelegt über bestimmte Zeiträume ("day", "month", "year", "infinit")
#
## Initial:
# Write-init -productname "PRODUCT_NAME" -lifetime 7 -GUI $false -rollingInterval "day", "month", "year", "infinit"
#
## Normal use:
# Write-Log -text "STRING"
#
## Error notification use:
# Default = Write-Log -text "STRING" (-mail -level 1 | 2)
# Level 1 = Write-Warning -text "STRING" (-mail)
# Level 2 = Write-Error -text "STRING" (-mail)
#
# Von HDS_UTILS_MODULE_Version 1.2.4
# Von BR_LOG_MODULE_Version 1.0.0
#
[string]$SCRIPT:MODULE_Version = "1.1.0"
[bool]$SCRIPT:LOGMODULE_InitFin = $false
function Send-NTFY {
param (
$text
)
$Request = @{
Method = "POST"
URI = "ntfy.spaeth-bayern.de/LOG_ERROR"
Body = "$text"
}
Invoke-RestMethod $Request
}
function Write-init {
param (
[string]$ProductName = "PRODUCT_NAME",
[bool]$GUI = $false,
[int]$Lifetime = 30,
[string]$rollingInterval = "infinit"
)
[string]$logFileFormat = Get-LogFileFormat($rollingInterval)
# Initial Config
if ($ProductName -ne "PRODUCT_NAME") {
$SCRIPT:LOGMODULE_ProductName = $ProductName
$SCRIPT:LOGMODULE_Gui = $GUI
$datValue = "infinit"
if ($logFileFormat -ne "infinit") {
$datValue = $(get-date -format $logFileFormat);
}
$SCRIPT:LOGMODULE_Logfile = "$PSScriptRoot\$SCRIPT:LOGMODULE_ProductName-logs\$SCRIPT:LOGMODULE_ProductName-SessionLog-$($datValue).log"
if (-not (Test-Path "$PSScriptRoot\$SCRIPT:LOGMODULE_ProductName-logs")) {
New-Item -Path "$PSScriptRoot" -Name "$SCRIPT:LOGMODULE_ProductName-logs" -ItemType "directory"
}
if ($lifetime -ne 0 -and $logFileFormat -ne "infinit" ) {
$date = (Get-Date).AddDays(-$lifetime)
$count = $SCRIPT:LOGMODULE_ProductName.length
foreach ($i in (Get-ChildItem -path "$PSScriptRoot\$SCRIPT:LOGMODULE_ProductName-logs")) {
try {
$sub = $i.Name.substring($count)
$val = [regex]::Match($sub, '\d{4}(-\d{2})?(-\d{2})?').Value
$datum = $null
try {
$datum = [DateTime]::ParseExact($val, 'yyyy', [Globalization.CultureInfo]::CreateSpecificCulture('de-DE'))
$datum = $datum.AddMonths(12)
}
catch {}
try {
$datum = [DateTime]::ParseExact($val, 'yyyy-MM', [Globalization.CultureInfo]::CreateSpecificCulture('de-DE'))
$datum = $datum.AddMonths(1)
}
catch {}
try {
$datum = [DateTime]::ParseExact($val, 'yyyy-MM-dd', [Globalization.CultureInfo]::CreateSpecificCulture('de-DE'))
}
catch {}
try {
$datum = [DateTime]::ParseExact($val, 'yyyy-MM-dd-ss', [Globalization.CultureInfo]::CreateSpecificCulture('de-DE'))
}
catch {}
if ($datum -lt $date) {
remove-item $i.FullName
}
}
catch {
}
}
}
$SCRIPT:LOGMODULE_InitFin = $true
"<MODULE INFORMATION>: Name = Powershell Log Module, Version = $($SCRIPT:MODULE_Version), Author = Florian Späth" | out-file $SCRIPT:LOGMODULE_Logfile -Append
#Write-Host "MODULE INFORMATION" -NoNewline; Write-Host ": Name = Utils Powershell Module, Version = $($SCRIPT:UTILSMODULE_Version), Author = Florian Späth"
}
else {
Write-Host "MODULE INFORMATION" -NoNewline; Write-Host ": Name = Powershell Log Module, Version = $($SCRIPT:MODULE_Version), Author = Florian Späth"
Write-Host "MODULE INFORMATION" -NoNewline; Write-Host ": Product Name = $SCRIPT:LOGMODULE_ProductName, Lifetime = $lifetime, GUI = $GUI, "
Write-Host ""
Write-Host "MODULE INFORMATION" -NoNewline; Write-Host ": Log Module: "
Write-Host "MODULE INFORMATION" -NoNewline; Write-Host ": Write-init -mailfrom `"hdsdev@br.de`" -mailto `"hdsdev@br.de`" -mailserver `"mail.br.de`" -productname `"PRODUCT_NAME`" (optional: -lifetime 7 -GUI `$false -rollingInterval `"day`", `"month`", `"year`", `"infinit`")"
Write-Host "MODULE INFORMATION" -NoNewline; Write-Host ": Write-Log -text `"STRING`" (optional: -mail '($true|$false)' -level (0 = Normal, 1 = Warning, 2 = Error)"
Write-Host ""
Write-Host "MODULE INFORMATION" -NoNewline; Write-Host ": Mail Module: "
Write-Host "MODULE INFORMATION" -NoNewline; Write-Host ": Send-init -mailfrom `"ZeroConfig <hdsdev@br.de>`" -mailto `"E-MAIL`" (optional if Log is initialized: -productname `"PRODUCT_NAME`") (optional: -mailserver `"mail.br.de`" -mailicon `"PATH_TO_ICON.PNG`")"
Write-Host "MODULE INFORMATION" -NoNewline; Write-Host ": Send-Mail -subject `"STRING`" -text `"STRING`" (optional: -body `"STRING`" -mailto `"STRING`" -attachment `"PATH_TO_FILE`")"
}
}
function Write-Log {
Param
(
[string]$Text,
[bool]$Mail = $false,
[int]$level = 0
)
if ($SCRIPT:LOGMODULE_InitFin) {
if ($level -eq 0) {
"$(get-date -format "yyyy-MM-dd HH:mm:ss")<INFO>$(GetCallerString): $($text)" | out-file $SCRIPT:LOGMODULE_Logfile -Append
#Write-Host "INFO" -NoNewline; Write-Host ": $text"
}
elseif ($level -eq 1) {
"$(get-date -format "yyyy-MM-dd HH:mm:ss")<WARN>$(GetCallerString): $($text)" | out-file $SCRIPT:LOGMODULE_Logfile -Append
Write-Host "$(get-date -format "yyyy-MM-dd HH:mm:ss")<WARN>$(GetCallerString): $($text)" -ForegroundColor Yellow
}
elseif ($level -eq 2) {
"$(get-date -format "yyyy-MM-dd HH:mm:ss")<ERROR>$(GetCallerString): $($text)" | out-file $SCRIPT:LOGMODULE_Logfile -Append
Write-Host "$(get-date -format "yyyy-MM-dd HH:mm:ss")<ERROR>$(GetCallerString): $($text)" -ForegroundColor Red
Send-NTFY "ERROR message from $($SCRIPT:LOGMODULE_ProductName): $text"
}
if ($SCRIPT:LOGMODULE_Gui -eq $true) {
GUI($text)
[System.Windows.Forms.Application]::DoEvents()
$GUI_Log.ScrollToEnd()
}
$SCRIPT:LOGMODULE_MailText = "$SCRIPT:LOGMODULE_MailText" + "$text" + " `n "
if ($mail) {
"$(get-date -format "yyyy-MM-dd HH:mm:ss")$(GetCallerString): ########## NTFY Message sent to Subscription LOG_ERROR ##########" | out-file $SCRIPT:LOGMODULE_Logfile -Append
Send-NTFY "Log module ERROR: $text"
}
}
else {
Write-Host "CONFIGURE LOG SETTINGS FIRST: Write-init -productname `"PRODUCT_NAME`" -lifetime 7"
exit 0
}
}
function GetCallerString {
try {
$callStack = Get-PSCallStack
$caller = $callStack[2] # Der direkte Aufrufer
$callerScript = $caller.ScriptName
$arr = $CallerScript.Split('\')
$scriptname = $arr[$arr.Count - 1]
$callerLine = $caller.ScriptLineNumber
return "$($scriptname):$($callerLine)"
}
catch {
return "";
}
}
function Get-LogFileFormat {
param (
[string]$rollingInterval
)
if ($rollingInterval -and $rollingInterval -notin @('day', 'month', 'year', 'infinit')) {
Write-Error "Ungültiger Wert für rollingInterval. Erlaubte Werte sind: 'day', 'month', 'year', 'infinit'."
return "yyyy-MM-dd-HH-mm-ss"
}
$logFileFormat = ""
switch ($rollingInterval) {
'null' {
$logFileFormat = "yyyy-MM-dd-HH-mm-ss"
break
}
'day' {
$logFileFormat = "yyyy-MM-dd"
break
}
'month' {
$logFileFormat = "yyyy-MM"
break
}
'year' {
$logFileFormat = "yyyy"
break
}
'infinit' {
$logFileFormat = "infinit"
break
}
default {
$logFileFormat = "yyyy-MM-dd-HH-mm-ss"
break
}
}
return $logFileFormat
}
function Write-Warning {
Param
(
[string]$Text,
[bool]$Mail = $false
)
Write-Log -Text $text -Mail $Mail -level 1
}
function Write-Error {
Param
(
[string]$Text,
[bool]$Mail = $false
)
Write-Log -Text $text -Mail $Mail -level 2
}
Export-ModuleMember -Function Write-Warning
Export-ModuleMember -Function Write-Error
Export-ModuleMember -Function Write-init
Export-ModuleMember -Function Write-Log

46
root.ps1 Normal file
View File

@@ -0,0 +1,46 @@
Remove-Module log
Import-Module "$PSScriptRoot\log.psm1"
Write-init -ProductName "OnlineTester-NTFY" -lifetime 7
# Define path to CSV file
$csvPath = "$PSScriptRoot\list.csv"
# Import CSV and iterate through each URL
$websites = Import-Csv -Path $csvPath
function Send-NTFY {
param (
$text
)
$Request = @{
Method = "POST"
URI = "https://ntfy.spaeth-bayern.de/Availability"
Headers = @{
Title = "Website down!"
Priority = "urgent"
Tags = "warning,skull"
}
Body = "$text"
}
Invoke-RestMethod @Request
}
foreach ($site in $websites) {
$url = $site.Url
try {
# Create a WebRequest object
$request = [System.Net.HttpWebRequest]::Create($url)
$request.Method = "HEAD"
$request.Timeout = 5000 # 5 seconds timeout
$request.AllowAutoRedirect = $true
# Get the response
$response = $request.GetResponse()
Write-Log "$url is reachable (Status: $($response.StatusCode))"
$response.Close()
}
catch {
Write-Warning "$url is NOT reachable. Error: $($_.Exception.Message)"
Send-NTFY "$url is NOT reachable. Error: $($_.Exception.Message)"
}
}