Creating automation script for cron task

This topic describes on how to create an automation script for the cron task to pull created provision requests status from mangeIQ and update the SR’s.

  1. Open the Automation Scripts application.
  2. On the navigation panel, click Create and select Script.
  3. On the Create Script pop-up, fill the fields as follows:
    • Script: Specify a name, for example, CRONSCRIPT and description as necessary.
    • Script Language: Search and select jython
    • Log Level: Search and select Debug
    • Version: Enter version, for example, 0.0
    • You can import the below script or paste it on the below script box, and click Create.
      Note: The container path used in the following script is a sample and for reference only - //opt//ibm//wlp//usr//servers//defaultServer//apps. Enter an appropriate path as per the container in use.
      # LOGO REST Client - Maximo Company to Logo Tiger Current Account
      from com.ibm.json.java import JSONObject, JSONArray
      from psdi.iface.router import Router
      from psdi.iface.mic import EndPointCache
      from java.io import BufferedReader, IOException, InputStreamReader
      from java.lang import System, Class, String, StringBuffer
      from java.nio.charset import Charset
      from java.util import Date, Properties, List, ArrayList ,HashMap , Calendar
      from psdi.app.ticket import TicketRemote
      
      from org.apache.commons.codec.binary import Base64
      from org.apache.http import HttpEntity, HttpHeaders, HttpResponse, HttpVersion
      from org.apache.http.client import ClientProtocolException, HttpClient
      from org.apache.http.client.entity import UrlEncodedFormEntity
      from org.apache.http.client.methods import HttpPost
      from org.apache.http.entity import StringEntity
      from org.apache.http.impl.client import DefaultHttpClient
      from org.apache.http.message import BasicNameValuePair
      from org.apache.http.params import BasicHttpParams, HttpParams, HttpProtocolParamBean
      
      from psdi.mbo import Mbo, MboRemote, MboSet, MboSetRemote,MboConstants,SqlFormat
      from psdi.mbo import MboSetEnumeration
      
      from psdi.security import UserInfo
      from psdi.server import MXServer
      
      from sys import *
      from org.python.core import PyTuple
      
      def getProvisionDetails(id):
          System.out.println('--------------IN getProvisionDetails()------------')
      
          credential_to_use = "MANAGEIQ_AUTH"
          handler = Router.getHandler(credential_to_use)
          username = handler.getUserName();
          endPointInfo = EndPointCache.getInstance().getEndPointInfo(credential_to_use)
          password  = endPointInfo.getProperty("PASSWORD").getValue()
      
          url = handler.getUrl() + "/api/provision_requests/" + id
      
          query = "_format=json"
          System.out.println('Get Deatils of Provision Request::'+ url)
          System.out.println('query'+query)
          paramMap = {"URL":url, "username":username, "password":password}
          result = String(handler.invoke(paramMap, query))
      
          jsnResp = JSONObject.parse(result)
          System.out.println("response::")
          System.out.print(jsnResp)
      
          return jsnResp
      
      
      # Main part
      
      try:
        System.out.println('--------------START MANAGEIQ CRON SCRIPT------------')
        newData = ""
        # read mapping file
        with open('//opt//ibm//wlp//usr//servers//defaultServer//apps', 'r') as myfile:
              data=myfile.read()
        if(len(data)>0):
            data = data[1:]
            System.out.println('Data::'+ str(data))
            arr = data.split(",")
            for request in arr:
              srId,srReq = request.split(':')
              System.out.println('--------------TicketID::'+srId+"   request:"+srReq)
              srSet = MXServer.getMXServer().getMboSet('SR', runAsUserInfo)
              srSet.setWhere("TicketID = '" + srId + "' and STATUS= 'INPROG'")
              srSet.reset()
              if(srSet.count()==1):
                  jsnResp = getProvisionDetails(srReq)
                  status = jsnResp.get("request_state")
                  message = jsnResp.get("message")
                  details = "Working on the request"
                  System.out.println("Status::"+status+" Message::"+message)
                  srMbo = srSet.moveFirst()
                  if(status == 'finished'):
                      System.out.println('Working on SR TicketID:'+srMbo.getString("TicketID"))
                      srMbo.setValue("STATUS", "RESOLVED",MboConstants.NOACCESSCHECK)
                      srMbo.setValue("DESCRIPTION_LONGDESCRIPTION",srMbo.getString("DESCRIPTION_LONGDESCRIPTION")+ "\n       "+message ,MboConstants.NOACCESSCHECK)
                      System.out.println('SR updated successfully with Worklog')
                      details = message
                  else :
                      newData = newData + "," + request
                  worklogSet = srMbo.getMboSet("WORKLOG")
                  worklogMbo = worklogSet.add()
                  worklogMbo.setValue("description", "CronTask",MboConstants.NOACCESSCHECK)
                  worklogMbo.setValue("description_longdescription", details,MboConstants.NOACCESSCHECK)
                  worklogMbo.setValue('CLIENTVIEWABLE', 1, MboConstants.NOACCESSCHECK)
                  worklogSet.save()
                  srSet().save()
                  srSet.clear()
                  srSet.close()        
            with open('//opt//ibm//wlp//usr//servers//defaultServer//apps', "w") as myfile1:
              myfile1.write(newData)      
        System.out.println('--------------END MANAGEIQ CRON SCRIPT------------')
      
      except:
        System.out.println('Error in script ')

What to do next

Once you created the CRONSCRIPT above, follow below step and create a cron task to run the CRONSCRIPT.
  1. In the Cron Task Setup application, create a new cron task definition.
  2. In the Class field, enter com.ibm.tivoli.maximo.script.ScriptCrontask.
  3. Create the cron task instance that runs the automation script.
    1. In the Cron Task Instances section, add a row.
    2. Specify the cron task instance name.
    3. Set the schedule for when the cron task is run.
    4. Select Run as MAXADMIN.
    5. Select the Active check box to activate the cron task instance.
  4. In the Parameters tab, in the SCRIPTARG row, specify a value that represents an argument for the cron task script such as c:\tmp\jout.txt..
  5. In the SCRIPTNAME row, specify CRONSCRIPT (the name of the script that you created above to run as part of the cron task). SCRIPTNAME is a mandatory parameter that must be populated. The script name that you specify is passed to the automation script in the form of the scriptName implicit variable.
  6. Save the record.