Commits

Randle Taylor committed bab1878

reconfigure with a settings.ps1 file

Comments (0)

Files changed (6)

+*~
+expected_files.txt
+log.txt
+# Beam Model Checksum Script #
+
+This folder contains two PowerShell scripts used to verify that the
+Monaco beam model is not changing.  The script is currently run nightly
+ using the Windows Task scheduler.  For more information
+please talk to Janet Hendry or Randy Taylor
+
+The two scripts are as follows:
+
+1. gen\_ref\_data.ps1 crawls all monaco\rtp1\\*\tele\ directories,
+calculates their md5 hash and outputs that to expected\_files.txt.  Any
+time a legitimate change is made to a beam model, this script needs to
+be run to update the MD5 hashes of the files.  This script can take
+half an hour or more to run.  For information on configuring it, see
+the gen\_ref\_data.ps1 file.
+
+2. check\_bm.ps1 which crawls the same directory and compares the md5
+hashes with the reference versions found in expected\_files.txt. If any
+changes are found (including new files, deleted files or modified
+files) an email is sent to the recipients specified in the script.
+Currently this script is automatically run nightly using the Windows
+task scheduler.  It can also be run on demand using PowerShell or from
+the Windows Task Scheduler.
+
+Output from each run is written to log.txt. The log file contains information
+about when the script was run, how many files were checked and the names of
+any files that were changed.
+
+The file run_script.bat can be used by the Windows Task Scheduler to schedule
+nightly checks of the beam model data. The start time, finish time and any
+error output from the scheduled run will be logged in run_log.txt
+
+
+
+Randy Taylor(rataylor@toh.on.ca) - 1 Oct 2012
+
+#Randy Taylor(rataylor@toh.on.ca) - 1 Oct 2012
+# This script crawls all of the rtp1/*/tele subdirectories of the Monaco beam model
+# directories and compares thir MD5 hashes to those found in expected_files.txt
+# If a difference in the file checksums are found, the users specified in the 
+# $email_recipients array below will be notified.
+#
+# If a beam model is updated re-run gen_ref_data.ps1 to generate new reference data
+#
+# To configure, 1) set $rtp_root below to the location of the live Monaco rtp1 beam model 
+# directory (e.g. \\your_server_here\Monaco\rtp1\) and 2) 
+
+. .\settings.ps1
+
+$ref_data_file = $script_root+"expected_files.txt"
+$log_file = $script_root+"log.txt"
+
+
+$smtp = New-Object Net.Mail.SmtpClient($smtp_host)	
+
+if (!(Test-Path $rtp_root)){
+	Add-Content $log_file "`nunable to access $rtp_root"
+	$smtp.Send($from_address,$admin_emails,"beam script unable to access rtp1 directory","$rtp_root")	 
+	Exit
+}
+
+$start_time = Get-Date;
+
+$bm_search_path = $rtp_root + "*\tele\" 
+$tele_files = Get-ChildItem $bm_search_path -Recurse | Where-Object {!$_.PSIsContainer}
+
+$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
+$enc = [system.Text.Encoding]::UTF8
+
+$deleted_files = @()
+$changed_files = @()
+$new_files = @()
+
+$file_hashes = Import-Csv -Delimiter "|" $ref_data_file
+$all_files = @()
+$changed_clinics = @{}
+
+foreach ($fhash in $file_hashes){
+	
+    $all_files += $fhash.file
+    $clinic = $fhash.file.Substring($fhash.file.IndexOf("rtp")).Split("\")[1]
+    if (!(Test-Path $fhash.file)){
+        $changed_files += $fhash.file
+        
+        if ($changed_clinics.ContainsKey($clinic)){
+            $changed_clinics[$clinic] += 1                    
+        }else{
+            $changed_clinics.set_Item($clinic,1)
+        }
+        
+    }else{
+        #hash of file to compare to reference
+	    $cmp_file_hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($fhash.file)))
+        
+        if ($fhash.md5 -ne $cmp_file_hash){
+			$changed_files += $fhash.file
+
+            if ($changed_clinics.ContainsKey($clinic)){
+                $changed_clinics[$clinic] += 1                    
+            }else{
+                $changed_clinics.set_Item($clinic,1)
+            }
+		}        
+    }	
+}
+
+$changed = $changed_files -join "`n"
+$changed_count = $changed_files.Length
+
+$new_count = $tele_files.Length - $file_hashes.Length
+
+if ($new_count -gt 0){
+    foreach ($tele_file in $tele_files){
+        if ($all_files -notcontains $tele_file.Fullname){
+            $new_files += $tele_file.Fullname
+        }
+    }
+}
+$new = $new_files -join "`n"
+
+$total_count = $tele_files.Length
+$end_time = Get-Date;
+
+# log results
+$log_contents = "`n$start_time; $end_time; Compared $total_count files; $changed_count Changed files:`n $changed;`n$new_count New files:`n $new;"
+Add-Content $log_file $log_contents
+
+
+# email results if necessary
+$email_contents = "`n$start_time; $end_time; Compared $total_count files; $changed_count Changed files; $new_count New files;`nEffected Clinics:"
+
+foreach ($key in $changed_clinics.Keys){
+    $clinic_count = $changed_clinics[$key]
+    $email_contents += "`n$key : $clinic_count files"
+}
+$email_contents += "`nDetails available in the logfile at $log_file"
+
+$smtp = New-Object Net.Mail.SmtpClient($smtp_host)	
+
+if (($changed_count -gt 0) -or ($new_count -gt 0) ){	
+    $recipients = $email_recipients -join ", "	
+    $smtp.Send($from_address,$recipients, "Change to beam files","$email_contents")
+}
+
+if ($always_email_admins -gt 0){
+    $admin_recipients = $admin_emails -join ", "
+	$smtp.Send($from_address,$admin_recipients,"beam script finished running","$email_contents")	 
+}
+# This script will crawl all of the tele subdirectories of the Monaco beam model
+# directories and output their MD5 hashes to expected_files.txt
+#
+# Configuration is done via the settings.ps1 file
+
+. .\settings.ps1
+
+#==============================================================================================
+$ref_data_file = $script_root+"expected_files.txt"
+
+$delim = "|"
+
+if (Test-Path $ref_data_file){
+    $date = Get-Date;
+    $old_ref =  "$ref_data_file-" + $date.ToShortDateString().Replace("/",".") 
+    mv $ref_data_file $old_ref
+}
+
+
+$bm_search_path = $rtp_root + "*\tele\" 
+$tele_files = Get-ChildItem $bm_search_path -Recurse | Where-Object {!$_.PSIsContainer}
+
+$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
+$enc = [system.Text.Encoding]::UTF8
+
+$stream = [System.IO.StreamWriter] $ref_data_file
+$header = "file"+ $delim + "md5" 
+
+$stream.WriteLine($header)
+
+foreach ($fname in $tele_files){	
+	
+	$cmp_file_hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($fname.FullName)))
+    $data = $fname.FullName + $delim + $cmp_file_hash
+	$stream.WriteLine($data)
+
+}
+
+$stream.Close()
+
+echo Started %Date% %Time% >> C:\scripts\beam_checksum_script\runlog.txt
+powershell.exe -ExecutionPolicy Unrestricted C:\Scripts\beam_checksum_script\check_bm.ps1 >> C:\scripts\beam_checksum_script\runlog.txt
+echo Finished %Date% %Time% >> C:\scripts\beam_checksum_script\runlog.txt
+# set rtp_root to the full path of the location of the Monaco\rtp1\ directory
+$rtp_root = "\\your_server_here\Monaco\rtp1\" #include trailing slash on rtp path
+
+# set the $script_root to the directory where the expected_files.txt lives.  
+# this is also where the log files will be written.
+$script_root = "\\your_other_server_here\scripts\beam_checksum_script\" #include trailing slash on script_root
+
+# users that should be emailed whenever a change occurs
+$email_recipients =  @("user1@toh.on.ca","user2@toh.on.ca","user3@toh.on.ca")
+
+# users to be mailed everytime the script is run (if always_email_admins is not 0)
+$admin_emails = @("user1@toh.on.ca","user2@toh.on.ca")
+$always_email_admins = 1  #set to 1 to email admins on every run. Set to 0 to disable 
+
+# email will be shown as being sent from this email address
+$from_address = "beamscript@toh.on.ca"
+
+# SMTP Email Server Address
+$smtp_host= "YOURSMTPSERVER"
+