Script Purpose
The following script implements the Rest API for the FglAM (Foglight Agent Manager) state, returning the json of FglAM states, similar to what can be found in Foglight under Administration │ Agents │ Agent Managers:
Note: These two variables on the script could be change for different names:
def cartridgeName = "RestAPItest"
1. How to register the Rest API
Click the Add button
Choose an Associated Cartridge (Optional): Forge-RestAPI
/*Hint: disable @variable usage, use server.context('variable') instead*/
import javax.ws.rs.*
import javax.ws.rs.core.*
import javax.ws.rs.container.*
import javax.servlet.http.*
import com.quest.forge.rest.annotation.Secured
import com.quest.forge.rest.service.*
import com.quest.forge.rest.api.*
import com.quest.wcf.servicelayer.*
import groovy.json.JsonOutput
import com.quest.nitro.service.util.JmxHelper
@Path("/test")
@Secured
@Produces("application/json")
class FglAMResource{
@Context
ContainerRequestContext requestContext
@Context
HttpServletRequest request
def foglightServiceLocator = ServiceFactory.getInstance().getFoglightServiceLocator()
def dataSubmissionMonitor = JmxHelper.getCoreMBeanImpl('com.quest.nitro:service=DataSubmissionMonitor')
def dataSvc = foglightServiceLocator.getDataService()
def retrieveFglAMs() {
def evlSvc = ServiceRegistry.getEvaluationService()
def agentManagers = evlSvc.invokeFunction("system:administration_agents_agenthost.allAgentManagers", [], null, null)
evlSvc.invokeFunction("system:administration_agents_agenthost.retrieveFglAMHAConfig", [agentManagers], null, null)
return agentManagers
}
def constructFglAMState(manager) {
def instance = manager.fglamInstance
def client = manager.adminRemoteClient
return [
id: manager.id,
alarmsState: instance.aggregateStateSeverity.name,
hostNanme: instance.host.name,
ipAddress: instance.host.primaryIpAddress.address,
adapterType: client.adapterName,
version: client.clientVersion,
build: client.clientBuildId,
osName: instance.host.os.name,
osArchitecture: client.architecture,
securityMode: manager.securityMode,
isConcentrator: manager.isConcentrator,
agentCounts: dataSvc.retrieveLatestValue(instance,"collectingAgents")?.value?.avg,
lastSubmissionTime: dataSubmissionMonitor.getRemoteClientLastSubmissionTime(instance.id),
haPartition: manager.haConfig?.partition,
haState: manager.haConfig?.state
]
}
@GET
@Path("fglams")
public Response getFglAMStates(){
try{
def states = retrieveFglAMs().collect{constructFglAMState(it)}
return Response.ok(JsonOutput.toJson([status: 1, data: states])).build()
}catch(Exception e){
return Response.serverError().entity(e).build()
}
}
}
class MyRestAPIExtension implements ForgeRestApiExtension{
String cartridgeName
public MyRestAPIExtension(String carName) {
this.cartridgeName = carName
}
@Override
public Class getRestResourceClz(){
return FglAMResource.class
}
@Override
public String getCartridgeName(){
return cartridgeName
}
}
def cartridgeName = "test"
def rrm = server.context('extensionContext').get(RestResourceManager.class)
if (rrm.getRestAPIExtensions(cartridgeName))
rrm.unregisterRestAPIExtensions(cartridgeName)
rrm.registerRestAPIExtension(new MyRestAPIExtension(cartridgeName))
After registered the REST API, the following information will appear under "Log Messages" tab:
2019-04-29 20:00:33.643 INFO [czfhhwdzdc-1] com.quest.forge.rest.config.FoglightRestResourceConfig - Rest API has been reloaded!
2. Define on Postman choosing: "Add New Request". This is one GET function defining the following Headers to retrieve the information:
http://{{serverURL}}:{{serverPort}}/{{apiPath}}/test/fglams
On the Headers
Key value
Access-Token {{tokenValue}}
Accept {{acceptValue}}
This should return the expected information.
Note: The {{tokenValue}} could be changed for token value that was obtain from "Account Login" function.
The following is one example of the Environment Variables that should be defined prior this exercise:
© ALL RIGHTS RESERVED. Feedback Terms of Use Privacy Cookie Preference Center