From d76598e167cecc0de2113e4083ca4ced44c61466 Mon Sep 17 00:00:00 2001 From: CitadelStationBot Date: Sun, 6 Aug 2017 03:10:55 -0500 Subject: [PATCH] Adds log parsing sql email alert script --- tools/SQLAlertEmail/email_config.ps1 | 11 +++++++++++ tools/SQLAlertEmail/email_script.ps1 | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 tools/SQLAlertEmail/email_config.ps1 create mode 100644 tools/SQLAlertEmail/email_script.ps1 diff --git a/tools/SQLAlertEmail/email_config.ps1 b/tools/SQLAlertEmail/email_config.ps1 new file mode 100644 index 0000000000..53865eb15f --- /dev/null +++ b/tools/SQLAlertEmail/email_config.ps1 @@ -0,0 +1,11 @@ +$Path = '..\..\data\logs\' #Server directory up to the year folder, this can be a relative or absolute path; remember the trailing \ +$StringToMatch = 'SQL:' +$From = 'admin@server.com' +[string[]]$To = 'email@address.com', 'a_different@address.org' #Email will be sent to each address listed here, you can have as many as you want +$Subject = 'SS13 server SQL error' +$Body = 'A SQL error was found in the following files:' #This parameter is optional, set it as '' if you want it gone +#SMTP server details; If you don't have one you can use the defaults provided here for Gmail's, provided you have a Google account +$SMTPServer = 'smtp.gmail.com' +$SMTPPort = '587' +$Account = "username" #SMTP server account name, excluding the domain address (this part: @domain.com) +$Password = 'password' #SMTP server password, if you're using Gmail's and have 2-factor authentication you'll have to use an App Password (Google for how) \ No newline at end of file diff --git a/tools/SQLAlertEmail/email_script.ps1 b/tools/SQLAlertEmail/email_script.ps1 new file mode 100644 index 0000000000..1ffa8d1b44 --- /dev/null +++ b/tools/SQLAlertEmail/email_script.ps1 @@ -0,0 +1,23 @@ +<# +This is a script designed to parse through your server logs and locate any SQL errors reported. +If found an email is sent to addresses specified in the configuration file: email_config.ps1. +A SMTP server is required, if you don't have one the defaults for Gmail's can be used. + +Suggested use is to schedule this task to be executed daily at server-time midnight so all the day's logs are checked. +You will likely find it helpful to set the configuration file to be untracked by git. +#> +. .\email_config.ps1 +$Date = Get-Date -format "yyyy\\MM\\dd" +$Matches = Get-ChildItem "$Path$Date" -recurse -include *.log | Select-String "$StringToMatch" -List | Select Path, Line + +$email = New-Object System.Net.Mail.MailMessage +$email.From = $From +foreach($i in $To) {$email.To.Add($i)} +$email.Subject = $Subject +$MatchList = foreach($m in $Matches) {"`t$m`n"} +$email.Body = $Body+"`n"+$MatchList + +$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort); +$smtp.Credentials = New-Object System.Net.NetworkCredential($Account, $Password); +$smtp.EnableSSL = $true +$smtp.Send($email); \ No newline at end of file