Friday, September 27, 2013

DFSR canary script

Param(
    [parameter(Mandatory=$true)]
    [alias("share")]
    $shareName,
    [alias("timeout")]
    $copyTimeout)

$emailTo = "status@example.com" # Recipient of notification e-mails
$emailFrom = "status@example.com" # E-mail address notifications will come from
$smtpServer = "smtp.example.com" # Your SMTP server
$testDir = "DFSTest" # This is the directory that the test file will be created in
$successMail = 1 # Set to 0 for no e-mail on 100% success or 1 to get report on success or fail

# List of servers to check for files

$serverList = @(
"fileserver1", 
"fileserver2", 
"fileserver3", 
"fileserver4")

if($copyTimeout -eq $null) {
$copyTimeout = 300 # Set the default timeout if one is not specified on the command line
}

$repFail = 0

$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = $emailFrom
$msg.ReplyTo = $emailFrom
$msg.To.Add($emailTo)
$msg.Body = "Copy status messages:`n" # Apparently Powershell uses backticks for escaping characters

# Copy file to remote servers and verify replication

ForEach($sourceServer in $serverList) {
$dateTime = Get-Date
$curDate = $dateTime.Month, $dateTime.Day, $dateTime.Year
$curTime = $dateTime.Hour.ToString("00"), $dateTime.Minute.ToString("00")

$strFullPath = "\\$sourceServer\$shareName\$testDir\"
$strFileName = "DFSTest_" + [string]::join('-', $curDate) + "_" + [string]::join('',$curTime) + "_$sourceServer.txt"

$msg.Body = $msg.Body + "`nCopying $strFileName from $sourceServer`n"
# Create a new text file on the current server in the list
New-Item $strFullPath$strFileName -type file | Out-Null
"Test data from $sourceServer" >> $strFullPath$strFileName # Write some data to the text file

ForEach($server in $serverList) {
$timer = 0
$msg.Body = $msg.Body + "Checking $server for file..."
$testFileName = "\\$server\$shareName\$testDir\$strFileName"
Do {
If(Test-Path "$testFileName") {
$msg.Body = $msg.Body + "SUCCESS: $testFileName exists ($timer seconds)`n"
break # If the file is there break out of the loop
}
$timer = $timer + 1
Start-Sleep -s 1 # Time is in seconds
} While($timer -ne $copyTimeout) # Had to use this method because I couldn't find a way to test for not exist
if($timer -eq $copyTimeout) {
$msg.Body = $msg.Body + "FAIL: Copy to $shareName on $server did not occur within $copyTimeout seconds`n"
$repFail = $repFail + 1
}
}

# Delete the file from the server and verify it disappears

Remove-Item $strFullPath$strFileName | Out-Null

ForEach($server in $serverList) {
$timer = 0
$msg.Body = $msg.Body + "Checking $server for file removal..."
$testFileName = "\\$server\$shareName\$testDir\$strFileName"
While (Test-Path "$testFileName") {
$timer = $timer + 1
Start-Sleep -s 1 # Time is in seconds
if($timer -eq $copyTimeout) {
$msg.Body = $msg.Body + "FAIL: Delete from $shareName on $server did not occur within $copyTimeout seconds`n"
$repFail = $repFail + 1
break
}
}
if($timer -ne $copyTimeout) {
$msg.Body = $msg.Body + "SUCCESS: $testFileName deleted ($timer seconds)`n"
}
}
}

if($repFail -ne 0) {
if($repFail -gt 1) {
$processes = "processes" 
} else {
$processes = "$process"
}
$msg.Subject = "WARNING: DFS replication to $shareName failed $repFail $processes"
} else {
$msg.Subject = "DFSR replication successful for $shareName ($copyTimeout second threshold)"
}

if ($successMail -eq 1 -or $repFail -ne 0) {
$smtp.Send($msg)
}

No comments:

Post a Comment