10 Replies Latest reply on Jun 7, 2018 11:54 AM by Bill Robinson

    How can I get the output of an NSH job in the REST API response?

    Siddhesh Bakkar
      Share:|

      Hi All,

       

      I am trying to execute a NSH job to check the uptime of a server through BSA REST APIs. I have 2 questions in this :

       

      (a) I am able to successfully execute the job against a particular target server.

       

      But the REST output response only contains the "OperationResultResponse" as shown below :

       

      <?xml version='1.0' encoding='UTF-8'?>

      <RESTXMLResponse>

          <OperationResultResponse>

              <OperationResult type="Statuses" value="/id/SystemObject/Job/NSH Script Job/0f2add4f-fa8d-45a3-bd05-808582f58156/Statuses/Schedule2003701" />

          </OperationResultResponse>

      </RESTXMLResponse>

       

      I know that I can check the job execution status by doing a GET on the above value. But how can I have the output of this job in this REST response itself ??

       

      (b) Further, here I would have to do multiple REST requests :

       

      1) To GET the URI of the target server

      2) POST the execution of the NSH job by passing the URI obtained above as an argument

      3) Check the status of the job by doing a GET on the reponse value

       

      Is there a way to perform the above activities in a single REST request ??

       

      BSA Version : 8.7 Patch 3

        • 1. Re: How can I get the output of an NSH job in the REST API response?
          Bill Robinson
          But how can I have the output of this job in this REST response itself ??

          did you try looking for jobrun log rest here ?  it's the first result:

          BSA: How to get job run logs using restful webservice in BSA?

           

           

           

          (b) Further, here I would have to do multiple REST requests :

           

          yes, of course.  because you are doing multiple things.

           

          Is there a way to perform the above activities in a single REST request ??

          you want to execute a job against a certain target and then get the entire job run log in a single call ?  no.

          1 of 1 people found this helpful
          • 2. Re: How can I get the output of an NSH job in the REST API response?
            Siddhesh Bakkar

            Thanks Bill.

             

            I am able to fetch the job run logs from the above link.

             

            Further, since I cannot do the task (b) in a single request, what are the other possible ways to achieve the result in a single hit to the appserver??

             

            Currently, I am trying the same through SOAP.

            • 3. Re: How can I get the output of an NSH job in the REST API response?
              Bill Robinson

              you will need to do this w/ multiple requests because you are doing multiple things.  why do you need to perform only a single request ?

              • 4. Re: How can I get the output of an NSH job in the REST API response?
                Siddhesh Bakkar

                Hi Bill,

                 

                The application which we are trying to integrate BSA with does want the response in a single hit.

                 

                We have made a SOAP request for the same using Python taking reference from the below link :

                 

                BSA WebService Examples

                 

                Although I am triggering the web services multiple times within the script, I need the NSH script output to be passed in the SOAP response.

                 

                Is there any way to achieve this ??

                 

                Currently our script is creating a SessionID, fetching the DBKey of the NSH job & executing it.

                 

                Attaching our Python script for your reference.

                 

                try:

                        logging.basicConfig(level=logging.INFO)

                        logging.getLogger('suds.client').setLevel(logging.DEBUG)

                 

                #       Fetch the Login Session Credentials

                        url1 = 'https://myappserver:9843/services/LoginService?wsdl'

                        client1 = Client(url1)

                        client1.set_options(location= 'https://myappserver:9843/services/LoginService?wsdl', cache=None)

                        xresult = client1.service.loginUsingUserCredential('BLAdmin', 'password', 'SRP')

                        print "=========================================="

                        print xresult

                        file1 = open("export.xml", 'w')

                        filewrite = file1.write(str(xresult))

                        file1.close()

                        print "=========================================="

                        file2 = open("export.xml", 'r')

                        linelist = file2.readlines()

                        file2.close()

                        sessionId = linelist[-2].split('=')[1].replace('"','').strip(' ').rstrip()

                        print sessionId

                 

                 

                          url2 = 'https://myappserver:9843/services/CLITunnelService?wsdl'

                          client2 = Client(url2)

                # Specify group name & job name to obtain the DBKey

                          args1 = ["/GroupName", "agentinfo"]

                          client2.set_options(soapheaders=(sessionId), location= 'https://myappserver:9843/services/CLITunnelService')

                 

                 

                # Fetch the DBKey for the NSH script

                         dbkey = client2.service.executeCommandByParamList('NSHScriptJob', 'getDBKeyByGroupAndName', args1)

                         print "-------------------------------------------------"

                         print dbkey

                         print "-------------------------------------------------"

                         file3 = open("export2.xml", 'w')

                         filewrite2 = file3.write(str(dbkey))

                        file3.close()

                        print "=========================================="

                        file4 = open("export2.xml", 'r')

                        linelist2 = file4.readlines()

                        file4.close()

                        jobdbkey = linelist2[-3].split('=')[1].replace('"','').strip(' ').rstrip()

                        print jobdbkey

                        print "=========================================="

                        args2 = [jobdbkey, target]

                        print args2

                # Execute the NSH Job in BSA using the DBKey obtained above

                # result = client1.service.executeCommandByParamList('NSHScriptJob', 'executeJobAndWait', jobdbkey)

                         result = client2.service.executeCommandByParamList('Job', 'executeAgainstServers', args2)

                         print "-------------------------------------------------"

                         print result

                         print "-------------------------------------------------"

                 

                 

                except WebFault, f:

                          errors += 1

                          print f

                          print f.fault

                except Exception, e:

                          errors += 1

                          print errors

                          print e

                • 5. Re: How can I get the output of an NSH job in the REST API response?
                  Bill Robinson
                  Although I am triggering the web services multiple times within the script, I need the NSH script output to be passed in the SOAP response.

                  ok, so do you want to use soap or rest now ?

                   

                  Currently our script is creating a SessionID, fetching the DBKey of the NSH job & executing it.

                  and then what?  you want to dump all the log items ?  so maybe JobRun.getLogItemsByJobRunId ?

                  • 6. Re: How can I get the output of an NSH job in the REST API response?
                    Siddhesh Bakkar

                    Hi Bill,

                     

                    We are able to get the NSH job logs in the SOAP response. We had used "JobRun.getLogItemsByJobRunId" blcli command to achieve the same.

                     

                    Working on filtering the exact output from all the logs.

                     

                    Thanks for your help !!

                    • 7. Re: How can I get the output of an NSH job in the REST API response?
                      Siddhesh Bakkar

                      Hi Bill,

                       

                      I have modified my script a bit to use the "NSHScriptJob.executeJobAndWait." command. But I was unable to set the target in this.

                      So I used the "Job.addTargetServer" command to add the target & after the job is executed used "Job.clearTargetServer". My job gets executed properly giving correct output when I check job logs in BSA.

                       

                      But when I run the "JobRun.getLogItemsByJobRunId" to display the output in the SOAP response, it shows returnvalue = void  & does not return any output. Any reason why is it doing so ??

                      • 8. Re: How can I get the output of an NSH job in the REST API response?
                        Bill Robinson

                        I have modified my script a bit to use the "NSHScriptJob.executeJobAndWait." command. But I was unable to set the target in this.

                        So I used the "Job.addTargetServer" command to add the target & after the job is executed used "Job.clearTargetServer". My job gets executed properly giving correct output when I check job logs in BSA.

                        this is not a good idea because if you have multiple requests come through at the same time to run the job they will all try and alter the target and stomp on each other.   you can try 'Job.executeAndWaitAgainstServers' or you can use one of the executeAgainst commands that returns the run id or schedule id and poll for completion.

                         

                        But when I run the "JobRun.getLogItemsByJobRunId" to display the output in the SOAP response, it shows returnvalue = void  & does not return any output. Any reason why is it doing so ??

                        which version of the command are you using - the one that takes the run id or the one w/ the dbkey and run id ?

                        1 of 1 people found this helpful
                        • 9. Re: How can I get the output of an NSH job in the REST API response?
                          Siddhesh Bakkar

                          Thanks Bill !!

                           

                          Finally able to get the desired result using 'Job.executeAndWaitAgainstServers'. I was searching in the BMC docs for such a command which would wait for the job to get executed & give me the output against a particular server. This command is also not updated in the list of blcli commands in the link below :

                           

                          Home - Documentation for BMC Server Automation Command Line Interface 8.7 - BMC Documentation

                           

                          For the query #2, I was running "JobRun.getLogItemsByJobRunId" using the DBKey & Run ID.

                          • 10. Re: How can I get the output of an NSH job in the REST API response?
                            Bill Robinson
                            This command is also not updated in the list of blcli commands in the link below :

                            probably because it's one of the unreleased commands...Unreleased blcli commands and documentation