<?xml version="1.0" encoding="UTF-8"?>
<Export generator="Cache" version="25" zv="Cache for Windows (x86-64) 2017.1 (Build 792U)" ts="2017-05-08 11:57:58">
<Class name="Webinars.Sample.SampleOSFreeSpaceMonitor">
<Super>%SYS.Task.Definition</Super>
<TimeChanged>64411,43062.279443</TimeChanged>
<TimeCreated>64406,38414.396379</TimeCreated>

<Parameter name="TaskName">
<Description>
Enter the drive you wish to monitor; i.e. "C:\" or "D:\" drive.
Also enter the freespace threshold before emails are sent.
Freespace thresholds should be entered without commas.
reminder:  One megabyte = 1,048,576 bytes
reminder:  One gigabyte = 1,073,741,824 bytes
reminder:  Ten gigabytes = 10,737,418,240 bytes</Description>
<Default>Webinar Sample OS Free Space Monitor</Default>
</Parameter>

<Property name="DirectoryToMonitor">
<Type>%String</Type>
</Property>

<Property name="FreeSpaceThresholdInGigaBytes">
<Type>%String</Type>
</Property>

<Property name="EmailRecipientList">
<Type>%String</Type>
<Parameter name="MAXLEN" value="500"/>
</Property>

<Property name="SMTPServer">
<Type>%String</Type>
</Property>

<Property name="SystemName">
<Type>%String</Type>
</Property>

<Property name="FromEmailAddress">
<Type>%String</Type>
</Property>

<Property name="AutoSendEmail">
<Type>%Boolean</Type>
</Property>

<Method name="OnTask">
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
	
	//For this task to function, the following QUIT must be removed. 
	//The QUIT is included to ensure that you read, edit, and test this code before using.	
	QUIT
	
		
	//call the DiskSizeAvail Method, return the status
	set tsc = ..DiskSizeAvail(..DirectoryToMonitor,..FreeSpaceThresholdInGigaBytes,..EmailRecipientList,..SMTPServer,..AutoSendEmail,..SystemName,..FromEmailAddress)
	Quit tsc
]]></Implementation>
</Method>

<Method name="DiskSizeAvail">
<FormalSpec>sDrive:%String,sFreeSpaceThreshold:%Integer,sEmailRecipientList:%String,sSMTPServer:%String,sAutoSendEmail:%String,sSystemName:%String,sFromEmailAddress:%String</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[

	
	
	
	//Set variables with email text. Allows daily email to be different than warning email.
	If sAutoSendEmail = 1 {
	
	Set subjectT0 = ""
	Set subjectT1 = " Daily Free Sace Monitor: "	
	Set subjectT2= " gigabytes available"
	Set bodyT0=""
	Set bodyT1 = " daily free space monitor. You have "
	Set bodyT2 = " gigabytes of free space available."

	}
	Else {
	
	Set subjectT0 = "WARNING! "
	Set subjectT1 = " Free Space Monitor: only "	
	Set subjectT2= " gigabytes available"
	Set bodyT0 = "Warning! " 
	Set bodyT1 = " free space monitor. You have only "
	Set bodyT2 = " gigabytes of free space available."
		
	}
	
	
	do ##class(%Library.File).GetDirectorySpace(sDrive,.free,.total,2)

	//Now we have disk space in 'free' variable

	
	//Evaluate and send email is AutoSendEmail = 1 or freespace is below threshold
	
	if ((free < sFreeSpaceThreshold) || (sAutoSendEmail = 1)) {
		
	//**************************************************************************************
	//Instantiate email message
	Set msg = ##class(%Net.MailMessage).%New()
    set msg.From = ..FromEmailAddress
    // loop through recipient list, comma separated, and insert recipients
    SET delim=","
    SET countdown=$LENGTH(sEmailRecipientList,delim)
    SET countup=1
    FOR reps=countdown:-1:1 {
      SET extract=$PIECE(sEmailRecipientList,delim,countup)
      Do msg.To.Insert(extract)
      SET countup=countup+1
    }
    
    //convert free space to GB
    set sRetvalInGB = free
    set sRetvalInGB = $NORMALIZE(sRetvalInGB,3)
    Set msg.Subject=subjectT0_sSystemName_subjectT1_sRetvalInGB_subjectT2
    Set msg.IsBinary=0
    Set msg.IsHTML=0
    Do msg.TextData.Write(bodyT0_sSystemName _bodyT1_sRetvalInGB_bodyT2)
    Set server=##class(%Net.SMTP).%New()
    Set server.smtpserver=sSMTPServer
    //HotPOP SMTP server uses the default port (25)
    Set server.port=25
    //Create object to carry authentication
    Set auth=##class(%Net.Authenticator).%New()
    Set auth.UserName=sFromEmailAddress
    Set auth.Password=""
    Set server.authenticator=auth
    Set server.AuthFrom=sFromEmailAddress
    Set tSC = ..SendMessage(server,msg)
    //END OF SEND EMAIL CODE
    //**************************************************************************************
	}
	//If here, AutoSendEmail is false and the threshold has not been exceeded, so simply quit with status = 1
	else
	{
		set tSC = 1
	}
	

	QUIT tSC
]]></Implementation>
</Method>

<Method name="SendMessage">
<ClassMethod>1</ClassMethod>
<FormalSpec>server:%Net.SMTP,msg:%Net.MailMessage</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
  Set From=server.authenticator.UserName
  //make sure From: user is same as used in authentication
  Set msg.From = From
  
  //Send the email
  Set status=server.Send(msg)
   
 
  If $$$ISERR(status) 
  {
    Do $System.Status.DisplayError(status)
    Write server.Error
    Quit $$$ERROR()
    }
  Quit $$$OK
]]></Implementation>
</Method>
</Class>
</Export>
