Tuesday, August 17, 2010

Auto Import Resources into Inventory

There is no way currently in RHQ through the UI to auto-import resources. You have to go to the discovery queue to explicitly select resources to import. Here is a short CLI script for auto-importing resources.

// auto_import.js
rhq.login('rhqadmin', 'rhqadmin');

var resources = findUncommittedResources();
var resourceIds = getIds(resources);


// returns a java.util.List of Resource objects
// that have not yet been committed into inventory
function findUncommittedResources() {
    var criteria = ResourceCriteria();
    return ResourceManager.findResourcesByCriteria(criteria);

// returns an array of ids for a given list
// of Resource objects. Note the resources argument
// can actually be any Collection that contains
// elements having an id property.
function getIds(resources) {
    var ids = [];
    for (i = 0; i < resources.size(); i++) { 
        ids[i] = resources.get(i).id;
    return ids;

In the function findUncommittedResources() we query for Resources objects having an inventory status of NEW. This results in a query that retrieves discovered resources that have been "registered" with the RHQ server (i.e., stored in the database) but not yet committed into inventory.

DiscoveryBoss is one of the remote EJBs exposed by the RHQ server to the CLI. It provides a handful of inventory-related operations. On line six we call DiscoveryBoss.importResources() which takes an array of resource ids.

 In a follow-up post we will use some additional CLI features to parametrize this script so that we have more control of what gets auto-imported.


  1. Hi John,

    I've created a similar post a few months back with advanced filtering (import only Apache HTTPD and JBoss AS) on my blog. You can see full script here.

    Maybe it'll help someone!


  2. Hi.

    You know if is possible to add resources that isn't auto-discovered by agent scan process?

    For example: I need to add a bunch of JVM using the generic JMX plugin. On UI I would do this via Platform > Inventory > Add Manually > JDK 5 etc.

    I thought in get the JMX addr from all JVM process (may be using a bsh script), put in a list and then import them via rhq-cli script.


  3. You need to use DiscoveryBoss.manuallyAddResource. Take a look at http://docs.redhat.com/docs/en-US/JBoss_Operations_Network/2.4/html/API_Guides/remote-api/org/rhq/enterprise/server/discovery/DiscoveryBossRemote.html.

  4. Hum... cool! I'll try it.

    BTW, Is there some interface on rhq remote API to deal with Alert/Metrics Templates?


  5. I think the only APIs we may expose are for querying. You might want to take a look at AlertDefinitionManagerRemote and MeasurementDefinitionManagerRemote. If there is functionality you would like to see please feel free to file a feature request at https://bugzilla.redhat.com. I'd be particularly interested to hear user feedback on ways to make the CLI an easier, more productive interface to RHQ.

  6. I found that MeasurementScheduleManagerRemote interface has some methods to deal with MeasurementTemplates. I'll try it soon.

    About alert templates the AlertDefinitionManagerRemote does not deal with templates yet. There is a BZ (https://bugzilla.redhat.com/show_bug.cgi?id=535829) requesting this functionality for AlertDefinitionManager.


  7. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Online Training from India . or learn thru JavaScript Online Training from India. Nowadays JavaScript has tons of job opportunities on various vertical industry. ES6 Online Training