mirror of
https://github.com/fulpstation/fulpstation.git
synced 2025-12-10 10:01:40 +00:00
adds simple script to parse logs for sql errors and send email
This commit is contained in:
11
tools/SQLAlertEmail/email_config.ps1
Normal file
11
tools/SQLAlertEmail/email_config.ps1
Normal file
@@ -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)
|
||||||
23
tools/SQLAlertEmail/email_script.ps1
Normal file
23
tools/SQLAlertEmail/email_script.ps1
Normal file
@@ -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);
|
||||||
Reference in New Issue
Block a user