12.6. Operations
12.6.1. Starting and Stopping a Resource
Example 12.16. Simple Start
//find the resource
criteria = new ResourceCriteria();
criteria.addFilterName('My JBossAS')
var servers = ResourceManager.findResourcesByCriteria(criteria);
var myJBossAS = ProxyFactory.getResource(servers.get(0).id)
myJBossAS.start()rhqadmin@localhost:7080$ server.operations Array of org.rhq.bindings.client.ResourceClientProxy$Operation name description ----------------------------------------------------------------------- restart Shutdown and then start this application server. start Start this application server. shutdown Shutdown this application server via script or JMX. 3 rows
Example 12.17. Starting an Array 1
//find the resources
//use a plugin filter to make sure they are all of the same type
criteria = new ResourceCriteria();
criteria.addFilterPluginName('JBossAS5')
var resources = ResourceManager.findResourcesByCriteria(criteria).toArray();
var resType = ResourceTypeManager.getResourceTypeByNameAndPlugin('JBossAS Server', 'JBossAS5');
// go through the array
var idx=0;
var jbossServers = new Array();
for( i in resources ) {
if( resources[i].resourceType.id == resType.id ) {
jbossServers[idx] = resources[i];
idx = idx + 1;
}
}
// restart the resources
for( a in resources ) {
var jboss = ProxyFactory.getResource(jbossServers[a].id);
jboss.restart()
}Example 12.18. Starting an Array 2
//find the resources
//use a plugin filter to make sure they are all of the same type
criteria = new ResourceCriteria();
criteria.addFilterPluginName('JBossAS5')
criteria.addFilterResourceTypeName('JBossAS Server');
var jbossServers = ResourceManager.findResourcesByCriteria(criteria).toArray();
// restart the resources
for( a in jbossServers ) {
var jboss = ProxyFactory.getResource(jbossServers[a].id);
jboss.restart()
}12.6.2. Scheduling Operations
Example 12.19. Immediate Operation
rhqadmin@localhost:7080$ var agent = ProxyFactory.getResource(10008)
rhqadmin@localhost:7080$ agent.executeAvailabilityScan(true)
Invoking operation executeAvailabilityScan
Configuration [13903] - null
isChangesOnly = true
agentName = server.example.com
resourceAvailabilities [0] {
}- The resource ID
- The operation name
- A delay period, meaning when in the future to start the operation (optional)
- A repeat interval and count (optional)
- A timeout period (optional)
- Configuration parameters, if required by the operation
- A description of the scheduled operation (optional)
Example 12.20. Scheduled Operation Example
// find the agent
var rc = ResourceCriteria();
rc.addFilterResourceTypeName("RHQ Agent");
rc.addFilterVersion("3.3");
var agent = ResourceManager.findResourcesByCriteria(rc);
//set the config properties for the operation
var config = new Configuration();
config.put(new PropertySimple("changesOnly", "true") );
//schedule the operation
OperationManager.scheduleResourceOperation(
agent.get(0).id,
"executeAvailabilityScan",
0, // 0 means that the delay was skipped
1,
0, // this skips the repeat count
10000000,
config,
"test from cli"
);ResourceOperationSchedule:
resource: Resource[id=10008, uuid=e11390ec-34c4-49df-a4b6-c37c516f545c, type={RHQAgent}RHQ Agent, key=server.example.com RHQ Agent, name=RHQ Agent, parent=server.example.com, version=3.3]12.6.3. Retrieving the Results of an Operation
fetchResults(true) method can be used to return the results of the operation as part of the search for the operation history.
// find the resource
criteria = new ResourceCriteria();
criteria.addFilterResourceTypeName('Linux');
criteria.addFilterName('server.example.com');
ResourceManager.findResourcesByCriteria(criteria);
var resource = ResourceManager.findResourcesByCriteria(criteria);
// search for the operation history
var opcrit = ResourceOperationHistoryCriteria();
// get the operation for the resource ID
opcrit.addFilterResourceIds(resource.get(0).id);
// filter by the operation name
opcrit.addFilterOperationName("viewProcessList")
opcrit.fetchResults(true);
// get the data and print the results
var r = OperationManager.findResourceOperationHistoriesByCriteria(opcrit)
var h = r.get(0);
var c = h.getResults();
pretty.print(c)Example 12.21. Printing the Results of a Process Scan
if (args.length != 1) {
throw "we need a resource id as an argument";
}
var platform = ResourceManager.getResource(args[0]);var ros = OperationManager.scheduleResourceOperation( platform.id, "viewProcessList", 0, 1, 0, 15, null, "test operation" );
- fetchResults(true), which is required to include the operation result data and not just the status
- a sort method, in this case addSortStartTime
var opcrit = ResourceOperationHistoryCriteria();
opcrit.addFilterResourceIds(platform.id);
opcrit.fetchResults(true); // request the additional optional data in the result
opcrit.addSortStartTime(PageOrdering.DESC); // sort by start time
java.lang.Thread.sleep(1000); // wait a second to make sure operation is in the history
// wait for up to 15 seconds for last operation to complete, then print result
now=new Date().getTime();
while (new Date().getTime() - now < 15000 ) {
operations = OperationManager.findResourceOperationHistoriesByCriteria(opcrit);
if (operations.get(0).getResults() == null) {
println("operation still pending result");
java.lang.Thread.sleep(1000);
} else {
pretty.print(operations.get(0).getResults());
break;
}
}
if (operations.get(0).getErrorMessage() != null) {
println("Error getting process list: ");
pretty.print(operations.get(0).getErrorMessage());
} } else {
pretty.print(operations.get(0).getResults());
break;
}12.6.4. Checking a Resource's Operations History
Example 12.22. Viewing the Operation History
// find the resource
var rc = ResourceCriteria();
rc.addFilterPluginName("RHQAgent");
rc.addFilterName("RHQ Agent");
rc.addFilterResourceTypeName("RHQ Agent");
rc.addFilterDescription("Agent");
var agent = ResourceManager.findResourcesByCriteria(rc);
// print the operation history for the resource
var opcrit = ResourceOperationHistoryCriteria()
opcrit.addFilterResourceIds(agent.get(0).id)
OperationManager.findResourceOperationHistoriesByCriteria(opcrit);resource results
-----------------------------------------------------------------------------------------------------------------
Resource[id=10008, uuid=e11390ec-34c4-49df-a4b6-c37c516f545c, type={RHQAgent}RHQ Agent, key=server.example.com RHQ Age Configuration[id=13903]
Resource[id=10008, uuid=e11390ec-34c4-49df-a4b6-c37c516f545c, type={RHQAgent}RHQ Agent, key=server.example.com RHQ Age Configuration[id=13913]
2 rows
Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.