Forum Discussion

Kiyoshi_Egawa's avatar
11 months ago

java.lang.NullPointerException on Groovy Collector Attribute script

The following script encounter NullPointerException. It seems hostProps.get() doesn’t work. Any advice will be appreciate.

import com.santaba.agent.groovyapi.expect.Expect;
import com.santaba.agent.groovyapi.snmp.Snmp;
import com.santaba.agent.groovyapi.http.*;
import com.santaba.agent.groovyapi.jmx.*;
import org.xbill.DNS.*;

Boolean debug = true

String hostname = hostProps.get("system.hostname") ;
String user = hostProps.get("scc.user");
String pass = hostProps.get("scc.pass");
String port = (String)hostProps.get("scc.port");
httpClient = HTTP.open(hostname, port, true);
httpClient.setAuthentication(user,pass);
//url="https://"+hostname+"/exposed?action=ping";
url="https://" + hostname + ":" + port + "/api/monitoring/memory";
//url="https://"+hostname+"/api/monitoring/performance/backends";

def getResponse=httpClient.get(url, ["Accept":"application/json"]);
if ( !(httpClient.getStatusCode() =~ /200/) )
{
    httpClient.close();
    return 1
}
println httpClient.getResponseBody();
httpClient.close();
return 0;

  • Anonymous's avatar
    Anonymous

    It will fail if the property doesn’t exist. You can specify a value to use if the property doesn’t exist.

    The groovy way is String user = hostProps.get("scc.user") ?: “unknownuser”

    The method also supposedly also can do that like this: String user = hostProps.get("scc.user", “unknown user”)

  • If you’re not already doing so, I would encourage you to use the Collector Debug Facility. I am nearly certain that your problem is not due to hostProps.get() not working.

    If `hostProps.get()` fails to find a given property, it will instead return a NullObject class.

    Here is a test:

    def testProp = hostProps.get("fake.prop");

    println testProp.getClass()

    Output:

    class org.codehaus.groovy.runtime.NullObject

    Here are a few things to look at:

    • If you’re using Collector Debug:
      • You must remember that ‘HostName’ must match the system.hostname property -- NOT the system.displayname property
      • If you’re leveraging Auto-balanced collector groups (ABCGs), you must make certain you are debugging from the collector on which the resource is currently being polled.
    • Validate each one of your variables that you are passing to your HTTP functions
      • This is the most common area that I find problems with my scripting, e.g. a property isn’t being applied at the collector.
        • As an aside, I believe I’ve seen some delays in properties being applied at the portal, but not yet ‘seen’ by the collectors. There is some synchronization that normally has to take place for the collector to become aware of newly-applied-properties.
    • ALL properties are read in as ‘Strings’ by default; your port _might_ need to be cast to an Integer? I don’t think this would result in a NullPointerException, however, as I think that the HTTP class you’re using should be able to cast this appropriately on the fly.
  • I resolved NullPointerException with 

    httpClient = HTTP.open(hostname, Integer.parseInt(port), true);

    Thank you

    Kiyoshi

  • Anonymous's avatar
    Anonymous

    You could also do:

    Integer port = hostProps.get("scc.port")

    Then:

    httpClient = HTTP.open(hostname, port, true)

    It’s cleaner and more direct.

  • It’s cleaner and more direct.

    It leads “exception:Cannot cast object '8433' with class 'java.lang.String' to class 'java.lang.Integer'”.

  • Anonymous's avatar
    Anonymous

    Have i mentioned how much I hate Groovy?

    Integer port = parseInt(hostProps.get("scc.port"))

    At any rate, converting the input variable to its destination type is better done at the beginning of the script when things are not yet complicated. Converting in the middle of opening the http client can be done, but it’s harder to follow what’s going on.