Questo contenuto non è disponibile nella lingua selezionata.
7. Operations
7.1. Starting and Stopping a Resource Copia collegamentoCollegamento copiato negli appunti!
start() function.
Example 13. 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()
operations method to list the available operations.
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 14. 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 15. 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()
}
7.2. Scheduling Operations Copia collegamentoCollegamento copiato negli appunti!
Example 16. 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 17. Scheduled Operation Example
// find the agent
var rc = ResourceCriteria();
rc.addFilterResourceTypeName("RHQ Agent");
rc.addFilterVersion("3.1.2");
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"
);
rhqadmin@localhost:7080$ exec -f /export/myscripts/test.js
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.1.2]
7.3. Retrieving the Results of an Operation Copia collegamentoCollegamento copiato negli appunti!
fetchResults(true) method can be used to return the results of the operation as part of the search for the operation history.
// search for the operation
var c = new ResourceOperationHistoryCriteria()
c.addFilterId(schedule.id)
c.fetchResults(true)
var r = OperationManager.findResourceOperationHistoriesByCriteria(c)
// get the operation data
var h = r.get(0);
// get the results
var c = h.getResults();
c
Example 18. 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;
}
7.4. Checking a Resource's Operations History Copia collegamentoCollegamento copiato negli appunti!
Example 19. 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);
Configuration objects in the results table.
rhqadmin@localhost:7080$ exec -f /export/myscripts/test.js
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