Národní úřad pro kybernetickou a informační bezpečnost vydal varování před kybernetickými hrozbami cílenými zejména na české...
Vlastní monitorovací skript v PowerShellu
Monitorování serverů je důležitou součástí každé infrastruktury. Pro základní (a zdarma dostupný) monitoring by mohl posloužit například i jednoduchý skript v PowerShellu spouštěný automaticky pomocí plánovače.
Napsat takový skript není nic složitého. Navíc již existuje hromada hotových řešení, kde si stačí vybrat to nejvhodnější pro vlastní potřeby. Databáze (nejen) PowerShell skriptů je například i na webu Technet, kde jsem se inspiroval i já a jeden takový skript si upravil k obrazu svému.
Skript čte adresy serverů z externího souboru a pokud narazí na nedostupný server, zašle notifikaci na uvedený e-mail. Celý skript je okomentovaný, takže asi není nutné to důkladně rozebírat. Skript i s doprovodným souborem je ke stažení zde. Původní skript i s komentářem autora zde.
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 | # reset the lists of hosts prior to looping $OutageHosts = $Null # specify the time you want email notifications resent for hosts that are down $EmailTimeOut = 720 # specify the time you want to cycle through your host lists. $SleepTimeOut = 30 # specify the maximum hosts that can be down before the script is aborted $MaxOutageCount = 15 # specify who gets notified # specify where the notifications come from # specify the SMTP server $smtpserver = "smtp.domain.com" Do{ $available = $Null $notavailable = $Null # Read the File with the Hosts every cycle, this way to can add/remove hosts # from the list without touching the script/scheduled task, # also hash/comment (#) out any hosts that are going for maintenance or are down. get-content servers.txt | Where-Object {!($_ -match "#")} | ForEach-Object { if(Test-Connection -ComputerName $_ -Count 1 -ea silentlycontinue) { } else { if(!(Test-Connection -ComputerName $_ -Count 4 -ea silentlycontinue)) { # If the host is still unavailable for 4 full pings, write error and send email [Array]$notavailable += $_ if ($OutageHosts -ne $Null) { if (!$OutageHosts.ContainsKey($_)) { # First time down add to the list and send email $OutageHosts.Add($_,(get-date)) $Now = Get-date $Body = "$_ has not responded for 5 pings at $Now" Send-MailMessage -Body "$body" -to $notificationto -from $notificationfrom ` -Subject "Host $_ is down" -SmtpServer $smtpserver } else { # If the host is in the list do nothing for 1 hour and then remove from the list. if (((Get-Date) - $OutageHosts.Item($_)).TotalMinutes -gt $EmailTimeOut) {$OutageHosts.Remove($_)} } } else { # First time down create the list and send email $OutageHosts = @{$_=(get-date)} $Body = "$_ has not responded for 5 pings at $Now" Send-MailMessage -Body "$body" -to $notificationto -from $notificationfrom ` -Subject "Host $_ is down" -SmtpServer $smtpserver } } } } sleep $SleepTimeOut if ($OutageHosts.Count -gt $MaxOutageCount) { # If there are more than a certain number of host down in an hour abort the script. $Exit = $True $body = $OutageHosts | Out-String Send-MailMessage -Body "$body" -to $notificationto -from $notificationfrom ` -Subject "More than $MaxOutageCount Hosts down, monitoring aborted" -SmtpServer $smtpServer } } while ($Exit -ne $True) |