operations bridge omi 10 - groovy scripts best practices…•groovy console can also be used for...
TRANSCRIPT
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Operations Bridge
OMi 10 - Groovy scriptsBest practicesVersion 2.2
10. October 2015
Last Edited: Harald Pfaender
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Agenda: Groovy Scripting
Types
Available APIs
Debugging
Memory & Performance
Examples
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Groovy Script Types in OMi
• Event Processing Customization (EPI)
• Custom Action
• Topology Synchronization
• Certificates
• Event Forwarding
• Service Health
• External Instruction rules
• Connected Servers
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Groovy scripts in general
• Groovy scripts have a template
• In most cases groovy scripts have:
– init() Method
– process(events) Method
– destroy() Method
• Use the init() Method for initialization or caching
• The process() Method is called for each update (e.g. event update or certificate request update)
• Use the destroy() Method for cleanup tasks
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Groovy Script Types in OMiEvent Processing Interface (EPI)
• Stored in the DB
– OMi LocationAdministration > Event Processing > Event Processing Customizations
– Script samples:<OMiInstallDir>/opr/examples/epi-scripts
• Called by Event Pipeline on DPS
– Executed in process hpbsm_opr-scripting-host
– Logs from opr-scripting hostlog/opr-scripting-host/opr-scripting-host.log
– Logs caused by script exceptionslog/opr-scripting-host/scripts.log
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Groovy Script Types in OMiCustom Actions
• Custom actions for automation of event browser functions
• Stored in DB– OMi location:Administration > Operations Console > Custom Actions
– Script samples<OMiInstallDir>/opr/examples/ca-scripts
• Launched by Operator– Executed in process on GW Serverhpbsm_opr-scripting-host
– Logs from opr-scripting hostlog/opr-scripting-host/opr-scripting-host.log
– Logs caused by script exceptionslog/opr-scripting-host/scripts.log
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Groovy Script Types in OMi Topology Synchronization 1/2
• Custom business logic during topology synchronization execution
• Stored in DB
– Uploaded from file system<OMiInstallDir>/conf/opr/topology-sync/sync-packages
– Tool for upload:bin/opr-sdtool.[bat|sh]
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Groovy Script Types in OMiTopology Synchronization 2/2
• Called by Topology Synchronization on GW Server– Basic topology executed by command line tool: opr-startTopologySync.[bat|sh]
– Logfile:log/opr-topologysync/opr-topologysync.log
– Dynamic Toplogy Synchronization: Executed in process hpbsm_wdelog/wde/opr-svcdiscserver.log
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Groovy Script Types in OMiCertificates
• Automates certificate handling
• Stored in the DB – Administration > Setup and Maintenance > Certificate Requests
– OOTB Template for Auto Grant script
• Called by Certificate handling on GW Server– Executed in process opr-scripting-host
– Logfile:log/opr-scripting-host
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Groovy Script Types in OMiEvent Forwarding
• For building adapters to 3rd party applications
• Stored in the DB – Administration > Setup and Maintenance > Connected Servers
– OOTB scripts:
• LogfileAdapter
• ServiceManagerAdapter
• Called by Event Forwarding on GW Server– Executed in process hpbsm_opr-scripting-host
– Logfile:log/opr-scripting-host/opr-event-sync-adapter.log
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Groovy Script Types in OMiService Health Rules
• Custom business logic to affect KPI status calculation
• Executed in Marble process
• API Group and Sibling Rule can be created
– Using KPI Text file
• Template: <Data Processing server root directory>\BLE\rules\groovy\templates
• File Location: <Data Processing server root directory>\BLE\rules\groovy\rules
– Directly via KPI and Health Indicator Customization
• For more Details see:OMi Extensibility Guide -> Service Health Rules API
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Available APIs
• OMi API documentation– <install>/opr/api/doc
• RTSM/UCMDB API– <install>/AppServer/webapps/site.war/amdocs/eng/API_docs/UCMDB_JavaAPI
• Service Health API– <install>/AppServer/webapps/site.war/amdocs/eng/API_docs/Rules_API
• RTSM WebService API– <install>/AppServer/webapps/site.war/amdocs/eng/API_docs/WebServiceAPI_Samples
• Downtime API– <install>/AppServer/webapps/site.war/amdocs/eng/API_docs/DowntimeREST_JavaAPI
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Available External APIs
• Java JDK
– Generic JDBC Access http://docs.oracle.com/javase/tutorial/jdbc/basics/index.html
– General Web Service access URL.openConnection()
– Command Line Tools
• Apache
– Log4j logging http://logging.apache.org/log4j/1.2/
– Wink JSR 311, REST Web Service APIs http://incubator.apache.org/wink/
– Commons HTTP Client http://hc.apache.org/httpclient-3.x/
– Axis2 - SOAP http://axis.apache.org/axis2/java/core/
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Available Documentation
•OMi Extensibility Guide– Event Processing Customization
– REST Event Webservice
– Service Health
– Groovy Scripts/Best Practices
– Downtime API
• RTSM Developer Reference Guide
– How to develop content and discovery adapters
– How to use APIs
– How to use Webservice API
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
OMi Script Dev KitOverview
• Framework for Eclipse available
• Useful for
– Formatting of scripts
– Auto-completion
– Testing
– Debugging
• Plugin can be found here:<install>/opt/support/script-devkit
• An Overview/Install description is also available:HP_Operations_Manager_i_Script_Development_Kit.rtf
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
OMi Script Dev KitIDE Integration
• More details in Blog: http://h30499.www3.hp.com/t5/Business-Service-Management-BAC/A-closer-look-at-the-functionality-of-OMi-Script-Dev-Kit/ba-p/6743615
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
DOs
• Use appropriate caching techniques wherever possible (CIs)
• Editing of scripts:
– Use OMi Script Development Kit for editing, validation & testing (EPI/Custom Actions)
– Other scripts: Groovy Console, Notepad++, VI, Emacs, etc.
• Use logging for debugging
• Use filtering mechanism (event filter to reduce event volume)
• Use comments & verbose variable names
• Mark EPI scripts read only that don‘t need to update events
• Make it robust
– Use try/catch
– Recover from DB disconnects by reconnecting
• Use ‚?‘ when dereferencing properties of a structure, e.g.if (event.assignedUser?.userName)
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
DONTs
• Do not use slow APIs in performance critical code (event pipeline)
• Avoid use of def keyword
• Do not use excessive logging in production
• Do not connect to DB on each call
• Copy/paste from emails/pdfs/word documents may result in invalid code
• Do not call System.exit(), etc...
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
EPI Specific TipsDOs
• Release resources and watch for memory leaks
• Use background threads and cache information where possible
• Open close files and DB in init()
• Mark Read Only when possible
• Watch for script timeouts
• Use reasonable values
• Read only versus Read/Write Scripts
• Logging
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
EPI Specific TipsDONTs
• Too many scripts
• Too much memory (see JVM statistics later)
• Calling slow APIs (avoid web services, CLIs)
• Script filter can slow execution
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Debugging
• Best practice is to use the OMi Script DevKit (for EPI)
• Generally use an Editor which can format code E.g.:
– Groovy Console
– Notepad++
– vi
– Emacs
• Groovy Console can also be used for debugging, testing and validation
• Don’t forget to “import” the classes you use
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Logging
• Logging can be used inside scripts:import org.apache.log4j.Logger;
...
class myClass
{
static Logger s_log = Logger.getLogger ("my.logger");
...
s_log.fatal("Fatal logging")
}
• Logging per default only logs level error and fatal
• Logging level for info and warning needs to be configured<install>/conf/core/Tools/log4j/opr-scripting-host/opr-scripting-
host.properties
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Memory Usage & Performance
• JVM Statistics
– OOTB for opr-backendlog/opr-backend/jvm_statistics.log
2012-09-21 16:13:26,159 INFO - JVMStatistics is started !!!
2012-09-21 16:13:26,173 INFO - Starting JVMStatistics task with interval
60000 ms.
2012-09-21 16:13:26,186 INFO - HEAP - [USAGE: 57.1, FREE: 188.2, TOTAL:
245.3, MAX: 245.3]; PERM - [USAGE: 23.9, FREE: 72.0, MAX: 96.0]; CLASSES -
[Loaded: 3921, Unloaded: 0, Left: 3921]; THREADS - [Count: 26]
– Can be added for other processes, e.g. opr-scripting-host
• Check log files for execution times
• Add logging at the beginning of a method and at the end (entry and exit) to see what happens
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Code SnippetsCommand Line Tool Execution
• Code Snippet: How to Call a Command Line Tool
• http://groovy.codehaus.org/Executing+External+Processes+From+Groovy
/* execute an external command and get the output in a string */
String cmd = "ipconfig"
String cmdArgs = "/all"
String externalCmd = "cmd /c ${cmd} ${cmdArgs}"
StringBuffer inputBuffer = new StringBuffer()
StringBuffer errorBuffer = new StringBuffer()
Process runcmd = "${externalCmd}".execute()
runcmd.consumeProcessOutput(inputBuffer, errorBuffer)
runcmd.waitFor()
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Code SnippetsEvent WS Call
• Code Snippet: How to Call the Event WS -> See also OMi Extensibility Guide
private final RestClient m_client = new RestClient()
private final PasswordAuthentication m_credentials =
new PasswordAuthentication(USERNAME, PASSWORD.toCharArray())
...
String address = "http://my_omi_gw.ex.com/opr-web/9.10/event_list"
Resource resource = m_client.resource(address)
resource.accept(MediaType.APPLICATION_ATOM_XML).contentType(MediaType.APPLICATION_ATOM_XML)
// Set the username and password in the request.
String encodedUserPassword = Base64.encodeBytes((m_credentials.userName
+ ":" + new String(m_credentials.password)).bytes)
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Code SnippetsEvent WS Call (cont.)
try {
final ClientResponse response = resource.post(oprEvent)
if (response.statusCode > 299) {
// just log an error
final String message = response.getEntity(String.class)
if (message)
s_log.error("HTTP error response on event creation - ${response.message}
(${response.statusCode}): ${message}")
else
s_log.error("HTTP error response on event creation - ${response.message}
(${response.statusCode})")
}
}
catch (ClientWebException e) {
// clear the cookies. they may be stale
clearCookies()
}
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Event WS Update Call
• Just add the ID to the end of the URL and PUT an event
• You can also „bulk“ update several events at once
– Instead of specifying event ID add a query string, e.g.
– http://host/opr-web/rest/9.10/event_list?query=title like “%node down%“
– Event with change will be applied to all events that match the query
– Careful! No query string will update ALL events.
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Code SnippetsGet the OMi version running
import com.hp.opr.api.Version
...
Integer omiVersion = 0
// determine the OPR version that is running
String versionString = Version.getProperty(Version.COMPONENT_VERSION)
String[] version = versionString.split('\\.')
omiVersion = (Integer.valueOf(version[0]) * 100) + Integer.valueOf(version[1])
// result = 1000, 1001, ...
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Code SnippetsAccess RTSM
• Query the RTSM for Cis
// Get UCMDB service provider injected
public UcmdbServiceProvider ucmdbServiceProvider
void init() {
if (ucmdbServiceProvider) {
println("Init UCMDB query service")
topologyQueryService = ucmdbServiceProvider.cmdbService.topologyQueryService
topologyQueryFactory = ucmdbServiceProvider.cmdbService.topologyQueryService.factory
} else {
println("UcmdbServiceProvider is null! Cannot initialize UCMDB query services")
}}
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Code SnippetsSOAP WS Call
• Apache Axis2
– http://axis.apache.org/axis2/java/core/
– Use wsdl2java to generate client stub code
– Best to download the WSDL to local directory
• wsdl2java.sh -s -o /tmp/om_axis -uri ./wsdls/IncidentService.wsdl
• Add Axis2 libraries to the DPS/Gateway server as appropriate rather than loading into DB
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Sample ScriptsEPI
• Additionally sample scripts can be found in the install directory under:
– <install>/opr/examples/ca-scripts
– <install>/opr/examples/epi-scripts
• Forwarding
– LogfileAdpater
• KPIProvided in the OMi OOTB Content Pack
• TopologyProvided in OOTB Topology Synchronization packages
– HPOprMss
– HPOprOra
– HPOprVir
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Questions/Feedback
• For questions or feedback please use the HP Live Network -> Forum
https://hpln.hp.com/group/operations-manager-i
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Thanks!