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;
LM User
·2 years agoHave i mentioned how much I hate Groovy?
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.
Kiyoshi_Egawa
OP2 years agoIt leads “exception:Cannot cast object '8433' with class 'java.lang.String' to class 'java.lang.Integer'”.
LM User
·2 years agoYou could also do:
Then:
It’s cleaner and more direct.
Kiyoshi_Egawa
OP2 years agoI resolved NullPointerException with
Thank you
Kiyoshi
AustinC
·2 years agoIf 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:
Output:
Here are a few things to look at: