Forum Discussion

sprusty's avatar
sprusty
Icon for Neophyte rankNeophyte
2 years ago
Solved

Groovy script failing for alinux server

We have a groovy script which is working fine for a data source. However when we are trying to run the same in debug mode, it’s failing. Can someone help on this.

  • If it’s working via datasource test, but not in debug (!groovy), can you make sure the hostprops are being populated correctly via debug? Consider manually entering them in the script as you run it via !groovy:

    host = "HOSTNAME"

    user = "SSHUSERHERE"

    pass = "SSHPASSHERE"

    port = hostProps.get("ssh.port")?.toInteger() ?: 22

    cert = hostProps.get("ssh.cert") ?: '~/.ssh/id_rsa'

    Note: For hostname, make sure to use the same value under INFO tab of device for system.hostname.

    If still failing, definitely create a support ticket, as it can be tested more in-depth from LM support side.

6 Replies

  • If it’s working via datasource test, but not in debug (!groovy), can you make sure the hostprops are being populated correctly via debug? Consider manually entering them in the script as you run it via !groovy:

    host = "HOSTNAME"

    user = "SSHUSERHERE"

    pass = "SSHPASSHERE"

    port = hostProps.get("ssh.port")?.toInteger() ?: 22

    cert = hostProps.get("ssh.cert") ?: '~/.ssh/id_rsa'

    Note: For hostname, make sure to use the same value under INFO tab of device for system.hostname.

    If still failing, definitely create a support ticket, as it can be tested more in-depth from LM support side.

  • The script :

    import com.jcraft.jsch.JSch
    import com.santaba.agent.util.Settings
    import java.text.SimpleDateFormat
    import static groovy.io.FileType.FILES
    import static groovy.io.FileVisitResult.*
    import groovy.time.TimeCategory
    import groovy.json.*

    host = hostProps.get("system.hostname")
    user = hostProps.get("ssh.user")
    pass = hostProps.get("ssh.pass")
    port = hostProps.get("ssh.port")?.toInteger() ?: 22
    cert = hostProps.get("ssh.cert") ?: '~/.ssh/id_rsa'
    timeout = 15000 // timeout in milliseconds

    def date = new Date()  //Get Current date stamp

    def sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss")  //format the date for later use in the event generation
    def _outArray = []
         
    command = ("ps H -u mqm | wc -l") 
    def command_output = getCommandOutput(command)  //run the command on the host
    command_output = command_output.trim()          //the output is in string format and has multiple blnk lines, clean up blanks
    command_output.eachLine         //for each of the lines in the command output
    { line ->
      int _line = line as int   //convert output to integer, this is the file count of older files if any exists
      if (_line>19000){         //if there are older files
          println _line
      }
      else{
          println _line
      }

    }
     
    return 0;


    ///////////////////////////////////////////
    ////////////////////////////////////////////
    // DO NOT Change anything below this line
    ///////////////////////////////////////////
    ////////////////////////////////////////////

    /**
     * Helper method which handles creating JSCH session and executing commands
     * @return
     */
    def getCommandOutput(String input_command) {
        try {
            // instantiate JSCH object.
            jsch = new JSch()

            // do we have an user and no pass ?
            if (user && !pass) {
                // Yes, so lets try connecting via cert.
                jsch.addIdentity(cert)
            }

            // create session.
            session = jsch.getSession(user, host, port)

            // given we are running non-interactively, we will automatically accept new host keys.
            session.setConfig("StrictHostKeyChecking", "no");
            String authMethod = Settings.getSetting(Settings.SSH_PREFEREDAUTHENTICATION, Settings.DEFAULT_SSH_PREFEREDAUTHENTICATION);
            session.setConfig("PreferredAuthentications", authMethod);

            // set session timeout, in milliseconds.
            session.setTimeout(timeout)

            // is host configured with a user & password?
            if (pass) {
                // set password.
                session.setPassword(pass);
            }

            // connect
            session.connect()

            // execute command.
            channel = session.openChannel("exec")
            channel.setCommand(input_command)

            // collect command output.
            def commandOutput = channel.getInputStream()
            channel.connect()

            def output = commandOutput.text;

            // disconnect
            channel.disconnect()

            return output
        }
        catch (Exception e) {
            e.printStackTrace()
        }
        // ensure we disconnect the session.
        finally {
            session.disconnect()
        }
    }

    the error in debug mode:

    $ !groovy
    Error when executing the script - 
    output:

    exception:Cannot invoke method trim() on null object
    java.lang.NullPointerException: Cannot invoke method trim() on null object
        at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:91)
        at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:47)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
        at org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:34)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
        at Script759.run(Script759.groovy:20)
        at com.santaba.agent.util.GroovyScriptShell.execute(GroovyScriptShell.java:126)
        at com.santaba.agent.util.GroovyScriptExecutor.execute(GroovyScriptExecutor.java:129)
        at com.santaba.agent.debugger.GroovyTask._executeInAgent(GroovyTask.java:207)
        at com.santaba.agent.debugger.GroovyTask._handle(GroovyTask.java:186)
        at com.santaba.agent.debugger.DebugTask.run(DebugTask.java:165)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:829)
     

  • exception:Cannot invoke method trim() on null object

    Your return of your command begin ran is returning nothing. You can’t trim nothing. That is basically what your error is.