Red Hat 3.3 Writing JON Command-Line Scripts JBoss Operations Network User manual
Реклама
Реклама
Red Hat JBoss Operations Network
3.3
Development - Writing JON
Command-Line Scripts
Using the JBoss ON CLI and Remote API
Jared Morgan Zach Rhoads Ella Deon Ballard
Red Hat JBoss Operations Network 3.3 Development - Writing JON
Command-Line Scripts
Using the JBoss ON CLI and Remote API
Jared Mo rgan jmo [email protected] m
Zach Rho ads [email protected] m
Ella Deo n Ballard [email protected] m
Legal Notice
Co pyright © 20 15 Red Hat.
The text o f and illustratio ns in this do cument are licensed by Red Hat under a Creative
Co mmo ns Attributio n–Share Alike 3.0 Unpo rted license ("CC-BY-SA"). An explanatio n o f CC-
BY-SA is available at http://creativeco mmo ns.o rg/licenses/by-sa/3.0 /
. In acco rdance with CC-BY-SA, if yo u distribute this do cument o r an adaptatio n o f it, yo u must pro vide the URL fo r the o riginal versio n.
Red Hat, as the licenso r o f this do cument, waives the right to enfo rce, and agrees no t to assert,
Sectio n 4 d o f CC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, Red Hat Enterprise Linux, the Shado wman lo go , JBo ss, MetaMatrix, Fedo ra, the Infinity
Lo go , and RHCE are trademarks o f Red Hat, Inc., registered in the United States and o ther co untries.
Linux ® is the registered trademark o f Linus To rvalds in the United States and o ther co untries.
Java ® is a registered trademark o f Oracle and/o r its affiliates.
XFS ® is a trademark o f Silico n Graphics Internatio nal Co rp. o r its subsidiaries in the United
States and/o r o ther co untries.
MySQL ® is a registered trademark o f MySQL AB in the United States, the Euro pean Unio n and o ther co untries.
No de.js ® is an o fficial trademark o f Jo yent. Red Hat So ftware Co llectio ns is no t fo rmally related to o r endo rsed by the o fficial Jo yent No de.js o pen so urce o r co mmercial pro ject.
The OpenStack ® Wo rd Mark and OpenStack Lo go are either registered trademarks/service marks o r trademarks/service marks o f the OpenStack Fo undatio n, in the United States and o ther co untries and are used with the OpenStack Fo undatio n's permissio n. We are no t affiliated with, endo rsed o r spo nso red by the OpenStack Fo undatio n, o r the OpenStack co mmunity.
All o ther trademarks are the pro perty o f their respective o wners.
Abstract
JBo ss Operatio ns Netwo rk pro vides its o wn co mmand shell that can interact directly with the
JBo ss ON server. This CLI uses the JBo ss ON remo te API to perfo rm mo st o f the tasks available in the JBo ss ON GUI, as well as additio nal o peratio ns like impo rting and expo rting server co nfiguratio n and expo rting histo ric metric data. The CLI allo ws administrato rs to script and auto mate their JBo ss ON deplo yment, which makes it easier to manage their infrastructure.
This guide co vers the basics o f installing and using the JBo ss ON CLI and pro vides examples o f scripts fo r co mmo n tasks. It is intended primarily fo r administrato rs who will be using the default JBo ss ON CLI to manage JBo ss ON. This manual has a seco ndary audience fo r plug-in writers and develo pers who intend to write custo m applicatio ns which leverage the remo te API.
Table of Contents
1. A Summary o f JBo ss O N Pub lic APIs
2. The JBo ss O N Server and Its Interfaces
3. JBo ss O N CLI Scrip ts and JBo ss O N Server Scrip ts
4. Differences Between the JBo ss O N CLI and JBo ss O N G UI O p eratio ns
2. Setting CLI Enviro nment Variab les
T able of Cont ent s
14
16
16
8
9
10
11
12
12
5
5
1. Ab o ut Scrip t Dep end encies and Exp o rting Functio ns
2. Default Lo catio ns fo r Scrip t Mo d ule So urces
3. Creating a New Mo d ule So urce Typ e
4. unlimited PC and p ag eCo ntro l
8 . Metho d s Availab le to Pro xy Reso urces
3. Passing Co mmand and Scrip t Arg uments
4. Disp laying Pretty-Print O utp ut
53
54
6 0
6 4
6 5
1
40
42
43
44
45
46
47
47
23
24
26
29
30
31
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
2. Differences Between Running the CLI Interactively and with Files
3. API Differences Between Reso urces Typ es and Versio ns
4. Availab le Utility and Samp le Scrip ts
5. Defining Custo m Functio ns
6 . Sched uling Scrip t Runs with Cro n
8 . Permissio ns and Setup fo r JBo ss O N Users
2. G etting the JBo ss O N ID fo r an O b ject
3. G etting Data fo r Sing le and Multip le Reso urces
1. Auto matically Imp o rt New Reso urces: auto imp o rt.js
2. Simp le Invento ry Co unt: invento ryCo unt.js
3. Uninvento ry a Reso urce After an Alert: uninvento ry.js
1. Declaring Custo m Functio ns
2. Checking the JBo ss O N G ro up s and Invento ry
3. Dep lo ying the New Reso urce
1. Creating a New Ap p licatio n
2. Creating the Wrap p er Scrip t and .co nf File
3. Defining Arg uments and O ther Parameters fo r the CLI Scrip ts
4. Creating a G ro up : g ro up .js
5. Ad d ing Reso urces to a G ro up : ad d Memb er.js
6 . G etting Invento ry and Status Info rmatio n: status.js
7. Starting , Sto p p ing , and Restarting the Server: restart.js
8 . Dep lo ying Ap p licatio ns to the G ro up Memb ers: d ep lo y.js
9 . Sched uling an Availab ility O p eratio n: avail.js
10 . G athering Metric Data o f Manag ed Servers: metrics.js
2
1. Ad d ing a Stand alo ne EAP 6 Server to a Cluster
2. Co p ying Dep lo yed Ap p licatio ns Between Stand alo ne Servers
10 9
110
111
115
115
118
121
124
74
79
8 0
8 1
8 8
9 2
9 8
10 2
10 5
6 6
6 7
6 8
6 8
71
72
72
72
135
136
137
138
139
128
129
132
133
133
141
145
2. Co p ying Dep lo yed Ap p licatio ns Between Stand alo ne Servers
1. Setting up Bund le Versio ns and Destinatio ns
2. Setting up the Drift Definitio n and Prep aring the Bund le
1. Simp le Exp o rt/Imp o rt Synchro nizatio n
2. Chang ing Server Co nfig uratio n Befo re Imp o rting
T able of Cont ent s
145
148
149
151
153
153
16 0
16 2
16 3
3
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
4
About T his G uide
About This Guide
The remote API for JBoss Operations Network, and affiliated APIs like the domain API for searches and the plug-in API for agent plug-ins, provides a framework for automating management tasks.
Automation can take different forms, from scheduling routine management tasks as cron jobs to initiating server scripts in response to fired alerts. The remote API lets administrators be more active and responsive in how they maintain resources.
Because of the different ways that scripts can be used with JBoss ON, there are different methods of both writing and calling scripts. Because of the variety of different implementing scripts, wriing scripts, and even tasks that scripts can perform, there is no way to give a comprehensive library of scripts so that "if you want to do A, run script B." The goal of this guide it to provide clarity on what the remote API provides to script writers and basic usage information and examples for the included interactive JBoss ON CLI utility.
With a good basis of understanding some of what JBoss ON server scripts can do, administrators can create whatever specific script they need for their environments.
Report a bug
1. Audience and Int ent
JBoss ON itself is written in Java, and the installed CLI utility communicates to the server in
Java. However, the CLI uses a JavaScript-style interpreter and can execute JavaScript files that are passed to it.
This guide is geared toward systems administrators and programmers with a basic understanding of JavaScript. It is beneficial (though not required) to have a basic grasp of
Java, as well.
The JBoss ON remote API can be used to write services in other Java-compatible languages, such as Groovy and Scala. That is outside the scope of this guide.
Report a bug
2. Giving Feedback
If there is any error in this guide or there is any way to improve the documentation, please let us know. Bugs can be filed in Bugzilla, http://bugzilla.redhat.com/bugzilla . Make the bug report as specific as possible, so we can be more effective in correcting any issues.
1. Select the JBo ss products group.
2. Select JBo ss O N from the list.
5
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
3. Set the component to D o cumentati o n.
4. Set the version number to 3.3.
5. For errors, give the page number (for the PDF) or URL (for the HTML), and give a succinct description of the problem.
For enhancements, put in what information needs to be added and why.
6. Give a clear title for the bug. For example, "Incorrect command example for setup script
options" is better than "Bad example".
Any feedback, positive or negative, is welcome.
Report a bug
6
Part I. G et t ing St art ed
Part I. Getting Started
JBoss ON can use scripts in different ways and through different methods. There are different
APIs available depending on what, exactly, you want to accomplish. Understanding how the
JBoss ON server interacts with clients through its different interfaces makes it easier to understand how to plan and write scripts for different management scenarios. Better scripting (and a better understanding of what JBoss ON is doing with scripts) improves your ability to automate how you manage resources in your IT environment.
Report a bug
7
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Chapter 1. Understanding How Scripts Work with the
JBoss ON Server and CLI
The JBoss Operations Network CLI and server-side scripts expose much of the core functionality of the JBoss ON server itself and give a lot of control to IT administrators on managing the resources in JBoss ON automatically and programmatically.
JBoss ON's remote API and the scripts which leverage it provide a way to access the JBoss
ON server directly. Ultimately, this allows administrators to interact with JBoss ON differently than a strictly UI-based deployment.
Understanding the different JBoss ON APIs and how they interact with the JBoss ON server can help administrators plan what scripts they need for using the CLI programmatically, for planning alerts, and for designing clients.
Report a bug
1. A Summary of JBoss ON Public APIs
There are three relevant, public APIs for JBoss ON, which each interact differently with the
JBoss ON server: the remote API, the domain API, and the plug-in API.
T ab le 1.1. JBo ss O N APIs
API Descrip t io n
Remote
Resource management functions for tasks such as managing the inventory, changing configuration, uploading and managing content, initiating operations, viewing metrics and alerts, managing configuration drift, creating groups, and creating and managing users and roles.
The remote API is accessible using standard Java enterprise client mechanisms or through the servlet-based JBoss remoting endpoint.
8
Chapt er 1 . Underst anding How Script s Work wit h t he JBoss O N Server and CLI
API Descrip t io n
Domain
Plug-in
Functions which parallel the server's local manager beans, particularly in how a feature area is configured.
Clients use the remote API to script actions.
The remote API relies on the domain API to supply most of that functionality, such as the criteria to use for object searches or the different properties for viewing resource configuration.
Functions related to both agent (resource) plug-ins. This is not used at all for the
JBoss ON CLI or server-side scripts. The
APIs are used by the agent plug-ins to receive and convey information between the agent and the JBoss ON server.
Report a bug
2. T he JBoss ON Server and It s Int erfaces
The JBoss ON server is a Java application. All of the subsystems — like resources and groups, monitoring, alerting, drift, and provisioning — are contained in Enterprise
JavaBeans in the server. The different JBoss ON APIs interact with those EJBs.
Specifically, most of the server EJBs are stateless session beans (SLSBs). These core server
SLSBs follow a certain naming logic:
The implementing class is *ManagerBean. This implements both the local API and the remote API.
SLSBs that are used internally by the server are *ManagerLocal.
SLSBs that define the remote API are *ManagerRemote.
Each SLSB exposes its remote interface in the CLI with the naming convention *Manager. For example, all of the methods associated with managing a resource belong to
ResourceManager.
The server itself uses EJBs to create its data structure. The CLI interacts with the server EJBs through the remote API, the *Manag ers. The remote API is the compilation of the *R emo te interfaces of the server's EJBs.
Fig u re 1.1. T h e Server In t erf aces an d Clien t In t eract io n s
9
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
10
So, how does all of that fit in with the JBoss ON server and using server-side scripts?
All of the different APIs interact with clients and with the server in slightly different ways.
Agents, and their underlying resources and resource plug-ins, use the plug-in API to communicate to the core server (over the remoting framework). Agent and server plug-ins frequently work in pairs to extend JBoss ON functionality. For example, implementing an additional recipe style for bundles would require an agent plug-in to work with resources and a corresponding server plug-in to work with the server-side data structures.
The JBoss ON CLI uses the remote and domain APIs to communicate to the core server, also over the remoting framework. Unlike the agent, the CLI does not implement functionality (or at least not a lot); rather, it calls on the existing functionality to perform management tasks.
The GUI, which is hosted by the JBoss ON server, uses its local interface directly. Because the GUI uses a different API than the CLI, there are slight differences in what can be
performed through the GUI as opposed to the CLI ( Section 4, “ Differences Between the JBoss
ON CLI and JBoss ON GUI Operations” ).
Report a bug
3. JBoss ON CLI Script s and JBoss ON Server Script s
There are two major ways to access the remote API: through the JBoss ON CLI and through a separate JBoss ON server script.
The scripts themselves are more or less identical, with the exception of some methods that are available when the JBoss ON CLI is run interactively. Both CLI scripts and server-side scripts are written in JavaScript and use the remote API primarily, with support from the domain API for tasks like searches.
The main difference is in how the scripts are invoked:
Chapt er 1 . Underst anding How Script s Work wit h t he JBoss O N Server and CLI
The JBoss ON CLI can be run manually, execute a script file, and even be invoked automatically by a system tool like cron. The CLI can connect to to any JBoss ON server and can be run remotely.
Essentially, the CLI is a script execution engine. It exposes the remote API in a scripting language, which makes it more convenient to interact dynamically the JBoss ON server.
Server-side scripts are uploaded into a content repository, managed in the JBoss ON server database, and are then invoked in response to a fired alert. Even though the server-side scripts are located on the server, they still use the same APIs to communicate with the server as other CLI scripts.
Report a bug
3.1. Using CommonJS wit h JBoss ON CLI and Server Script s
JBoss ON also supports use of CommonJS module loading mechanism for expressing dependencies among scripts. CommonJS is supported by both the CLI and for server scripts using the req ui re function to locate and load scripts using variables.
Imp o rt an t
Use of the legacy exec function in the CLI interactive session, while still supported, is not the preferred method. It is recommended to port scripts to the loading mechanisms supported by CommonJS as they are more secure, portable and extensible.
For more information about using CommonJS, a list of script sources and their compatibility
with the CLI and server scripts, as well as how to locate and load scripts, refer to Chapter 6,
Using Script Modules for Dependencies and Loading Custom Functions
Report a bug
4 . Differences Bet ween t he JBoss ON CLI and JBoss ON GUI
Operat ions
While there is a lot of overlap between what can be done in the JBoss ON UI and the CLI scripts, there is not complete parity. There are a couple of critical differences:
Alert definition configuration. This is probably the most critical difference. While fired alerts can be retrieved through the CLI and scripts, there is no way to create or edit an alert definition, to set alert notifications, or to set alert conditions.
Manually creating child resources
Defining failover lists for servers
Defining affinity groups for agents
11
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Some other local managers are used internally by the server, so there is no reason for them to be exposed or accessed through the remote API.:
Raw measurement data compression
Processing, caching, and logging alert conditions
Processing partition events
Determining authorization to resources based on role membership
Schedule loaders for metric collection, drift detection, and other scheduled events
Report a bug
5. Using Ot her Client s
Because JBoss ON exposes a large amount of functionality through its APIs, a number of different clients can be written to perform management tasks or to offer custom displays or views for resource data.
There are a number of different types of clients that can be written for JBoss ON:
Java clients, such as a desktop application to view alerts or monitoring charts. The
JBoss ON CLI itself is a Java shell that works as a script execution engine.
Clients in JVM-compatible languages, such as Scala or Groovy
REST clients
Writing and using custom clients is outside the scope of this guide. Still, the breadth and flexibility of JBoss ON's APIs open up opportunities for organizations to integrate JBoss ON with other applications or to create their own, environment-specific clients to make infrastructure management easier.
Report a bug
6. Addit ional Resources
Writing JBoss ON server scripts and using theJBoss ON CLI assumes a certain amount of familiarity with Java principles and JavaScript writing. These are some good tutorials and references to get a better understanding of Java and JavaScript.
Java Scripting Programmer's Guide
12
Chapt er 1 . Underst anding How Script s Work wit h t he JBoss O N Server and CLI
CommonJS resources
Rhino: JavaScript for Java
The Java Persistence Query Language (JEE 5 Tutorial)
JBoss Operations Network Development: Writing Custom Plug-ins
The JBoss ON API
Report a bug
13
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Chapter 2. Installing the JBoss ON CLI
There are two default available ways to run a JBoss ON server script:
In response to an alert
Through the CLI utility
Running a script as part of an alert is configured with the alert definition and does not require any other user intervention.
Running a script manually requires that the CLI utility be installed.
Report a bug
1. Inst alling t he CLI
The CLI utility can be installed on any system, not just a system with a JBoss ON server or agent installed. That CLI utility package is simply unzipped in the desired location.
NO T E
The Java 6 JDK must be installed on the system.
IMPO RT ANT
The remote API can n o t be run from a client inside an application server. For example, the remote API cannot be run from a client inside an EAP instance; it fails with errors like the following:
Caused by: java.
lang .
IllegalArgumentException :
i nterface
org.
rhq .
enterprise .
server .
auth .
SubjectManagerRemote is not visible from
cl ass
loader
at java.
lang .
reflect .
Proxy .
getProxyClass (Proxy.
java : 353 )
at java.
lang .
reflect .
Proxy .
newProxyInstance (Proxy.
java : 581 )
at
org.
rhq .
enterprise .
client .
RemoteClientProxy .
getProcessor (RemoteClie ntProxy.
java : 69 )
1. Open the JBoss ON GUI.
http://server.example.com:7080
2. Click the Ad mi ni strati o n tab in the top menu.
14
3. Select the D o wnl o ad s menu item.
Chapt er 2 . Inst alling t he JBoss O N CLI
4. Scroll to the C o mmand Li ne C l i ent D o wnl o ad section, and click D o wnl o ad
C LI 4 . 9 . JO N. 3. 3.
5. Save the . zi p file into the directory where the CLI should be installed.
6. Unzip the packages. For example:
[jsmith@ server opt]$ unzip rhq-remoting-cli-4.9.JON.3.3.zip
Report a bug
15
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
2. Set t ing CLI Environment Variables
The JBoss ON CLI utility is a Java application, so it supports a large variety of environment variables, JVM settings, and other Java options.
The Java settings and environment variables are defined in the rhq -cl i -env. sh| bat file.
T IP
The rhq -cl i -env. sh| bat file is fully annotated, so all available parameters are listed with full descriptions. If you want to change some of the JVM settings or parameters, read through the rhq -cl i -env. sh| bat file to get an idea of what parameters are available.
Default values are supplied for most of these arguments, and those defaults are sufficient for most operating environments.
T h ere is o n e en viro n men t variab le wh ich mu st b e set f o r every in st allat io n : t h e
Java h o me d irect o ry. This can be set by editing either the R HQ _C LI_JAVA_HO ME or the
R HQ _C LI_JAVA_EXE_FILE_P AT H variable in the rhq -cl i -env. sh| bat file. For example:
RHQ_CLI_JAVA_HOME="/usr/lib/jvm/java-1.6.0-openjdk-
1.6.0.0.x86_64/jdk"
IMPO RT ANT
Do not edit the rhq -cl i . sh| bat file or attempt to set environment variables in that file.
Environment variables and Java options should only be set in the rhq -cl i -
env. sh| bat file.
Editing the rhq -cl i . sh| bat file may cause unexpected behavior or prevent the utility from running.
Report a bug
3. CLI Files and Direct ories
These are the directories and relevant files for the CLI utility.
T ab le 2.1. Imp o rt an t CLI Ut ilit y Direct o ries an d Files
File o r Direct o ry
cliRoot/rhq -remo ti ng -cl i -
4 . 9 . 0 . JO N320 G A
Descrip t io n
The installation directory. The CLI utility is simply unzipped, so the installation directory can be anywhere on a system.
16
Chapt er 2 . Inst alling t he JBoss O N CLI
File o r Direct o ry
CLI Scrip t s
cliRoot/rhq -remo ti ng -cl i -
4 . 9 . 0 . JO N320 G A/bi n rhq -cl i . sh | rhq -cl i . bat
Contains both Linux (shell) and Windows
(batch) scripts for the CLI utility.
The CLI utility.
Descrip t io n
rhq -cl i -env. sh | rhq -cl i -env. bat Sets environment variables for the CLI utility, such as the Java home directory and Java options.
cliRoot/rhq -remo ti ng -cl i -
4 . 9 . 0 . JO N320 G A/sampl es/mo d ul es/
Contains sample CommonJS modules. This is also the default directory for loading
CommonJS modules using the mo d ul es: /
URI.
util.js
Defines search functions for iterating through an array of object (fo reach), to return the first matching object (fi nd ) or to return all matching objects (fi nd Al l ). It also has functions for converting information from JavaScript hashes to
JBoss ON configuration objects and back.
The same as the sampl es/uti l . js script, but written as a CommonJS module.
bundles.js
Defines functions to create and deploy a bundle, create a bundle destination, or get information on supported base directories for a resource.
The same as the sampl es/bund l es. js script, but written as a CommonJS module.
drift.js
Defines functions to create and diff snapshots, get a definition, and show the history for a resource or specific file.
The same as the sampl es/d ri ft. js script, but written as a CommonJS module.
17
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Descrip t io n
jbossas.js
File o r Direct o ry
Defines several functions to manage EAP 6 /
AS 7 resources, including deploying web applications and managing EAP clusters.
The same as the sampl es/d epl o y-to -
and -restart-JBAS. js and
sampl es/ad d -as7-stand al o ne-
server-to -cl uster. js scripts, but combined and written as a CommonJS module.
Samp les
cliRoot/rhq -remo ti ng -cl i -
4 . 9 . 0 . JO N320 G A/sampl es uti l . js measurement_uti l s. js d ri ft. js bund l es. js d epl o y-to -and -restart-JBAS. js
Mo d u le Samp le Scrip t s
Lo g g in g
cliRoot/rhq -remo ti ng -cl i -
4 . 9 . 0 . JO N320 G A/co nf
Contains both sample JavaScript files and supplemental JavaScript files which can be adapted to work with other scripts.
A utility script which provides additional functions for search, mapping, and listing objects.
A utility script which simplifies updating metrics schedules. Instead of having to understand the underlying measurement
APIs, it provides a way to define the metrics to change based on their friendly (UI) name and then both enables/disables and sets collection intervals in simpler terms.
A sample script which contains functions to create a snapshot, search for a specific drift definition, compare snapshots, and view drift histories.
A sample script which sets up a bundles definition. This includes generating a bundle archive, creating a destination group, and then deploying the bundle.
A sample script which deploys a new bundle or updated bundle to a group of JBoss servers, and then restarts the servers in the destination group.
Contains the XML files to configure the log4j logger for the CLI utility.
18
File o r Direct o ry l o g 4 j. xml l o g 4 j-d ebug . xml
cliRoot/rhq -remo ti ng -cl i -
4 . 9 . 0 . JO N320 G A/l o g s rhq -server-cl i . l o g
Lib raries
cliRoot/rhq -remo ti ng -cl i -
4 . 9 . 0 . JO N320 G A/l i b
Report a bug
Chapt er 2 . Inst alling t he JBoss O N CLI
Descrip t io n
Sets the configuration for regular and debug logging for the CLI utility.
Contains the error logs for the CLI utility.
This directory is created when the CLI is first run.
The error log for the CLI utility.
Contains all of the libraries used specifically by the CLI utility, including some libraries for proxy resources and CLI-specific commands.
19
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Part II. Basic Examples for Running JBoss ON Scripts
Through the CLI
One of the ways to run scripts on the JBoss ON server is through the JBoss ON CLI.
[1]
The
JBoss ON CLI is essentially a script interpreter which processes JavaScript — either interactively or from a file — and executes it inside the Java virtual machine. The CLI uses the server's remote API, an underlying domain API, and some of its own CLI-specific methods to make common tasks easier to execute.
The remote API, domain API, and CLI-specific methods are exposed to CLI and to scripts so that they can be easily used.
Report a bug
Other methods include alert scripts and custom clients.
20
Chapt er 3. Commands and O pt ions wit h t he CLI
Chapter 3. Commands and Options with the CLI
The JBoss Operations Network CLI is only one way to run scripts on the server, but it is a flexible and powerful way to create and execute scripts.
The JBoss ON CLI is essentially a script interpreter which processes JavaScript — either interactively or from a file — and executes it within the Java virtual machine. (The
background concepts for the CLI are covered in more detail in Chapter 1, Understanding How
Scripts Work with the JBoss ON Server and CLI
The JBoss ON CLI builds on the defined remote and domain API with its own internal commands, options, and methods which make writing scripts and managing resources easier. One of the biggest assets is the ability to create resource proxies, which are simplified
(and therefore easier to use) resource objects.
This section focuses on the CLI options and basic usage tips. For scripting basics, see
Report a bug
21
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Chapter 4. Checking Compatible CLI and Server Versions
Like the agent and other JBoss ON components, the CLI utility has a version. The CLI utility must be the same version as the JBoss ON server to which it is trying to connect.
If the local JBoss ON CLI is not the same version as the JBoss ON server to which it is trying to connect, then the connection is refused.
NO T E
The CLI utility is not compatible with other versions of the JBoss ON server, neither newer nor older versions.
This behavior is configurable. There may be some instances where there is an external client or wrapper used with the JBoss ON CLI which does not (appear to) match the server version.
To prevent the client connection from being rejected, add a Java option to disable the version checking property in the CLI configuration file.
[root@ server ~]# vim cliRoot/rhq-remoting-cli-
4.9.0.JON320GA/bin/rhq-cli-env.sh
RHQ_CLI_ADDITIONAL_JAVA_OPTS=-Drhq.client.version-check=false
Report a bug
22
Chapt er 5. T he JBoss O N CLI Command Synt ax
Chapter 5. The JBoss ON CLI Command Syntax
The JBoss ON CLI is a shell and interpreter so that commands and statements can be executed interactively against the JBoss ON server. Scripts stored in files can also be executed, so it is possible to automate operations for the JBoss ON server.
Report a bug
1. T he CLI Script
The CLI script is run directly from its cli-install-dir/bi n directory. There are two files associated with launching the JBoss ON CLI:
A script (rhq -cl i . sh| bat)
A file of environment variables (rhq -cl i -env. sh| bat)
The environment variables in the rhq -cl i -env. sh| bat file use defaults that are reasonable for most deployments, so this file usually does not need to be edited. It is possible to reset variables to point a server that doesn't follow the default installation, such as a virtual machine or a non-default JVM. The comments at the top of the rhq -
cl i -env. | bat file contain a detailed list of available environment variables.
IMPO RT ANT
Do not edit the rhq -cl i . sh| bat file. Only set environment variables through the terminal or in the rhq -cl i -env. sh| bat file, not the script itself.
NO T E
Be sure to set the correct path to the Java 6 installation in the R HQ _C LI_JAVA_HO ME or the R HQ _C LI_JAVA_EXE_FILE_P AT H variable.
The rhq -cl i . sh| bat script has the following general syntax: rhq-cli.sh|bat options
It is possible to launch the CLI script without any arguments, including specifying a username. This opens the CLI client without connecting to the server.
[jsmith@ server bin]$ cliRoot/rhq-remoting-cli-
4.9.0.JON320GA/bin/rhq-cli.sh
RHQ - RHQ Enterprise Remote CLI unconnected$
23
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
While scripts can be executed without logging in, most of the functionality of the CLI is unavailable. To truly use the JBoss ON CLI, log into the server as a JBoss ON user, either by passing a username and password or by using the l o g i n command after starting the CLI.
[jsmith@ server bin]$ rhq-cli -u rhqadmin -p rhqadmin
The CLI provides two modes of operation: interactive and non-interactive.
Interactive mode executes an individual statement. Interactive mode provides a simple environment for prototyping, testing, learning, and discovering features of the CLI.
Non-interactive mode loads a specified script file and executes multiple commands in sequence. Non-interactive mode provides the capability to automate tasks such as collecting metrics on managed resources or executing a scheduled operation.
IMPO RT ANT
These native commands, like q ui t, are available only in interactive mode. They can n o t be used in a script when the CLI is used in non-interactive mode, such as when running a script from file. In these instances, use the Java method.
After logging in, any commands (covered in
Section 3, “ Interactive CLI Commands” ) can be
passed to the server.
IMPO RT ANT
Whatever user you run the CLI as — meaning, whatever system user runs the rhq -
cl i . sh script — must have write access to the l o g s/ directory for the CLI.
If the CLI is installed as root, for example, then a regular user cannot run the CLI; it fails with write errors.
Report a bug
2. CLI Script Opt ions
Both rhq -cl i . bat and rhq -cl i . sh scripts accept the options listed in Table 5.1,
T ab le 5.1. Co mman d - Lin e O p t io n s
-h
Sh o rt O p t io n
--help
Lo n g O p t io n Descrip t io n
Displays the help text of the command line options of the
CLI.
24
-u
-p
-P
Sh o rt O p t io n
-s
-t
-c
-f
-v
--user
Lo n g O p t io n
--password
--host
--port
--command
--file
--args-style
--version
--transport
Chapt er 5. T he JBoss O N CLI Command Synt ax
Descrip t io n
The username used to log into the JBoss ON server.
The password used to log into the JBoss ON server.
Displays a password prompt where input is not echoed backed to the screen.
The JBoss ON server against which the CLI executes commands.
Defaults to localhost.
The port on which the JBoss
ON server is accepting HTTP requests. The default is
7080.
A command to be executed.
The command must be encased in double quotes.
The CLI will exit after the command has finished executing.
The full path and filename of a script to execute.
Indicates the style or format of arguments passed to the script.
Displays CLI and JBoss ON server version information once connected to the CLI.
Determines whether or not
SSL will be used for the communication layer protocol between the CLI and the JBoss ON server. If not specified the value is determined from the {port} option. If you use a port that ends in 443, SSL will be used. You only need to explicitly specify the transport when your JBoss
ON server is listening over
SSL on a port that does not end with 443.
Report a bug
25
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
3. Int eract ive CLI Commands
Some native commands are included in the org.rhq.enterprise.client.commands inside the
CLI JAR itself. These commands are part of the CLI itself. Other input in the JBoss ON CLI is passed through the JavaScript interpreter to the server; these commands are passed to the
CLI module.
Section 3.5, “ exec (deprecated)”
IMPO RT ANT
These native commands are available only in interactive mode. They can n o t be used in a script when the CLI is used in non-interactive mode, such as when running a script from file. In these instances, you must use the Java method.
Report a bug
3.1. login
Logs into a JBoss ON server with the specified username and password.
Optionally, the hostname (or IP address) and port can be specified. The hostname defaults to localhost, and the port defaults to 7080.
It is also possible to specify a transport protocol, which sets whether to use SSL to communicate with the server. If the transport is not given, then the CLI evaluates the transport based on the port. A port ending in 443 automatically uses SSL, while all other ports use standard connections. The only reason to explicitly set a transport method is if the server is listening over SSL over a port which does not end in 443.
login username password [host] [port]
The l o g i n command can be used in a module script or with the rhq -cl i . sh --f option.
Report a bug
3.2. logout
26
Chapt er 5. T he JBoss O N CLI Command Synt ax
Logs off of the JBoss ON server without existing from the CLI.
logout
The l o g o ut command can be used in a module script or with the rhq -cl i . sh --f option.
Report a bug
3.3. quit
Exits the CLI.
quit
This only works when the CLI is running interactively. In a script, use
java. l ang . System. exi t.
Report a bug
3.4 . record
Records user input commands to a file. This is very useful if you are running the CLI interactively to test a new script that will later be run non-interactively in the CLI or as an alert server-side script.
record [-b | -e] [-a] -f filename
O p t io n Descrip t io n
-b, --start
-e, --end
-a, --append
Specify this option to start recording.
Specify this option to stop recording.
Appends output to the end of a file. If not specified, output will be written starting at the beginning of the file.
The file where output will be written.
-f, --file
Report a bug
27
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
3.5. exec (deprecat ed)
NO T E
While an external script can be loaded into the CLI session using the exec command, this method of loading custom scripts and functions is deprecated. It is recommended that developers use a CommonJS module script. The default location for module scripts is cliRoot/rhq-remoting-cli-4.9.0.JON320GA/samples/modules.
Executes a statement or a script with the specified file name. A statement wraps onto multiple lines using backslashes.
O p t io n Descrip t io n
-f, --file
-s, --style=named|indexed
The full path filename of the script to execute. The full path must be given, or the
CLI cannot locate the script.
Indicates the style or format of arguments passed to the script. It must have a value of either i nd exed or named .
Report a bug
28
Chapt er 6 . Using Script Modules for Dependencies and Loading Cust om Funct ions
Chapter 6. Using Script Modules for Dependencies and
Loading Custom Functions
When automating management tasks or creating clients to interact withJBoss ON, it is frequently necessary to create custom classes and functions. The JBoss ON CLI implements
CommonJS to support script modules.
No t e
Script modules can be either JavaScript or Python files.
Report a bug
1. About Script Dependencies and Export ing Funct ions
When executing a script from a file by using the rhq -cl i . sh -f option, there is no way to define an explicit dependency within an external file. Any functions required by that script must be contained in the script or accessible to the script.
The JBoss ON CLI uses CommonJS to support script modules. CommonJS is designed as a loading mechanism to express dependencies between scripts. If a script is in the default modules directory, then the script can be loaded into the CLI session or referenced in a script, and any exported function is available to the CLI. Modules can be used to load other modules into the CLI session, as well.
The only part of a script that is exposed is a function prepended with expo rts (following the
CommonJS convention).
For example, the sample uti l . js file exports a fo reach function which can search for any object in JBoss ON, as generic objects, arrays, criteria (search) objects, or maps.
exports.foreach =
functi o n
(obj, fn) {
var
criteriaExecutors = {
Alert :
functi o n
(criteria) {
return
AlertManager.findAlertsByCriteria(criteria); },
...
8 < ...
}
All of the functions and objects that are not exported are private and cannot be used outside the module.
A module can be loaded into another mdule (creating a dependency) by listing the other module as a requirement.
var
printResults = require( "modules:/samples/modules/util.js" );
29
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
T IP
Only an exported function is available, and then it is only available through a variable defined in the script. For example, for the pri ntR esul ts variable, the fo reach function is available, referenced as pri ntR esul ts. fo reach.
This means that difference script modules can have functions defined in them with the same name, but because there are different variable namespaces, there is no conflict.
Report a bug
2. Default Locat ions for Script Module Sources
The req ui re statement loads a modules from an identified location to be used in a script.
The JBoss ON CLI, CLI script, or server-side script can load a module from any of several locations, for both the local and remote sources.
The req ui re line has the format sourceType:/path/moduleFile.
The default module locations are defined in the following list.
Mo d u les Direct o ry
Description — Loads the specified module file from a configured location on the local system. The CLI has a system property (rhq.scripting.modules.root-dir) which defines a default root directory for modules. The modules location value then sets a subdirectory, within that root, where the module is located.
The location can be set or changed by updating the Java properties in the rhq -
cl i -env. sh file:
RHQ_CLI_ADDITIONAL_JAVA_OPTS='-Drhq.scripting.modules.rootdir=/opt/rhq/modules' bin/rhq-cli.sh
Format — modules:/relative-path
Example:
var
myModule = require( "modules:/myCompany/production/serverLocations" ); //This will load the module from a file called "serverLocations.js" from a file that is located in the "myCompany/production" directory under a configured "root-dir" location.
30
Available For — CLI
Lo cal Filesyst em
Chapt er 6 . Using Script Modules for Dependencies and Loading Cust om Funct ions
Loads the specified module file from an absolute path on the local system.
Format — file:/path
Example:
var
myModule = require( "file:/opt/jon/js-modules/myModule" );
//This will load the module from a file called "myModule.js" in the /opt/jon/js-modules directory on the local filesystem.
Available For — CLI
JBo ss O N Rep o sit o ry
Downloads the specified module file from the given JBoss ON repository. Because this connects to the server, a user must be logged into the JBoss ON server first.
Format — rhq://repositories/repo_name/module_name
Example:
var
myModule = require( "rhq://repositories/myRepo/myModule" );
//This will load the module stored in the RHQ server repository
"myRepo" called "myModule.js"
Available for — CLI
JBo ss O N Do wn lo ad s
Downloads the specified module file from the downloads location on the JBoss ON server. JBoss ON could make some scripts available that can be used for alert server-side scripts or for the CLI. Because this connects to the server, a user must be logged into the JBoss ON server first.
Format — rhq://downloads/path/module_name
Example:
var
myModule = require( "rhq://downloads/script-modules/util" );
//This will load the module stored in the RHQ server downloads section called "js/util.js"
Available For — CLI, Server
Report a bug
3. Creat ing a New Module Source T ype
31
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
JBoss ON can load a CommonJS script module from different locations, including both local directories and remote repositories.
It is possible to create support for loading modules from a different source, such as a URL.
Creating a new module source location requires three things:
Creating a new Scri ptSo urceP ro vi d er interface.
Adding the Java service entry in the MET A-INF/servi ces directory of a JAR for the class.
Putting the new JAR into the l i b/ directory of the CLI installation directory to be available to clients and in serverRoot/jo n-server-
3. 3/mo d ul es/o rg /rhq /server-startup/mai n/d epl o yments/rhq . ear/l i b on the JBoss ON server so that it is available to server-side scripts (alert scripts).
Examp le 6 .1. HT T P Mo d u le So u rce Lo cat io n
This script source provider downloads a script module from a given URI as long as te
Java version supports HTTP or HTTPS protocols.
public class URLScriptSourceProvider implements
ScriptSourceProvider {
public Reader getScriptSource(URI scriptUri) {
i f
(scriptUri == null) {
return
null;
}
try {
return new
InputStreamReader(scriptUri.toURL().openStream())
}
catch
(MalformedURLException e ) {
return
null;
}
catch
(IOException e ) {
return
null;
}
}
}
In the MET A-INF directory of the script provider JAR, create a servi ces directory.
Then, within the MET A-INF/servi ces/ directory, create a file called
o rg . rhq . scri pti ng . Scri ptSo urceP ro vi d er. Put the full class names of any source providers implemented in the JAR, one per line. For example, if the
UR LScri ptSo urceP ro vi d er is the only source provider: com.example.URLScriptSourceProvider
This is the layout of the JAR:
32
* com
* example
* URLScriptSourceProvider.class
* META-INF
* services
* org.rhq.scripting.ScriptSourceProvider
This JAR must then ba added to the l i b of the CLI installation directory. After that, scripts run through the JBoss ON CLI on that system can use the HTTP provider to download the file from the network:
var
myModule = require( "http://server.example.com/rhqscripts/stuff.js" );
For the same source provider to be available to alert scripts, the JAR needs to be added to the mo d ul es/o rg /rhq /server-startup/mai n/d epl o yments/rhq . ear/l i b/ directory of the JBoss ON server.
Report a bug
Chapt er 6 . Using Script Modules for Dependencies and Loading Cust om Funct ions
33
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Chapter 7. Available Implicit Variables in the JBoss ON API
In the Java programming language, classes in the java.lang package do not have to be imported; they are automatically made available. Classes in other packages, however, have to be explicitly imported.
In the JBoss ON CLI, there are a number of classes, particularly from the domain API, that are used routinely. To simplify using the JBoss ON CLI, everything under the
o rg . rhq . co re. d o mai n class is automatically imported, which makes it easier to use the
CLI for managing resources, alerts, and other configuration areas. For example, the class
o rg . rhq . co re. d o mai n. cri teri a. R eso urceC ri teri a is commonly used to query resources. The entire class path can be given when calling that class:
var
criteria =
new
org.rhq.core.
domain .criteria.ResourceCriteria();
var
resource =
new
org.rhq.core.
domain .resource.Resource();
Because the domain class is already imported, this can be more succinctly written as:
var
criteria =
new
ResourceCriteria();
var
resource =
new
Resource();
Common variables used with the CLI scripts are listed in Table 7.1, “ Variables Available by
Default to the JBoss ON CLI” . Methods and other information about these variables are in
Chapter 8, Methods Specific to the JBoss ON CLI
.
T ab le 7.1. Variab les Availab le b y Def au lt t o t h e JBo ss O N CLI
rhq
Variab le T yp e
org.rhq.enterprise.cli
ent.Controller
Descrip t io n Access Req u ires
Lo g in
Provides built-in commands to the interactive CLI: login, logout, quit, exec, and version. Two of these methods, login and logout, can be called in server script files, such as
rhq . l o g i n(' rhq a d mi n' ,
' rhq ad mi n' ).
YES
34
Variab le
subject
Assert pretty unlimitedPC pageControl exporter
ProxyFactory scriptUtil
AlertManager
Chapt er 7 . Available Implicit Variables in t he JBoss O N API
T yp e
org.rhq.core.domain
.auth.Subject
org.rhq.bindings.util
.ScriptAssert
org.rhq.enterprise.cli
ent.TabularWriter
Descrip t io n
Represents the current, logged in user. For security purposes, all remote service invocations require the subject to be passed; however, the CLI will implicitly pass the subject for you.
Provides assertion utilities for CLI scripts.
Access Req u ires
Lo g in
YES
NO
Provides for tabularformatted printed and handles converting objects, particularly domain objects in the packages under org.rhq.core.domain, into a format suitable for display in the console.
NO
NO org.rhq.core.domain
.util.PageControl
org.rhq.core.domain
.util.PageControl
org.rhq.enterprise.cli
ent.Exporter
Used to specify paging and sorting on data retrieval operations
Used to export output to a file.
Supported formats are plain text in tabular format and
CSV.
NO
NO
NO org.rhq.bindings.clie
nt org.rhq.enterprise.cli
ent.utility.ScriptUtil
org.rhq.enterprise.se
rver.alert.AlertManag
erRemote
Provides methods that can be useful when writing scripts.
Provides an interface into the alerts subsystem.
NO
YES
35
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Variab le T yp e Descrip t io n Access Req u ires
Lo g in
YES AlertDefinitionMana ger org.rhq.enterprise.se
rver.alert.AlertDefiniti
onManagerRemote
Provides an interface into the alerts definition subsystem.
AvailabilityManager org.rhq.enterprise.se
rver.measurement.Av
ailabilityManagerRe mote
Provides an interface into the measurement subsystem that can be used to determine resources' availability.
YES
YES CallTimeDataManag er org.rhq.enterprise.se
rver.measurement.C
allTimeDataManage rRemote
Provides an interface into the measurement subsystem for retrieving call-time metric data.
YES RepoManager org.rhq.enterprise.se
rver.content.RepoMa
nagerRemote
Provides an interface into the content subsystem for working with repositories.
ConfigurationManag er org.rhq.enterprise.se
rver.configuration.C
onfigurationManage rRemote
Provides an interface into the configuration subsystem.
YES
DataAccessManager org.rhq.enterprise.se
rver.report.DataAcce
ssRemote
Provides an interface for executing userdefined queries.
EventManager
MeasurementBaselin eManager org.rhq.enterprise.se
rver.event.EventMan
agerRemote org.rhq.enterprise.se
rver.measurement.M
easurementBaseline
ManagerRemote
Provides an interface into the events subsystem.
Provides an interface into the measurement subsystem for working with measurement baselines.
MeasurementDataM anager org.rhq.enterprise.se
rver.measurement.M
easurementDataMan agerRemote
Provides an interface into the measurement subsystem for working with measurement data.
YES
YES
YES
YES
36
Chapt er 7 . Available Implicit Variables in t he JBoss O N API
MeasurementDefiniti onManager
ResourceGroupMan ager
ResourceTypeMana ger
Variab le
MeasurementSched uleManager
OperationManager
ResourceManager
GroupDefinitionMan ager
RoleManager
SubjectManager
T yp e Descrip t io n
org.rhq.enterprise.se
rver.measurement.M
easurementDefinitio nManagerRemote
Provides an interface into the measurement subsystem for working with measurement definitions.
org.rhq.enterprise.se
rver.measurement.M
easurementSchedul eManagerRemote
Provides an interface into the measurement subsystem for working with measurement schedules.
org.rhq.enterprise.se
rver.operation.Opera
tionManagerRemote
Provides an interface into the operation subsystem.
org.rhq.enterprise.se
rver.resource.Resour
ceManagerRemote
Provides an interface into the resource subsystem.
org.rhq.enterprise.se
rver.resource.group.
ResourceGroupMan agerRemote
Provides an interface into the resource group subsystem.
org.rhq.enterprise.se
rver.resource.group.
definition.GroupDefi
nitionManagerRemot e
Provides an interface for creating and managing dynagroups.
org.rhq.enterprise.se
rver.resource.Resour
ceTypeManagerRem ote
Provides an interface into the resource subsystem for working with resource types.
org.rhq.enterprise.se
rver.authz.RoleMana
gerRemote
Provides an interface into the security subsystem for working with security rules and roles.
org.rhq.enterprise.se
rver.auth.SubjectMa
nagerRemote
Provides an interface into the security subsystem for working with users.
Access Req u ires
Lo g in
YES
YES
YES
YES
YES
YES
YES
YES
YES
37
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Variab le
SupportManager
T yp e Descrip t io n
org.rhq.enterprise.se
rver.support.Support
ManagerRemote
Provides an interface into the reporting subsystem for getting reports of managed resources.
Access Req u ires
Lo g in
YES
Report a bug
38
Chapt er 8 . Met hods Specific t o t he JBoss O N CLI
Chapter 8. Methods Specific to the JBoss ON CLI
Some classes and methods are available to the JBoss ON CLI and JBoss ON server scripts which are not part of the regular API.
Report a bug
39
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Chapter 9. Methods Available to the CLI and Server Scripts
1. Assert
Provides assertion utilities for CLI scripts. More information about using Java assertions is in the Java language documentation .
Met h o d
Assert.assertEquals
Sig n at u re
assertEquals(float, float, float, String) assertEquals(short, short,
String) assertEquals(double, double, double) assertEquals(long, long,
String) assertEquals(byte, byte,
String) assertEquals(Object, Object) assertEquals(char, char,
String) assertEquals(Object, Object,
String) assertEquals(double, double, double, String) assertEquals(byte[], byte[]) assertEquals(boolean, boolean) assertEquals(Object[], Object[],
String) assertEquals(Collection,
Collection) assertEquals(Object[], Object[]) assertEquals(byte, byte) assertEquals(float, float, float) assertEquals(char, char) assertEquals(int, int) assertEquals(long, long) assertEquals(Collection,
Collection, String) assertEquals(short, short) assertEquals(String, String,
String) assertEquals(byte[], byte[],
String) assertEquals(boolean, boolean,
String) assertEquals(String, String) assertEquals(int, int, String)
4 0
Met h o d
Assert.assertEqualsNoOrder
Chapt er 9 . Met hods Available t o t he CLI and Server Script s
Sig n at u re
assertEqualsNoOrder(Object[],
Object[], String) assertEqualsNoOrder(Object[],
Object[])
Assert.assertExists
Assert.assertFalse
Assert.assertNotNull
Assert.assertNotSame
Assert.assertNull
Assert.assertNumberEqualsJS
Assert.assertSame
Assert.assertTrue
Assert.fail
assertExists(String) assertFalse(boolean) assertFalse(boolean, String) assertNotNull(Object) assertNotNull(Object, String) assertNotSame(Object, Object,
String) assertNotSame(Object, Object) assertNull(Object) assertNull(Object, String) assertNumberEqualsJS(double, double, String) assertSame(Object, Object,
String) assertSame(Object, Object) assertTrue(boolean, String) assertTrue(boolean) fail() fail(String, Throwable) fail(String)
4 1
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Report a bug
2. Subject
Represents the current logged-in JBoss ON user.
Met h o d
subject.addLdapRole
Sig n at u re
addLdapRole(Role) subject.addRole
subject.department
subject.emailAddress
subject.factive
subject.firstName
subject.fsystem
subject.id
subject.lastName
subject.ldapRoles
addRole(Role) addRole(Role, boolean)
Prints the department value (if any) for the current user.
Prints the email address for the current user.
Prints whether the user account is active.
Prints whether the first name of the user.
Prints the ID number for the user account within JBoss ON.
Prints the surname for the user.
Lists any roles associated with LDAP groups to which the current user is a member.
Prints the JBoss ON user ID of the current user.
subject.name
subject.ownedGroups
subject.phoneNumber
subject.removeLdapRole
Prints the phone number, if any exists, for the current user.
removeLdapRole(Role) subject.removeRole
removeRole(Role)
4 2
subject.roles
Met h o d
subject.sessionId
subject.smsAddress
subject.toString
subject.userConfiguration
Chapt er 9 . Met hods Available t o t he CLI and Server Script s
Sig n at u re
Prints the role name, permissions, associated LDAP users and groups, associated resource groups, and other information about every role to which the current user belongs.
Prints the current session ID number.
Returns the pager number, if it exists, for the user.
String toString()
Returns all of the dashboard information, based on the configured portlets, dashboards, and settings that are specific to the logged-in user.
Report a bug
3. pret t y
Converts CLI objects (particularly search results and other domain objects) into a pretty-print format in the output.
pretty.exportMode
Met h o d
pretty.print
pretty.width
Sig n at u re
Prints the current export setting for the server.
print(String[][]) print(PropertySimple, int) print(Configuration) print(PropertyMap, int) print(PropertyList, int) print(Collection) print(Map) print(Object[]) print(Object)
Prints the current width settings for the console display.
Report a bug
4 . unlimit edPC and pageCont rol
4 3
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Sets paging and sorting settings for returned data.
Met h o d
unlimitedPC.addDefaultOrderingField
Sig n at u re
addDefaultOrderingField(String,
PageOrdering) addDefaultOrderingField(String) unlimitedPC.clone
unlimitedPC.firstRecord
unlimitedPC.getExplicitPageControl
clone()
Returns the first record in the results page.
PageControl getExplicitPageControl(int, int) unlimitedPC.getSingleRowInstance
PageControl getSingleRowInstance() unlimitedPC.getUnlimitedInstance
PageControl getUnlimitedInstance() unlimitedPC.initDefaultOrderingField
initDefaultOrderingField(String
) initDefaultOrderingField(String
, PageOrdering) unlimitedPC.orderingFields
unlimitedPC.orderingFieldsAsArray
unlimitedPC.pageNumber
unlimitedPC.pageSize
unlimitedPC.primarySortColumn
unlimitedPC.primarySortOrder
unlimitedPC.removeOrderingField
Returns the current page number for paged results.
Returns the current configured page size
(number of returned entries per page).
removeOrderingField(String)
4 4
unlimitedPC.reset
Met h o d
unlimitedPC.setPrimarySort
unlimitedPC.setPrimarySortOrder
unlimitedPC.sortBy
unlimitedPC.startRow
unlimitedPC.toString
unlimitedPC.truncateOrderingFields
Chapt er 9 . Met hods Available t o t he CLI and Server Script s
Sig n at u re
reset() setPrimarySort(String,
PageOrdering) setPrimarySortOrder(PageOrderin g) sortBy(String)
Returns the current starting row number.
String toString() truncateOrderingFields(int)
Report a bug
5. export er
Writes the CLI output to a specified file.
Met h o d
exporter.close
exporter.file
exporter.format
exporter.pageWidth
exporter.setFormat
close()
Sig n at u re
Shows the current configured output format.
Shows the configured line length for content in the output file.
setFormat(String)
4 5
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
exporter.setFile
Met h o d Sig n at u re
setFile(String) exporter.setPageWidth
setPageWidth(int) exporter.setTarget
setTarget(String, String) exporter.write
write(Object)
Report a bug
6. ProxyFact ory
Provides specialized methods to make it easier and simpler to manage resource objects.
Met h o d
ProxyFactory.getResource
Sig n at u re
ResourceClientProxy getResource(int)
ProxyFactory.outputWriter
ProxyFactory.remoteClient
ProxyFactory.resource
Report a bug
7. script Ut il
Provides utilities to use for writing CLI scripts.
Returns information about the managers and configuration used by the remote client.
In the interactive CLI, this prints information about the manager beans used by the interactive CLI.
Met h o d Sig n at u re
4 6
Met h o d
scriptUtil.findResources
Chapt er 9 . Met hods Available t o t he CLI and Server Script s
Sig n at u re
PageList<Resource> findResources(String) scriptUtil.getFileBytes
byte[] getFileBytes(String) scriptUtil.isDefined
boolean isDefined(String) scriptUtil.saveBytesToFile
saveBytesToFile(byte[], String) scriptUtil.sleep
sleep(long) scriptUtil.waitForScheduledOperationToCo
mplete
ResourceOperationHistory waitForScheduledOperationToComp lete(ResourceOperationSchedule, long, int)
ResourceOperationHistory waitForScheduledOperationToComp lete(ResourceOperationSchedule)
Report a bug
8. Met hods Available t o Proxy Resources
The ProxyFactory classes provide shortcuts for a lot of common resource management tasks, such as viewing monitoring data, running operations, or changing the resource or plug-in configuration. These methods are not in the regular API, but they can be used both by the JBoss ON CLI and by JBoss ON server-side scripts.
The shortcuts and methods available through ProxyFactory are different, depending on the resource type. Methods are only available if the resource type supports that functional area.
This section lists the three most common resource types:
Table 9.1, “ Proxy Methods for Platforms”
4 7
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Table 9.2, “ Proxy Methods for JBoss AS/EAP Servers”
Table 9.3, “ Proxy Methods for Content Sources (EARs, WARs, JARs)”
T IP
Use tab-complete in the interactive CLI to find the specific methods available for a resource type or to get the method signatures for individual methods.
Using proxy resources is covered in Section 2, “ Using Resource Proxies” .
T ab le 9 .1. Pro xy Met h o d s f o r Plat f o rms
measurements operations
OSName description freeSwapSpace systemLoad userLoad
In f o rmat io n Met h o d s
Displays a pretty-print list of the available metrics, current values, and description of all measurements for the platform resource.
Lists the available operations for the resource type.
Sh o rt cu t Met ric Met h o d s
OSVersion architecture distributionName hostname distributionVersion idle totalSwapSpace modifiedDate usedSwapSpace waitLoad createdDate freeMemory totalMemory usedMemory version id (inventory ID number)
Sh o rt cu t Reso u rce En t ry Met h o d s
resourceType name (inventory name) manualAutodiscovery
Sh o rt cu t O p erat io n Met h o d s
cleanYumMetadataCache viewProcessList editPluginConfiguration()
Sh o rt cu t Co n f ig u rat io n Met h o d s
pluginConfiguration pluginConfigurationDefinition
Sh o rt cu t Co n t en t Met h o d s
contentTypes
Sh o rt cu t In ven t o ry Met h o d s
children
4 8
platform.getChild
Met h o d
Chapt er 9 . Met hods Available t o t he CLI and Server Script s
Sig n at u re
ResourceClientProxy getChild(String) platform.getMeasurement
platform.updatePluginConfiguration
platform.toString
Measurement getMeasurement(String)
PluginConfigurationUpdate updatePluginConfiguration(Confi guration)
String toString()
T ab le 9 .2. Pro xy Met h o d s f o r JBo ss AS/EAP Servers
measurements operations
In f o rmat io n Met h o d s
Displays a pretty-print list of the available metrics, current values, and description of all measurements for the JBoss resource.
Lists the available operations for the resource type.
Sh o rt cu t Met ric Met h o d s
JVMMaxMemory JVMTotalMemory buildDate createdDate activeThreadCount description
JVMFreeMemory activeThreadGroup
Count modifiedDate transactionsCommitt ed partitionName startDate transactionsCommitt edperMinute versionName totalTransactions transactionsRolledb ack version totalTransactionsper
Minute transactionsRolledb ackperMinute id (inventory ID number)
Sh o rt cu t Reso u rce En t ry Met h o d s
resourceType name (inventory name) restart
Sh o rt cu t O p erat io n Met h o d s
shutdown start
4 9
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
editPluginConfiguration()
Sh o rt cu t Co n f ig u rat io n Met h o d s
pluginConfiguration pluginConfigurationDefinition
Sh o rt cu t Co n t en t Met h o d s
contentTypes
Sh o rt cu t In ven t o ry Met h o d s
children
Met h o d Sig n at u re
jbossas.getChild
ResourceClientProxy getChild(String) jbossas.getMeasurement
Measurement getMeasurement(String) jbossas.updatePluginConfiguration
PluginConfigurationUpdate updatePluginConfiguration(Confi guration) jbossas.toString
String toString()
T ab le 9 .3. Pro xy Met h o d s f o r Co n t en t So u rces ( EARs, WARs, JARs)
measurements operations
In f o rmat io n Met h o d s
Displays a pretty-print list of the available metrics, current values, and description of all measurements for the content resource.
Lists the available operations for the resource type.
createdDate path
Sh o rt cu t Met ric Met h o d s
modifiedDate version description exploded id (inventory ID number)
Sh o rt cu t Reso u rce En t ry Met h o d s
resourceType name (inventory name)
50
Chapt er 9 . Met hods Available t o t he CLI and Server Script s
Sh o rt cu t O p erat io n Met h o d s
revert editPluginConfiguration()
Sh o rt cu t Co n f ig u rat io n Met h o d s
pluginConfiguration pluginConfigurationDefinition contentTypes
Sh o rt cu t Co n t en t Met h o d s
backingContent
Sh o rt cu t In ven t o ry Met h o d s
children
Met h o d Sig n at u re
content.getChild
ResourceClientProxy getChild(String) content.getMeasurement
Measurement getMeasurement(String) content.updatePluginConfiguration
PluginConfigurationUpdate updatePluginConfiguration(Confi guration) content.toString
String toString() content.retrieveBackingContent
retrieveBackingContent(String fileName) content.updateBackingContent
updateBackingContent(String filename, String displayVersion)
Report a bug
51
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Chapter 10. Common Actions with JBoss ON CLI Scripts
IMPO RT ANT
The remote API can n o t be run from a client inside an application server. For example, the remote API cannot be run from a client inside an EAP instance; it fails with errors like the following:
Caused by: java.
lang .
IllegalArgumentException :
i nterface
org.
rhq .
enterprise .
server .
auth .
SubjectManagerRemote is not visible from
cl ass
loader
at java.
lang .
reflect .
Proxy .
getProxyClass (Proxy.
java : 353 )
at java.
lang .
reflect .
Proxy .
newProxyInstance (Proxy.
java : 581 )
at
org.
rhq .
enterprise .
client .
RemoteClientProxy .
getProcessor (RemoteClie ntProxy.
java : 69 )
Report a bug
1. Logging In
The CLI actually connects to the JBoss ON server, much like connecting through the GUI.
This means that you have to log into the server before you can perform most tasks.
There are two ways to log into the server through the CLI:
By passing user credentials when the rhq -cl i . sh script is run.
By using the co mmand in a script or after starting the CLI without connecting to the server.
When logging into the server, other connection information, such as a server name and port number, can be passed with the login command.
Examp le 10.1. Lo g g in g in t o a Sp ecif ied Server
This connects to the CLI and logs into the JBoss ON server on 192.168.1.100 over port
70443. Because the port number ends with 443, the CLI automatically attempts to connect over SSL.
rhq-cli.sh -u rhqadmin -p rhqadmin -s 192.168.1.100 -t 70443
Examp le 10.2. Pro mp t in g f o r a Passwo rd
52
Chapt er 1 0 . Common Act ions wit h JBoss O N CLI Script s
Instead of sending the password in clear text, it is possible to have the server prompt for a password. This is probably unnecessary when connecting to a server on the local host, but it is useful if the target JBoss ON server is on a different system.
rhq-cli.sh -u rhqadmin -P
Report a bug
2. Using Resource Proxies
The JBoss ON CLI interacts directly with the JBoss ON server through remote APIs for handling resource objects and through the domain APIs for tasks like searches.
The JBoss ON CLI itself provides another API layer that can make it easier to perform common operations. The CLI can create a resource proxy object in the CLI, and then that object uses the classes available in the P ro xyFacto ry to interact with the remote and domain API.
One thing to remember is that proxy resources still use the remote and domain API. The proxy API just provides a simpler and clearer API on top of the remote and domain APIs that can make it easier to script many operations.
NO T E
The P ro xyFacto ry is available to the JBoss ON CLI in interactive mode or when using a script file. It is also available to server scripts, such as scripts used for alerting.
The P ro xyFacto ry gets information about a resource, which is identified in the
g etR eso urce() method with the resource's ID number.
At its simplest, P ro xyFacto ry can return a complete summary of information about the specified resource, such as its current monitoring data and traits, resource name, available metrics, available operations, content information, and child inventory, all dependent on the resource type. For example:
rhqadmin@ localhost:7080$ ProxyFactory.getResource
(
10001
)
ResourceClientProxy_
$$ _javassist_0:
OSName: Linux
OSVersion: 2.6.32-220.4.1.el6.x86_64
architecture: x86_64
children:
contentTypes: { rpm= RPM File }
createdDate: Mon Feb 06 11:24:50 EST 2012
description: Linux Operating System
distributionName:
Red Hat Enterprise Linux Server
distributionVersion: release 6.2
(
Santiago
)
freeMemory: 16.7GB
freeSwapSpace: 25.6GB
handler:
53
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
hostname : server.example.com
id : 10001
idle: 70.8%
measurements: [
Wait Load, Used Memory, System
Load, Distribution Version, Total Memory, OS Name, Free Memory,
Hostname , Architecture, Distribution Name, Idle, Total Swap Space,
Used Swap Space, User Load, OS Version, Free Swap Space]
modifiedDate: Mon Feb 06 11:24:50 EST 2012
name: server.example.com
operations: [viewProcessList, cleanYumMetadataCache, manualAutodiscovery]
pluginConfiguration:
pluginConfigurationDefinition:
ConfigurationDefinition[xml: id= 10009, name= Linux]
resourceType: Linux
systemLoad: 0.0%
totalMemory: 23.5GB
totalSwapSpace: 25.6GB
usedMemory: 6.8GB
usedSwapSpace: 0.0B
userLoad: 15.8%
version: Linux 2.6.32-220.4.1.el6.x86_64
waitLoad: 0.0%
To truly manage resources, the P ro xyFacto ry creates a resource proxy object.
Examp le 10.3. Def in in g a Plat f o rm Pro xy Reso u rce var
rhelServerOne = ProxyFactory.getResource( 10001 )
54
The methods that are available to a resource proxy depend on the resource type and the resource's own configuration. There are five major types of operations that can be performed on resource proxies:
Viewing basic information about the resource, such as its children
Getting measurement information
Running operations
Changing resource and plug-in configuration
Updating and retrieving content
For each resource type, methods are exposed which allow you to find and use specific information about the resource. Additionally, the proxy API includes "shortcuts" which provide one-word methods, without requiring any parameters, to perform common remote API tasks, like getting monitoring information.
Chapt er 1 0 . Common Act ions wit h JBoss O N CLI Script s
The proxy API for common resource types is listed in Section 8, “ Methods Available to Proxy
T IP
Use tab-complete in the interactive CLI to find the specific methods available for a resource type or to get the method signatures for individual methods.
Examp le 10.4 . Viewin g a Reso u rce' s Ch ild ren
P ro xyFacto ry has a method for all proxy objects, chi l d ren, which lists all of the children for the proxy resource.
var rhelServerOne = ProxyFactory.getResource(10001) rhqadmin@ localhost:7080$ platform.children
Array of org.rhq.bindings.client.ResourceClientProxy
[10027] Bundle Handler - Ant (Ant Bundle Handler::AntBundlePlugin)
[10026] CPU 6 (CPU::Platforms)
[10025] CPU 0 (CPU::Platforms)
[10024] CPU 5 (CPU::Platforms)
[10023] CPU 1 (CPU::Platforms)
[10022] CPU 4 (CPU::Platforms)
[10021] CPU 2 (CPU::Platforms)
[10020] CPU 3 (CPU::Platforms)
[10019] CPU 7 (CPU::Platforms)
[10018] /boot (File System::Platforms)
[10017] / (File System::Platforms)
[10016] /dev/shm (File System::Platforms)
[10015] /home (File System::Platforms)
[10014] eth1 (Network Adapter::Platforms)
[10013] eth2 (Network Adapter::Platforms)
[10012] eth0 (Network Adapter::Platforms)
[10011] lo (Network Adapter::Platforms)
[10004] postgres (Postgres Server::Postgres)
[10003] AS server.example.com RHQ Server (JBossAS Server::JBossAS)
[10002] RHQ Agent (RHQ Agent::RHQAgent)
Examp le 10.5. Viewin g Reso u rce Met rics
P ro xyFacto ry provides a set of shortcut metrics for each individual measurement for a resource type. This corresponds to the fi nd Li veD ata() method in the remote API, but it is much easier to get monitoring information quickly and it is simpler to identify what metrics are available.
To get a single measurement value, use the method for that resource type. (Get a list of all methods for a proxy object using tab-complete.) var jbossas = ProxyFactory.getResource(14832)
55
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
rhqadmin@ localhost:7080$ jbossas.JVMTotalMemory
Measurement:
name: JVM Total Memory
displayValue: 995.3MB
description: The total amount of memory currently available in the app server JVM for current and fut...
Alternatively, simply get a list of metrics with their current values using the measurements method: var rhelServerOne = ProxyFactory.getResource(10001) rhqadmin@ localhost:7080$ rhelServerOne.measurements
Array of org.rhq.bindings.client.ResourceClientProxy$Measurement name displayValue description
-------------------------------------------------------------------
----------------------------------------------------
Wait Load 0.0%
Percentage of all CPUs waiting on I/O
Used Memory 6.3GB The total used system memory
System Load 0.0%
Percentage of all CPUs running in system mode
Distribution Version release 6.2 (Santiago) version of the Linux distribution
Total Memory 31.4GB The total system memory
OS Name Linux Name that the operating system is known as
Free Memory 25.2GB The total free system memory
Hostname server.example.com Name that this platform is known as
Architecture x86_64 Hardware architecture of the platform
Distribution Name Red Hat Enterprise Linux Server name of the Linux distribution
Idle 92.6% Idle percentage of all CPUs
Total Swap Space 33.6GB The total system swap
Used Swap Space 0.0B The total used system swap
User Load 16.7%
Percentage of all CPUs running in user mode
OS Version 2.6.32-220.4.2.el6.x86_64 Version of the operating system
Free Swap Space 33.6GB The total free system swap
16 rows
56
Chapt er 1 0 . Common Act ions wit h JBoss O N CLI Script s
Examp le 10.6 . Ru n n in g O p erat io n s o n a Pro xy
P ro xyFacto ry has a shortcut method for every operation available for a resource.
First, get the list of operations available for the resource type using the o perati o ns method: var rhelServerOne = ProxyFactory.getResource(10001) rhqadmin@ localhost:7080$ rhelServerOne.operations
Array of org.rhq.bindings.client.ResourceClientProxy$Operation name description
-------------------------------------------------------------------
---------------------------------------------------viewProcessList View running processes on this system cleanYumMetadataCache Deletes all cached package metadata manualAutodiscovery Run an immediate discovery to search for resources
3 rows
Then, run the given operation method.
rhqadmin@ localhost:7080$ rhelServerOne.viewProcessList();
Invoking operation viewProcessList
Configuration [11951] - null
processList [305] { pid name size userTime kernelTime
-------------------------------------------------------------------
-----------------------------------------------
1 init 19865600 150
10050
....
26285 httpd 214618112 90
80
26286 httpd 214618112 90
80
26288 httpd 214618112 110
70
26289 httpd 214618112 90
80
27357 java 4734758912 1289650
373890
30458 postgres 218861568 1820
27440
30460 postgres 180985856 1210
5330
30462 postgres 218984448 13080
42200
30463 postgres 218861568 3970
26940
30464 postgres 219328512 10600
15320
30465 postgres 181407744 18680
57
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
78760
30482 httpd 185905152 1660
7520
32410 bash 108699648 0
10
32420 java 6024855552
3890240 669810
305 rows
}
Examp le 10.7. Ch an g in g Co n f ig u rat io n Pro p ert ies
If the resource type supports resource configuration editing or if the resource type has plug-in connection properties, then the resource proxy has methods —
ed i tR eso urceC o nfi g urati o n() and ed i tP l ug i nC o nfi g urati o n(), respectively — to edit those properties.
The current configuration can be printed using the g et*C o nfi g urati o n). For example, for the plug-in configuration: var rhelServerOne = ProxyFactory.getResource(10001) rhqadmin@ localhost:7080$ rhelServerOne.getPluginConfiguration()
Configuration [10793] - null
metadataCacheTimeout = 1800
enableContentDiscovery = false
yumPort = 9080
enableInternalYumServer = false
logs [0] {
}
The ed i t*C o nfi g urati o n() method brings up a configuration wizard that goes through all of the properties individually and prompts to keep or change each value. The properties are even grouped according to the same organization that the JBoss ON web
UI uses. For example:
rhqadmin@ localhost:
7080 $ rhelServerOne.editPluginConfiguration();
Non-Grouped Properties:
Group: Content
enableContentDiscovery[
fal se
]:
enableInternalYumServer[
fal se
]:
yumPort[
9080 ]:
metadataCacheTimeout[
1800 ]:
Group: Event Logs
[R]eview, [
E ]dit, Re[V]ert [S]ave or [C]ancel:
...
After each group, you have the option to revert or save the changes. Once the changes are saved, they are immediately updated on the JBoss ON server.
58
return ctrl-d ctrl-k ctrl-e
Keys
Chapt er 1 0 . Common Act ions wit h JBoss O N CLI Script s
Act io n
Selects the default or existing value for a property.
The same as selecting the unset checkbox in the configuration UI.
Exits the configuration wizard.
Displays the help description for the current property.
Examp le 10.8. Man ag in g Co n t en t o n Reso u rces
Some types of resources have content associated with them. These are typically EAR or
WAR resources within an application server. The content file actually associated with that
EAR/WAR resource is called backing content. These are usually JARs.
This content can be updated or downloaded from the resource.
To retrieve backing content (meaning, to download the JAR/EAR/WAR file), specify the filename and file path on the application server. For example: var contentResource = ProxyFactory.getResource(14932) contentResource.retrieveBackingContent("/resources/backup/original.w
ar")
To update the content for the resource, use the upd ateBacki ng C o ntent method and specify the filename with the path on the application server to put the content and the version number of the content. For example: contentResource.updateBackingContent("/resources/current/new.war",
"2.0")
Report a bug
3. Passing Command and Script Argument s
When connecting to the CLI using rhq -cl i . sh, commands or full scripts can be passed simultaneously. This is a non-interactive way to connect to the CLI, since the CLI runs the specified command or script and then exits, rather than staying connected in interactive mode.
Examp le 10.9 . Passin g Variab les t o t h e Server
59
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
A single command can be passed to the CLI by using the -c. In this example, the server searches for and prints all supported resource types for the server and prints the results to reso urce_types. txt
rhq-cli.sh -u rhqadmin -p rhqadmin -c
"pretty.print(ResourceTypeManager.findResourceTypesByCriteria(new
ResourceTypeCriteria()))" > resource_types.txt
The R eso urceT ypeManag er. fi nd R eso urceT ypesByC ri teri a(new
R eso urceT ypeC ri teri a()) class invokes the fi nd R eso urceT ypesByC ri teri a operation on R eso urceT ypeManag er. A new R eso urceT ypeC ri teri a object is passed as the argument.
Nothing has been specified on the criteria object so all resource types will be returned.
pretty is an implicit object made available to commands and scripts by the CLI. This is useful for outputting objects in a readable, tabular format which is designed for domain objects.
This single command provides a nicely formatted, text-based report of the resource types in the inventory.
Examp le 10.10. Ru n n in g a Scrip t
This executes the script file, my_scri pt. js. The CLI terminates immediately after the script has finished executing.
cliRoot/rhq-remoting-cli-4.9.0.JON320GA/bin/rhq-cli.sh -f
/export/myScripts/my_script.js
Examp le 10.11. Han d lin g Scrip t Arg u men t s
A feature common to most programming languages is the ability to pass arguments to the program to be executed. In Java, the entry point into a program is a class's main method, and it takes a String array as an argument. That array holds any arguments passed to the program. Similarly, arguments can be passed to CLI scripts. Arguments passed to a script can be accessed in the implicit args array:
i f
(args.
length < 2 ) {
throw
"Not enough arguments!" ;
}
fo r
(i
i n
args) {
println(
'args[' + i + '] = ' + args[i]);
}
60
Chapt er 1 0 . Common Act ions wit h JBoss O N CLI Script s
The custom arg s variable is only available if it is explicitly defined in a CLI script or if it is added as a module in the cliRoot/rhq -remo ti ng -cl i -
4 . 9 . 0 . JO N320 G A/sampl es/mo d ul es directory.
In addition to the traditional style of indexed-based arguments, named arguments can also be passed to a script:
[jsmith@ server ~]$ cliRoot/rhq-remoting-cli-4.
9.0
.JON320GA/bin/rhqcli.sh -s jon-server.example.com -u rhqadmin -p rhqadmin -t 7080 echo_args.js --args-style=named x = 1 y = 2
The echo _arg s. js, for example, is written to accept the two named option with the script invocation, x and y.
fo r
(i
i n
args) {
println(
'args[' + i + '] = ' + args[i]);
}
println(
'named args...' );
println(
'x = ' + x );
println(
'y = ' + y );
This simple script echoes the given x and y values.
args[0] =
1
args[
1 ] = 2
named args...
x = 1
y = 2
Be aware of the following:
Explicitly specify that you are using named arguments with the --arg s-styl e option.
The values of the named arguments are still accessible through the implicit args array.
The named arguments, such as x and y, are bound into the script context as variables.
Examp le 10.12. Execu t in g a Sin g le St at emen t
When running the CLI interactively, commands can still be passed and executed, same as using the -c option with rhq -cl i . sh.
localhost:
7080 >
var
x = 1
61
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Examp le 10.13. Execu t in g a Mu lt i- Lin e St at emen t
The CLI is a Java shell interpreter, and it can handle multi-line statements properly. To indicate a new line in the same statement, use the backslash (\) character.
localhost:
7080 (rhqadmin)>
fo r
(i = 1 ; i < 3 ; ++i) { \
localhost:
7080 (rhqadmin)> println(i); \
localhost:
7080 (rhqadmin)> }
1
2
localhost:
7080 (rhqadmin)>
Examp le 10.14 . Execu t in g a Scrip t
To run a script from a file, use the -f option with the rhq -cl i . sh| bat script.
The -f option must give the absolute location of the script, even if it is in the same directory as the rhq -cl i . sh script. The CLI will not find a script with only a relative path.
[jsmith@ server ~]$ cliRoot/rhq-remoting-cli-4.
9.0
.JON320GA/bin/rhqcli.sh -u rhqadmin -p rhqadmin -s jon-server.example.com -p 7080 -f
/absolute/path/to/myscript.js
Examp le 10.15. Execu t in g a Scrip t wit h Arg u men t s
A script can be written to accept or require arguments; this is described more in Section 3,
“ Defining Arguments and Other Parameters for the CLI Scripts” . Indexed arguments can
be passed simply by supplying them in the proper order, as specified in the JavaScript file.
[jsmith@ server ~]$ cliRoot/rhq-remoting-cli-4.
9.0
.JON320GA/bin/rhqcli.sh -u rhqadmin -p rhqadmin -s jon-server.example.com -p 7080 -f
/absolute/path/to/myscript.js 1 2 3
Examp le 10.16 . Execu t in g a Scrip t wit h Named Arg u men t s
Script arguments can be indexed or named.
[jsmith@ server ~]$ cliRoot/rhq-remoting-cli-4.
9.0
.JON320GA/bin/rhqcli.sh -u rhqadmin -p rhqadmin -s jon-server.example.com -p 7080 -args-style=named -f /absolute/path/to/myscript.js x = 1 y = 2 y = 3
62
Report a bug
Chapt er 1 0 . Common Act ions wit h JBoss O N CLI Script s
4 . Displaying Pret t y-Print Out put
The JBoss ON CLI has a special class that formats JBoss ON information into table-style output. This class (T abul arWri ter) is implicit for all CLI commands, so almost all output is properly formatted automatically. This class is also available as an implicit variable called
pretty, which is useful when writing scripts.
For example:
rhqadmin@ localhost:
7080 $ criteria = ResourceCriteria()
rhqadmin@ localhost:
7080 $ criteria.addFilterResourceTypeName( 'service-alpha' )
rhqadmin@ localhost:
7080 $ criteria.addFilterParentResourceName( 'server-omega-0' )
rhqadmin@ localhost:
7080 $ resources =
ResourceManager.findResourcesByCriteria(criteria)
id name version resourceType
------------------------------------------------
11373 service-alpha8 1.0
service-alpha
11374 service-alpha1 1.0
service-alpha
11375 service-alpha-0 1.0
service-alpha
11376 service-alpha-4 1.0
service-alpha
11377 service-alpha2 1.0
service-alpha
11378 service-alpha3 1.0
service-alpha
11379 service-alpha5 1.0
service-alpha
11380 service-alpha9 1.0
service-alpha
11381 service-alpha6 1.0
service-alpha
11382 service-alpha7 1.0
service-alpha
10 rows
pretty formats any object defined in the domain (org.rhq.core.domain) package.
Simply printing the output is much less readable:
rhqadmin@ localhost:
7080 $ println(resources)
PageList[Resource[id=
11373 , type=service-alpha, key=servicealpha8 , name =service-alpha8 , version= 1.0
],
Resource[id=
11374 , type=service-alpha, key=service-alpha1 , name =service-alpha1 , version= 1.0
],
....
8 < ....
For a single object, pretty checks for the summary information (@ Summary), so that it only displays a subset of information. It then prints the summary information for the single object as a formatted list. For example:
rhqadmin@ localhost:
7080 $ pretty.
print (resources.get(0))
Resource:
id:
11373
name : service-alpha8
63
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
version:
1.0
resourceType: service-alpha
Report a bug
5. Export ing Out put
expo rt is another implicit script variable that writes output to a specified file. expo rter uses
pretty. pri nt to output all of the information to a plaintext file that matches the table-style formatting used in the interactive display.
rhqadmin@ localhost:
7080 $ exporter.setTarget( 'raw' , 'output.txt' )
rhqadmin@ localhost:
7080 $ exporter.
write (resources)
File IO operations like opening or closing the file are not a problem because expo rter handles the IO operations.
Alternatively, expo rter can write the raw resource or other information to a CSV file:
rhqadmin@ localhost:
7080 $ exporter.setTarget( 'csv' , 'output.csv' )
rhqadmin@ localhost:
7080 $ exporter.
write (resources)
Report a bug
64
Chapt er 1 0 . Common Act ions wit h JBoss O N CLI Script s
Chapter 11. Tips and Tricks for Using the CLI
JBoss ON CLI scripts make it possible to automate tasks, from simply importing discovered resources to running complex management operations to remedy configuration drift or schedule web app upgrades.
The JBoss ON CLI has some usability features to make it easier to use interactively for developing scripts, to integrate with system tools to help task automation, and to create custom functions for more versatile, real-life applications.
Report a bug
1. Using T ab Complet e
In interactive mode, the JBoss ON CLI is aware of the implicit variables ( Chapter 7, Available
Implicit Variables in the JBoss ON API
) in the domain API and the JBoss ON remote API, as well
as the specific CLI commands and methods (
Chapter 8, Methods Specific to the JBoss ON CLI ).
When running the CLI interactively, any of these methods can be filled in using T ab complete.
Filling in part of a class lists potential matching classes (or, if it matches, commands). For example:
[jsmith@ server bin]# ./rhq-cli.sh -u rhqadmin -p rhqadmin
RHQ Enterprise Remote CLI 3.3
Remote server version is: 3.3
(
2484565
)
Login successful
rhqadmin@ localhost:7080$ Resource
ResourceFactoryManager ResourceGroupManager ResourceManager
ResourceTypeManager
rhqadmin@ localhost:7080$ ex
exporter exec
After selecting a class, hitting T ab once lists all of the methods for that class.
rhqadmin@ localhost:7080$ ResourceManager.
availabilitySummary disableResources
enableResources findChildResources
findResourceLineage findResourcesByCriteria
getAvailabilitySummary getLiveResourceAvailability
getParentResource getResource
getResourcesAncestry liveResourceAvailability
parentResource resource
toString uninventoryResources
updateResource
65
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Hitting T ab twice prints the full method signatures:
rhqadmin@ localhost:7080$
List
< Integer > enableResources
(
GenericArrayTypeImpl[int]
)
Resource updateResource
(
Resource resource
)
List
< Integer > uninventoryResources
(
GenericArrayTypeImpl[int] resourceIds
)
ResourceAvailabilitySummary getAvailabilitySummary
(
int resourceId
)
PageList
< Resource > findChildResources
(
int resourceId,
PageControl pageControl
)
Map
< Integer,String > getResourcesAncestry
(
GenericArrayTypeImpl[Integer] resourceIds,
ResourceAncestryFormat format
)
List
< Integer > disableResources
(
GenericArrayTypeImpl[int]
)
List
< Resource > findResourceLineage
(
int resourceId
)
ResourceAvailability getLiveResourceAvailability
(
int resourceId
)
PageList
< Resource > findResourcesByCriteria
(
ResourceCriteria criteria
)
Resource getResource
(
int resourceId
)
String toString()
Resource getParentResource
(
int resourceId
)
Autocomplete is very useful for finding what methods or objects are implicitly available, for building criteria-based searches (because it is easier to select criteria), and for developing server scripts.
Report a bug
2. Differences Bet ween Running t he CLI Int eract ively and wit h
Files
Scripts can be run in the CLI either by entering the lines directly (in interactive mode), by passing a script file to the CLI using the -f option, or by defining the function in a
CommonJS module script.
Scripts that are entered interactively and scripts in a file are substantively the same, with one
exception. In interactive mode, the CLI has a set of commands ( Section 3, “ Interactive CLI
Commands” ). These commands are part of the CLI, and most of those commands are only
available in interactive mode. These commands cannot be referenced inside a referenced script file: quit (which exits the CLI) record
The login and logout commands are not directly available, either, but there are login and logout functions available which can be used within a script.
66
Chapt er 1 1 . T ips and T ricks for Using t he CLI
rhq.login(
'rhqadmin' , 'rhqadmin' );
rhq.logout();
As with the differences between the GUI, CLI, and alert scripts, this is a matter of context. Most of the CLI-defined commands only make sense within an interactive environment, such as recording inputted commands. Outside that interactive context, other Java methods should be used.
Report a bug
3. API Differences Bet ween Resources T ypes and Versions
One important thing to remember about the structure of objects in JBoss ON is that each resource type is defined individually, in its own plug-in descriptor. (There can be multiple resource types in a single plug-in descriptor, and these are all related as parent and children resource types.)
Each resource type has different management APIs, which reflect the software application.
Obviously, a Tomcat server is different than a Postgres database, so they have different metrics, operations, and configuration properties. Even seemingly common traits like stopping and starting a resource are different depending on the resource type descriptor.
Tomcat has an operation to stop gracefully (something not needed for a database or most services), while JBoss AS 5 servers are stopped and then started as separate tasks instead of restarting in one step.
An agent plug-in defines a resource type not only by the application or service, but also by the version of that application or service. An EJB resource is difference than an EJB3 resource, with a different definition.
This distinction — that different versions of the same application are treated as different resource types — is particularly critical for server resources.
Different resource types have configuration properties and operations available. This can impact CLI scripts written to manage those resources. For example, a script written for a
JBoss AS 5 resource may not work for a JBoss AS 4 or JBoss AS 7 resource because the different resources have different APIs.
If you are writing a script which may be used for different versions of a server, create a CLI script which first identifies the resource type and then calls the appropriate method.
Report a bug
4 . Available Ut ilit y and Sample Script s
Several scripts are provided that supply additional functionality, like simplified functions for parsing search results, deploying bundles, or managing drift. These utility scripts add functions without performing any concrete task. They are available as a reference for and
67
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
extension to custom scripts. These example functions can be copied into a script file or can
be loaded before a script is run, as in Section 1, “ About Script Dependencies and Exporting
A couple of scripts are available that both provide additional functions (for reference or ease of use) and also perform management tasks when run. This scripts can be used directly to manage resources, without having to write a custom script.
IMPO RT ANT
Utility and sample scripts have not been extensively tested and may not be updated between releases. These are included for convenience and for real-life, complete examples of writing server scripts.
Test these scripts for quality and consistency within your environment.
T ab le 11.1. Ut ilit y an d Samp le Scrip t s
Scrip t Descrip t io n
Co mmo n JS Mo d u le Ut ilit y Scrip t s
util.js
Defines search functions for iterating through an array of object (fo reach), to return the first matching object
(fi nd ) or to return all matching objects
(fi nd Al l ). It also has functions for converting information from JavaScript hashes to JBoss ON configuration objects and back.
Lo cat io n
cliRoot/rhq-remoting-cli-
4.9.0.JON320GA/samples/m odules/
The same as the
sampl es/uti l . js script, but written as a CommonJS module.
68
bundles.js
Scrip t
drift.js
jbossas.js
Chapt er 1 1 . T ips and T ricks for Using t he CLI
Descrip t io n
Defines functions to create and deploy a bundle, create a bundle destination, or get information on supported base directories for a resource.
Lo cat io n
cliRoot/rhq-remoting-cli-
4.9.0.JON320GA/samples/m odules/
The same as the
sampl es/bund l es. js
script, but written as a
CommonJS module.
Defines functions to create and diff snapshots, get a definition, and show the history for a resource or specific file.
cliRoot/rhq-remoting-cli-
4.9.0.JON320GA/samples/m odules/
The same as the
sampl es/d ri ft. js script, but written as a CommonJS module.
Defines several functions to manage EAP 6 / AS 7 resources, including deploying web applications and managing EAP clusters.
cliRoot/rhq-remoting-cli-
4.9.0.JON320GA/samples/m odules/
The same as the
sampl es/d epl o y-to and -restart-JBAS. js
and sampl es/ad d -as7-
stand al o ne-server-to -
cl uster. js scripts, but combined and written as a
CommonJS module.
Ut ilit y Scrip t s
69
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
util.js
Scrip t
measurement_utils.js
bundles.js
drift.js
Descrip t io n
Defines search functions for iterating through an array of object (fo reach), to return the first matching object
(fi nd ) or to return all matching objects
(fi nd Al l ). It also has functions for converting information from JavaScript hashes to JBoss ON configuration objects and back.
Defines functions to enable, disable, or update metric schedules.
Defines functions to create and deploy a bundle, create a bundle destination, or get information on supported base directories for a resource.
Defines functions to create and diff snapshots, get a definition, and show the history for a resource or specific file.
Lo cat io n
cliRoot/rhq-remoting-cli-
4.9.0.JON320GA/samples/
cliRoot/rhq-remoting-cli-
4.9.0.JON320GA/samples/
cliRoot/rhq-remoting-cli-
4.9.0.JON320GA/samples/
cliRoot/rhq-remoting-cli-
4.9.0.JON320GA/samples/
Samp le Scrip t s
add-as7-standalone-serverto-cluster.js
deploy-to-and-restart-
JBAS.js
fix-bundle-deployment.js
Defines a series of functions that add a specified AS 7 to a cluster.
Defines two functions to deploy new content to EAP servers and to update existing content. With the proper variables set, this script can be run directly to deploy content and restart the given app server.
An example server-side alert script that reverts a bundle deployment to a specified version.
cliRoot/rhq-remoting-cli-
4.9.0.JON320GA/samples/
cliRoot/rhq-remoting-cli-
4.9.0.JON320GA/samples/
serverRoot/jon-server-
3.3/jbossas/server/default/de ploy/rhq.ear/rhqdownloads/cli-alert-scripts/
Report a bug
5. Defining Cust om Funct ions
70
Chapt er 1 1 . T ips and T ricks for Using t he CLI
Custom functions are permissible and supported in CLI scripts as long as they are properly structured and conform to the JBoss ON remote API.
Report a bug
6. Scheduling Script Runs wit h Cron
Administrators may create some scripts to perform routine maintenance on their JBoss ON systems, such as deleting old bundle versions to improve database performance or to add newly-discovered resources to the inventory automatically.
Scripts can be run through the JBoss ON CLI using a simple cron job. For example, an administrator writes i mpo rt. js to add discovered resources to the inventory automatically and runs it daily: vim /etc/cron.daily/rhq-cli
#!/bin/sh
# run JON CLI every morning to import new resources
cliRoot/rhq-remoting-cli-4.9.0.JON320GA/bin/rhq-cli.sh -u rhqadmin
-p rhqadmin -f /export/scripts/import.js
More information about scheduling jobs with cron is available in the crontab manpage .
Report a bug
7. Using Wrapper Script s
Ultimately, JBoss ON CLI scripts are regular JavaScript files. For simplicity or ease of programming, you may write a number of different scripts, each performing a different task or for a different resource type.
This collection of scripts can be controlled through wrapper scripts. Using simple shell scripts can make running the JBoss ON CLI scripts more user-friendly (by providing a nicer interface) or it can make it easier to pass variables to manage specific resources.
There is a simplistic example of using a wrapper script in Chapter 18, Example: Managing
Report a bug
8. Permissions and Set up for JBoss ON Users
Every CLI script, either for a logged in user or through the rhq . l o g i n method, is run on the
JBoss ON server by a JBoss ON user.
71
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
The JBoss ON CLI does enforce access controls to resources touched by JBoss ON CLI scripts. This means that the script's user must meet the same authorization requirements as a
GUI user to perform the given operation.
Additionally, any user who runs a CLI script must already exist in JBoss ON as a user. When the JBoss ON server is configured to create LDAP users, these users are created automatically the first time the LDAP user logs into the JBoss ON web UI. The same account creation does not occur when logging in with the CLI. Therefore, LDAP users must log into the web UI before they can run the JBoss ON CLI.
Report a bug
72
Chapt er 1 1 . T ips and T ricks for Using t he CLI
Chapter 12. Short Examples
CLI scripts follow a certain flow of events. Each functional element within that workflow, such as searching for a resource or object and then running an operation, has its own classes in the remote API.
Most actions in JBoss ON CLI scripts are repeatable — the part for running a search for a platform or for collecting live data for a metric is pretty similar across CLI scripts.
This section provides some basic examples for running a single, specific functional task.
These individual script examples can be used consistently in larger script workflows and as part of task automation.
Report a bug
1. Searches
All object managers define operations for retrieving data. Most of the managers define
criteria-based operations for data retrieval — meaning that the search can be based on attributes within the JBoss ON objects.
Criteria-based searches have methods in the form fi nd ObjectByC ri teri a, so a resource find method is fi nd R eso urcesByC ri teri a and a group find method is
fi nd R eso urceG ro upsByC ri teri a.
Searches are translated into a corresponding JPA-QL query.
The criteria classes reside in the org.rhq.core.domain.criteria package.
Report a bug
1.1. Set t ing Basic Search Crit eria
The simplest criteria is to define results based on what they are, such as resource type, without any additional search parameters.
For example, this fetches all committed resources in the inventory because it has no filters to limit by resource type or ID.
rhqadmin@ localhost:
7080 $
var
criteria =
new
ResourceCriteria() // this sets the criteria to use for the search
rhqadmin@ localhost:
7080 $ criteria.clearPaging() // this clears the 200 item page size to return all entries
rhqadmin@ localhost:
7080 $
var
resources =
ResourceManager.findResourcesByCriteria(criteria) // this actually runs the search
rhqadmin@ localhost:
7080 $ pretty.
print (resources) // this prints the search results
73
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
id name versio curren resour
---------------------------------------------------------------------
----------
10001 server Linux UP Linux
10002 server AS 4.
2 UP JBossA
10392 full-h UP Profil
10014 AlertH UP EJB3 S
10015 Adviso UP EJB3 S
10016 DataAc UP EJB3 S
10017 Affini UP EJB3 S
10011 Access UP Access
10391 ha UP Profil
..
.8
<...
While the fi nd R eso urcesByC ri teri a() is what runs the search, the pretty. pri nt method is required to display the results.
This basic criteria search is translated into the following JPA-QL query:
SELECT r
FROM Resource r
WHERE ( r.inventoryStatus = InventoryStatus.COMMITTED
Report a bug
1.2. Using Sort ing
The basic search criteria can be refined so that the resource results are sorted by plug-in.
To add sorting, call cri teri a. ad d So rtP l ug i nName(). Sorting criteria have methods in the form ad d So rtXXX(P ag eO rd eri ng o rd er).
For example:
rhqadmin@ localhost:
7080 $
var
criteria =
new
ResourceCriteria()
rhqadmin@ localhost:
7080 $ criteria.addSortPluginName(PageOrdering.ASC)
// adds a sort order to the results
rhqadmin@ localhost:
7080 $ resources =
ResourceManager.findResourcesByCriteria(criteria)
This criteria is translated into the following JPA-QL query:
SELECT r
FROM Resource r
WHERE ( r.inventoryStatus = InventoryStatus.COMMITTED )
ORDER BY r.resourceType.
plugin ASC
Report a bug
74
Chapt er 1 2 . Short Examples
1.3. Using Filt ering
Adding additional matching criteria, like resource name in this example, further narrows the search results. To add filtering to any criteria, use methods of the form ad d Fi l terXXX().
rhqadmin@ localhost:
7080 $
var
criteria =
new
ResourceCriteria()
rhqadmin@ localhost:
7080 $ criteria.addFilterResourceTypeName( 'JBossAS
Server' ) // a search filter
rhqadmin@ localhost:
7080 $ resources =
ResourceManager.findResourcesByCriteria(criteria)
The resulting JPA-QL query is as follows:
SELECT r
FROM Resource r
WHERE ( r.inventoryStatus = InventoryStatus.COMMITTED
AND LOWER( r.resourceType.
name ) like 'JBossAS Server' ESCAPE '\\' )
This code retrieves all JBoss servers in the inventory. There can be multiple filters used with a single search. For example, this searches for JBoss servers that have been registered by a particular agent:
rhqadmin@ localhost:
7080 $
var
criteria =
new
ResourceCriteria()
rhqadmin@ localhost:
7080 $ criteria.addFilterResourceTypeName( 'JBossAS
Server' )
rhqadmin@ localhost:
7080 $ criteria.addFilterAgentName( 'localhost.localdomain' )
rhqadmin@ localhost:
7080 $ resources =
ResourceManager.findResourcesByCriteria(criteria)
This generates the following JPA-QL query:
SELECT r
FROM Resource r
WHERE ( r.inventoryStatus = InventoryStatus.COMMITTED
AND LOWER( r.agent.
name ) like 'localhost.localdomain' ESCAPE '\\' )
Report a bug
1.4 . Fet ching Associat ions
An association shows the hierarchy of parent and child resources. After retrieving the resources, it is possible to view their associated parent or child resources using a special
fetch method.
Simply printing a list of children after a search will fail, even if the given server has child resources.
75
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
..
.8
<...
rhqadmin@ localhost:
7080 $ resource = resources.get(0)
rhqadmin@ localhost:
7080 $
i f
(resource.childResources == null) print ( 'no child resources' )
The reason for this is that lazy loading is used throughout the domain layer for one-to-many and many-to-many associations. Since child resources are lazily loaded, the list of children has to explicitly set as a fetch in the search criteria.
For example:
rhqadmin@ localhost:
7080 $ criteria.addFilterResourceTypeName( 'JBossAS
Server' )
rhqadmin@ localhost:
7080 $ criteria.fetchChildResources(true)
rhqadmin@ localhost:
7080 $ resources =
ResourceManager.findResourcesByCriteria(criteria)
rhqadmin@ localhost:
7080 $ resource = resources.get(0)
rhqadmin@ localhost:
7080 $
i f
(resource.childResources == null) print ( 'no child resources' );
el se
pretty.
print (resource.childResources)
id name versio resourceType
-----------------------------------------------------------
222 AlertManagerBean EJB3 Session Bean
222 SchedulerBean EJB3 Session Bean
222 AlertDefinitionManagerBean EJB3 Session Bean
222 AlertConditionConsumerBean EJB3 Session Bean
222 PartitionEventManagerBean EJB3 Session Bean
..
.8
<...
The output varies depending on what is inventoried. These are the child resources of the
JBoss ON server. The JPA-QL query that is generated appears as follows:
SELECT r
FROM Resource r
LEFT JOIN FETCH r.childResources
WHERE ( r.inventoryStatus = InventoryStatus.COMMITTED
AND LOWER( r.resourceType.
name ) like 'JBossAS Server' ESCAPE '\\' )
Report a bug
1.5. Set t ing Page Siz es
Almost all searches return a paged list of results. By default, paged results are capped at
200 entries. So, for example, attempting to return all resources in the inventory only returns the first 200 resources, while querying the database directly may return several hundred resources.
The C ri teri a class defines some methods which can be used to control page sizes for search results.
76
Chapt er 1 2 . Short Examples
Examp le 12.1. Clearin g t h e Pag e Siz e
If there are more than 200 results, and all matching resources need to be contained in a single results set, the page limit can be cleared with the cl earP ag i ng var criteria = new ResourceCriteria() criteria.clearPaging() var resources = ResourceManager.findResourcesByCriteria(criteria)
Examp le 12.2. Set t in g a Pag e Siz e
There can be times when a different page limit needs to be used, but for clarity or control, some paging still needs to be in effect.
The setP ag i ng method sets the number of pages and the page size for the given search. Generally, since there is only a single page, the page number is set to 0, and then the page size can be reset higher or lower, as desired.
rhqadmin@ localhost:
7080 $
var
criteria =
new
ResourceCriteria()
rhqadmin@ localhost:
7080 $ criteria.getPageSize()
200
rhqadmin@ localhost:
7080 $ criteria.getPageNumber()
0
rhqadmin@ localhost:
7080 $ criteria.setPaging(0, 300 )
rhqadmin@ localhost:
7080 $
var
resources =
ResourceManager.findResourcesByCriteria(criteria)
id name currentAvailability resourceType
-------------------------------------------------------------------
-----------------------------------------------
10032 RHQDS UP
Datasource
10033 ResourceFactoryManagerBean UP
EJB3 Session Bean
10034 CoreTestBean UP
EJB3 Session Bean
10035 rhq-postinstaller.war ( //localhost/installer) UP
Web Application (WAR)
10036 ResourceMetadataManagerBean UP
EJB3 Session Bean
10037 SystemInfoManagerBean UP
EJB3 Session Bean
10105 wstools.sh UP
Script
10038 PartitionEventManagerBean UP
EJB3 Session Bean
10039 CallTimeDataManagerBean UP
EJB3 Session Bean
77
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
10040 AlertDefinitionManagerBean UP
EJB3 Session Bean
10041 DiscoveryTestBean UP
EJB3 Session Bean
10123 wsconsume.sh UP
Script
10042 ROOT.war ( //localhost/) UP
Web Application (WAR)
10044 AlertManagerBean UP
EJB3 Session Bean
10045 AgentManagerBean UP
EJB3 Session Bean
...
8 < ...
300 rows
Report a bug
2. Get t ing t he JBoss ON ID for an Object
Everything in JBoss ON — resources, configuration properties, bundle archives, templates, alerts, everything — is identified with a unique ID number. Most of these ID numbers are visible in the web UI as part of the URL which goes to that object's details page.
Not every ID is easily found, though. For example, searching for a metric template requires the resource type ID, but the resource type ID is hard to find.
In other instances, you may want to run a script against multiple resources, which means there are multiple resource IDs which may not be explicitly known at the time you write the script.
Searching for a resource (
Section 1, “ Searches” ) retrieves the resource's details, including
its ID.
rhqadmin@ localhost:
7080 $ criteria =
new
ResourceCriteria();
ResourceCriteria:
inventoryManagerRequired:
fal se
persistentClass: class org.rhq.core.
domain .resource.Resource
rhqadmin@ localhost:
7080 $ criteria.addFilterResourceTypeName( 'Linux' )
rhqadmin@ localhost:
7080 $
ResourceManager.findResourcesByCriteria(criteria);
one row
Resource:
id:
10001
name : gs-dl585g2-01.rhts.eng.bos.redhat.com
version: Linux
2.6
.
32 220.
el6.x86_64
currentAvailability: UP
resourceType: Linux
78
Chapt er 1 2 . Short Examples
This is true for any *C ri teri a search. It retrieves the ID for the object is searches for, even if that ID is not explicitly displayed.
This ID can then be used in other operations.
rhqadmin@ localhost:
7080 $
var
metrics =
MeasurementDataManager.findLiveData(resources.get(0).id,
[mdefs.get(0).id]);
Report a bug
3. Get t ing Dat a for Single and Mult iple Resources
When you run any sort of operation or function, you frequently have to pull the results from that operation, the data, into another function.
For example, this exports and writes whatever the results from the data variable was: exporter.write(data.get(0))
Using g et(0 ) takes the first object that was returned and uses it.
There can be instances when there are a lot of objects returned that could potentially be used in the next step of the script, and there are a couple of different ways to handle it.
One option, instead of g et(0 ), is to work incrementally through each object in a list. For example, this gets the data for each metric definition (i) for a resource.
Examp le 12.3. g et ( i) i f
( mdefs != null ) {
i f
( mdefs.size() > 1 ) {
fo r
( i =0; i < mdefs.size(); ++i) {
mdef = mdefs.get(i);
var
data =
MeasurementDataManager.findDataForResource(resources.get(0).id,
[mdef.id],start,end, 1 )
exporter.
write ( data .get(0));
}
}
..
.8
<...
Other objects can be sent to an array, and then the operation or task can be performed for each object in that array. This example searches for all JBoss AS 5 servers, sends the results to an array, and then performs a restart operation on each server.
79
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Examp le 12.4 . An Array
//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 ();
fo r
( i
i n
resources ) {
i f
( resources[i].resourceType.id == resType.id ) {
jbossServers[idx] = resources[i];
idx = idx +
1 ;
}
}
// restart the resources
fo r
( a
i n
resources ) {
var
jboss = ProxyFactory.getResource(jbossServers[a].id);
jboss.restart()
}
The uti l . js support script has a fo reach function that also iterates cleanly through arrays, collections, and maps, as well as generic objects.
Which method you use to handle multiple objects or whether you even want to handle multiple objects depends on the type of information and the purpose of the script. These are just some options to keep in mind.
Report a bug
4 . Resources and Groups
4 .1. Creat ing and Updat ing Cont ent -Backed Resources (Web Apps)
Web applications (EARs and WARs) are content-backed resources, a cross between a managed resource and a content package.
When running a CLI script, there are some methods available specifically to retrieve or create the content for the resource.
80
Chapt er 1 2 . Short Examples
To create a content-backed resource, upload the file with a specified version number.
This script's steps are:
1. Search for the resource to upload the content to. This example looks for a JBoss AS
5 server.
2. Make sure the server is running. The JBoss server has to be running for content to be deployed successfully.
Examp le 12.5. Creat in g a Co n t en t - Backed Reso u rce
// fill this information in before running the script
var
pathName = '/home/jon/myExampleApp.ear'
var
resTypeName = 'JBossAS Server'
var
pluginName = "JBossAS5"
var
appTypeName = "Enterprise Application (EAR)"
// define a custom function to parse the filename and path info
functi o n
PackageParser( pathName ) {
var
file =
new
java.io.File( pathName );
var
fileName = file.getName();
var
packageType = fileName .
substring ( fileName .
lastIndexOf ( '.' ) +1 );
var
tmp = fileName .
substring (0, fileName .
lastIndexOf ( '.' ));
var
version = 1 ;
var
realName = tmp;
var
packageName = fileName ;
// parse the package version, only if version is included
i f
(tmp.
indexOf ( '-' ) != -1 ){
realName = tmp.
substring (0, tmp.
lastIndexOf ( '-' ));
version = tmp.
substring (tmp.
lastIndexOf ( '-' ) + 1 );
packageName = realName +
"." + packageType;
}
this.packageType = packageType.toLowerCase();
this.packageName = packageName;
this.version = version;
this.realName = realName;
this.
fileName = fileName ;
}
criteria =
new
ResourceCriteria();
criteria.addFilterResourceTypeName(resTypeName);
criteria.addFilterPluginName(pluginName);
var
resources = ResourceManager.findResourcesByCriteria(criteria);
// create the config options for the new EAR
var
deployConfig =
new
Configuration();
deployConfig.put(
new
PropertySimple( "deployExploded" , "false" ));
deployConfig.put(
new
PropertySimple( "deployFarmed" , "false" ));
81
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
// stream in the file bytes
var
file =
new
java.io.File( pathName );
var
inputStream =
new
java.io.FileInputStream(file);
var
fileLength = file.
length ();
var
fileBytes = java.lang.reflect.
Array .newInstance(java.lang.Byte.
TYPE , fileLength);
fo r
(numRead=0, offset=0; ((numRead >= 0) & & (offset < fileBytes.
length )); offset += numRead ) {
numRead = inputStream.read(fileBytes, offset, fileBytes.
length - offset);
}
// parse the filename and path info
PackageParser( pathName );
// identifies the type of resource being created
var
appType =
ResourceTypeManager.getResourceTypeByNameAndPlugin(appTypeName, pluginName);
// create the new EAR resource on each discovered app server
i f
( resources != null ) {
fo r
( i =0; i < resources.size(); ++i) {
var
res = resources.get(i);println( "res: " + res);
ResourceFactoryManager.createPackageBackedResource(
res.id,
appType.id,
packageName,
null,
// pluginConfiguration
packageName,
version,
null,
// architectureId
deployConfig,
fileBytes,
null
// timeout
);
}
}
82
Updating a package is slightly simpler. It requires sending a new file and version number to the existing EAR resource.
Examp le 12.6 . Up d at in g a Co n t en t - Backed Reso u rce
// update this
var
fullPathName = '/export/myfiles/updatedApp.ear'
// define a custom function to parse the filename and path info
functi o n
PackageParser( pathName ) {
Chapt er 1 2 . Short Examples
var
file =
new
java.io.File( pathName );
var
fileName = file.getName();
var
packageType = fileName .
substring ( fileName .
lastIndexOf ( '.' ) +1 );
var
tmp = fileName .
substring (0, fileName .
lastIndexOf ( '.' ));
var
version = 1 ;
var
realName = tmp;
var
packageName = fileName ;
// parse the package version, only if version is included
i f
(tmp.
indexOf ( '-' ) != -1 ){
realName = tmp.
substring (0, tmp.
lastIndexOf ( '-' ));
version = tmp.
substring (tmp.
lastIndexOf ( '-' ) + 1 );
packageName = realName +
"." + packageType;
}
this.packageType = packageType.toLowerCase();
this.packageName = packageName;
this.version = version;
this.realName = realName;
this.
fileName = fileName ;
}
// parse the filename and path info
PackageParser(fullPathName);
// search for the JBoss AS 5 server by name
criteria =
new
ResourceCriteria();
criteria.addFilterName(
'My JBoss AS 5 Server' );
var
res = ResourceManager.findResourcesByCriteria(criteria);
var
jboss = ProxyFactory.getResource(res.get(0).id);
var
children = jboss.children;
fo r
( c
i n
children ) {
var
child = children[c];
i f
( child.
name == packageName ) {
child.updateBackingContent(fullPathName,version);
}
}
While a content-backed resource is added or updated as if it were a software package
(which it is), it is deleted as if it were a resource.
NO T E
Deleting is not the same as uninventorying a resource. Uninventorying removes the resource from the inventory but leaves it intact on the platform. Deleting a resource deletes it from the platform itself.
Examp le 12.7. Delet in g a Co n t en t - Backed Reso u rce
83
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
// search for the content resource by name
criteria =
new
ResourceCriteria();
criteria.addFilterName(
'updatedApp.ear' );
var
res = ResourceManager.findResourcesByCriteria(criteria);
ResourceFactoryManager.deleteResource(res.get(0).id)
84
Report a bug
4 .2. Creat ing a Resource Group and Adding Members
When a group is created by a script, it is initially created as a mixed group. All empty groups are treated as mixed groups. Once members are added, if all the members are the same resource type, then the group is automatically tagged as a compatible group.
Compatible groups are great for scripted management tasks, because the same operation or configuration change can be performed on each member iteratively.
The script steps are:
1. Search for the resource type.
2. Create the group, based on the resource type.
3. Find resources of that resource type.
4. Iterate through the returned resources and add them to the group.
Examp le 12.8. An n o t at ed Examp le
// search for the resource type to use for the compat group
var
resType =
ResourceTypeManager.getResourceTypeByNameAndPlugin( "Linux" , "Platfor ms" );
//create the new resource group
var
rg =
new
ResourceGroup(resType);
rg.setRecursive(
fal se
);
rg.setName(
'Linux Group - ' + java.util.
Date ());
rg = ResourceGroupManager.createResourceGroup(rg);
//find resources to add to the group based on their resource type
criteria =
new
ResourceCriteria();
criteria.addFilterResourceTypeId(resType.id);
var
resources = ResourceManager.findResourcesByCriteria(criteria);
Chapt er 1 2 . Short Examples
// add the found resources to the group
i f
( resources != null ) {
fo r
( i =0; i < resources.size(); ++i) {
var
resource = resources.get(i);
ResourceGroupManager.addResourcesToGroup(rg.id,
[resource.id]);
}
}
Report a bug
4 .3. Creat ing and Edit ing Dynagroups
A dynamic groups specifies a search term to use to search the inventory and identify matching resources to belong to the group. Since the search results change automatically as results are added and removed from the inventory, the group membership is always changing and always current.
Dynamic groups (or dynagroups) have their own classes to create and update groups.
A dynagroup is created by defining a set of expressions which are search filters to use to search the inventory for matching members. The group membership is calculated (using those expressions) by an administrative task.
NO T E
Dynagroups expressions are covered extensively in the Users Guide.
Both for creating an editing a dynagroup, then, it is necessary to recalculate the group membership to capture changes to matching members. It can also be reasonable to recaculate group membership whenever there are changes to the inventory, to update the dynagroups with new members.
Examp le 12.9 . Creat in g a Dyn ag ro u p
This uses simple expressions to identify group members.
// create the new dynagroup
var
dynaGroupDef =
new
GroupDefinition( "Example Linux Server
Group" );
dynaGroupDef.setExpression(
"resource.type.name=Linux" );
dynaGroupDef.setExpression(
"resource.type.category=Platform" );
dynaGroupDef.setExpression(
"resource.availability=UP" );
var
def =
85
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
GroupDefinitionManager.createGroupDefinition(dynaGroupDef);
// calculate the group members
GroupDefinitionManager.calculateGroupMembership(def.getId());
It is possible to use more complex expressions such as groupby within the
setExpressi o n value.
Examp le 12.10. Ed it in g a Dyn ag ro u p
This adds another expression to the dynagroup, updates the definition, and then recalculates the group membership.
// search for the dynagroup
criteria =
new
ResourceGroupDefinitionCriteria()
criteria.addFilterName(
"All agents" );
var
orig =
GroupDefinitionManager.findGroupDefinitionsByCriteria(criteria)
// get the dynagroup entry
var
originalGroupDef = orig.get
// add the new expression
originalGroupDef.setExpression(
"resource.name=*.example.com" );
var
def =
GroupDefinitionManager.updateGroupDefinition(originalGroupDef);
// calculate the group members
GroupDefinitionManager.calculateGroupMembership(def.getId());
Dynagroup memberships are only updated after a calculation operation. So, if there are changes to the inventory, even if there are not changes to the dynagroup expression itself, the dynagroup must be updated to update the members.
Examp le 12.11. Recalcu lat in g a G ro u p Def in it io n
This updates a single dynagroup, based on the group name.
// search for the dynagroup
criteria =
new
ResourceGroupDefinitionCriteria()
criteria.addFilterName(
"All agents" );
var
orig =
GroupDefinitionManager.findGroupDefinitionsByCriteria(criteria)
// calculate the group members
GroupDefinitionManager.calculateGroupMembership(orig.getId());
86
Chapt er 1 2 . Short Examples
It is possible to recalculate all dynagroups or a set of dynagroups, by incrementing through the search results and running the calculatedGroupMembership() method.
This is covered in
Section 3, “ Getting Data for Single and Multiple Resources” .
Report a bug
5. Resource Configurat ion
There are two areas of resource configuration:
The plug-in configuration. In the web UI, this is called the connection setting; it is the information which the agent uses to discover and connect to the resource, such as a PID file path or a port number.
Every resource has some kind of plug-in configuration.
Resource configuration. Configuration properties are drawn from the configuration files for a resource, such as . co nf or . xml . The configuration files and properties that are exposed within JBoss ON are defined in the plug-in descriptor.
Resource configuration is optional, and many resources do not support configuration editing or only expose a subset of possible properties.
T IP
When changing the configuration for a resource, it is simplest to use the configuration
editor with a proxy resource, as in Changing Configuration Properties in Section 2, “ Using
The configuration editor is only available with the interactive CLI; for running an alert script or script automatic changes to resource configuration, the remote API can be used directly.
Report a bug
5.1. Viewing Current Configurat ion
Plug-in (connection settings) configuration and resource configuration are both defined per resource type, in the resource type's plug-in descriptor.
The g et*C o nfi g urati o nD efi ni ti o nFo rR eso urceT ype methods can display the descriptor-defined templates used for the resource type's configuration. These are the configuration properties available to every resource of that type.
Examp le 12.12. Viewin g Plu g - in Co n f ig u rat io n f o r a Reso u rce T yp e
87
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
rhqadmin@ localhost:
7080 $
var
res =
ResourceTypeManager.getResourceTypeByNameAndPlugin( 'Linux' , 'Platfor ms' ) //get the resource type ID
rhqadmin@ localhost:
7080 $
ConfigurationManager.getPluginConfigurationDefinitionForResourceTyp
e(res.id) //use the type ID to search for the resource type template
ConfigurationDefinition:
configurationFormat: Structured
defaultTemplate: ConfigurationTemplate[id=
10443 , name =
d efaul t
, config=Linux]
description :
groupDefinitions: [PropertyGroupDefinition[id=
10058 , name =Content], PropertyGroupDefinition[id= 10059 , name =Event Logs]]
id:
10437
name : Linux
nonGroupedProperties: []
propertyDefinitions: {logs=PropertyDefinitionList[id=
11792 , name =logs, config=Linux, group=Event Logs], metadataCacheTimeout=SimpleProperty[metadataCacheTimeout] ( Type :
INTEGER)(Group: Content), enableContentDiscovery=SimpleProperty[enableContentDiscovery]
( Type : BOOLEAN )(Group: Content), enableInternalYumServer=SimpleProperty[enableInternalYumServer]
( Type : BOOLEAN )(Group: Content), yumPort=SimpleProperty[yumPort]
( Type : INTEGER)(Group: Content)}
templates: {
d efaul t
=ConfigurationTemplate[id= 10443 , name =
d efaul t
, config=Linux]}
88
The pro pertyD efi ni ti o ns parameter contains the information about the configuration properties that can be set, including details about the configuration property setup. For example, for the event logs configuration: logs=[id=11792, name=logs, config=Linux, group=Event Logs]
The property name is logs, and the type of property is a list, PropertyDefinitionList. The property list ID is 11792, though not every type of property has a template ID. The event log properties are organized together in the Event Logs group.
The cache timeout property has a slightly different configuration. This is a simple property type (SimpleProperty), and the expected value is given in the Type: element. The group for the property is Content.
metadataCacheTimeout=SimpleProperty[metadataCacheTimeout] (Type:
INTEGER)(Group: Content)
Group settings for a property are mainly used to create a logical organization in the web UI.
For the event logs configuration, all of the members in the properties list belong to the group, and only those values. For the cache timeout, it belongs to the content group but other, discrete properties also belong to the group, so the way the group is defined is different.
Chapt er 1 2 . Short Examples
The resource configuration template can be retrieved similarly to retrieving the plug-in configuration template.
Examp le 12.13. Viewin g t h e Co n f ig u rat io n Pro p ert ies f o r t h e Reso u rce T yp e
rhqadmin@ localhost:
7080 $
var
res =
ResourceTypeManager.getResourceTypeByNameAndPlugin( 'Samba Server' ,
'Samba' ) //get the resource type ID
rhqadmin@ localhost:
7080 $
ConfigurationManager.getResourceConfigurationDefinitionForResourceTy
pe(res.id)
ConfigurationDefinition:
configurationFormat: Structured
defaultTemplate: ConfigurationTemplate[id=
11123 , name =
d efaul t
, config=Samba Server]
description :
groupDefinitions: [PropertyGroupDefinition[id=
10905 , name =Basic Configurations], PropertyGroupDefinition[id= 10906 , name =Security], PropertyGroupDefinition[id= 10907 , name =Printing],
PropertyGroupDefinition[id= 10908 , name =Active Server Directory]]
id:
11087
name : Samba Server
nonGroupedProperties: []
propertyDefinitions: {winbind enum groups=SimpleProperty[winbind enum groups] ( Type : BOOLEAN )(Group:
Active Server Directory), winbind separator=SimpleProperty[winbind separator] ( Type : STRING )(Group: Active Server Directory), cups options =SimpleProperty[cups options ] ( Type : STRING )(Group:
Printing), workgroup=SimpleProperty[workgroup] ( Type : STRING )
(Group: Basic Configurations), encrypt passwords=SimpleProperty[encrypt passwords] ( Type : BOOLEAN )(Group:
Security), winbind enum users=SimpleProperty[winbind enum users]
( Type : BOOLEAN )(Group: Active Server Directory), security=SimpleProperty[security] ( Type : STRING )(Group: Security), template shell=SimpleProperty[template shell] ( Type : STRING )(Group:
Active Server Directory), password =SimpleProperty[ password ] ( Type :
PASSWORD )(Group: Active Server Directory), load printers=SimpleProperty[ load printers] ( Type : BOOLEAN )(Group:
Printing), username=SimpleProperty[username] ( Type : STRING )(Group:
Active Server Directory), realm=SimpleProperty[realm] ( Type :
STRING )(Group: Active Server Directory), idmap gid=SimpleProperty[idmap gid] ( Type : STRING )(Group: Active Server
Directory), server string =SimpleProperty[server string ] ( Type :
STRING )(Group: Basic Configurations), controller=SimpleProperty[controller] ( Type : STRING )(Group: Active
Server Directory), enableRecycleBin=SimpleProperty[enableRecycleBin] ( Type : BOOLEAN )
(Group: Basic Configurations), idmap uid=SimpleProperty[idmap uid]
( Type : STRING )(Group: Active Server Directory)}
templates:
{
d efaul t
=ConfigurationTemplate[id= 11123 , name =
d efaul t
, config=Samba
Server]}
89
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
The property values for a specific resource can be viewed using the g et*C o nfi g urati o n methods.
T IP
If you are only viewing the resource configuration and are running the interactive CLI, this is done more easily using a proxy resource, as in Changing Configuration Properties in
Section 2, “ Using Resource Proxies” .
Examp le 12.14 . Viewin g a Reso u rce' s Co n f ig u rat io n Set t in g s
rhqadmin@ localhost:
7080 $ criteria =
new
ResourceCriteria(); // find the resource
rhqadmin@ localhost:
7080 $ criteria.addFilterResourceTypeName( 'Samba' )
rhqadmin@ localhost:
7080 $ criteria.addFilterAgentName( 'agent1.example.com' )
rhqadmin@ localhost:
7080 $
var
resource =
ResourceManager.findResourcesByCriteria(criteria);
rhqadmin@ localhost:
7080 $
ConfigurationManager.getResourceConfiguration(resource.get(0).id)
Configuration [
12082 ] - Loaded from Augeas at Wed May 02 12 :04: 24
EDT 2012
winbind separator = null
winbind enum groups = null
cups options = null
workgroup = null
winbind enum users = null
encrypt passwords = null
security = null
template shell = null
password = null
load printers = null
username = null
realm = null
idmap gid = null
server string = null
controller = null
enableRecycleBin =
fal se
idmap uid = null
90
Report a bug
5.2. Changing Simple Configurat ion Propert ies
The way that a configuration property is edited depends on the type of property, whether it is simple, list, or map. The process for changing configuration is the same for both plug-in configuration properties and resource configuration properties.
Chapt er 1 2 . Short Examples
The steps in this script:
1. Search for the resource.
2. Create a configuration object.
3. Set the new property value. The method to set the property value depends on the format of the property. In this case, it uses setSi mpl eVal ue since this is a simple property. For a simple property, the value is set by passing ('property','value').
4. Run an update operation for the resource. For a resource configuration update, the method is upd ateR eso urceC o nfi g urati o n. For a plug-in configuration update, it is upd ateP l ug i nC o nfi g urati o n.
Examp le 12.15. Ch an g in g a Simp le Pro p ert y
// find the resource
criteria =
new
ResourceCriteria();
criteria.addFilterResourceTypeName(
'Samba' )
// this only updates the resource for this specific agent
criteria.addFilterAgentName(
'agent1.example.com' )
var
resources = ResourceManager.findResourcesByCriteria(criteria);
//get current configuration
var
config =
ConfigurationManager.getResourceConfiguration(resources.get(0).id);
//set the new value in the form 'property', 'value'
config.setSimpleValue(
"workgroup" , "example" )
// run the update operation
ConfigurationManager.updateResourceConfiguration(resources.get(0).id
,config)
Report a bug
6. Operat ions
6.1. St art ing and St opping a Resource
A resource can simple be started by using an operation.
This example looks for a specific resource by name, and then runs the start() function.
Examp le 12.16 . Simp le St art
91
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
//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()
Each resource type has its own defined operations, and even simple tasks like start and stop may have different methods depending on the resource. Try using a proxy resource and then the o perati o ns 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
A more complex start or stop script can be used to iterate over an array of resources of the same type.
Examp le 12.17. St art in g 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 ();
fo r
( i
i n
resources ) {
i f
( resources[i].resourceType.id == resType.id ) {
jbossServers[idx] = resources[i];
idx = idx +
1 ;
}
}
// restart the resources
92
Chapt er 1 2 . Short Examples fo r
( a
i n
resources ) {
var
jboss = ProxyFactory.getResource(jbossServers[a].id);
jboss.restart()
}
Examp le 12.18. St art in g 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
fo r
( a
i n
jbossServers ) {
var
jboss = ProxyFactory.getResource(jbossServers[a].id);
jboss.restart()
}
Report a bug
6.2. Scheduling Operat ions
The simplest way to run an operation is to create a proxy for the resource, and then run the operation on that proxy, in the form proxyName.operationName().
Examp le 12.19 . Immed iat e O p erat io n
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] {
}
Operations can be run on a schedule. A schedule requires several configuration pieces:
The resource ID
93
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
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)
This example runs an availability scan on a specific agent.
Examp le 12.20. Sch ed u led O p erat io n Examp le
// 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"
);
94
This immediately prints the information for the scheduled operation.
ResourceOperationSchedule:
resource: Resource[id=
10008 , uuid=e11390ec34 c4-49df-a4b6c37c516f545c, type={RHQAgent}RHQ Agent, key=server.example.com RHQ
Agent, name =RHQ Agent, parent =server.example.com, version= 3.3
]
Report a bug
Chapt er 1 2 . Short Examples
6.3. Ret rieving t he Result s of an Operat ion
Some operations really only report a success or failure, and that is all the information that is required. Other operations, however, may retrieve information from a resource or make a more complex change on the resource. In that case, that information needs to be returned so it can be used in further tasks.
The fetchR esul ts(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)
Examp le 12.21. Prin t in g t h e Resu lt s o f a Pro cess Scan
In this longer example, the script first runs an operation (a process scan on a platform) and then prints the results.
The first part of the script sets up the requirement for the resource ID and then searchs for that resource.
i f
(args.
length != 1 ) {
throw
"we need a resource id as an argument" ;
}
var
platform = ResourceManager.getResource(args[0]);
The next part schedules the process scan operation. As covered in Section 6.2,
“ Scheduling Operations” , this sets the resource ID, operation name, and operation
settings.
95
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s var
ros = OperationManager.scheduleResourceOperation(
platform .id,
"viewProcessList" ,
0,
1 ,
0,
15 ,
null,
"test operation"
);
The last part retrieves the operation history with two additional search settings:
fetchR esul ts(true), which is required to include the operation result data and not just the status a sort method, in this case ad d So rtStartT i me
There are also a couple of sleeps in the script to ensure that the operation has time to complete before the script attempts to retrieve the results.
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 ();
whi l e
(
new
Date ().
getTime () - now < 15000 ) {
operations =
OperationManager.findResourceOperationHistoriesByCriteria(opcrit);
i f
(operations.get(0).getResults() == null) {
println(
"operation still pending result" );
java.lang.Thread.sleep(
1000 );
}
el se
{
pretty.
print (operations.get(0).getResults());
break
;
}
}
i f
(operations.get(0).getErrorMessage() != null) {
println(
"Error getting process list: " );
pretty.
print (operations.get(0).getErrorMessage());
}
This script prints the results of the operation as long as the operation has completed successfully:
96
Chapt er 1 2 . Short Examples
}
el se
{
pretty.
print (operations.get(0).getResults());
break
;
}
Report a bug
6.4 . Checking a Resource's Operat ions Hist ory
The operation history for a resource exists as an object, so it can be searched for, by criteria, same as other objects.
Examp le 12.22. Viewin g t h e O p erat io n Hist o ry
// 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);
The (successful) operation results are in C o nfi g urati o n objects in the results table.
resource results
---------------------------------------------------------------------
--------------------------------------------
Resource[id=
10008 , uuid=e11390ec34 c4-49df-a4b6-c37c516f545c, type=
{RHQAgent}RHQ Agent, key=server.example.com RHQ Age
Configuration[id= 13903 ]
Resource[id=
10008 , uuid=e11390ec34 c4-49df-a4b6-c37c516f545c, type=
{RHQAgent}RHQ Agent, key=server.example.com RHQ Age
Configuration[id= 13913 ]
2 rows
Report a bug
7. Monit oring
7.1. Get t ing Resource Availabilit y
97
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Many operations should only occur if the target resource is running, such as deploying a new web application. Including an availability check in a larger script is helpful for controlling when state-dependent operations are run.
The script steps are:
1. Search for the resource. In this case, the script just looks for any resource which matches the resource type (Linux platform), and uses the first match for the availability scan.
2. Get the current availability status.
Examp le 12.23. Cu rren t Availab ilit y
// get the resource ID
criteria =
new
ResourceCriteria();
criteria.addFilterResourceTypeName(
'Linux' )
var
res = ResourceManager.findResourcesByCriteria(criteria);
// check the current availability
AvailabilityManager.getCurrentAvailabilityForResource(res.get(0).id
)
98
The JBoss ON server returns the resource information, its current status, and the time what the current status began (meaning, if the server is up, the time the server started).
Availability:
availabilityType: UP
endTime:
id:
10192
resource: Resource[id=
10001 , uuid=null, type=<null>, key=null, name =null, parent =<null>]
startTime:
1335974397214
Report a bug
7.2. Get t ing Specific Met rics
The monitoring information in JBoss ON is not a live reading. There are two reasons for that: scans are periodic, not streaming, and the information for baselines and averages are processed (aggregated).
The fi nd Li veD ata method is a way to pull in the current, un-average, live reading of a given metric.
The script steps are:
Chapt er 1 2 . Short Examples
1. Search for the available metric definitions, based on the resource type and then filtered to a single metric. This example grabs the free memory metric for the Linux platform.
2. Search for the resource.
3. Get the current reading for the metric.
4. Print the data to the terminal.
Examp le 12.24 . An n o t at ed Examp le
// search for the resource
criteria =
new
ResourceCriteria();
criteria.addFilterResourceTypeName(
'Linux' );
var
resources = ResourceManager.findResourcesByCriteria(criteria);
// search for the resource type to use in the metrics definition
var
rt =
ResourceTypeManager.getResourceTypeByNameAndPlugin( "Linux" ,
"Platforms" );
// search for the metric definition
var
mdc = MeasurementDefinitionCriteria();
mdc.addFilterDisplayName(
"Free Memory" );
mdc.addFilterResourceTypeId(rt.id);
var
mdefs =
MeasurementDefinitionManager.findMeasurementDefinitionsByCriteria(md c);
//get the data
var
metrics =
MeasurementDataManager.findLiveData(resources.get(0).id,
[mdefs.get(0).id]);
// as a nice little display, print the retrieved metrics value
i f
( metrics !=null ) {
println(
" Metric value for " + resources.get(0).id + " is "
+ metrics );
}
With this example, the current, live reading for the metric is printed to the screen.
Metric value
fo r
10001 is [MeasurementDataNumeric[ value =
[ 6.3932239872E10
], MeasurementData [MeasurementDataPK: timestamp=[Tue
May 08 20 : 10 : 15 EDT 2012 ], scheduleId=[ 10002 ]]]]
Report a bug
7.3. Export ing Met ric Dat a for a Resource
99
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
7.3. Export ing Met ric Dat a for a Resource
Raw metrics data are only saved in the database for a week by default. After that, only the processed (aggregated) data are saved. It can be useful to export raw measurements to a
CSV or text file so that long-term historical data can be preserved.
The script steps are:
1. Search for the available metric definitions, based on the resource type. In this example, it is for the Linux platform.
2. Search for the resource.
3. Set a date range for the metric information. This is configured in seconds, relative to the time the script is run.
4. Set up the file information to which to write the data.
5. Iterate through all the metric definitions for the resource, and print the data to the given CSV file.
Examp le 12.25. Exp o rt in g All Met rics Def in it io n s f o r a Lin u x Server
// search for the available metrics definitions
var
rt =
ResourceTypeManager.getResourceTypeByNameAndPlugin( "Linux" , "Platfor ms" )
var
mdc = MeasurementDefinitionCriteria();
mdc.addFilterResourceTypeId(rt.id);
var
mdefs =
MeasurementDefinitionManager.findMeasurementDefinitionsByCriteria(md c);
// search for the resource
criteria =
new
ResourceCriteria();
criteria.addFilterResourceTypeName(
'Linux' )
var
resources = ResourceManager.findResourcesByCriteria(criteria);
// give the date range for the metrics collection
// this is in seconds
var
start =
new
Date () - 8 * 3600 * 1000 ;
var
end =
new
Date ()
// setup up the CSV to dump the data to
exporter.file =
'/opt/myfile.csv'
exporter.format =
'csv'
// iterate through the metrics definitions for the resource
// and export all the collected metrics for all definitions
// within the given date range
100
Chapt er 1 2 . Short Examples i f
( mdefs != null ) {
i f
( mdefs.size() > 1 ) {
fo r
( i =0; i < mdefs.size(); ++i) {
mdef = mdefs.get(i);
var
data =
MeasurementDataManager.findDataForResource(resources.get(0).id,
[mdef.id],start,end, 1 )
exporter.
write ( data .get(0)); // write the data to the CSV file
}
}
el se i f
( mdefs.size() == 1 ) {
mdef = mdefs.get(0);
var
data =
MeasurementDataManager.findDataForResource(resources.get(0).id,
[mdef.get(0).id],start,end, 60 )
exporter.
write ( data .get(0))
}
}
Report a bug
7.4 . Get t ing Baseline Calculat ions
Baselines are the normal operating ranges for a specific resource, based on its own performance.
Getting a baseline is really easy; all it requires is the resource ID.
rhqadmin@ localhost:
7080 $
MeasurementBaselineManager.findBaselinesForResource( 10001 )
one row
MeasurementBaseline:
computeTime: Tue May 08
21 : 28 :05 EDT 2012
id:
10001
max : 6.4005419008E10
mean:
6.3933904981333336E10
min : 6.380064768E10
schedule: [MeasurementSchedule, id=
10002 ]
userEntered: true
Report a bug
8. Alert s
Alert definitions cannot be created or edited using the CLI, but there are some management tasks that can still be performed. For fired alerts themselves, server-side scripts can be used as alert responses (and the alert referenced as an implicit variable) and the fired alert can be viewed and acknowledged by an administrator. Definitions can be enabled or disabled.
101
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Report a bug
8.1. Using Alert s wit h Script s
One possible response to an alert is for the server to automatically run a stored server script
(essentially a stored CLI script file).
That server-side alert script can reference the alert object for the triggered alert. The server defines an implicit alert variable, which pulls in the alert information, ID, definition, and other information for the fired alert.
Because the al ert method identifies the alert definition, it identifies the resource which triggered the alert. This allows you to create a reusable proxy resource definition in the script that could be applied to any resource which uses that alert script.
For example: var myResource =
ProxyFactory.getResource(alert.alertDefinition.resource.id)
NO T E
This method is only available to server-side script used with alerts, not to the interactive
CLI or external CLI script files.
Report a bug
8.2. Acknowledging Alert s
One common administrative action when an alert is fired is for an administrator to review and then acknowledge the alert, which effectively closes it. This script example acknowledges all of the current alerts for all Linux platform resources.
The criteria for the search can be set to be more restrictive so that only certain alerts or only certain resources are included in the action.
The steps in this script:
1. Search for fired alerts; in this case, the search is based on the resource type (Linux).
2. Retrieve the data for the search results.
3. Acknowledge all returned alerts.
Examp le 12.26 . Ackn o wled g in g Alert s f o r Plat f o rm Reso u rces
102
Chapt er 1 2 . Short Examples
// set the criteria and search for the alerts
var
criteria =
new
AlertCriteria()
criteria.addFilterResourceTypeName(
'Linux' )
var
alerts = AlertManager.findAlertsByCriteria(criteria)
// go through the results and then acknowledge the alerts
i f
( alerts != null ) {
i f
( alerts.size() > 1 ) {
fo r
( i =0; i < alerts.size(); ++i) {
alert = alerts.get(i);
AlertManager.acknowledgeAlerts([ alert .id])
}
}
el se i f
( alerts.size() == 1 ) {
alert = alerts.get(0);
AlertManager.acknowledgeAlerts([ alert .id])
}
}
Report a bug
8.3. Enabling or Disabling Alert Definit ions
Alert definitions cannot be created or edited through the CLI, but they can be enabled or disabled.
This example script disables all of the definitions returned in the search. In real life, the criteria for the search are crucial to make sure that only the right definitions are disabled, rather than disabling large blocs of definitions.
The steps in this script:
1. Search for matching alert definitions based on priority (low, in this case).
2. Retrieve the data for the search results.
3. Disable all returned alert definitions, based on the IDs in the retrieved search list.
Examp le 12.27. Disab lin g Alert s Based o n Prio rit y
// set the search criteria for the alert definitions with a reasonable filter
var
criteria =
new
AlertDefinitionCriteria()
criteria.addFilterPriority(
'Low' )
//search for the alert definitions
alertdefs =
AlertDefinitionManager.findAlertDefinitionsByCriteria(criteria)
103
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
//get the data from the results
alertdef = alertdefs.get(0);
//disable the matching alerts, based on ID
AlertDefinitionManager.disableAlertDefinitions([alertdef.id]);
104
Report a bug
9. Users and Roles
Roles are the primary access control method in JBoss ON. A role creates a relationship between users and resources (through resource groups). The permissions set for the role define what permissions the users in the role have for the resource in the role.
Report a bug
9.1. Creat ing Roles
A user can only see and manage what resources are in the roles to which a user belongs. If there are no resources, then the user can do very little in JBoss ON, regardless of whatever their permissions are.
This script creates a role and adds a mixed group to it. It could be made more complex, like using different criteria to get different types of groups or adding multiple groups to the role at once.
The script steps are:
1. Create a role and assigning the appropriate permissions. In this case, the role has manage inventory and view user permissions.
2. Search for the group to add as a member.
3. Search for the new role entry.
4. Add the group to the role.
Examp le 12.28. A New Ro le
// create the role
var
role = Role( 'Role Name - ' + java.util.
Date ());
role.
description = 'This role is an example' ;
role.addPermission(Permission.MANAGE_INVENTORY);
role.addPermission(Permission.VIEW_USERS);
RoleManager.createRole(role);
Chapt er 1 2 . Short Examples
//search for the group to add to the role
groupcriteria =
new
ResourceGroupCriteria();
groupcriteria.addFilterGroupCategory.toString(
'MIXED' );
var
groups =
ResourceGroupManager.findResourceGroupsByCriteria(groupcriteria);
//search for the new role
var
c =
new
RoleCriteria();
c.addFilterName(
'Role Name' );
var
roles = RoleManager.findRolesByCriteria( c );
RoleManager.addResourceGroupsToRole(roles.get(0).id,
[groups.get(0).id]);
Report a bug
9.2. Creat ing Users
There are two parts to a user entry: the descriptive entry in JBoss ON and the principal, which is the login username/password pair.
The script steps are:
1. Create a new user (subject) entry.
2. Create a principal for the new user.
3. Search for roles to add the user to and create an array.
4. Add the user to the roles.
Examp le 12.29 . Creat t in g a User an d Ad d in g Ro les
//create the new user entry
var
newSubject =
new
Subject();
newSubject.setEmailAddress(
'admin@ example.com' );
newSubject.setFirstName(
'John' );
newSubject.setLastName(
'Smith' );
newSubject.setFactive(true);
newSubject.setFsystem(
fal se
);
newSubject.setName(
'jsmith' );
var
s = SubjectManager.createSubject(newSubject);
//create the login principal for the user
SubjectManager.createPrincipal( s.
name , 'password' );
//search for the role and create an array
105
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s var
c =
new
RoleCriteria();
c.addFilterName(
'Role Name' );
var
roles = RoleManager.findRolesByCriteria( c );
var
role = roles.get(0);
var
rolesArray =
new
Array ( 1 );
rolesArray[0] = role.getId();
//add the new user to the roles in the array
RoleManager.addRolesToSubject(s.getId(), rolesArray );
Report a bug
106
Chapt er 1 2 . Short Examples
Part III. Extended Examples and Use Scenarios
Chapter 12, Short Examples runs through discrete code examples that perform limited and
specific tasks. In real life, those code examples (and others) are going to be strung together to perform more coherent and useful management tasks. These examples illustrate some potential script workflows.
Report a bug
107
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Chapter 13. Example: Scripts to Manage Inventory (All
Resource Types)
Servers and services are routinely added or removed from a local machine. While discovery scans are scheduled regularly, actually adding or removing that resource within the JBoss
ON inventory is all manual — and administrator must actually choose to import the resource.
An administrator can manage the JBoss ON inventory by automatically adding new resources and automatically deleting other ones based on whatever criteria are defined.
Report a bug
1. Aut omat ically Import New Resources: aut oimport .js
As soon as a resource is discovered it is, technically, already in the JBoss ON inventory. It is included with a status of NEW. That's an in-between state, because JBoss ON is aware that the resource exists, but the resource has not been committed so JBoss ON can't manage it.
A script can be created and run regularly so that any newly-discovered resources can be automatically added to the inventory. This script bases its identification on new resources on the inventory state, so ignored or already imported resources aren't included.
The CLI script runs through three steps:
It identifies new resources using the fi nd Unco mmi tted R eso urces() method.
It gets those new resources' IDs.
It then imports those resources by invoking the discovery system's import operation.
//Usage: autoImport.js
//Description: Imports all auto-discovered inventory into JON
// autoImport.js
rhq.login(
'rhqadmin' , 'rhqadmin' );
println(
"Running autoImport.js" );
var
resources = findUncommittedResources();
var
resourceIds = getIds(resources);
DiscoveryBoss.importResources(resourceIds);
rhq.logout();
Only one of the operations is already defined in the remote API — i mpo rtR eso urces. The other two functions — fi nd Unco mmi tted R eso urces and g etId s — have to be defined in the script.
108
Chapt er 1 3. Example: Script s t o Manage Invent ory (All Resource T ypes)
Uncommitted (new) resources can be identified through a R eso urceC ri teri a search by adding a search parameter based on the inventory status.
// returns a java.util.List of Resource objects
// that have not yet been committed into inventory
functi o n
findUncommittedResources() {
var
criteria = ResourceCriteria();
criteria.addFilterInventoryStatus(InventoryStatus.
NEW
);
return
ResourceManager.findResourcesByCriteria(criteria);
}
The second function checks that the inventory search actually returned resources and, if so, gets the ID for each resource in the array.
// 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.
functi o n
getIds(resources) {
var
ids = [];
i f
(resources.size() > 0) {
println(
"Found resources to import: " );
fo r
(i = 0; i < resources.size(); i++) {
resource = resources.get(i);
ids[i] = resource.id;
println(
" " + resource.
name );
}
}
el se
{
println(
"No resources found awaiting import..." );
}
return
ids;
}
Report a bug
2. Simple Invent ory Count : invent oryCount .js
Searches are performed using *C ri teri a classes; for resources, this is
R eso urceC ri teri a. A search can be very specific, passing criteria so that it returns only one resource or a small subset of resource. It is also possible to return everything in inventory.
This script runs a search with no specific criteria (R eso urceC ri teri a()), so that every resource matches the search. It then takes the size of the results to produce a simple inventory count.
// inventory.js
rhq.login(
'rhqadmin' , 'rhqadmin' );
var
resources =
109
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
ResourceManager.findResourcesByCriteria(ResourceCriteria());
println(
'There are ' + resources.size() + ' resources in inventory' );
// end script
Report a bug
3. Uninvent ory a Resource Aft er an Alert : uninvent ory.js
Removing a resource from the inventory removes it from JBoss ON; the server or application itself remains intact on the local system. (This allows the resource to be re-discovered and re-imported later.)
The alert system can launch CLI scripts in response to a fired alert. One possible response is to uninventory a resource which is not performing well.
This can be a pretty simple little script. To uninventory the resource, use the resource ID which was included in the alert and the uni nvento ryR eso urce method:
ResourceManager.uninventoryResource( alert .alertDefinition.resource.id)
;
It is possible to combine the uninventory operation with another task. For example, uninventory one resource and automatically create and import another resource to take its place.
No t e
This script should be uploaded to the server and then referenced as an alert notification.
Report a bug
110
Chapt er 1 3. Example: Script s t o Manage Invent ory (All Resource T ypes)
Chapter 14. Example: Scripts to Manage Resources of a
Specific Type
Chapter 13, Example: Scripts to Manage Inventory (All Resource Types)
shows a general resource discovery and import script. In some environments, it may be more useful to discover and import only specific types of resources. For example, and administrator may be constantly deploying new applications to an app server, or maybe spinning up and destroying application servers dynamically to respond to load. In that case, the administrator only wants to find the specific resources which he knows are frequently created.
For this example, the workflow has some pretty basic steps:
1. Search for new, uncommitted resources of a specific type.
2. Get the resource IDs.
3. Import those new resources.
4. Do something with the newly-imported resources.
Steps 1 through 3 are basically the same as the auto i mpo rt. js example in Section 1,
“ Automatically Import New Resources: autoimport.js” , with one significant change. The
search has an extra filter for the resource type.
Uncommitted (new) resources can be identified through a R eso urceC ri teri a search by adding a search parameter based on the inventory status and the resource type (in this example, JBoss EAP 6 domain deployments, for new web applications). The new
fi nd Unco mmi tted JbasApps() function runs that search.
functi o n
findUncommittedJbasApps() {
var
criteria = ResourceCriteria();
criteria.addFilterInventoryStatus(InventoryStatus.
NEW
);
criteria.addFilterResourceTypeName(
'DomainDeployment' );
return
ResourceManager.findResourcesByCriteria(criteria);
}
As with the auto i mpo rt. js example, a g etId s function retrieves an array of the resource
IDs.
functi o n
getIds(resources) {
var
ids = [];
i f
(resources.size() > 0) {
println(
"Found resources to import: " );
fo r
(i = 0; i < resources.size(); i++) {
resource = resources.get(i);
111
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
ids[i] = resource.id;
println(
" " + resource.
name );
}
}
el se
{
println(
"No resources found awaiting import..." );
}
return
ids;
}
Those two functions accomplish the first two steps: search for uncommitted resources of a specific type and then retrieve those IDs.
Step 3 then runs the D i sco veryBo ss method to import the discovered resources.
The first half of the script, then mimics the original auto i mpo rt. js, with the slight adjustment to the new function to search for JBoss EAP 6 domain deployments.
rhq.login(
'rhqadmin' , 'rhqadmin' );
println(
"Running autoImport.js" );
var
resources = findUncommittedJbasApps();
var
resourceIds = getIds(resources);
DiscoveryBoss.importResources(resourceIds);
The last step simply does something with the new resource.
For a new domain deployment, then it is probably most useful to assign the new domain deployment to server group, so the new application is deployed. For example, this assigns all new applications automatically to a server group used for the staging environment, using the promote operation:
var
config =
new
Configuration();
config.put(
new
PropertySimple( "server-group" , "Staging-Server-Group" )
);
OperationManager.scheduleResourceOperation(
resources.get(0).id,
"promote" ,
0,
// 0 means that the delay was skipped
1 ,
0,
// this skips the repeat count
10000000 ,
config,
"promote new app to server group"
);
112
Alternatively, if this script is used to add a new service or a new server, then it may be more appropriate to add the new resource to a group. This example uses an explicit group ID
(15001) for an existing compatible group. The assumption is that the group has already been created, before this script is run, and there is no need to create a new group for the
Chapt er 1 4 . Example: Script s t o Manage Resources of a Specific T ype
resources.
ResourceGroupManager.addResourcesToGroup(
15001 , [resourceIds]);
Then, have the script log out from the CLI and close.
rhq.logout();
Report a bug
113
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Chapter 15. Example: Scripting Resource Deployments
(JBoss EAP 5)
A common use case for management tools is to automate deployments of new or existing applications. This example creates an easy script for basic management tasks:
1. Find all JBoss EAP instances for a specified JBoss ON group.
2. Shut down each EAP instance.
3. Update binaries for existing deployed applications or create new deployments.
4. Restart the EAP instance.
5. End the loop.
Report a bug
1. Declaring Cust om Funct ions
This script will use two custom functions to deploy the packages to create new resources.
functi o n
usage() {
println(
"Usage: deployToGroup <fileName> <groupName>" );
throw
"Illegal arguments" ;
}
functi o n
PackageParser(fullPathName) {
var
file =
new
java.io.File(fullPathName);
var
fileName = file.getName();
var
packageType = fileName .
substring ( fileName .
lastIndexOf ( '.' ) +1 );
var
tmp = fileName .
substring (0, fileName .
lastIndexOf ( '.' ));
var
realName = tmp.
substring (0, tmp.
lastIndexOf ( '-' ));
var
version = tmp.
substring (tmp.
lastIndexOf ( '-' ) + 1 );
var
packageName = realName + "." + packageType;
this.packageType = packageType.toLowerCase();
this.packageName = packageName;
this.version = version;
this.realName = realName;
}
Report a bug
2. Checking t he JBoss ON Groups and Invent ory
114
Chapt er 1 5. Example: Script ing Resource Deployment s (JBoss EAP 5)
The script should have two command-line parameters. The first should be the path of the new application that is installed in the group. The second is the name of the group itself.
These parameters are parsed in the script (as described in more detail in Section 3,
“ Passing Command and Script Arguments” ).
For example:
i f
( args.
length < 2 ) usage();
var
fileName = args[0];
var
groupName = args[ 1 ];
Next, check if the path is valid and if the current user can read it. This is done by using Java classes as shown here:
// check that the file exists and that we can read it
var
file =
new
java.io.File( fileName );
i f
( !file.exists() ) {
println( fileName + " does not exist!" );
usage();
}
i f
( !file.canRead() ) {
println( fileName + " can't be read!" );
usage();
}
Verify that the group really exists on the JBoss ON server:
// find resource group
var
rgc =
new
ResourceGroupCriteria();
rgc.addFilterName(groupName);
rgc.fetchExplicitResources(true);
var
groupList =
ResourceGroupManager.findResourceGroupsByCriteria(rgc);
The important part here is to fetch the resources.
rgc.fetchExplicitResources(true);
Check if there is a group found:
i f
( groupList == null || groupList.size() != 1 ) {
println(
"Can't find a resource group named " + groupName);
usage();
}
var
group = groupList.get(0);
115
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
println(
" Found group: " + group.
name );
println(
" Group ID : " + group.id );
println(
" Description: " + group.
description );
After validating that there is a group with the specified name, check if the group contains explicit resources:
i f
( group.explicitResources == null || group.explicitResources.size()
== 0 ) {
println(
" Group does not contain explicit resources --> exiting!"
);
usage();
}
var
resourcesArray = group.explicitResources.toArray();
reso urceArray now contains all resources which are part of the group. Next, check if there are JBoss AS 5 Server instances which need to be restarted before the application is deployed.
fo r
( i
i n
resourcesArray ) {
var
res = resourcesArray[i];
var
resType = res.resourceType.
name ;
println(
" Found resource " + res.
name + " of type " + resType + " and ID " + res.id);
i f
( resType != "JBossAS5 Server" ) {
println(
" ---> Resource not of required type. Exiting!" );
usage();
}
// get server resource to start/stop it and to redeploy application
var
server = ProxyFactory.getResource(res.id);
}
This requires a group with only JBoss AS 5 Server resource types as top level resources.
Now server contains the JBoss AS 5 instance. This requires re-reading the server because it needs to be fully populated. Internally, the CLI is using simple JPA persistence, and it is necessary to not always fetch all dependent objects.
Next, traverse all the children of the server instance and find the resource name of the application:
var
children = server.children;
fo r
( c
i n
children ) {
var
child = children[c];
i f
( child.
name == packageName ) {
}
}
116
Chapt er 1 5. Example: Script ing Resource Deployment s (JBoss EAP 5)
packag eName is the name of the application without version information and path as shown in the JBoss ON GUI as deployed applications.
Create a backup of the original version of the application:
println(
" download old app to /tmp" );
child.retrieveBackingContent(
"/tmp/" + packageName + "_" + server.
name
+ "_old" );
A copy of the old application with the server name decoded in path is available in the /tmp/ directory.
Shut down the server and upload the new application content to the server.
println(
" stopping " + server.
name + "...." );
try {
server.shutDown();
}
catch
( ex ) {
println(
" --> Caught " + ex );
}
println(
" uploading new application code" );
child.updateBackingContent( fileName );
println(
" restarting " + server.
name + "....." );
try {
server.start();
}
catch
( ex ) {
println(
" --> Caught " + ex );
}
Report a bug
3. Deploying t he New Resource
At this point, existing application can be updated. The next step is to create the resource through the CLI and then deploy it to the JBoss server.
First, get the resource type for the application. This depends on several parameters:
1. The type of the application (e.g., WAR or EAR)
2. The type of the container the app needs to be deployed on (such as Tomcat or JBoss
AS 5)
117
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
NO T E
All of the information about the resource type, such as the appType and appTypeName, is defined in the resource agent plug-in, in the rhq -pl ug i n. xml descriptor. The attributes, configuration parameters, operations, and metrics for each default resource type are listed in the Resource Monitoring and Operations Reference.
For example:
var
appType = ResourceTypeManager.getResourceTypeByNameAndPlugin( appTypeName, "JBossAS5" );
i f
( appType == null ) {
println(
" Could not find application type. Exit." );
usage();
}
Then get the package type of the application.
var
realPackageType = ContentManager.findPackageTypes( appTypeName,
"JBossAS5" );
i f
( realPackageType == null ) {
println(
" Could not find JBoss ON's packageType. Exit." );
usage();
}
Each resource in JBoss ON has some configuration parameters, including the WARs or
EARs deployed on a JBoss AS 5 resource. As with the descriptive information, this is defined in the resource type's agent plug-in, in the rhq -pl ug i n. xml descriptor. To be able to create a new resource, these parameters need to be filled in.
// create deployConfig
var
deployConfig =
new
Configuration();
deployConfig.put(
new
PropertySimple( "deployExploded" , "false" ));
deployConfig.put(
new
PropertySimple( "deployFarmed" , "false" ));
The property names can be retrieved by calling a list of supported properties by the package type by calling this method:
var
deployConfigDef =
ConfigurationManager.getPackageTypeConfigurationDefinition(realPackage
Type.getId());
118
Provide the package bits as a byte array:
var
inputStream =
new
java.io.FileInputStream(file);
var
fileLength = file.
length ();
var
fileBytes = java.lang.reflect.
Array .newInstance(java.lang.Byte.
TYPE , fileLength);
fo r
(numRead=0, offset=0; ((numRead >= 0) & & (offset <
Chapt er 1 5. Example: Script ing Resource Deployment s (JBoss EAP 5)
fileBytes.
length )); offset += numRead ) {
numRead = inputStream.read(fileBytes, offset, fileBytes.
length - offset);
}
Then, create the resource. The information is defined in the resource type's agent plug-in, in the rhq -pl ug i n. xml descriptor. For example:
ResourceFactoryManager.createPackageBackedResource(
server.id,
appType.id,
packageName,
null,
// pluginConfiguration
packageName,
packageVersion,
null,
// architectureId
deployConfig,
fileBytes,
null
// timeout
);
Make sure that the given JBoss AS 5 server instance is still running and that JBoss ON knows that it is running, or it will throw an exception saying that the JBoss ON agent is not able to upload the binary content to the server.
Report a bug
119
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Chapter 16. Example: Deploying an Application with
Bundles (JBoss EAP 4, 5, and 6)
Bundles are a very clean and easy way to deploy updated applications to JBoss EAP 6 servers. The bundles system maintains multiple versions of a given package and can deploy any of those versions to a specified compatible group. This is a great workflow for application lifecycles, since a stable version can be deployed to production servers while a development version can be deployed to test machines.
Report a bug
1. Creat ing a New Applicat ion
The jbo ssas. js script in the cliRoot/rhq -remo ti ng -cl i -
4 . 9 . 0 . JO N320 G A/sampl es/mo d ul es directory defines a set of custom functions that quickly create a bundle version and definition and then deploy it to the given JBoss servers.
The jbo ssas. js script requires the uti l . js and bund l es. js scripts for supporting functions. These are all loaded automatically from the cliRoot/rhq -remo ti ng -cl i -
4 . 9 . 0 . JO N320 G A/sampl es/mo d ul es directory.
The d epl o y-to -and -restart-JBAS. js script defines a function called
createAppAnd R estartJBAS that creates and then deploys a bundle. The function takes seven parameters:
The path to the bundle archive file.
A configuration object that contains any tokens or variables required for the bundle configuration. In this example, no properties are passed, so the value is null. Details about the configuration are in the comments in the bund l es. js file and general
configuration information is in Section 5.2, “ Changing Simple Configuration Properties” .
The name of the compatible group to which to deploy the bundle.
A name of the bundle destination.
A description for the bundle destination.
The base directory to which to deploy the bundle and, optionally, a subdirectory to deploy the bundle to.
For example:
rhqadmin@ localhost:
7080 $
var
bundleZipFile =
/export/ bundles/myBundle.zip
rhqadmin@ localhost:
7080 $
var
deploymentConfiguration = null
rhqadmin@ localhost:
7080 $
var
groupName = "JBoss EAP 6 Group"
120
Chapt er 1 6 . Example: Deploying an Applicat ion wit h Bundles (JBoss EAP 4 , 5, and 6 )
rhqadmin@ localhost:
7080 $
var
destinationName = "My App - JBoss EAP 6
Destination"
rhqadmin@ localhost:
7080 $
var
destinationDescription = "For my application"
rhqadmin@ localhost:
7080 $
var
baseDirName = "Deploy Directory"
rhqadmin@ localhost:
7080 $
var
deployDir = "helloWorldApp"
rhqadmin@ localhost:
7080 $ createAppAndRestartJBAS(bundleZipFile, deploymentConfiguration, groupName, destinationName, destinationDescription, baseDirName, deployDir)
There are a number of private functions defined in the d epl o y-to -and -restart-
JBAS. js script which provide support to the createAppAnd R estartJBAS function. Those are explained in the comments in the d epl o y-to -and -restart-JBAS. js file.
NO T E
The baseDirName variable must have a value of D epl o y D i recto ry or P ro fi l e
D i recto ry. Those two locations are then identified based on the connection information for the specified JBoss EAP resource which is accessed by the script.
The createAppAnd R estartJBAS function has three parts: finding the resource group, creating the bundle, and restarting the JBoss servers in the group.
The first part searches for the specific group by the given group name, and it must match only a single resource group. Otherwise, it returns an error.
functi o n
createAppAndRestartJBAS(bundleZipFile, deploymentConfiguration, groupName, destinationName, destinationDescription, baseDirName, deployDir) {
var
gcrit =
new
ResourceGroupCriteria;
gcrit.addFilterName(groupName);
gcrit.fetchResourceType(true);
var
groups =
ResourceGroupManager.findResourceGroupsByCriteria(gcrit);
i f
(groups.empty) {
throw
"Could not find a resource group called " + groupName;
}
el se i f
(groups.size() > 1 ) {
throw
"There are more than 1 groups called " + groupName;
}
var
group = groups.get(0);
var
targetResourceType = group.resourceType;
Part of the search instruction is to fetch the resource type for the given compatible group.
That resource type is then used to create the bundle destination, which is associated with a group.
The next part creates the bundle and restarts the server resources.
var
deployFn =
functi o n
(restartFn) {
121
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
The first part of create a bundle deployment is uploading the bundle archive to create a bundle version in the JBoss ON configuration.
var
bundleVersion = createBundleVersion(bundleZipFile);
Next, it creates the destination, which is the definition of where the bundle is to be deployed
(the base and deployment directories), associated with a compatible group and with the bundle version.
var
destination =
BundleManager.createBundleDestination(bundleVersion.bundle.id, destinationName, destinationDescription, baseDirName, deployDir, group.id);
Once the destination and the version are defined, then they can be saved and deployed to the target resource group.
var
deployment = deployBundle(destination,bundleVersion, deploymentConfiguration, "Web application" ,
fal se
);
i f
(deployment.
status != BundleDeploymentStatus.SUCCESS) {
throw
"Deployment wasn't successful: " + deployment;
}
When the deployment completes, then the script iterates through the group members (defined in one of the help functions in the d epl o y-to -and -restart-JBAS. js script) and restarts each resource. It then prints the deployment information.
restartFn(group);
return
deployment;
};
The d epl o y-to -and -restart-JBAS. js script can deploy content to any supported version of JBoss EAP: 4, 5, or 6. The bundle system uses the configuration defined in the resource plug-in, based on the resource type of the group. For the restart operation, different restart function are defined for each version of JBoss EAP.
i f
(targetResourceType.
plugin == "JBossAS" & & targetResourceType.
name == "JBossAS Server" ) {
return
deployFn(_restartAS4);
}
el se i f
(targetResourceType.
plugin == "JBossAS5" & & targetResourceType.
name == "JBossAS Server" ) {
return
deployFn(_restartAS5);
}
el se i f
(targetResourceType.
plugin == "JBossAS7" & &
(targetResourceType.
name == "JBossAS7 Standalone Server"
||
targetResourceType.
name == "JBossAS-Managed" )) {
return
deployFn(_restartAS7);
}
122
Chapt er 1 6 . Example: Deploying an Applicat ion wit h Bundles (JBoss EAP 4 , 5, and 6 )
throw
"The resource group the destination targets doesn't seem to be a JBoss AS server group." ;
}
Report a bug
2. Updat ing Applicat ions
Updating an application is simpler than creating one because the bundle definition already exists.
To update an application, give the path to the updated bundle archive, any tokens or properties to set, and the existing bundle destination.
rhqadmin@ localhost:
7080 $
var
bundleZipFile =
/export/ bundles/myBundle.zip
rhqadmin@ localhost:
7080 $
var
deploymentConfiguration = null
rhqadmin@ localhost:
7080 $
var
jbasDestination = "My App - JBoss EAP 6
Destination"
rhqadmin@ localhost:
7080 $ updateAppAndRestartJBAS(bundleZipFile, jbasDestination, deploymentConfiguration)
If the bundle requires any tokens to be realized, like a port number to be entered, then you must create a configuration object and pass the values to that. In this example, no properties are passed, so the value is null. Details about the configuration are in the comments in the
bund l es. js file and general configuration information is in
Section 5.2, “ Changing Simple
The destination identifier — which could be the destination name or the ID — is used in a criteria search to fetch the resource type for the compatible group. This, as with the create function, identifies which version of JBoss EAP is being used.
functi o n
updateAppAndRestartJBAS(bundleZipFile, jbasDestination, deploymentConfiguration) {
// first figure out the jbas version we are deploying to
var
destinationId = jbasDestination;
i f
(typeof(jbasDestination) == 'object' ) {
destinationId = jbasDestination.id;
}
var
destCrit =
new
BundleDestinationCriteria
destCrit.fetchGroup(true)
It uses the name to search for the destination ID. When it retrieves the destination entry (in the g et(0 ) call), the destination configuration contains the resource type.
var
destinations =
BundleManager.findBundleDestinationsByCriteria(destCrit);
123
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
i f
(destinations.empty) {
throw
"No destinations corresponding to " + jbasDestination +
" found on the server." ;
}
var
destination = destinations.get(0);
var
targetResourceType = destination.group.resourceType;
i f
(targetResourceType == null) {
throw
"This function expects a compatible group of JBoss AS
(4,5,6 or 7) resources but the provided destination is connected with
" + destination.group;
}
Then, it uploads the new bundle archive as a new bundle version, in the
createBund l eVersi o n method.
var
deployFn =
functi o n
(restartFn) {
var
bundleVersion = createBundleVersion(bundleZipFile);
Then, it deploys the new bundle version to the existing destination, along with any defined or required tokens in a configuration object (deploymentConfiguration). When the deployment completes, it restarts the JBoss resources in the group and prints the deployment information.
var
deployment = deployBundle(destination,bundleVersion, deploymentConfiguration, "Web application" ,
fal se
);
i f
(deployment.
status != BundleDeploymentStatus.SUCCESS) {
throw
"Deployment wasn't successful: " + deployment;
}
restartFn(destination.group);
return
deployment;
};
124
As with the creation function, there are version-specific restart methods for any supported version of JBoss EAP: 4, 5, or 6. The bundle system uses the configuration defined in the resource plug-in, based on the resource type of the group.
i f
(targetResourceType.
plugin == "JBossAS" & & targetResourceType.
name == "JBossAS Server" ) {
return
deployFn(_restartAS4);
}
el se i f
(targetResourceType.
plugin == "JBossAS5" & & targetResourceType.
name == "JBossAS Server" ) {
return
deployFn(_restartAS5);
}
el se i f
(targetResourceType.
plugin == "JBossAS7" & &
(targetResourceType.
name == "JBossAS7 Standalone Server"
||
targetResourceType.
name == "JBossAS-Managed" )) {
return
deployFn(_restartAS7);
Chapt er 1 6 . Example: Deploying an Applicat ion wit h Bundles (JBoss EAP 4 , 5, and 6 )
}
throw
"The resource group the destination targets doesn't seem to be a JBoss AS server group." ;
}
Report a bug
125
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Chapter 17. Example: Remote JNDI Lookups After an Alert
(JBoss EAP 5)
Imp o rt an t
This script is intended to be run directly on the server, such as using the -f parameter or through a server-side alert script. This cannot be run using the interactive CLI.
For information on running server-side scripts in response to alerts, see "Using JBoss
Operations Network for Monitoring, Deploying, and Managing Resources ."
Imp o rt an t
For security reasons, it is not possible to run a local JNDI lookup from an alert CLI script.
It is possible to perform a remote JNDI lookup.
The alert system can run a script in response to a fired alert. One possible response for a
JBoss AS 5 server is to check the JNDI directory and look up the JMX information.
This script first connects to the remote JNDI directory over JNP, then uses the
assertNo tNul l method to get the JMX object. The script then prints the JMX information.
//This test requires a remote JBoss AS 5 server running with JNDI directory remotely accessible using JNP (without authz)
//This script assumes that there is a bound object called "jmx" in the directory (which it should be)
var
jbossHost = 'localhost' ;
var
jbossJnpPort = 1299 ;
var
env =
new
java.util.Hashtable();
env.put(
'java.naming.factory.initial' ,
'org.jboss.naming.NamingContextFactory' );
env.put(
'java.naming.provider.url' , "jnp://" + jbossHost + ":" + jbossJnpPort);
var
ctx =
new
javax.naming.InitialContext(env);
var
jmx = ctx.lookup( 'jmx' );
assertNotNull(jmx);
pretty.
print (jmx);
Report a bug
126
Chapt er 1 7 . Example: Remot e JNDI Lookups Aft er an Alert (JBoss EAP 5)
Chapter 18. Example: Managing Grouped Servers (JBoss
EAP 5)
A lot of enterprise servers have a concept of managed servers. A managed server means that there is a central instance that deploys content or sends configuration to all registered application servers. Using managed servers helps administrators ensure that all active application servers have the same version of the deployed packages and configuration.
JBoss ON can imitate the behavior of managed or clustered servers for applications like
Tomcat or JBoss EAP 5 by creating a management script that can be invoked to perform actions simultaneously on all members of a JBoss ON group. All of the EAP 5 instances are functionally managed servers, while JBoss ON itself acts as the domain controller.
NO T E
JBoss EAP 6 has a very different server topology than JBoss EAP 5, so domain controllers, managed servers, and domain configuration are defined and manageable by default.
Report a bug
1. T he Plan for t he Script s
The JBoss ON CLI can run defined JavaScripts using the -f parameter. The idea here is to create a series of small management scripts that perform specific tasks on a group of JBoss
EAP servers. This example has seven scripts for:
Creating a group
Adding EAP instances to the group
Checking EAP status
Starting the EAP instance
Scheduling an operation
Deploying new content to the group
Checking metrics
A wrapper script and configuration file will be set up so that only one command needs to be run; the wrapper invokes the appropriate JBoss ON CLI script depending on the command passed to the wrapper.
127
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Report a bug
2. Creat ing t he Wrapper Script and .conf File
The wrapper script takes command-line arguments and calls the JBoss ON CLI with one of the scripts as argument. The command-line arguments themselves are defined in the JBoss
ON JavaScript files.
This wrapper script makes a few assumptions:
The wrapper script is run as a regular user, which means that any JavaScript files must be accessible to a regular user.
The scripts are located in a scri pts/ directory that is in the same directory as the wrapper script.
A separate configuration file defines connection information for the JBoss ON server.
Each JavaScript file is invoked by a separate CLI command invocation, defined in the wrapper.
Any options or information required by the JBoss ON CLI command is defined in the
JavaScript file and can, potentially, be passed with the wrapper script as an option.
#!/bin/bash
#
# groupcontrol
# ------------
# This is a simple wrapper script for all the java script scripts in this folder.
# Start this script with some parameters to automate group handling from within the
# command line.
#
# With groupcontrol you can do the following:
# create : Create a new group
# addMember: Add a new EAP instance to the specified group
# status : Print the status of all resources of a group
# start : start all EAP instances specified by group name
# deploy : Deploys an application to all AS instances specified by group name
# avail : Runs an availability operation on all discovered agent instances
# metrics : Gets the specified metric value for all AS instances specified by group name
#
#
## Should not be run as root.
i f
[ " $EUID " = "0" ] ;
then
echo " Please use a normal user account and not the root account"
128
Chapt er 1 8 . Example: Managing G rouped Servers (JBoss EAP 5)
exit 1
fi
## Figure out script home
MY_HOME=$( cd
`
dirname $0
` & &
pwd )
SCRIPT_HOME=$MY_HOME /scripts
## Source some defaults
.
$MY_HOME /groupcontrol.conf
## Check to see if we have a valid CLI home
i f
[ ! -d ${JON_CLI_HOME} ] ;
then
echo "JON_CLI_HOME not correctly set. Please do so in the file"
echo $MY_HOME /groupcontrol.conf
exit 1
fi
RHQ_OPTS= "-s $JON_HOST -u $JON_USER -t $JON_PORT "
# If JBoss ON_PWD is given then use it as argument. Else let the user enter the password
i f
[ "x $JON_PWD " == "x" ] ;
then
RHQ_OPTS= " $RHQ_OPTS -P"
el se
RHQ_OPTS= " $RHQ_OPTS -p $JON_PWD "
fi
#echo "Calling groupcontrol with $RHQ_OPTS"
usage()
{
echo " Usage $0 :"
echo " Use this tool to control most group related tasks with a simple script."
echo " --------------------------------------------------------
----------------- "
}
Each command that the wrapper should define has a d o Command() section which defines the JBoss ON CLI command to run and the JavaScript file to use.
doDeploy()
{
$JON_CLI_HOME /bin/rhq-cli.sh $RHQ_OPTS -f $SCRIPT_HOME /deploy.js
$2 $3
}
doCreate()
{
$JON_CLI_HOME /bin/rhq-cli.sh $RHQ_OPTS -f $SCRIPT_HOME /group.js
$2
}
doAddMember()
{
$JON_CLI_HOME /bin/rhq-cli.sh $RHQ_OPTS -f
$SCRIPT_HOME /addMember.js $2 $3 $4
}
doStatus()
{
129
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
$JON_CLI_HOME /bin/rhq-cli.sh $RHQ_OPTS -f $SCRIPT_HOME /status.js
$2
}
doRestart()
{
$JON_CLI_HOME /bin/rhq-cli.sh $RHQ_OPTS -f $SCRIPT_HOME /restart.js
$2
}
doAvail()
{
$JON_CLI_HOME /bin/rhq-cli.sh $RHQ_OPTS -f $SCRIPT_HOME /avail.js
}
doMetrics()
{
$JON_CLI_HOME /bin/rhq-cli.sh $RHQ_OPTS -f $SCRIPT_HOME /metrics.js
$2 $3
} case
" $1 "
i n
'deploy'
)
doDeploy
$*
;;
'create'
)
doCreate
$*
;;
'addMember'
)
doAddMember
$*
;;
'status'
)
doStatus
$*
;;
'restart'
)
doRestart
$*
;;
'avail'
)
doAvail
$*
;;
'metrics'
)
doMetrics
$*
;;
*
)
usage
$*
;; esac
This script uses a configuration file, g ro upco ntro l . co nf, which defines the connection information to connect to the JBoss ON server (which is required by the JBoss ON CLI).
##
## This file contains some defaults for the groupcontrol script
##
JON_CLI_HOME= cliRoot/rhq-remoting-cli-4.9.0.JON320GA
JON_HOST= localhost
JON_PORT= 7080
130
Chapt er 1 8 . Example: Managing G rouped Servers (JBoss EAP 5)
# The user you want to connect with
JON_USER= rhqadmin
# if you omit the password here, you'll be prompted for it.
JON_PWD= rhqadmin
Report a bug
3. Defining Argument s and Ot her Paramet ers for t he CLI Script s
There may be multiple groups or some tasks (like searching for resources or running an operation) may have multiple options.
Each JavaScript file can define its own script options in arg s methods. At a minimum, each script should accept the name of the group on which to perform the task.
It is also a really good idea to define a usag e function, so that each command can print what options are expected. For example:
function usage () {
println ( "Usage: deploy groupName" );
thro w
"Illegal arguments" ;
}
i f
( args.
length < 1 ) usage ();
var groupName = args[
0 ];
NO T E
When adding arguments for a script, be sure to set the proper number of tokens in the wrapper script for the CLI invocation. For example, for groupName and fileName, add $2
$3.
doDeploy()
{
$JON_CLI_HOME /bin/rhq-cli.sh $RHQ_OPTS -f
$SCRIPT_HOME /deploy.js $2 $3
}
Aside from the script for creating a group, every script must also include a search for the group to perform the operations on. For example:
groupcriteria =
new
ResourceGroupCriteria ();
groupcriteria.
addFilterName (groupName);
var groups =
ResourceGroupManager.
findResourceGroupsByCriteria (groupcriteria);
i f
( groups !=
nul l
) {
i f
( groups.
size () > 1 ) {
println ( "Found more than one group." );
}
131
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
el se i f
( groups.
size () == 1 ) {
group = groups.
get ( 0 );
}
}
Report a bug
4 . Creat ing a Group: group.js
Set up the script. This script only uses a single argument, for the name of the new group
(groupName). The resource type in the example is hard-coded to JBossAS5, which is a JBoss
AS 5 server; optionally, it is possible to also add arguments to set the plug-in name and type so that other JBoss versions could be specified.
functi o n
usage() {
println(
"Usage: deploy groupName" );
throw
"Illegal arguments" ;
}
i f
( args.
length < 1 ) usage();
var
groupName = args[0];
Create the group:
var
rg =
new
ResourceGroup(resType);
rg.setRecursive(
fal se
);
rg.setDescription(
"Created via groupcontrol scripts on " +
new
java.util.
Date ().toString());
rg.setName(groupName);
rg = ResourceGroupManager.createResourceGroup(rg);
var
resType =
ResourceTypeManager.getResourceTypeByNameAndPlugin( "JBossAS 5
Server" , "JBossAS5" );
Report a bug
5. Adding Resources t o a Group: addMember.js
Set up the script. This identifies three required arguments for the script:
groupName for the group to add the resources to
resourceName for the name of the resource to add; this is one of the search criteria
resourceTypeName for the type of resource to add; this is one of the search criteria
132
Chapt er 1 8 . Example: Managing G rouped Servers (JBoss EAP 5)
This also includes a search to find the group specified in the argument.
functi o n
usage() {
println(
"Usage: addMember groupName resourceName resourceTypeName" );
throw
"Illegal arguments" ;
}
i f
( args.
length < 3 ) usage();
var
groupName = args[0];
var
resourceName = args[ 1 ];
var
resourceTypeName = args[ 2 ];
groupcriteria =
new
ResourceGroupCriteria();
groupcriteria.addFilterName(groupName);
var
groups =
ResourceGroupManager.findResourceGroupsByCriteria(groupcriteria);
i f
( groups != null ) {
i f
( groups.size() > 1 ) {
println(
"Found more than one group." );
}
el se i f
( groups.size() == 1 ) {
group = groups.get(0);
}
}
Search for the resources to add to the group. The script is designed to add only a single resource to the group, so the given search criteria, resourceName and resourceTypeName, must be specific enough to match only a single resource.
criteria =
new
ResourceCriteria();
criteria.addFilterName(resourceName);
criteria.addFilterResourceTypeName(resourceTypeName);
var
resources = ResourceManager.findResourcesByCriteria(criteria);
i f
( resources != null ) {
i f
( resources.size() > 1 ) {
println(
"Found more than one JBossAS Server instance. Try to specialize." );
fo r
( i =0; i < resources.size(); ++i) {
var
resource = resources.get(i);
println(
" found " + resource.
name );
}
}
el se i f
( resources.size() == 1 ) {
resource = resources.get(0);
println(
"Found one JBossAS Server instance. Trying to add it." );
println(
" " + resource.
name );
ResourceGroupManager.addResourcesToGroup(group.id,
[resource.id]);
println(
" Added to " + group.
name + "!" );
}
el se
{
133
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
println(
"Did not find any JBossAS Server instance matching your pattern. Try again." );
}
}
When this script is run, it prints the name of the found JBoss instance and that it was added to the group.
[jsmith@ server cli]$ ./wrapper.sh addMember myGroup
"JBossAS App 1"
"JBossAS Server"
Remote server version is: 3.0.1.GA
(
b2cb23b:859b914
)
Login successful
Found one JBossAS Server instance. Trying to add it.
AS server.example.com JBossAS App 1
Added to myGroup!
Report a bug
6. Get t ing Invent ory and St at us Informat ion: st at us.js
This is a simple little script, just to print the current status of all the JBoss instances in the group.
As with the other scripts, set up the group information.
functi o n
usage() {
println(
"Usage: status groupName" );
throw
"Illegal arguments" ;
}
i f
( args.
length < 1 ) usage();
var
groupName = args[0];
groupcriteria =
new
ResourceGroupCriteria();
groupcriteria.addFilterName(groupName);
var
groups =
ResourceGroupManager.findResourceGroupsByCriteria(groupcriteria);
i f
( groups != null ) {
i f
( groups.size() > 1 ) {
println(
"Found more than one group." );
}
el se i f
( groups.size() == 1 ) {
group = groups.get(0);
}
}
Also include information to search for the resources, based on the group:
criteria =
new
ResourceCriteria();
134
Chapt er 1 8 . Example: Managing G rouped Servers (JBoss EAP 5) var
groupArray=
new
Array ();
groupArray[0]=group.id;
criteria.addFilterExplicitGroupIds(groupArray);
var
resources = ResourceManager.findResourcesByCriteria(criteria);
fo r
( i =0; i < resources.size(); ++i) {
var
resource = resources.get(i);
println(
" found " + resource.
name );
}
Then, run through the resources and print their availability.
var
server = ProxyFactory.getResource(resource.id);
var
avail =
AvailabilityManager.getCurrentAvailabilityForResource(server.id);
println(
" " + server.
name );
println(
" - Availability: " + avail.availabilityType.getName());
println(
" - Started : " + avail.startTime.toGMTString());
println(
"" );
var
avail =
AvailabilityManager.getCurrentAvailabilityForResource(server.id);
i f
( avail.availabilityType.toString() == "DOWN" ) {
println(
" Server is DOWN. Please first start the server and run this script again!" );
println(
"" );
}
When the script is run, it prints the availability and last start time for the servers.
[jsmith@ server cli]$ ./wrapper.sh status myGroup
Remote server version is: 3.0.1.GA (b2cb23b:859b914)
Login successful
found AS server.example.com JBossAS App 1
AS server.example.com JBossAS App 1
- Availability: UP
- Started : 11 Feb 2012 04:07:37 GMT
Report a bug
7. St art ing, St opping, and Rest art ing t he Server: rest art .js
Set up the script with the usage information and the group search, as in Section 6, “ Getting
Inventory and Status Information: status.js” .
This example only performs one operation, restarting a JBoss server. It iterates through all the resources in the group.
It is possible to write similar scripts for starting and stopping the server.
135
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
shutd o wn() for AS4 servers and shutD o wn() for AS5 servers
start()
criteria =
new
ResourceCriteria();
var
groupArray=
new
Array ();
groupArray[0]=group.id;
criteria.addFilterExplicitGroupIds(groupArray);
var
resources = ResourceManager.findResourcesByCriteria(criteria);
fo r
( i =0; i < resources.size(); ++i) {
var
resource = resources.get(i);
var
resType = resource.resourceType.
name ;
println(
" found " + resource.
name );
i f
( resType != "JBossAS Server" ) {
println(
" ---> Resource not of required type.
Exiting!" );
usage();
}
var
server = ProxyFactory.getResource(resource.id);
println(
" stopping " + server.
name + "...." );
try {
server.shutdown()
}
catch
( ex ) {
println(
" --> Caught " + ex );
}
println(
" restarting " + server.
name + "....." );
try {
server.start();
}
catch
( ex ) {
println(
" --> Caught " + ex );
}
}
Report a bug
8. Deploying Applicat ions t o t he Group Members: deploy.js
Set up the usage information and the group search as in the other scripts, then use the
deployment script described in Chapter 15, Example: Scripting Resource Deployments (JBoss
The script uses two parameters, one for the group name and one for the file to upload.
136
Chapt er 1 8 . Example: Managing G rouped Servers (JBoss EAP 5)
As one easy improvement, the last part of Section 2, “ Checking the JBoss ON Groups and
Inventory” stops the JBoss server, uploads the content, and restarts it. Instead, simply check
that the server is running first, and then upload the content:
// we need check to see if the given server is up and running
var
avail =
AvailabilityManager.getCurrentAvailabilityForResource(server.id);
// unfortunately, we can only proceed with deployment if the server is running. Why?
i f
( avail.availabilityType.toString() == "DOWN" ) {
println(
" Server is DOWN. Please first start the server and run this script again!" );
println(
"" );
co nti nue
;
}
Report a bug
9. Scheduling an Availabilit y Operat ion: avail.js
Unlike the other tasks in this script set, the operation task is run on the agent, so it is not necessary to search for the group or JBoss resource. This runs an availability scan on the agent; it is also possible to run a specific command on the agent using the Execute
pro mpt co mmand operation.
First, get a list of all agent resources:
println(
"Scanning all RHQ Agent instances" );
var
rc = ResourceCriteria();
var
resType = ResourceTypeManager.getResourceTypeByNameAndPlugin( "RHQ
Agent" , "RHQAgent" );
rc.addFilterPluginName(
"RHQAgent" );
rc.addFilterResourceTypeName(
"RHQ Agent" );
rc.addFilterParentResourceTypeId(
"10001" );
var
resources = ResourceManager.findResourcesByCriteria(rc).toArray();
var
idx=0;
fo r
( i
i n
resources ) {
i f
( resources[i].resourceType.id == resType.id ) {
resources[idx] = resources[i];
idx = idx +
1 ;
}
}
Then, traverse the agents array and schedule the operation:
fo r
( a
i n
resources ) {
var
agent = resources[a]
var
resType = agent.resourceType.
name ;
137
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
println(
" Found resource " + agent.
name + " of type " + resType
+ " and ID " + agent.id);
println(
" executing availability scan on agent" );
println(
" -> " + agent.
name + " / " + agent.id);
var
config =
new
Configuration();
config.put(
new
PropertySimple( "changesOnly" , "true" ) );
var
ros = OperationManager.scheduleResourceOperation(
agent.id,
"executeAvailabilityScan" ,
0,
1 ,
0,
10000000 ,
config,
"test from cli"
);
println(ros);
println(
"" );
}
138
Report a bug
10. Gat hering Met ric Dat a of Managed Servers: met rics.js
JBoss ON collects a number of metrics for each resource type. This information can be retrieved by using the fi nd Li veD ata method, which returns the current active value for the resource.
This script takes two arguments, the groupName and the metricName. As with the other scripts, this searches for the group and then the resource by the group ID.
functi o n
usage() {
println(
"Usage: metrics groupName metricName" );
throw
"Illegal arguments" ;
}
i f
( args.
length < 2 ) usage();
var
groupName = args[0];
var
metricName = args[ 1 ];
groupcriteria =
new
ResourceGroupCriteria();
groupcriteria.addFilterName(groupName);
var
groups =
ResourceGroupManager.findResourceGroupsByCriteria(groupcriteria);
i f
( groups != null ) {
i f
( groups.size() > 1 ) {
println(
"Found more than one group." );
}
el se i f
( groups.size() == 1 ) {
group = groups.get(0);
Chapt er 1 8 . Example: Managing G rouped Servers (JBoss EAP 5)
}
}
criteria =
new
ResourceCriteria();
var
groupArray=
new
Array ();
groupArray[0]=group.id;
criteria.addFilterExplicitGroupIds(groupArray);
The actual metric search looks for the metrics available to the resource type (hard-coded to
JBoss AS 5 in this example). The metric itself is identified solely by the metricName argument.
var
rt = ResourceTypeManager.getResourceTypeByNameAndPlugin( "JBossAS 5
Server" , "JBossAS5" );
var
mdc = MeasurementDefinitionCriteria();
mdc.addFilterDisplayName(metricName);
mdc.addFilterResourceTypeId(rt.id);
var
mdefs =
MeasurementDefinitionManager.findMeasurementDefinitionsByCriteria(mdc)
;
var
resources = ResourceManager.findResourcesByCriteria(criteria);
var
metrics = MeasurementDataManager.findLiveData(resources.get(0).id,
[mdefs.get(0).id]);
i f
( metrics !=null ) {
println(
" Metric value for " + resources.get(0).id + " is " + metrics );
}
When the script is run, it prints the resource ID and the current value for the metric.
[jsmith@ server cli]$ ./wrapper.sh metrics myGroup "Active Thread
Count"
Remote server version is: 3.0.1.GA (b2cb23b:859b914)
Login successful
Metric value for 10003 is [MeasurementDataNumeric[value=[64.0],
MeasurementData [MeasurementDataPK: timestamp=[Wed Feb 15 22:14:38 EST
2012], scheduleId=[1]]]]
Report a bug
139
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Chapter 19. Example: Deploying a Standalone Server to a
Cluster (JBoss EAP 6)
There is a sample script in the cliRoot/rhq-remoting-cli-4.9.0.JON320GA/sampl es directory the defines a series of functions that allow a JBoss EAP 6 standalone server to be added to an existing cluster.
JBoss EAP 6 has the idea of domains, which can be subdivided into server groups which share configuration. Server groups allows multiple server instances to have consistent, uniform configuration settings, to share profiles, and to deploy the same applications through a central command point.
However, JBoss EAP 6 still has the idea of a classic standalone server, a single JBoss instance that is unaffiliated with a domain or server group. A standalone server can be joined in a cluster, a loose association of standalone servers that work together to distribute the work load, either for load balancing or high availability. Unlike a domain, a cluster does not manage configuration or content.
JBoss ON provides the centralized management over configuration and content for standalone servers, introducing some of the ease of maintenance that EAP 6 domains offer.
The cliRoot/rhq -remo ti ng -cl i -
4 . 9 . 0 . JO N320 G A/sampl es/mo d ul es/jbo ssas. js file defines some useful functions that simplify identifying cluster servers, deployed content, and relevant configuration settings.
The bulk of the functions defined are private and are well-documented within the
cliRoot/rhq -remo ti ng -cl i -4 . 9 . 0 . JO N320 G A/sampl es/mo d ul es/jbo ssas. js
file, so they are out of the scope of this example. The main purpose of this is example is to review the two public functions which perform two distinct cluster management tasks:
ad d T o C l uster co pyD epl o yments
Report a bug
1. Adding a St andalone EAP 6 Server t o a Clust er
A cluster is defined by servers which use the same multicast properties on the same network.
If the servers are configured with the same settings, they all automatically associate with each other in a cluster.
There are three configuration properties for a standalone server within a cluster:
An identifying name for the JBoss instance to use within the cluster (the node-name)
Multicast settings, including the multicast port, a UDP port, and multicast address
14 0
Chapt er 1 9 . Example: Deploying a St andalone Server t o a Clust er (JBoss EAP 6 )
Socket-binding group information used by mod_cluster
A cluster does not directly manage either configuration properties or deployed content.
However, if two EAP 6 standalone servers are in the JBoss ON inventory, then JBoss ON can work somewhat as a bridge, comparing the configuration and content deployments between servers and copying between them. That comparison is what the ad d T o C l uster function uses to add a standalone server to a cluster. It uses the configuration properties in an existing cluster member and copies them over to the standalone server.
Actually running the script requires the name of the standalone server, the name of an existing cluster member, a node name for the standalone server, and a boolean that sets whether to copy over the deployments from the existing cluster to the new member.
Assuming that you already know resource IDs of the standalone server and an existing cluster member:
[root@ server bin]# ./rhq-cli.sh -u rhqadmin -p rhqadmin
rhqadmin@ localhost:
7080 $
var
newAs7Resource =
ProxyFactory.getResource( 10381 )
rhqadmin@ localhost:
7080 $
var
existingClusterMemberResource =
ProxyFactory.getResource( 10577 )
rhqadmin@ localhost:
7080 $
var
newNodeName = jbas7-standalone1
rhqadmin@ localhost:
7080 $ addToCluster(newAs7Resource, newNodeName, existingClusterMemberResource, true)
The ad d T o C l uster function makes some assumptions that the cache configuration between the two servers is compatible, both for concrete caches and the cache containers for individual subsystems.
The script runs through a few steps to copy the configuration from the cluster to the standalone server:
1. It checks the plug-in connection properties in the cluster server and compares them to the plug-in connection properties in the standalone server. If necessary, it copies over the plug-in configuration from the cluster server and restarts the standalone server, loading the new configuration.
2. It checks the given node name for the standalone server. If necessary, it changes the default node name to the one passed with the function.
3. It then compares the socket-binding settings for the cluster and standalone servers. If necessary, it copies over the socket-binding configuration for the jgroups, messaging, and mod_cluster bindings from the cluster server and restarts the standalone server.
4. If set, then the script copies the deployments from the cluster server to the standalone server and restarts the standalone server.
The first part of the function pulls the plug-in configuration (defined by the private function
_g etC l usterSi g ni fi cantC o nfi g ) for the cluster and then for the standalone server.
14 1
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s functi o n
addToCluster(newAs7Resource, newNodeName, existingClusterMemberResource, copyDeployments) {
println(
"Reading config of the existing cluster member" );
var
clusterConfig =
_getClusterSignificantConfig(existingClusterMemberResource);
println(
"Reading config of the new member" );
var
memberConfig = _getClusterSignificantConfig(newAs7Resource);
var
memberResourceConfiguration = newAs7Resource.resourceConfiguration;
If the configuration properties are different, then the script copies over the new plug-in configuration and restarts the standalone server to load the new connection settings.
i f
(memberConfig[ 'config' ] != clusterConfig[ 'config' ]) {
println(
"The configurations of the servers differ.\n" +
"The new cluster member's configuration will be changed to match the configuration of the existing member." );
//switch to the same configuration
var
pluginConfig = newAs7Resource.pluginConfiguration;
pluginConfig.getSimple( 'config' ).setValue(clusterConfig[ 'config' ]);
newAs7Resource.updatePluginConfiguration(pluginConfig);
//we need to restart straight away so that we see the changes to the
//rest of the configuration caused by the change of current config.
println(
"Restarting the new cluster member to switch it to the new configuration." );
newAs7Resource.restart();
//refresh the resource
newAs7Resource = ProxyFactory.getResource(newAs7Resource.id);
//refresh the cluster specific config after the restart with the new
//config
memberConfig = _getClusterSignificantConfig(newAs7Resource);
memberResourceConfiguration = newAs7Resource.resourceConfiguration;
}
It then applies the node name that was given with the script, if it is different than the one set by default.
//now check what's the node name we see
i f
(memberConfig[ 'node-name' ] != newNodeName) {
println(
"Updating the node name of the new cluster member from
'" + memberConfig[ 'node-name' ] + "' to '" + newNodeName + "'" );
_updateNodeName(memberResourceConfiguration, newNodeName);
14 2
Chapt er 1 9 . Example: Deploying a St andalone Server t o a Clust er (JBoss EAP 6 )
newAs7Resource.updateResourceConfiguration(memberResourceConfiguration
);
}
The next configuration area for the cluster is the socket-binding settings for important subsystems, jgroups, messaging, and mod_cluster.
//now apply the socket binding changes for jgroups and other cluster
//significant subsystems
//first find the socket binding group config in the new member
fo r
(i
i n
newAs7Resource.children) {
var
child = newAs7Resource.children[i];
i f
(child.resourceType.
name == 'SocketBindingGroup' & &
child.resourceType.
plugin == 'jboss-as-7' ) {
println(
"Updating socket bindings of jgroups, messaging and modcluster subsystems" );
var
portOffset = javascriptString(child.resourceConfiguration.getSimpleValue( 'portoffset' , '0' ));
var
clusterMemberPortOffset = clusterConfig[ 'port-offset' ];
var
newConfig = child.resourceConfiguration.deepCopy(
fal se
);
_updateSocketBindings(newConfig, portOffset, clusterMemberPortOffset, clusterConfig[ 'jgroups' ]);
_updateSocketBindings(newConfig, portOffset, clusterMemberPortOffset, clusterConfig[ 'messaging' ]);
_updateSocketBindings(newConfig, portOffset, clusterMemberPortOffset, clusterConfig[ 'modcluster' ]);
child.updateResourceConfiguration(newConfig);
}
}
println(
"Restarting the new member for the new socket bindings to take effect." );
newAs7Resource.restart();
Although not strictly part of the cluster configuration, part of what JBoss ON can do is compare other parts of the resource setup, like deployed applications. Synchronizing the deployed applications between one server and another, even standalone instances, helps maintain consistency, and this can be done conveniently at the time that a server is added to a cluster simply by syncing the given cluster server's deployments.
i f
(copyDeployments) {
println(
"Copying the deployments to the new cluster member..." );
copyDeployments(existingClusterMemberResource,
14 3
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
newAs7Resource);
println(
"Restarting the new cluster member." );
newAs7Resource.restart();
}
}
Report a bug
2. Copying Deployed Applicat ions Bet ween St andalone Servers
While EAP 6 server groups manage content centrally for all group members, standalone servers are on their own. JBoss ON can help as an intermediary to sync application content between separate server instances. The ad d T o C l uster function has this as an option when joining a standalone server to a cluster. The co pyD epl o yments function can copy deployments between any two standalone instances.
Invoking the function requires only the name of the source EAP 6 server (the one to copy the deployments from) and then the name of the target EAP 6 server (the one to copy the deployments to).
Assuming that you already know resource IDs of the two EAP 6 server resources, set the source and target resources. For example, in interactive mode:
[root@ server bin]# ./rhq-cli.sh -u rhqadmin -p rhqadmin
rhqadmin@ localhost:
7080 $
var
source = ProxyFactory.getResource( 10381 )
rhqadmin@ localhost:
7080 $
var
target = ProxyFactory.getResource( 10577 )
rhqadmin@ localhost:
7080 $ copyDeployments( source , target)
The first part of the function gets the server resource IDs.
functi o n
copyDeployments(sourceAS7, targetAS7) {
i f
(typeof sourceAS7 == 'object' ) {
sourceAS7 = sourceAS7.id;
}
i f
(typeof targetAS7 == 'object' ) {
targetAS7 = targetAS7.id;
}
All of the deployed applications are listed as children of the source JBoss EAP 6 server. The
co pyD epl o yments function retrieves each deployment by searching for all of the children of the server that are of a deployment resource type.
var
deploymentResourceType =
ResourceTypeManager.getResourceTypeByNameAndPlugin( 'Deployment' ,
'jboss-as-7' );
var
deploymentsCrit =
new
ResourceCriteria;
deploymentsCrit.addFilterParentResourceId(sourceAS7);
14 4
Chapt er 1 9 . Example: Deploying a St andalone Server t o a Clust er (JBoss EAP 6 )
deploymentsCrit.addFilterResourceTypeId(deploymentResourceType.id);
var
unlimitedPageControl = PageControl.unlimitedInstance;
var
sourceDeployments =
ResourceManager.findResourcesByCriteria(deploymentsCrit);
var
iterator = sourceDeployments.iterator();
whi l e
(iterator.hasNext()) {
var
deploymentResource = iterator.
next ();
//get a resource proxy for easy access to configurations, etc.
deploymentResource =
ProxyFactory.getResource(deploymentResource.id);
println(
"Copying deployment " + deploymentResource.
name );
Each discovered deployment is then copied over as a new child resource to the target server.
These are content-backed resources, so they are exported and uploaded as content. The function also searches for and pulls in the content metadata and the content history, so that any important historical information about the deployment is also copied over.
var
installedPackage =
ContentManager.getBackingPackageForResource(deploymentResource.id);
var
content =
ContentManager.getPackageBytes(deploymentResource.id, installedPackage.id);
var
runtimeName = deploymentResource.resourceConfiguration.getSimpleValue( 'runtimename' , deploymentResource.
name );
var
deploymentConfiguration =
new
Configuration;
deploymentConfiguration.put(
new
PropertySimple( 'runtimeName' , runtimeName));
//so now we have both metadata and the data of the deployment, let's
//push a copy of it to the target server
var
history =
ResourceFactoryManager.createPackageBackedResource(targetAS7,
deploymentResourceType.id, deploymentResource.
name ,
deploymentResource.pluginConfiguration,
installedPackage.packageVersion.generalPackage.
name ,
installedPackage.packageVersion.version,
installedPackage.packageVersion.architecture.id,
deploymentConfiguration, content, null);
whi l e
( history .
status .
name () == 'IN_PROGRESS' ) {
java.lang.Thread.sleep(
1000 );
//the API for checking the create histories is kinda weird..
var
histories =
ResourceFactoryManager.findCreateChildResourceHistory(targetAS7, null, null, unlimitedPageControl);
var
hit = histories.iterator();
14 5
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
var
found =
fal se
;
whi l e
(hit.hasNext()) {
var
h = hit.
next ();
i f
(h.id == history .id) {
history = h;
found = true;
break
;
}
}
i f
(!found) {
throw
"The history object for the deployment seems to have disappeared, this is very strange." ;
}
}
println(
"Deployment finished with status: " + history .
status .toString() +
( history .
status .
name () == 'SUCCESS' ? "." : ( ", error message: " + history .errorMessage + "." )));
}
}
Report a bug
14 6
Chapt er 1 9 . Example: Deploying a St andalone Server t o a Clust er (JBoss EAP 6 )
Chapter 20. Example: Deploying Applications Through
Bundles (General)
Bundles are a very clean and easy way to deploy full applications, configuration files, or other content to resources. Whether a given resource type supports bundles is defined in its plug-in descriptor. By default, platform resources and JBoss AS 4, 5, and 6 resources all support bundles.
Bundles are convenient from an administrative perspective because all of the content is maintained in a single place that is resource-agnostic. The main bundle entry or bundle definition contains a set of versions of the actual bundle files and a set of destinations for where that content can be deployed. A destination is a combination of a compatible group, resource type, and directory path. When a version is actually deployed to a destination, it is saved as a specific deployment for that destination.
The bundles system maintains multiple versions of a given package and can deploy any of those versions to any destination. This is a great workflow for application lifecycles, since a stable version can be deployed to production servers while a development version can be deployed to test machines. Having each deployment represented as a different child of the destination makes it easy to revert changes; you can move from the live version to a previous version and know exactly what that previous deployment looked like.
Report a bug
1. Set t ing up Bundle Versions and Dest inat ions
There are two parts to the bundle definition: the bundle version and the destination. Both of these parts are set up independently, and then saved into the final definition.
The bund l es. js script in the cliRoot/rhq -remo ti ng -cl i -
4 . 9 . 0 . JO N320 G A/sampl es/mo d ul es/ directory defines a set of custom functions that quickly create a bundle version and definition. (The bund l es. js script requires the
uti l . js script.)
To create the destination, give the absolute path on the local system to the bundle archive to upload.
rhqadmin@ localhost:
7080 $
var
path = '/export/files/myApp.zip'
rhqadmin@ localhost:
7080 $ createBundleVersion(path)
The createBund l eVersi o n function in the bund l es. js file uploads the files as a byte array.
functi o n
createBundleVersion(pathToBundleZipFile) {
var
bytes = getFileBytes(pathToBundleZipFile)
return
BundleManager.createBundleVersionViaByteArray(bytes)
}
14 7
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
The second part of the bundle definition is creating at least one destination where the bundle version could be deployed. Creating a destination requires two things to exist already:
A bundle version (which was just created with createBund l eVersi o n)
A compatible group
A destination is a combination of a compatible group and the directory to deploy to. Each resource type defines its own available base directory, then a subdirectory beneath that root can be specified as the deployment directory.
The other configuration properties are details for the entry, such as the destination name and description.
rhqadmin@ localhost:
7080 $
var
destinationName = 'New Destination'
rhqadmin@ localhost:
7080 $
var
description = 'My new example destination'
rhqadmin@ localhost:
7080 $
var
bundleName = 'myApp'
rhqadmin@ localhost:
7080 $
var
groupName = 'Linux Group'
rhqadmin@ localhost:
7080 $
var
baseDirName = '/'
rhqadmin@ localhost:
7080 $
var
deployDir = 'var/www/html/'
rhqadmin@ localhost:
7080 $ createBundleDestination(destinationName, description , bundleName, groupName, baseDirName, deployDir)
The createBund l eD esti nati o n function runs the searches for the group and the bundle based on the specified names, which makes it possible to set up the destination without having to run additional searches.
functi o n
createBundleDestination(destinationName, description , bundleName, groupName, baseDirName, deployDir) {
var
groupCrit =
new
ResourceGroupCriteria;
groupCrit.addFilterName(groupName);
var
groups =
ResourceGroupManager.findResourceGroupsByCriteria(groupCrit);
...
8 < ...
var
group = groups.get(0);
var
bundleCrit =
new
BundleCriteria;
bundleCrit.addFilterName(bundleName);
var
bundles = BundleManager.findBundlesByCriteria(bundleCrit);
...
8 < ...
}
Report a bug
2. Deploying Bundles
14 8
Chapt er 2 0 . Example: Deploying Applicat ions T hrough Bundles (G eneral)
Deploying a bundle sends a bundle version to a specific destination. The cliRoot/rhq -
remo ti ng -cl i -4 . 9 . 0 . JO N320 G A/sampl es/mo d ul es/bund l es. js file has a function, d epl o yBund l e, which makes this pretty easy, but you need to obtain some information first.
Get the ID for the destination. This searches for the destination by name.
rhqadmin@ localhost:
7080 $
var
destinationName = "New Destination"
rhqadmin@ localhost:
7080 $
var
destcrit =
new
BundleDestinationCriteria()
rhqadmin@ localhost:
7080 $ destcrit.addFilterName(destinationName)
var
dest = BundleManager.findBundleDestinationsByCriteria(destcrit)
Then, get the ID number for the bundle version to deploy. Any version can be deployed, not just the most recent. This little script prints all of the versions for the bundle, with their ID numbers.
rhqadmin@ localhost:
7080 $
var
crit =
new
BundleVersionCriteria()
rhqadmin@ localhost:
7080 $ crit.addFilterBundleName( name )
rhqadmin@ localhost:
7080 $
var
vers =
BundleManager.findBundleVersionsByCriteria(crit)
rhqadmin@ localhost:
7080 $
i f
( vers != null ) { \
rhqadmin@ localhost:
7080 $
i f
( vers.size() > 1 ) { \
rhqadmin@ localhost:
7080 $
fo r
( i =0; i < vers.size(); ++i) { \
rhqadmin@ localhost:
7080 $ ver = vers.get(i); \
rhqadmin@ localhost:
7080 $ println( "Version: " + ver.version +
" " + "ID: " + ver.id) \
rhqadmin@ localhost:
7080 $ } \
rhqadmin@ localhost:
7080 $ } \
rhqadmin@ localhost:
7080 $
el se i f
( vers.size() == 1 ) { \
rhqadmin@ localhost:
7080 $ ver = vers.get(0); \
rhqadmin@ localhost:
7080 $ println( "Version: " + ver.version + + "
" + "ID: " + ver.id) \
rhqadmin@ localhost:
7080 $ } \
rhqadmin@ localhost:
7080 $ }
Version:
2.0
ID: 10021
Version:
1.0
ID: 10012
With those two ID numbers, you can deploy the bundle. The first parameter is the destination
ID, then the bundle version ID, then a configuration object if the bundle configuration has any tokens to realize. In this example, no properties are passed, so the value is null. Details about the configuration are in the comments in the bund l es. js file and general configuration information is in
Section 5.2, “ Changing Simple Configuration Properties”
.
rhqadmin@ localhost:
7080 $ deployBundle(dest.get(0).id, 10021 ,null, 'my description' ,true)
BundleDeployment:
bundleVersion:
BundleVersion[id= 10021 , name =null,version=null]
14 9
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
configuration: Configuration[id=
15021 ]
ctime:
1337286719259
description : my description
destination: BundleDestination[id=
10021 , bundle=driftBundle, group=Linux Group - Thu May 10 15 : 10 : 28 EDT 2012 , name =NewDestination]
duration: 0
errorMessage:
id:
10051
live: true
mtime:
1337286719259
name : Deployment [ 1 ] of Version [ 2.0
] to
[NewDestination]
replacedBundleDeploymentId:
resourceDeployments: [BundleResourceDeployment: bdd=
[BundleDeployment[id= 10051 , name =Deployment [ 1 ] of Version [ 2.0
] to
[new-test]]], resource=[Resource[id= 10001 , uuid= 535 b3f54-0bd8-4653bdd3323 ea69b98fd, type={Platforms}Linux, key=gs-dl585g2-
01.rhts.eng.bos.redhat.com, name =server.example.com, parent =<null>, version=Linux 2.6
.
32 220.
el6.x86_64]]]
status : Failure
subjectName: rhqadmin
tags:
The d epl o yBund l e function runs through a couple of steps to manage the deployment.
This uses one of the functions from the uti l . js file to convert the deployment configuration
(if any is sent) into the proper into a hash.
...
8 < ...
var
deploymentConfig = deploymentConfiguration;
i f
(!(deploymentConfiguration instanceof Configuration)) {
deploymentConfig = asConfiguration(deploymentConfiguration);
}
The next creates the deployment (through the remote API) and then schedules the deployment.
...
8 < ...
var
deployment =
BundleManager.createBundleDeployment(bundleVersionId, destinationId, description , deploymentConfig);
deployment =
BundleManager.scheduleBundleDeployment(deployment.id, isCleanDeployment);
...
8 < ...
Report a bug
3. Revert ing a Bundle
150
Chapt er 2 0 . Example: Deploying Applicat ions T hrough Bundles (G eneral)
Reverting a bundle automatically moves a destination one step backward, from whatever version is currently deployed to whatever version was last deployed.
This is done only with the remote API, not using any functions from the bund l es. js file.
The method to run is sched ul eR evertBund l eD epl o yment. This requires two interesting pieces of information. The first is the destination ID, which can be retrieved with a simple criteria search.
rhqadmin@ localhost:
7080 $
var
destinationName = "NewDestination"
rhqadmin@ localhost:
7080 $
var
destCrit =
new
BundleDestinationCriteria()
rhqadmin@ localhost:
7080 $ destCrit.addFilterName(destinationName)
rhqadmin@ localhost:
7080 $
var
dest =
BundleManager.findBundleDestinationsByCriteria(destCrit)
The next, and more interesting, piece of information is the deployment description. The description is what is passed to the revert method to help identify the deployment to revert.
rhqadmin@ localhost:
7080 $
var
depCrit =
new
BundleDeploymentCriteria()
rhqadmin@ localhost:
7080 $ depCrit.addFilterDestinationName(destinationName)
rhqadmin@ localhost:
7080 $
var
deploy =
BundleManager.findBundleDeploymentsByCriteria(depCrit)
rhqadmin@ localhost:
7080 $
var
dep = deploy.get(0);
rhqadmin@ localhost:
7080 $
var
description = dep.
description ;
The last part actually invokes the method.
rhqadmin@ localhost:
7080 $
BundleManager.scheduleRevertBundleDeployment(dest.get(0).id, description , true)
Report a bug
151
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Chapter 21. Example: Remedying Resource Drift
Maintain servers, particularly production or business critical servers and applications, requires keeping rein on the configuration files and packages on those systems. When an unexpected change occurs, the system moves away from the administrator-defined state.
That is configuration drift.
JBoss ON can monitor configuration files and target directories and track any changes to those area. This uses a drift definition which sets where the JBoss ON agent monitors configuration and at what frequency. If drift is detected, then the JBoss ON server can fire an alert and run an alert CLI script that reverts, or remedies, the changed configuration files.
Report a bug
1. T he Plan for t he Script s
There are two different scripts in play because there are two different sets of situations for managing drift:
First, there is a script to set up drift for a resource. This shell script runs through a series of setup steps at once:
It creates the drift definition for a resource (through the d ri ftD ef. js CLI script).
It creates a generic d epl o y. xml recipe, zips the drift directory, and creates a new bundle and bundle deployment (through the createBund l e. js CLI script).
After waiting for the initial snapshot, it then pins the snapshot to the definition
(through the snapsho t. js CLI script).
All of those files are generated by the shell script.
An alert definition has to be created through the UI (not the CLI), but it can be configured to use any drift detection as a condition and then to run a server-side script in response.
This second script simply deploys the bundle that was made of the pristine base directory and overwrites the drift.
Report a bug
2. Set t ing up t he Drift Definit ion and Preparing t he Bundle
The setup script actually runs through three CLI scripts and some system commands. Having all of the steps in a single script makes it possible to set up a drift definition and a backup bundle by running a single command:
[root@ server ~]# ./driftBundle.sh
152
Chapt er 2 1 . Example: Remedying Resource Drift
NO T E
Both drift definitions and bundle deployments take a lot of resource- and infrastructurespecific settings. The d ri ftBund l e. sh script in this example defines a lot of variables in the script to account for each piece of information.
The variables could be defined using a . co nf or even a set of . co nf files (cf.
Section 2, “ Creating the Wrapper Script and .conf File”
), but for simplicity in this example, all of the variables are defined in the d ri ftBund l e. sh script itself.
The first part of the script simply defines the connection settings to use when running the
JBoss ON CLI. This example only defines a username and password, so it assumes that the script is run on a system which also has a JBoss ON server running locally. The options could be edited to supply a remote JBoss ON server hostname and port.
There are three general variables defined:
The location of the rhq -cl i . sh script
Any options, such as the username and password, to pass with the CLI command
The directory to use both to save the generated JavaScript files and to use for the path to
JavaScript files
#!/bin/bash
# options for the CLI
CLI= 'cliRoot/rhq-remoting-cli-4.9.0.JON320GA/bin/rhq-cli.sh'
OPTS= ' -u rhqadmin -p rhqadmin'
SCRIPTS= '/opt'
The first part of the script sets up the drift definition. By default, drift is only enabled for a handful of resource types — JBoss servers, Tomcat servers, and platforms — so it is easiest to identify the resource based on a combination of its resource type and name.
Once the resource is identified, then the definition can be created. A general definition will identify the base directory to monitor, set some rules about what files or subdirectories to ignore (like log files), and set an interval or frequency for drift detection scans.
All of these definition parameters are defined as individual variables in the shell script. In this example, drift is configured for a platform.
# set parameters for the drift definition
RESTYPE= 'Linux'
RESPLUGIN= 'Platforms'
RESNAME= "server.example.com"
NAME= 'example drift'
DESC= 'drift from script'
BASEDIR= '/opt/drift'
BASEDIRTYPE= 'fileSystem'
153
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
EXCLUDE= './logs/'
PATTERN=
MODE= 'normal'
INTERVAL= '3600'
The shell script will eventually create a CLI script that is run automatically in the CLI. The first part of the CLI script defines a resource type criteria search for the platform, and then the resource platform itself.
driftDef() {
cat <<-EOF
//set the resource type
var
resType =
ResourceTypeManager.getResourceTypeByNameAndPlugin( "$RESTYPE" , "$RESPLU
GIN" );
//get the resource to associate with the drift definition
rcrit = ResourceCriteria()
rcrit.addFilterResourceTypeName(
"$RESTYPE" )
rcrit.addFilterName(
"$RESNAME" )
var
resources = ResourceManager.findResourcesByCriteria(rcrit)
var
res = resources.get(0)
No t e
This script searches for a single resource to configure for drift. You could also create the script to search for multiple resource and add them to a compatible group, and the iterate through the compatible group to add the drift definition to each resource.
The next part configures the drift definition itself. The D ri ftD efi ni ti o nManag er is a wrapper for a C o nfi g urati o n() object. The CLI script first calls for the default drift template for the given resource type and then creates a definition object based on that template.
154
//get the default template for the resource type
criteria = DriftDefinitionTemplateCriteria()
criteria.addFilterResourceTypeId(resType.id)
templates = DriftTemplateManager.findTemplatesByCriteria(criteria)
template = templates.get(0)
//create a new drift definition instance, based on the template
definition = template.createDefinition()
Once the configuration object is created, then the definition options are assigned values.
This script creates a real drift definition with one exception: it sets a very low scan interval,
30 seconds. In fact, that is the shortest configurable interval. This allows the agent to collect the initial snapshot fairly quickly, which helps the overall setup go faster. This interval will be reset to a more reasonable value (the one defined in the variables) at the end of the script execution.
Chapt er 2 1 . Example: Remedying Resource Drift
//set the drift definition configuration options
definition.resource = res
definition.
name = '$NAME'
definition.
description = '$DESC'
definition.setAttached(
fal se
) // this is false so that template changes don't affect the definition
// this is set low to trigger an early initial detection run
definition.setInterval(
30 )
var
basedir =
new
DriftDefinition.BaseDirectory(DriftConfigurationDefinition.BaseDirValu
eContext.
valueOf ( '$BASEDIRTYPE' ), '$BASEDIR' )
definition.basedir = basedir
// there can be multiple exclude statements made, as desired
var
f =
new
Filter( "$EXCLUDE" , "$PATTERN" ) // location, pattern
definition.addExclude(f)
//this defaults to normal, which means that any changes will
// trigger an alert. plannedChanges is the other option, which
// disables alerting for drift changes.
definition.setDriftHandlingMode(DriftConfigurationDefinition.DriftHand
lingMode.
valueOf ( '$MODE' ))
Once the configuration is complete, it needs to be written to the definition.
//apply the new definition to the resource
DriftManager.updateDriftDefinition
(
EntityContext.forResource
(
res.
id
)
,d efinition
)
EOF
}
No t e
The drift definition uses an entity context rather than the resource ID alone to identify the resource. An entity context first identifies the type of object (the entity) and then its associated inventory ID.
There are actually several different steps for creating a "bundle" because there is no one part to a "bundle." The script makes a Z IP archive of the given drift base directory, and that makes the bundle archive. Then, for defining the bundle, there are two steps. There is defining the bundle destination, which is a compatible group to which bundles (any bundles) can be deployed plus the location on the resources for deploying the bundles. Then the package itself is uploaded as a bundle version.
The variables define both the information for the bundle version and bundle archive and for the bundle destination.
155
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
There is one other variable included: the path to the CLI's samples directory. Helper functions to create bundle versions, to create bundle destinations, and to deploy specified bundles are already defined in the bund l es. js sample script. Using those functions makes deploying bundles very easy.
# options for the bundle
SAMPLES= 'cliRoot/rhq-remoting-cli-4.9.0.JON320GA/samples'
DESTNAME= 'drift destination'
BUNDLEDESC= 'bundle to remediate drift'
BUNDLENAME= 'driftBundle'
GROUPNAME= 'Linux Group'
ZIP= 'driftBundle.zip'
BVER= '1.0'
BUNDLE= '/opt/bundles/' $ZIP
ARCHIVE= '/opt/bundles/' $ZIP
This particular bundle deployment is pretty simple. The target bundle destination is the same as the drift base directory.
Since there are no tokens to realize or external content to pull in, just the backup archive itself, the recipe can be pretty simple. This script creates the recipe (d epl o y. xml ) which is used in the bundle archive.
deploy() {
cat
<< _EOF_
<pro ject
name= "$BUNDLENAME" default= "main"
xmlns:rhq= "antlib:org.rhq.bundle"
>
<rhq : bund l e
name= "$BUNDLENAME" version= "$BVER" description= "$BUNDLEDESC"
>
<rhq : d epl o yment-uni t
name= "drift" compliance= "full"
>
<rhq : archi ve
name= "$ZIP" exploded= "true"
>
</rhq : archi ve>
</rhq : d epl o yment-uni t>
</rhq : bund l e>
<targ et
name= "main"
/>
</pro ject>
_EOF_
}
The bund l es. js sample script already defines all of the functions required to deploy bundles, but it relies on the uti l . js sample script. When the CLI is run non-interactively, there is no way to import an external script that another script requires.
156
So, this shell script first concatenates the bund l es. js and uti l . js scripts together, and then appends the calls to create the bundle version and the bundle destination.
createBundle() {
cat $SAMPLES/util.js $SAMPLES/bundles.js
cat << _EOF_
// set the location of the bundle archive
Chapt er 2 1 . Example: Remedying Resource Drift var
path = '$BUNDLE'
// create the bundle version in JON
createBundleVersion(path)
// set all of the variables for the bundle destination
var
destinationName = '$DESTNAME'
var
description = '$BUNDLEDESC'
var
bundleName = '$BUNDLENAME'
var
groupName = '$GROUPNAME'
var
baseDirName = '$BASEDIR'
var
deployDir = "."
// create the new destinition in JON
createBundleDestination(destinationName, description , bundleName, groupName, baseDirName, deployDir)
_EOF_
}
No t e
Make sure that the resource already belongs to a compatible group and that the compatible group has a unique enough name so that it is the only one returned in the search.
The last CLI script created by the shell script pins the initial snapshot to the new drift definition. A snapshot, as the name implies, is a picture of the current settings of the base directory. Pinning a snapshot to a definition sets a baseline, or comparison, for the agent to use to evaluate drift. A pinned snapshot is a specific and identified configuration that must be maintained (as opposed to rolling changes).
Once the snapshot is pinned, this script then resets the drift definition configuration so that it uses a longer (more realistic) interval between scans.
snapshot()
{
cat << _EOF_
//find the resource
rcrit = ResourceCriteria()
rcrit.addFilterResourceTypeName("
$RESTYPE ")
rcrit.addFilterName("
$RESNAME ")
var resources = ResourceManager.findResourcesByCriteria(rcrit)
var res = resources.get(0)
//find the new drift definition
criteria = DriftDefinitionCriteria()
criteria.addFilterName('
$NAME ')
criteria.addFilterResourceIds(res.id)
def = DriftManager.findDriftDefinitionsByCriteria(criteria)
definition = def.get(0)
definition.setInterval(
$INTERVAL )
// it is necessary to redefine the complete configuration when you're
157
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
// resetting the interval or the other values will be overwritten with default
// or set to null
var basedir = new
DriftDefinition.BaseDirectory(DriftConfigurationDefinition.BaseDirValu
eContext.valueOf(' $BASEDIRTYPE '),' $BASEDIR ')
definition.basedir = basedir
definition.name = '
$NAME '
// there can be multiple exclude statements made, as desired
var f = new Filter("
$EXCLUDE ", " $PATTERN ") // location, pattern
definition.addExclude(f)
DriftManager.updateDriftDefinition(EntityContext.forResource(res.id),d efinition)
// pin to the initial snapshot, which is version 0
// this gets the most recent snapshot if that is the better version to use
// snap =
DriftManager.getSnapshot(DriftSnapshotRequest(definition.id))
DriftManager.pinSnapshot(definition.id,0)
_EOF_
}
The last part of the script actually runs all of the defined JBoss ON CLI scripts and sets up both the drift definition and the bundle definition (as a backup in case any drift is detected).
There are two system commands sandwiched between the JBoss ON CLI scripts. The first is the zi p commands to create the bundle archive. The second is a sl eep command which pauses the script to give the JBoss ON agent time to collect the initial snapshot for drift before attempting to pin the snapshot.
# create the drift definition
driftDef
> $SCRIPTS /driftDef.js
$CLI $OPTS -f $SCRIPTS /driftDef.js
# create the recipe file and then zip up the
# drift base directory to make the bundle archive
deploy
> /deploy.xml
zip $ARCHIVE $BASEDIR zip $BUNDLE $ARCHIVE /deploy.xml
# create the bundle from the recipe and archive
# and then create the bundle definition
createBundle
> $SCRIPTS /createBundle.js
$CLI $OPTS -f $SCRIPTS /createBundle.js
# sleep to allow the server to get the first snapshot
# this only sleeps for a minute, but it really depends on your environment
# whether that is long enough
158
Chapt er 2 1 . Example: Remedying Resource Drift
sleep 1m
# this pins the new snapshot to the new drift definition
# and then changes the drift interval to the longer, variablespecified
# value
snapshot
> $SCRIPTS /snapshot.js
$CLI $OPTS -f $SCRIPTS /snapshot.js
There is no error handling in this shell script. If any step fails, like the initial snapshot taking longer than the sleep period, there is no indication of what went wrong aside from malformed drift or bundle configuration.
Report a bug
3. Remedying Drift
To remediate drift, define an alert in the UI and upload a CLI script which can be run, automatically, whenever drift is detected. All the script has to do is deploy the backup bundle to the resource, and there are several different ways to do that.
This example goes through all the basic steps: it pulls the resource information from the alert, searches for the bundle version, and then deploys it to the resource. One nifty thing about this script is that it writes a log file, capturing the alert information that triggered the remediation.
This script can be uploaded directly when the alert definition is created. Before uploading the script, be sure to set the variables to the bundle destination and bundle version that you created when the drift definition was set up.
// - The 'alert' variable is seeded by the alert sender
// SET THESE VARIABLES
var
bundleDestinationName = 'drift destination'
var
bundleVersion = 1.0
var
logFile = '/tmp/alert-cli-demo/logs/alert-' + alert .id + '.log'
// Log what we're doing to a file tied to the fired alert id
//
var
e = exporter e .setTarget( 'raw' , logFile )
// Dump the alert
// e .
write ( alert )
// get a proxy for the alerted-on Resource
//
var
alertResource =
ProxyFactory.getResource( alert .alertDefinition.resource.id)
159
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
// Dump the resource
// e .
write ( " " ) e .
write ( alertResource )
// Remediate file
// Find the Bundle Destination
//
var
destCrit =
new
BundleDestinationCriteria()
destCrit.addFilterName( bundleDestinationName )
var
result = BundleManager.findBundleDestinationsByCriteria( destCrit
)
var
dest = result.get( 0 )
// Find the Bundle Version
//
var
versionCrit =
new
BundleVersionCriteria()
versionCrit.addFilterVersion( bundleVersion )
result = BundleManager.findBundleVersionsByCriteria( versionCrit )
var
ver = result.get( 0 )
// Create a new Deployment for the bundle version and the destination
//
var
deployment = BundleManager.createBundleDeployment(ver.getId(), dest.getId(), 'remediate drift' ,
new
Configuration())
// Schedule a clean deploy of the deployment. This will wipe out the edited file and lay down a clean copy
//
BundleManager.scheduleBundleDeployment(deployment.getId(), true) e .
write ( " " ) e .
write ( "REMEDIATION COMPLETE!" )
Report a bug
160
Chapt er 2 1 . Example: Remedying Resource Drift
Chapter 22. Example: Managing JBoss ON Server
Configuration
Even in different environments, JBoss ON servers can share a lot of the same configuration.
For example, different JBoss ON servers may manage a development environment, staging environment, and production environment, yet on all three, the servers use similar metric templates and configuration settings. To simplify managing separate but similar environments, JBoss ON can export the configuration for a server and then import that configuration into another server.
Any user with permissions to manage settings can export the server configuration.
Report a bug
1. Simple Export /Import Synchronizat ion
At its simplest, synchronizing server settings exports all metrics and server configuration settings and then imports that information directly, without any adjustment to the data before its imported or filters on what data to import.
T IP
This can be automated by including login information for both JBoss ON servers, and then running the script. This could also be broken into two scripts, one run against server1 and the other against server2. Using a wrapper script would allow you to run the export script, then to run a utility like SCP to copy over the archive, and then to run the import script.
The first part of the script exports the data from server1 and creates a zipped XML archive.
//log into the first server
rhq.login(
'rhqadmin' , 'rhqadmin' , 'server1.example.com' , '7080' );
//export the settings
var
ex = SynchronizationManager.exportAllSubsystems();
rhqadmin@ localhost:
7080 $ saveBytesToFile(ex.exportFile,
'export.xml.gz' );
// log out of the first server
rhq.logout()
The archive file then needs to be copied over to server2 in some way.
When the archive is copied over, it can then be imported into server2. The null parameter means that the import process uses the default settings in the XML file or, if the defaults are missing from the XML, that it uses the settings defined on the target server.
// log into the second server
161
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
rhq.login(
'rhqadmin' , 'rhqadmin' , 'server2.example.com' , '7080' );
// import the settings
var
data = getFileBytes( 'export.xml.gz' );
SynchronizationManager.importAllSubsystems( data , null);
// log out of the second server
rhq.logout()
Report a bug
2. Changing Server Configurat ion Before Import ing
Metrics schedules and server configuration are applied through synchronizers.
Synchronizers control what elements are imported into the JBoss ON server and how to apply them to the server. The synchronizer has a default template which applies configuration changes to every import operation.
The synchronizer configuration can be changed to change what settings are imported into the target server.
The first part of the script would export the XML archive, as before.
//log into the first server
rhq.login(
'rhqadmin' , 'rhqadmin' , 'server1.example.com' , '7080' );
//export the settings
var
ex = SynchronizationManager.exportAllSubsystems();
rhqadmin@ localhost:
7080 $ saveBytesToFile(ex.exportFile,
'export.xml.gz' );
// log out of the first server
rhq.logout()
162
The XML file contains the full configuration information, so just checking that file can give you an idea of what settings to change.
O n t h e seco n d server, change the synchronizer settings.
1. Get the default definition.
rhqadmin@ localhost:
7080 $ var systemSettingsImportConfigurationDefinition =
SynchronizationManager.
getImportConfigurationDefinition ('org.
rhq
.
enterprise .
server .
sync .
SystemSettingsSynchronizer ')
2. Create a new configuration instance.
rhqadmin@ localhost:
7080 $ var configurationObject =
Chapt er 2 2 . Example: Managing JBoss O N Server Configurat ion
systemSettingsImportConfigurationDefinition.
configurationDefinit ion .
defaultTemplate .
createConfiguration ()
rhqadmin@ localhost:
7080 $ var systemSettingsImportConfiguration =
new
ImportConfiguration (systemSettingsImportConfigurationDefinition.
synchronizerClassName , configurationObject)
3. Change the settings.
For example, this edits the server synchronizer so that it imports only the database settings for storing monitoring data.
rhqadmin@ localhost:
7080 $ configurationObject.
getSimple ('propertiesToImport').
setValue ('CA
M_DATA_PURGE_1H, CAM_DATA_PURGE_6H, CAM_DATA_PURGE_1D,
CAM_DATA_MAINTENANCE')
For the metrics template synchronizer, define which metric schedules to import per resource type, based on a properties list or a properties map. For example:
rhqadmin@ localhost:
7080 $ configurationObject.
getSimple ('updateAllSchedules').
setBooleanVa lue (
true
)
rhqadmin@ localhost:
7080 $ var updateList =
new
PropertyList ('metricUpdateOverrides')
rhqadmin@ localhost:
7080 $ var update =
new
PropertyMap ('metricUpdateOverride')
rhqadmin@ localhost:
7080 $ update.
put (
new
PropertySimple ('metricName', 'MCBean|ServerInfo|*|freeMemory'))
rhqadmin@ localhost:
7080 $ update.
put (
new
PropertySimple ('resourceTypeName', 'JBossAS Server'))
rhqadmin@ localhost:
7080 $ update.
put (
new
PropertySimple ('resourceTypePlugin', 'JBossAS5'))
rhqadmin@ localhost:
7080 $ update.
put (
new
PropertySimple ('updateSchedules', 'true'))
rhqadmin@ localhost:
7080 $ updateList.
add (update)
rhqadmin@ localhost:
7080 $ configurationObject.
put (updateList)
After changing the synchronizer settings, then import the configuration.
rhqadmin@ localhost:
7080 $
var
configsToImport =
new
java.util.ArrayList()
rhqadmin@ localhost:
7080 $ configsToImport.add(systemSettingsImportConfiguration);
rhqadmin@ localhost:
7080 $ configsToImport.add(metricTemplatesImportConfiguration);
rhqadmin@ localhost:
7080 $
SynchronizationManager.importAllSubsystems(ex, configToImport);
163
Red Hat JBoss O perat ions Net work 3.3 Writ ing JBoss O N Command- Line Script s
Report a bug
164
Chapt er 2 2 . Example: Managing JBoss O N Server Configurat ion
Appendix A. Document History
Revisio n 3.3.2- 1 T u e 28 Ap r 2015
Prepared for JBoss ON 3.3.2 Release
Revisio n 3.3.1- 1 Wed Feb 18 2015
Prepared for JBoss ON 3.3.2.GA Release
Revisio n 3.3- 20 Mo n No v 17 2014
See 3.3 Release Notes for Customer Reported Defects
Jared Mo rg an
Jared Mo rg an
Jared Mo rg an
165
Изтегляне
Реклама