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;
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:
LM User
·2 years agoIt 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”)