This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findResourceType(plugin, resourceTypeName) { | |
var criteria = ResourceTypeCriteria(); | |
criteria.strict = true; | |
criteria.addFilterPluginName(plugin); | |
criteria.addFilterName(resourceTypeName); | |
var types = ResourceTypeManager.findResourceTypesByCriteria(criteria); | |
if (types.size() > 0) { | |
return types.get(0); | |
} | |
return null; | |
} | |
function findPlatform(hostname) { | |
var criteria = new ResourceCriteria(); | |
criteria.addFilterResourceCategories([ResourceCategory.PLATFORM]); | |
criteria.addFilterResourceKey(hostname); | |
var resources = ResourceManager.findResourcesByCriteria(criteria); | |
if (resources.size() > 0) { | |
return resources.get(0); | |
} | |
return null; | |
} | |
function createTomcatConnectionProps(path) { | |
var config = new Configuration(); | |
config.put(new PropertySimple('catalinaBase', path)); | |
config.put(new PropertySimple('installationPath', path)); | |
config.put(new PropertySimple('connectorAddress', | |
'service:jmx:rmi:///jndi/rmi://localhost:9876/jxrmi')); | |
config.put(new PropertySimple('type', | |
'org.mc4j.ems.connection.support.metadata.Tomcat55ConnectionTypeDescriptor')); | |
return config; | |
} | |
function manuallyAddTomcat(hostname, path) { | |
var platform = findPlatform(hostname); | |
var type = findResourceType('Tomcat', 'Tomcat Server'); | |
var pluginConfig = createTomcatConnectionProps(path); | |
return DiscoveryBoss.manuallyAddResource(type.id, platform.id, | |
pluginConfig); |
The findResourceType and findPlatform functions are pretty straightforward. The interesting work happens in createTomcatConnectionProps and in manuallyAddTomcat. The key to it all though is on line 44. DiscoveryBoss provides methods for importing resources from the discovery queue as well as for manually adding resources. manuallyAddResources expects as arguments a resource type id, a parent resource id, and the plugin configuration (i.e., connection properties).
Determining the connection properties that you need to specify might not be entirely intuitive. I looked at the plugin descriptor as well as the TomcatDiscoveryComponent class from the tomcat plugin to determine the minimum, required connection properties that need to be included.
Here is how the script could be used from the CLI shell:
rhqadmin@localhost:7080$ login rhqadmin rhqadmin rhqadmin@localhost:7080$ exec -f manual_add.js rhqadmin@localhost:7080$ hostname = '127.0.0.1' rhqadmin@localhost:7080$ tomcatDir = '/home/jsanda/Development/tomcat6' rhqadmin@localhost:7080$ manuallyAddTomcat(hostname, tomcatDir) Resource: id: 12071 name: 127.0.0.1:8080 version: 6.0.24.0 resourceType: Tomcat Server rhqadmin@localhost:7080$
This effectively adds the Tomcat server to the inventory of managed resources. This same approach can be used with other resource types. The key is knowing what connection properties you need to specify so that the plugin (in which the resource type is defined) knows how to connect to and manage the resource.
Cool!
ReplyDeleteI used this approach to add a JMX Server Resource (JVM) manually in a group of Linux Servers. The script is available here: http://bit.ly/lc5K8t
;-)
This comment has been removed by the author.
ReplyDelete