Ci-dessous une nouvelle version d’un script de fermeture des alertes liées a des monitors en état Healthy. En effet meme si le cas contraire est plus fréquent (alerte fermée alors que le monitor est encore en état Warning ou Critical), il se peut que l’on doivent fermer les alertes de monitors en état Healthy.
Le script affiche clairement la sortie des alertes a traiter et log cette sortie dans l’eventlog.
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | # SCRIPT TO CHECK INCONSISTENCY BETWEEN NOT CLOSED ALERTS AND HEALTHY MONITORS Param ( # Treat alerts that has been modified since less than $LastModifHours $LastModifHours = 2 ) $ScriptName = "CloseAlertsFromHealthyMonitors.ps1" # FUNCTIONS # Check if a source with script name exist in operationsmanager eventlog to log some specific events Function NewEventSource { if (!( Test-Path "HKLM:\SYSTEM\CurrentControlSet\services\eventlog\Operations Manager\$ScriptName" )) { New-EventLog -LogName "Operations Manager" -Source $ScriptName } } # END FUNCTIONS #Log of script execution NewEventSource write-eventlog -logname "Operations Manager" -Source $ScriptName -EventID 1000 -Message "Execution of script $ScriptName (Value of LastModifHours is $LastModifHours hours)" -EntryType Information # Import of Scom Module Import-Module OperationsManager # Get list of closed alerts with following criterias: # - Not Closed # - Generated by a monitor $NotClosedAlerts = Get-SCOMAlert -ResolutionState (0..254) | where { ( $_ .ismonitoralert -eq $true ) -and $_ .LastModified -gt ( Get-Date ).addhours(- $LastModifHours )} # Variable to store the result of alert treatment. # Header $Result = "`n*********** CHECK INCONSISTENCY BETWEEN NOT CLOSED ALERTS AND HEALTHY MONITORS ***********" $Result += "`n START `n" for ( $i =0; $i -le $NotClosedAlerts .GetUpperBound(0); $i = $i +1) { # Display alert Nb $NotClosedAlert = $NotClosedAlerts [ $i ] $Result += "`n`nalert $i ------------------------------" # Get IDs from Closed alert $mrid = $NotClosedAlert .monitoringruleid $mcid = $NotClosedAlert .monitoringclassid $moid = $NotClosedAlert .monitoringobjectid # Get corresponding class $monitoringclass = Get-SCOMClass -id $mcid # Get the corresponding instance with following criterias: # - HealthState equal Success $MyInstance = Get-SCOMMonitoringObject -Class $monitoringclass | where { $_ .id -eq $moid -and $_ .HealthState -eq "Success" } # If there is no instances no need to reset (exit the loop and treat the next closed alert) If (!( $MyInstance )) { $Result += "No Instance found in Success state for the alert `"$($NotClosedAlert.Name)`" - NO NEED TO CLOSE ALERT" } Else { $Result += "`nThe following alert must be closed: `n" $Result += "NAME: $($NotClosedAlert.Name)`n" $Result += "ID: $($NotClosedAlert.Id)`n" $Result += "COMPUTER OR OBJECT: $($NotClosedAlert.MonitoringObjectDisplayName)`n" $Result += "COMPUTER OR OBJECT FULL PATH: $($NotClosedAlert.MonitoringObjectFullName)`n" $Result += "COMPUTER OR OBJECT HEALTH STATE: $($NotClosedAlert.MonitoringObjectHealthState)`n" $Result += "TIME RAISED: $($NotClosedAlert.TimeRaised)`n" $Result += "TIME LAST MODIFIED: $($NotClosedAlert.LastModified)`n" try { Set-SCOMAlert -Alert $NotClosedAlert -ResolutionState 255 $Result += "Closing of alert `"$($NotClosedAlert.Name)`"..." $Result += "-------------------------------------------------------`n`n`n" # Update of Alert History Text $NotClosedAlert .Update( "Alert closed by $ScriptName script" ) } catch { $Result += "Error during the close of alert `"$($NotClosedAlert.Name)`" (ALERT ID: $($NotClosedAlert.Id))" } } } $Result += "`n`n END `n" # Check if $Result contains error of alert closing if ($( $Result | Out-String ).Contains( "Error during the close of alert" )) { $Result += "At least one Error has been encountered during closing of some alerts" NewEventSource write-eventlog -logname "Operations Manager" -Source $ScriptName -EventID 1001 -Message $Result -EntryType Warning } Else { $Result += "`n OK - Treatment of Alerts has ending without Error" NewEventSource write-eventlog -logname "Operations Manager" -Source $ScriptName -EventID 1002 -Message $Result -EntryType Information } # Display Result $Result |
0 commentaires