368d6fafea
Code backup
124 lines
3.7 KiB
PowerShell
124 lines
3.7 KiB
PowerShell
##########################
|
|
# Writer: Claudio Boggian
|
|
# Company: PAL s.r.l.
|
|
#-------------------------
|
|
# Date: 2025/02/21
|
|
# v: 1.0
|
|
# Reason: Emission
|
|
#-------------------------
|
|
##########################
|
|
|
|
# Imposta il percorso della cartella da monitorare
|
|
$cartella = "E:\CyberPlanWeb_Data\cybinstance\PLANNING\logs"
|
|
$displayName = "CyberPlan Logs"
|
|
|
|
# Funzioni base di \\pal.local\NETLOGON\Powershell\Base-Functions.ps1
|
|
|
|
function Initialize-Graylog {
|
|
Param ([string]$Facility)
|
|
|
|
if(!(Test-Path -PathType Container -Path "C:\temp\")){
|
|
New-Item -ItemType Directory -Force -Path "C:\temp\" | Out-Null
|
|
}
|
|
$logFile = "C:\temp\$($Facility).log"
|
|
if (Test-Path $logFile)
|
|
{
|
|
Remove-Item $logFile
|
|
}
|
|
|
|
try{
|
|
# Installing Graylog Module
|
|
if(!(Get-InstalledModule -Name "PSGELF")){
|
|
Add-Content -Path $logFile -Value "Installing PSGELF module"
|
|
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
|
|
Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted
|
|
Install-Module -Name PSGELF
|
|
Add-Content -Path $logFile -Value "PSGELF module installed!"
|
|
}
|
|
if(!(Get-Module -Name PSGELF)){
|
|
Add-Content -Path $logFile -Value "Importing PSGELF module"
|
|
Import-Module -Name PSGELF
|
|
Add-Content -Path $logFile -Value "PSGELF module imported!"
|
|
}
|
|
}
|
|
catch{
|
|
Add-Content -Path $logFile -Value $_
|
|
}
|
|
}
|
|
|
|
function Write-Log {
|
|
Param ([string]$Facility, [string]$LogString, [int32]$Level = 5)
|
|
Initialize-Graylog $Facility
|
|
Send-PSGelfUDP -GelfServer "palgraylog01.pal.local" -Port 12204 -ShortMessage $LogString -Level $Level -Facility $Facility
|
|
}
|
|
|
|
# Funzione per ottenere il file più recente che segue il pattern debug.*.log*
|
|
function Get-LatestLogFile {
|
|
return Get-ChildItem -Path $cartella -Filter "debug.*.log*" |
|
|
Sort-Object LastWriteTime -Descending |
|
|
Select-Object -First 1
|
|
}
|
|
|
|
# Funzione per inviare una riga di log a Graylog
|
|
function Send-ToGraylog {
|
|
param ([string]$logMessage)
|
|
|
|
try {
|
|
Write-Log $displayName $logMessage
|
|
Write-Host "Log inviato: $logMessage"
|
|
} catch {
|
|
Write-Host "Errore nell'invio del log: $_"
|
|
}
|
|
}
|
|
|
|
# Monitoraggio delle modifiche in tempo reale
|
|
function Monitor-LogFile {
|
|
param ([string]$filePath)
|
|
|
|
Write-Host "Monitorando il file: $filePath"
|
|
$reader = [System.IO.StreamReader]::new([System.IO.File]::Open($filePath, 'Open', 'Read', 'ReadWrite'))
|
|
|
|
# Sposta il lettore alla fine del file
|
|
$reader.BaseStream.Seek(0, [System.IO.SeekOrigin]::End) | Out-Null
|
|
|
|
while ($true) {
|
|
Start-Sleep -Milliseconds 500 # Attendi 500ms prima di controllare nuove righe
|
|
|
|
# Leggi nuove righe, se presenti
|
|
$line = $reader.ReadLine()
|
|
while ($line -ne $null) {
|
|
Send-ToGraylog -logMessage $line
|
|
$line = $reader.ReadLine()
|
|
}
|
|
|
|
# Se il file cambia, termina il ciclo
|
|
if ((Get-LatestLogFile).FullName -ne $filePath) {
|
|
Write-Host "Nuovo file rilevato, cambiando monitoraggio..."
|
|
break
|
|
}
|
|
}
|
|
|
|
$reader.Close()
|
|
}
|
|
|
|
# Funzione per monitorare la cartella e gestire nuovi file
|
|
function Monitor-Folder {
|
|
while ($true)
|
|
{
|
|
try {
|
|
Write-Host "In attesa di file nella cartella: $cartella"
|
|
|
|
# Recupera il file iniziale da monitorare
|
|
$latestFile = Get-LatestLogFile
|
|
if ($latestFile) {
|
|
Monitor-LogFile -filePath $latestFile.FullName
|
|
}
|
|
} catch {
|
|
Write-Log $displayName "Il servizio è stato interrotto a causa di un errore. Errore: $_"
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
# Avvia il monitoraggio della cartella
|
|
Monitor-Folder |