Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
236 changes: 138 additions & 98 deletions docs/modules/operation/pages/deep-dive/events/scriptd.adoc

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ The only requirement is that the script returns the string `OK` if it passes.

The BSFDetector passes the `map`, `ip_addr`, `svc_name`, and `results` variables as beans to the script.

NOTE: Groovy is the scripting language that ships with {page-component-title}.
BeanShell is no longer supported; rewrite any existing `.bsh` scripts in Groovy and update the `bsfEngine` and `langClass` parameters of the affected detectors.

== Detector facts

[options="autowidth"]
Expand All @@ -32,16 +35,14 @@ The BSFDetector passes the `map`, `ip_addr`, `svc_name`, and `results` variables
| Short name of the language the script is written in.
Choices:

* jython
* beanshell
* groovy
* jython
| none

| bsfEngine
| BSF engine to use when running the script.
Choices:

* `bsh.util.BeanShellBSFEngine`
* `org.codehaus.groovy.bsf.GroovyEngine`
* `org.apache.bsf.engines.jython.JythonEngine`
| none
Expand Down Expand Up @@ -91,54 +92,24 @@ The following table provides the required setup for commonly used languages.
| bsfEngine parameter
| Required library

| http://www.beanshell.org[BeanShell]
| beanshell
| bsh.util.BeanShellBSFEngine
| supported by default

| https://groovy-lang.org/[Groovy]
| groovy
| org.codehaus.groovy.bsf.GroovyEngine
| groovy-all-[version].jar
| ships with {page-component-title}

| http://www.jython.org[Jython]
| jython
| org.apache.bsf.engines.jython.JythonEngine
| jython-[version].jar
|===

IMPORTANT: If you are using a Groovy or Jython script, you need to place the required library file in the `$\{OPENNMS_HOME}/lib` directory and restart the {page-component-title} service.
IMPORTANT: If you are using a Jython script, you need to place the required library file in the `$\{OPENNMS_HOME}/lib` directory and restart the {page-component-title} service.

== Example configurations

=== BeanShell example

.BeanShell example
[source, xml]
----
<detector name="MinimalBeanShell" class="org.opennms.netmgt.provision.detector.bsf.BSFDetector">
<parameter key="bsfEngine" value="bsh.util.BeanShellBSFEngine"/>
<parameter key="langClass" value="beanshell"/>
<parameter key="fileName" value="/opt/opennms/etc/scripts/MinimalBeanShell.bsh"/>
<parameter key="runType" value="eval"/>
</detector>
----

.BeanShell example `MinimalBeanShell.bsh` script file
[source, java]
----
File testFile = new File("/tmp/TestFile");
if (testFile.exists()) {
return "OK";
} else {
results.put("reason", "file does not exist");
return "NOK";
}
----

=== Groovy example

The Groovy language requires the installation of an additional library.
Groovy ships with {page-component-title}, so no additional library is required.

.Groovy example for `run-type` of `eval`
[source, xml]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Users can write scripts to perform highly customized service checks.
IMPORTANT: This monitor is not optimized for scale.
It is intended for a small number of custom checks or prototyping of monitors.

NOTE: Groovy is the scripting language that ships with {page-component-title}.
BeanShell is no longer supported; rewrite any existing `.bsh` scripts in Groovy and update the `bsf-engine` and `lang-class` parameters of the affected services.

== BSFMonitor versus SystemExecuteMonitor

The BSFMonitor avoids the overhead of fork(2) that the SystemExecuteMonitor uses.
Expand Down Expand Up @@ -40,7 +43,6 @@ The BSFMonitor also grants access to a selection of {page-component-title} inter
| bsf-engine
| The BSF Engine to run the script in different languages:

* bsh.util.BeanShellBSFEngine
* org.codehaus.groovy.bsf.GroovyEngine
* org.apache.bsf.engines.jython.JythonEngine
| n/a
Expand All @@ -52,7 +54,7 @@ The BSFMonitor also grants access to a selection of {page-component-title} inter
| eval

| lang-class
| The BSF language class, like `groovy` or `beanshell`
| The BSF language class, like `groovy`
| filename extension is interpreted by default

| file-extensions
Expand Down Expand Up @@ -155,7 +157,7 @@ If the response time should be persisted, add the following parameters:
<parameter key="rrd-repository" value="/opt/opennms/share/rrd/response" />

<!-- name of the rrd file -->
<parameter key="rrd-base-name" value="minimalbshbase" />
<parameter key="rrd-base-name" value="myscriptbase" />

<!-- name of the data source in the rrd file -->
<!-- by default "response-time" is used as ds-name -->
Expand Down Expand Up @@ -196,54 +198,20 @@ The following table provides the required setup for commonly used languages.
| bsf-engine
| required library

| http://www.beanshell.org[BeanShell]
| beanshell
| `bsh.util.BeanShellBSFEngine`
| supported by default

| https://groovy-lang.org/[Groovy]
| groovy
| `org.codehaus.groovy.bsf.GroovyEngine`
| `groovy-all-[version].jar`
| ships with {page-component-title}

| http://www.jython.org[Jython]
| jython
| `org.apache.bsf.engines.jython.JythonEngine`
| `jython-[version].jar`
|===

== BeanShell example

Note that you must include the `monitor` section for each service in your definition.

.BeanShell example `poller-configuration.xml`
[source, xml]
----
<service name="MinimalBeanShell" interval="300000" user-defined="true" status="on">
<parameter key="file-name" value="/tmp/MinimalBeanShell.bsh"/>
<parameter key="bsf-engine" value="bsh.util.BeanShellBSFEngine"/>
</service>

<monitor service="MinimalBeanShell" class-name="org.opennms.netmgt.poller.monitors.BSFMonitor" />
----

.BeanShell example `MinimalBeanShell.bsh` script file
[source, java]
----
bsf_monitor.log("ERROR", "Starting MinimalBeanShell.bsf", null);
File testFile = new File("/tmp/TestFile");
if (testFile.exists()) {
return "OK";
} else {
results.put("reason", "file does not exist");
return "NOK";
}
----

== Groovy example

The use of the Groovy language requires an additional library.
Copy a compatible `groovy-all.jar` into the `$\{OPENNMS_HOME}/lib` folder and restart {page-component-title} to make Groovy available for the BSFMonitor.
Groovy ships with {page-component-title}, so no additional library is required.

Note that you must include the `monitor` section for each service in your definition.

Expand All @@ -259,7 +227,7 @@ Note that you must include the `monitor` section for each service in your defini
----

.Groovy example `MinimalGroovy.groovy` script file for `run-type` set to `eval`
[source, java]
[source, groovy]
----
bsf_monitor.log("ERROR", "Starting MinimalGroovy.groovy", null);
File testFile = new File("/tmp/TestFile");
Expand All @@ -284,7 +252,7 @@ if (testFile.exists()) {
----

.Groovy example `MinimalGroovy.groovy` script file for `run-type` set to `exec`
[source, java]
[source, groovy]
----
bsf_monitor.log("ERROR", "Starting MinimalGroovy", null);
def testFile = new File("/tmp/TestFile");
Expand Down Expand Up @@ -362,7 +330,7 @@ For Debian/Ubuntu, use `/var/lib/opennms/rrd/response`.
----

.Groovy example Bean referencing script file
[source, java]
[source, groovy]
----
bsf_monitor.log("ERROR", "Starting MinimalGroovy", null);

Expand Down
7 changes: 5 additions & 2 deletions opennms-alarms/bsf-northbounder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@
<groupId>bsf</groupId>
<artifactId>bsf</artifactId>
</dependency>
<!-- provides the Groovy BSF engine, the default for this northbounder -->
<dependency>
<groupId>org.apache-extras.beanshell</groupId>
<artifactId>bsh</artifactId>
<groupId>org.opennms.dependencies</groupId>
<artifactId>groovy-dependencies</artifactId>
<type>pom</type>
<scope>runtime</scope>
</dependency>
<!-- test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ public class BSFEngineHandler implements Destination {
private String m_filter;

/** The engine language. */
@XmlElement(name = "language", required = false, defaultValue = "beanshell")
@XmlElement(name = "language", required = false, defaultValue = "groovy")
private String m_language;

/** The engine class name. */
@XmlElement(name = "className", required = false, defaultValue = "bsh.util.BeanShellBSFEngine")
@XmlElement(name = "className", required = false, defaultValue = "org.codehaus.groovy.bsf.GroovyEngine")
private String m_className;

/** The engine file extensions. */
@XmlElement(name = "extensions", required = false, defaultValue = "bsh")
@XmlElement(name = "extensions", required = false, defaultValue = "groovy,gy")
private String m_extensions;

/** The on-start content. */
Expand Down Expand Up @@ -136,7 +136,7 @@ public void setFilter(String filter) {
* @return the language
*/
public String getLanguage() {
return m_language == null ? "beanshell" : m_language;
return m_language == null ? "groovy" : m_language;
}

/**
Expand All @@ -154,7 +154,7 @@ public void setLanguage(String language) {
* @return the class name
*/
public String getClassName() {
return m_className == null ? "bsh.util.BeanShellBSFEngine" : m_className;
return m_className == null ? "org.codehaus.groovy.bsf.GroovyEngine" : m_className;
}

/**
Expand All @@ -172,7 +172,7 @@ public void setClassName(String className) {
* @return the extensions
*/
public String getExtensions() {
return m_extensions == null ? "bsh" : m_extensions;
return m_extensions == null ? "groovy,gy" : m_extensions;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
<xs:sequence>
<xs:element name="name" type="xs:string" minOccurs="1"/>
<xs:element name="filter" type="xs:string" minOccurs="0"/>
<xs:element name="language" type="xs:string" default="beanshell" minOccurs="0"/>
<xs:element name="className" type="xs:string" default="bsh.util.BeanShellBSFEngine" minOccurs="0"/>
<xs:element name="extensions" type="xs:string" default="bsh" minOccurs="0"/>
<xs:element name="language" type="xs:string" default="groovy" minOccurs="0"/>
<xs:element name="className" type="xs:string" default="org.codehaus.groovy.bsf.GroovyEngine" minOccurs="0"/>
<xs:element name="extensions" type="xs:string" default="groovy,gy" minOccurs="0"/>
<xs:element name="onStart" type="xs:string" minOccurs="0"/>
<xs:element name="onStop" type="xs:string" minOccurs="0"/>
<xs:element name="onAlarm" type="xs:string" minOccurs="1"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@
<engine>
<name>Example</name>
<filter>ipAddr != '0.0.0.0'</filter>
<language>beanshell</language>
<className>bsh.util.BeanShellBSFEngine</className>
<extensions>bsh</extensions>
<language>groovy</language>
<className>org.codehaus.groovy.bsf.GroovyEngine</className>
<extensions>groovy,gy</extensions>
<onStart>
log = bsf.lookupBean("log");
log.info("Starting...");
// assign without 'def' so the binding is shared with onStop and onAlarm
log = bsf.lookupBean("log")
log.info("Starting...")
</onStart>
<onStop>
log.info("Stopping...");
log.info("Stopping...")
</onStop>
<onAlarm>
a = bsf.lookupBean("alarm");
log.info("processing alarm " + a);
a = bsf.lookupBean("alarm")
log.info("processing alarm " + a)
</onAlarm>
</engine>
-->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,53 @@
<?xml version="1.0"?>
<scriptd-configuration>
<engine language="beanshell" className="bsh.util.BeanShellBSFEngine" extensions="bsh"/>
<start-script language="beanshell">
<engine language="groovy" className="org.codehaus.groovy.bsf.GroovyEngine" extensions="groovy gy"/>
<start-script language="groovy">

import org.opennms.netmgt.scriptd.ins.events.InsServerListener;
import org.opennms.netmgt.config.DataSourceFactory;
import org.opennms.netmgt.scriptd.ins.events.InsServerListener
import org.opennms.netmgt.config.DataSourceFactory

log = bsf.lookupBean("log");
log.debug("Starting Script");
// assign without 'def' or a type so these stay in the binding shared
// with the stop-script and the event-script
log = bsf.lookupBean("log")
log.debug("Starting Script")

log.debug("Start TCP PROXY for INS Event ");
log.debug("Start TCP PROXY for INS Event ")

isl = new InsServerListener()

InsServerListener isl = new InsServerListener();

//optional (if not setted, default port (8154) is used)
//isl.setListeningPort(8152);
//isl.setListeningPort(8152)

//optional (if not setted, no authentication is required)
//isl.setSharedASCIIString("1234567890");
//isl.setSharedASCIIString("1234567890")

//required properties
isl.setCriteriaRestriction("eventuei = 'uei.opennms.org/internal/alarms/AlarmRaised' and EXISTS (select 1 from alarms where alarmtype = 1 and severity > 3 and eventoperinstruct = alarmid and eventtime > lasteventtime)");
isl.start();
isl.setCriteriaRestriction("eventuei = 'uei.opennms.org/internal/alarms/AlarmRaised' and EXISTS (select 1 from alarms where alarmtype = 1 and severity > 3 and eventoperinstruct = alarmid and eventtime > lasteventtime)")
isl.start()

</start-script>

<stop-script language="beanshell">
<stop-script language="groovy">

isl.interrupt();
log.debug("executing a stop script");
isl.interrupt()
log.debug("executing a stop script")

</stop-script>

<event-script language="beanshell">
event = bsf.lookupBean("event");
<event-script language="groovy">
event = bsf.lookupBean("event")

if (
(event.uei.equals("uei.opennms.org/internal/alarms/NotificationAlarm"))
|| (event.uei.equals("uei.opennms.org/internal/alarms/AlarmCleared"))
|| (event.uei.equals("uei.opennms.org/internal/alarms/AlarmRaised"))
)
{
isl.flushEvent(event);

isl.flushEvent(event)
}

</event-script>

</event-script>

</scriptd-configuration>

</scriptd-configuration>
Loading
Loading