Advertisement
Guest User

Untitled

a guest
May 24th, 2017
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.39 KB | None | 0 0
  1. Note: This code is intented for Caché 5.1 or higher.
  2.  
  3. Task Definition:
  4.  
  5. -------------------------
  6.  
  7. /// Task to automate the collection of Buttons reports, and to automate them being sent to a recipient for archival and configuration history
  8.  
  9. Class %SYS.Task.SendButtons Extends (%SYS.Task.Definition, %XML.Adaptor)
  10.  
  11. {
  12.  
  13.  
  14.  
  15. Parameter TaskName = "SendButtons";
  16.  
  17.  
  18.  
  19. Parameter XMLNAME = "ProfilerData";
  20.  
  21.  
  22.  
  23. /// if this field is filled, then the files are left in the local Archive  
  24.  
  25. Property ArchiveDirectory As %String(XMLPROJECTION = "NONE");
  26.  
  27.  
  28.  
  29. /// flag to indicate if this should be a Big Buttons; default is to collect a "Small" Buttons
  30.  
  31. Property BigButtons As %Boolean [ InitialExpression = 0 ];
  32.  
  33.  
  34.  
  35. /// used to populate the "From:" field in the email generated to send the Buttons report
  36.  
  37. Property EmailCC As %String(XMLPROJECTION = "NONE");
  38.  
  39.  
  40.  
  41. /// used to populate the "From:" field in the email generated to send the Buttons report
  42.  
  43. Property EmailFrom As %String(XMLPROJECTION = "NONE");
  44.  
  45.  
  46.  
  47. /// used to populate the "Reply-To:" field in the email generated to send the Buttons report
  48.  
  49. Property EmailReplyTo As %String(XMLPROJECTION = "NONE");
  50.  
  51.  
  52.  
  53. /// name of SMTP server to use for sending %Buttons; if this is blank, the Buttons report will not be emailed
  54.  
  55. Property SMTPServer As %String(XMLPROJECTION = "NONE");
  56.  
  57.  
  58.  
  59. /// name of SMTP Authenticated User
  60.  
  61. Property SMTPUser As %String(XMLPROJECTION = "NONE");
  62.  
  63.  
  64.  
  65. /// password of SMTP Authenitcated User
  66.  
  67. Property SMTPPass As %String(XMLPROJECTION = "NONE");
  68.  
  69.  
  70.  
  71. /// SMTP port to use
  72.  
  73. Property SMTPPort As %String(XMLPROJECTION = "NONE") [ InitialExpression = 25 ];
  74.  
  75.  
  76.  
  77. /// flag to indicate if Butttons should be emailed to the Support when collected and not associated with a WRC issue#
  78.  
  79. Property WRCHealthCheckEnabled As %Boolean(XMLPROJECTION = "NONE") [ InitialExpression = 0 ];
  80.  
  81.  
  82.  
  83. /// WRC Issue Number to be included in subject; NOTE: This is cleared after a buttons is sent with this in the Subject line, and needs to be reinitilized through the System Management Portal
  84.  
  85. Property WRCIssueNumber As %Integer(XMLPROJECTION = "NONE");
  86.  
  87.  
  88.  
  89. /// if there are any Adhocs applies as changed files only, which didn't update the $ZV string, they should be listed here
  90.  
  91. Property zInfoAdhocNotListedInZV As %String(XMLNAME = "AdhocNotListedInZV");
  92.  
  93.  
  94.  
  95. /// manual field indicating the types of backups used on this instance of Caché
  96.  
  97. Property zInfoBackupType As %String(XMLNAME = "BackupType");
  98.  
  99.  
  100.  
  101. /// manual field indicating the type and quantity of CPU(s) used on this system
  102.  
  103. Property zInfoCPUDetails As %String(XMLNAME = "CPUDetails");
  104.  
  105.  
  106.  
  107. /// manual field indicating the type of hardware that this system is running on
  108.  
  109. Property zInfoHardwareType As %String(MAXLEN = 500, XMLNAME = "HardwareType");
  110.  
  111.  
  112.  
  113. /// manual property indicates what the primary use is for this Caché Instance: Development, Testing, QA or Production
  114.  
  115. Property zInfoPrimaryPurpose As %String(DISPLAYLIST = ",Development,Testing,Quality Assurance,Production,", VALUELIST = ",Dev,Test,QA,Prod,", XMLNAME = "PrimaryPurpose");
  116.  
  117.  
  118.  
  119. /// total physical memory on the machine
  120.  
  121. Property zInfoTotalPhysicalMemory As %String(XMLNAME = "TotalPhysicalMemory");
  122.  
  123.  
  124.  
  125. /// any special notes that the customer wants included with the %Buttons
  126.  
  127. Property zzInfoNotesFromCustomer As %String(MAXLEN = 500, XMLNAME = "NotesFromCustomer");
  128.  
  129.  
  130.  
  131. ClassMethod ArchiveDirectoryIsValid(ArchiveDirectory As %String) As %Status
  132.  
  133. {
  134.  
  135.      If (ArchiveDirectory'="") {
  136.  
  137.             If '##class(%Library.File).DirectoryExists(ArchiveDirectory) Quit $$$ERROR($$$GeneralError,"Directory does not exist")
  138.  
  139.      }
  140.  
  141.      Quit $$$OK
  142.  
  143. }
  144.  
  145.  
  146.  
  147. /// This method is responsible for executing the task
  148.  
  149. /// At the scheduled time, the Task Manager
  150.  
  151. /// - creates an instance of this object,
  152.  
  153. /// - Sets any property values using the stored "Settings" for the task,
  154.  
  155. /// - and invokes this method to execute the task.
  156.  
  157. Method OnTask() As %Status
  158.  
  159. {
  160.  
  161.      If ..ArchiveDirectory'="" {
  162.  
  163.             Set Status = ##class(%SYS.Task.SendButtons).ArchiveDirectoryIsValid(..ArchiveDirectory)
  164.  
  165.             If 'Status Quit Status
  166.  
  167.      }
  168.  
  169.      If ..BigButtons {
  170.  
  171.            Set LogType = "Advanced"      
  172.  
  173.      }
  174.  
  175.      Else {
  176.  
  177.            Set LogType = "Basic"  
  178.  
  179.      }
  180.  
  181.      Set Status = $$INT^Buttons(LogType,..ArchiveDirectory)
  182.  
  183.      If $P(Status,"|",1) '= 1 Quit $$$ERROR($$$GeneralError,$P(Status,"|",2))
  184.  
  185.       Set FullFile = $P(Status,"|",2)
  186.  
  187.       // Buttons on VMS returns a trailing ";" in the filename which needs to be removed
  188.  
  189.       Set FullFile = $P(FullFile,";",1)
  190.  
  191.       // send if they have filled in the required information for Sending the report to the WRC
  192.  
  193.       If ..SMTPServer '= "",..EmailFrom '= ""  {
  194.  
  195.             Set s=##class(%Net.SMTP).%New()
  196.  
  197.             Set AuthUser=$ZSTRIP(..SMTPUser,"<>W")
  198.  
  199.             Set objAuth=""
  200.  
  201.             If AuthUser]"" {
  202.  
  203.                   #; use default authentication list
  204.  
  205.                   set objAuth=##class(%Net.Authenticator).%New()
  206.  
  207.                   Set objAuth.UserName=AuthUser
  208.  
  209.                   Set objAuth.Password=..SMTPPass
  210.  
  211.             }
  212.  
  213.             If objAuth set s.authenticator=objAuth
  214.  
  215.             Set s.smtpserver=..SMTPServer
  216.  
  217.             Set s.port=..SMTPPort
  218.  
  219.             Set m = ##class(%Net.MailMessage).%New()
  220.  
  221.             Set m.From = ..EmailFrom
  222.  
  223.             Set m.ReplyTo = ..EmailReplyTo
  224.  
  225.             Set m.Subject = "Automated sending of "_FullFile
  226.  
  227.             If ..WRCIssueNumber '="" {
  228.  
  229.                  Do m.To.Insert("support@foo.com")
  230.  
  231.                  Set m.Subject = "Buttons for WRC"_..WRCIssueNumber
  232.  
  233.            }
  234.  
  235.            Do m.To.Insert("HealthCheck@foo.com")
  236.  
  237.            If ..EmailCC'="" {
  238.  
  239.                   for i=1:1:$L(..EmailCC,",") {
  240.  
  241.                         Do m.Cc.Insert($P(..EmailCC,",",i))
  242.  
  243.                   }    
  244.  
  245.             }
  246.  
  247.             Set m.Charset="iso-8859-1"
  248.  
  249.             Set Status = ..XMLExportToString(.body,,",indent")
  250.  
  251.             Set Status = m.TextData.Write(body)
  252.  
  253.             If $$$ISERR(Status) Quit $$$ERROR($$$GeneralError,Status)
  254.  
  255.             Set dir = ##class(%File).GetDirectory(FullFile)
  256.  
  257.             Set file = ##class(%File).GetFilename(FullFile)
  258.  
  259.             Set Status = m.AttachFile(dir,file,0,"iso-8859-1")
  260.  
  261.             If $$$ISERR(Status) Quit $$$ERROR($$$GeneralError,Status)
  262.  
  263.             Set Status = s.Send(m)
  264.  
  265.             If $$$ISERR(Status) Quit $$$ERROR($$$GeneralError,Status)
  266.  
  267.       }
  268.  
  269.       //if the customer hasn't defined an Archive Directory, but they are automatically creating reports on a schedule, then delete the file
  270.  
  271.      If ..ArchiveDirectory="",..WRCHealthCheckEnabled {
  272.  
  273.            Set Status = ##class(%File).Delete(FullFile)
  274.  
  275.      }
  276.  
  277.      //clear out the WRCIssue number so the customer will need to manually enter it again
  278.  
  279.      If ..WRCIssueNumber'="" {
  280.  
  281.             Set ..WRCIssueNumber = ""
  282.  
  283.             Set ..BigButtons = 0
  284.  
  285.       }
  286.  
  287.       Quit $$$OK
  288.  
  289. }
  290.  
  291.  
  292.  
  293. }
  294.  
  295.  
  296.  
  297. -------------------------
  298.  
  299.  
  300.  
  301. Initialization Routine (using %SYS.Task which inherits from %SYS.TaskSuper):
  302.  
  303. ------------------------
  304.  
  305. addbuttonstask
  306.  
  307.       write !,"This routine will initialize an automated Buttons task on this system.",!
  308.  
  309.       Write "This is intended for use with Cache 5.1 and greater",!,!
  310.  
  311.  
  312.  
  313.       Write "---  Adding a Task called 'Send Buttons' ---",!
  314.  
  315.       Write "This task can be used to email a Buttons report to Support.",!
  316.  
  317.       Write "If the Buttons is for an open ticket, prior to running this task, fill in",!
  318.  
  319.       Write "the WRCIssueNumber field in the task properties through the System",!
  320.  
  321.       Write "Management Portal.",!,!
  322.  
  323.      
  324.  
  325.       ///add task if not already found in schedule
  326.  
  327.       &sql(select Name from %SYS.Task where Name like 'Send Buttons')
  328.  
  329.       ///if not present add task
  330.  
  331.       if (SQLCODE'=0) {
  332.  
  333.            set Task=""
  334.  
  335.            Set Task=##Class(%SYS.Task).%New()
  336.  
  337.            Set Task.Name="Send Buttons"
  338.  
  339.            Set value=$$STRINGOPTION("Do you want to schedule Buttons to be collected on a regular basis","Y",1)
  340.  
  341.            If ($ZCONVERT(value,"L")="y")||($ZCONVERT(value,"L")="yes")||(value=$C(0)){
  342.  
  343.                  Set Task.TimePeriod=0
  344.  
  345.                  Set value=$$STRINGOPTION("Collect Buttons once every how many days","14",1)
  346.  
  347.                  Set Task.TimePeriodEvery=value
  348.  
  349.            }
  350.  
  351.            else {
  352.  
  353.                  Set Task.TimePeriod=5              
  354.  
  355.            }
  356.  
  357.            Set Task.TimePeriodDay=""
  358.  
  359.            Set Task.Description="Send Buttons to Support On Demand, and/or on a schedule"
  360.  
  361.            Set Task.TaskClass="%ZSYS.Task.SendButtons"
  362.  
  363.            Set Task.RescheduleOnStart=1
  364.  
  365.  
  366.  
  367.            Set objTaskClass=$zobjclassmethod(Task.TaskClass,"%New")
  368.  
  369.            Do objTaskClass.SetSettings(Task.Settings)
  370.  
  371.            Set Settings=objTaskClass.GetSettings()
  372.  
  373.            
  374.  
  375.            For i=1:2:($ll(Settings)) {
  376.  
  377.                  Set name=$li(Settings,i)
  378.  
  379.                  Set value=$li(Settings,i+1)
  380.  
  381.                  Set prompt=""
  382.  
  383.                  If name="WRCHealthCheckEnabled" {
  384.  
  385.                        Write !,!,"---  WRC Health Check  ---",!
  386.  
  387.                        Write "Your system can be configured to automatically send your periodically collected",!
  388.  
  389.                        Write "Buttons reports to HealthCheck@foo.com.",!,!
  390.  
  391.                        
  392.  
  393.                        Set value=$$STRINGOPTION("Do you want to automatically send in Buttons reports","Y",1)
  394.  
  395.                        If ($ZCONVERT(value,"L")="y")||($ZCONVERT(value,"L")="yes")||(value=$C(0)){
  396.  
  397.                              Set $li(Settings,i,i+1)=$lb(name,1)
  398.  
  399.                        }
  400.  
  401.                        else {
  402.  
  403.                              Set $li(Settings,i,i+1)=$lb(name,0)
  404.  
  405.                        }
  406.  
  407.                        continue
  408.  
  409.                  }    
  410.  
  411.                  Elseif name="ArchiveDirectory" {
  412.  
  413.                        Set prompt="If you want these Buttons archived, what directory should they be stored in"
  414.  
  415.                  }
  416.  
  417.                  ElseIf name="EmailCC" {
  418.  
  419.                        Set prompt="Additional email addresses to CC: for outgoing emails (comma separated)"
  420.  
  421.                  }    
  422.  
  423.                  ElseIf name="EmailFrom" {
  424.  
  425.                        Set prompt="Address for the 'From:' field for outgoing emails"
  426.  
  427.                  }    
  428.  
  429.                  ElseIf name="SMTPServer" {
  430.  
  431.                        Set prompt="What SMTP Server should be used for outgoing emails"
  432.  
  433.                  }    
  434.  
  435.                  ElseIf name="zInfoAdhocNotListedInZV" {
  436.  
  437.                        Write !,!,"---  Additional Information for ISC Support  ---",!
  438.  
  439.                        Write "Answer these questions to give Support a better understanding of your system:"
  440.  
  441.                        Set prompt="What (if any) Adhocs are applied that do not show up in $ZV"
  442.  
  443.                  }    
  444.  
  445.                  ElseIf name="zInfoBackupType" {
  446.  
  447.                        Set prompt="What kind of Backup is used on this system (Cache, External, OS, etc)"
  448.  
  449.                  }    
  450.  
  451.                  ElseIf name="zInfoHardwareType" {
  452.  
  453.                        Set prompt="What kind of hardware is Cache running on (as much detail as possible)"
  454.  
  455.                  }    
  456.  
  457.                  ElseIf name="zInfoPrimaryPurpose" {
  458.  
  459.                        Set prompt="What is the Primary Purpose of this Cache Instance (Dev/Test/QA/Prod)"
  460.  
  461.                  }    
  462.  
  463.                  ElseIf name="zInfoTotalPhysicalMemory" {
  464.  
  465.                        Set prompt="What is the total physical memory on this machine"
  466.  
  467.                  }    
  468.  
  469.                  ElseIf name="zzInfoNotesFromCustomer" {
  470.  
  471.                        Set prompt="What else would you like ISC Support to know about this Cache instance"
  472.  
  473.                  }
  474.  
  475.                  if prompt '="" {
  476.  
  477.                         f  {
  478.  
  479.                               Set value=$$STRINGOPTION(prompt,value,1) q:value=$C(0)
  480.  
  481.                               Set status=$zobjmethod(objTaskClass,name_"IsValid",value)
  482.  
  483.                               Continue:'status
  484.  
  485.                              Set $li(Settings,i,i+1)=$lb(name,value)
  486.  
  487.                              Quit
  488.  
  489.                        }
  490.  
  491.                  }
  492.  
  493.            }
  494.  
  495.            Do objTaskClass.SetSettings(Settings)
  496.  
  497.            Set Settings=objTaskClass.GetSettings()
  498.  
  499.            Set Task.Settings=Settings
  500.  
  501.            Set Status=Task.%Save()
  502.  
  503.            if Status {
  504.  
  505.                  write !,!,"Added Send Buttons task to schedule",!,!
  506.  
  507.                  Set value=$$STRINGOPTION("Do you want to run a test Buttons report now","Y",1)
  508.  
  509.                  If ($ZCONVERT(value,"L")="y")||($ZCONVERT(value,"L")="yes")||(value=$C(0)){
  510.  
  511.                        set Status = ##class(%SYS.Task).RunNow(Task.%Id())
  512.  
  513.                        if Status {
  514.  
  515.                              Write !,"Task 'Send Buttons' was started successfully.",!
  516.  
  517.                              Write "Please check the Task History in the SysMgtPtl to confirm that it completd.",!}
  518.  
  519.                        else {Write !,"Failed to start 'Send Buttons' task",!}
  520.  
  521.                  }
  522.  
  523.                  Set Task=""
  524.  
  525.            }
  526.  
  527.            else { write !,!,"Failed to add Send Buttons task",! }
  528.  
  529.      } else {
  530.  
  531.            write !,!,"Task Send Buttons found already present",!
  532.  
  533.      }
  534.  
  535.      
  536.  
  537.      Quit
  538.  
  539. quitRoutine
  540.  
  541.      w !,"Exiting Routine without creating Task",!
  542.  
  543.      q
  544.  
  545.  
  546.  
  547. STRINGOPTION(prompt,default,allownull,same) [] {
  548.  
  549. s $zt="STRINGOPTIONE"
  550.  
  551. STRINGOPTION1 w !,prompt_"? " i default'="" w default_" => "
  552.  
  553.  s same=0
  554.  
  555.  r Option i Option="",(default'="") s same=1 q default
  556.  
  557. i Option="",(allownull=0) w !,$get(^%qCacheMsg("%Utility",$select($isObject($get(%response)):%response.MatchLanguage(),1:$get(^%SYS("LANGUAGE","CURRENT"),"en")),"1380879397"),"Please enter a non-null value") g STRINGOPTION1
  558.  
  559. q Option
  560.  
  561. STRINGOPTIONE s $zt=""
  562.  
  563. i $ze'["<INTERRUPT>" w !,$$FormatText^%occMessages($get(^%qCacheMsg("%Utility",$select($isObject($get(%response)):%response.MatchLanguage(),1:$get(^%SYS("LANGUAGE","CURRENT"),"en")),"1936651305"),"ERROR: %1"),$ZE)
  564.  
  565.  q $C(0)
  566.  
  567. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement