-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReport_Storage_Disconnect
More file actions
103 lines (86 loc) · 3.49 KB
/
Report_Storage_Disconnect
File metadata and controls
103 lines (86 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#Script per verificare eventuali disconnessioni dello storage. Viene analizzato il vmkernel log e se trova la dicitura "STATE IN DOUBT"
#procede con il salvataggio del log e l'invio della mail
#Nicolas Martinelli
#Variabili VCenter
$VcenterTest = "Test vCenter"
$VcenterClienti = "Production vCenter"
$VcenterInterni = "Internal vCenter"
#Variabili connessione
$User = 'DOMAIN\USER'
$pswdFile = 'D:\TEMP\vmware\DisconnessioniStorage\Store\pass.sec'
$keyFile = "D:\TEMP\vmware\DisconnessioniStorage\Store\chiave.key"
$encryptedPswd = Get-Content -Path $pswdFile | ConvertTo-SecureString -Key (Get-Content -Path $keyFile)
$cred = New-Object System.Management.Automation.PSCredential($user,$encryptedPswd)
#Variabili Path e File
$OutputPath = "D:\TEMP\vmware\DisconnessioniStorage\WRK\"
$ZipOutputFileName = "LogVmware.zip"
$ZipOutput = $OutputPath + $ZipOutputFileName
$ArchivePath = "D:\TEMP\vmware\DisconnessioniStorage\Archive\"
$PathToArchive = "D:\TEMP\vmware\DisconnessioniStorage\WRK\*.zip"
$ClearPath = "D:\TEMP\vmware\DisconnessioniStorage\WRK\*.*"
#Connessione a tutti i vCenter
connect-VIServer -Server $VcenterTest -Credential $cred
connect-VIServer -Server $VcenterClienti -Credential $cred
connect-VIServer -Server $VcenterInterni -Credential $cred
$HostEsx = Get-VMHost| Select Name, @{N="Cluster";E={Get-Cluster -VMHost $_}}
$ProblemCluster = @()
#Variabili Data
$Date = get-date
$DateFormat = $Date.ToString("yyyy-MM-dd")
$ZipoOutputOLD = $DateFormat + $ZipOutputFileName
#Inizio processo
foreach ($i in $hostEsx){
#Nome Esxi
$HostEsxName = $i.Name
$Cluster = $i.Cluster
#Variabili output Esx
$OutputLogFileName = $HostEsxName + ".txt"
$OutputFile = $OutputPath + $OutputLogFileName
#Acquisizione vmkernel
$VmkernelLog= (Get-Log -VMHost (Get-VMHost $HostEsxName ) vmkernel).Entries
#Verifica presenza disconnessioni
$CheckStateDoubt = $VmkernelLog | Select-String -Pattern "STATE IN DOUBT"
if ($CheckStateDoubt -eq $q){
Write-host "NESSUNA DISCONNESSIONE" -foreground "green"
}
else{
#Verifica se ci sono disconnessioni odierne
if ($CheckStateDoubt -match $DateFormat){
Write-host "RILEVATA DISCONNESSIONE su $HostEsxName nel cluster $Cluster" -foreground "red"
$VmkernelLog | Out-file $OutputFile
$CheckStateDoubt
$ProblemCluster += $Cluster
$ProblemCluster += ";"
#Variabile Booleana per invio log
$boolean = 1
}
else {Write-host "RILEVATA DISCONNESSIONE GIORNI PASSATI" -foreground "yellow"}
#$CheckStateDoubt
}
}
#Preparazione output mail
$Tmp = $ProblemCluster -split ";"
$TotClusterImpact = $tmp | select -Unique
#Chiusura connessioni aperte sui vCenter
Disconnect-VIServer -Server * -Confirm:$false -Force
#Compressione risultato
Compress-Archive -Path $OutputPath -DestinationPath $ZipOutput
#Invio mail solo se ho trovato una disconnessione
if ($boolean -eq 1){
#####Generazione Mail#####
$Subject = "!!! "+ $DateFormat + " RILEVATE DISCONNESSIONI STORAGE INFRASTRUTTURA VMWARE !!!"
$body = "Buongiorno,`r`n"
$body += "`r`n"
$body += "in allegato i log delle disconnessioni rilevate oggi.`r`n"
$body += "`r`n"
$body += "I Cluster impattati sono:`r`n"
$body += "`r`n"
$body += "$TotClusterImpact `r`n"
$body += "`r`n"
$body += "Grazie mille!"
Send-MailMessage -from "SENDER_MAIL_ADDRESS" -to "RECIPIENT_ADDRESS" -Subject "!!! RILEVATE DISCONNESSIONI !!!" -Body $body -Attachment $ZipOutput -SmtpServer "MAIL_RELAY_ADDRESS"
}
#Pulizia cartella di lavoro
Rename-Item $ZipOutput $ZipoOutputOLD
Move-Item -Path $PathToArchive -Destination $ArchivePath
Remove-Item $ClearPath