All dbWatch extensions are written in the Java programming language, so creating a custom extension requires some basic knowledge of Java.
Dependencies
You will need the following jar files located in the [dbWatch Server]/server/lib/ catalog.
- domain.jar
- utils.jar
An extension skeleton
Let’s take a look at a minimum extension. We create a file called MyTestExtension.java and paste in the following code:
import no.onedge.dbwatch.domain.utils.DefaultExtensionImplementation;
public class MyTestExtension extends DefaultExtensionImplementation {
public void catchSignal(DBWatchSignal signal) {
}
public String getName() {
return "My Test Extension";
}
public String getDescription() {
return "My Test Extension Description";
}
public String getVersion() {
return "1.0";
}
}
All extensions inherit from the DefaultExtensionImplementation class located in the domain.jar file and must implement (at least) the 4 methods getName(), getDescription(), getVersion() and catchSignal(DBWatchSignal).
The first 3 (the get methods) are used by dbWatch to identify this extension, while catchSignal will be invoked with a DBWatchSignal argument each time an “event” occurs in dbWatch. Theese signals can be a lot of things but the most interresting are TaskSignal and EngineSignal. (For a complete list see the classes defined in the no.onedge.dbwatch.signals package in domain.jar.)
So in the catchSignal method you typically want to identify if the signal argument is one of these types, and handle them in some way.
public void catchSignal(DBWatchSignal signal) {
if (s instanceof TaskSignal) {
handleTaskSignal((TaskSignal) s);
} else if(s instanceof EngineSignal) {
handleEngineSignal((EngineSignal) s);
}
}
EngineSignal
An EngineSignal tells you that some event has happened relating to a particular database instance.
The event can be one of three types, that an instance has been added, an instance has been removed or that the state of an instance has changed.
To figure out what type of event this signal is about use the method getType() and compare the return value to EngineSignal.STATE_CHANGED, EngineSignal.ENGINE_ADDED and EngineSignal.ENGINE_REMOVED.
If the signal is a STATE_CHANGED signal, the methods getOldStatus() and getNewStatus() will tell you what the instance status has changed from and to.
The status will be one of the statuses defined in the StatusConverter class (STATUS_NOT_CONNECTED, STATUS_SCHEDULED_POLLING_PAUSE, STATUS_CONNECTING, STATUS_CONNECTED, STATUS_OK, STATUS_WARNING, STATUS_ALARM or STATUS_LOST_CONNECTION).
In the handleEngineSignal method you typically want to inform some 3-party system about the events. The following example just writes to standard output.
void handleEngineSignal(EngineSignal signal) {
if (signal.getType() == EngineSignal.STATE_CHANGED) {
System.out.println("Instance " + signal.getName() + " changed status from "
+ StatusConverter.convertToString(signal.getOldStatus() + " to "
+ StatusConverter.convertToString(signal.getNewStatus());
} else if(signal.getType() == EngineSignal.ENGINE_REMOVED) {
System.out.println("Instance " + signal.getName() + " removed");
} else if(signal.getType() == EngineSignal.ENGINE_ADDED) {
System.out.println("Instance " + signal.getName() + " added");
}
}
TaskSignal
A TaskSignal tells you that an event has occured regarding a Task or Alert (Check).
The event can be one of five types, that an task/alert has been queued for execution, it is executing, the execution has succeded, the execution has failed, or the task/alert has been deleted.
To figure out what type of event this signal is about use the method getType() and compare the return value to TaskSignal.EXECUTING, TaskSignal.EXECUTION_SUCCEEDED, TaskSignal.EXECUTION_FAILED, TaskSignal.QUEUED, TaskSignal.DELETED.
To see what instance the Task/Alert belongs to use signal.getEngineGivenName(), to see what Task/Alert the signal concerns use signal.getTask().getName().
In the handleTaskSignal method you, again, typically want to inform some 3-party system about the events. The following example just writes to standard output.
void handleTaskSignal(TaskSignal signal) {
if (signal.getType() == TaskSignal.EXECUTION_SUCCEEDED) {
if(signal.getTask() instanceof EngineCheck) {
EngineCheck alert = (EngineCheck) signal.getTask();
System.out.println("Alert " + alert.getName() + " on instance " + signal.getEngineGivenName()
+ " executed successfully - status now: " + StatusConverter.convertToString(alert.getStatus()));
} else {
EngineTask task = (EngineTask) signal.getTask();
System.out.println("Task" + task.getName() + " on instance " + signal.getEngineGivenName()
+ " executed successfully");
}
} else if(signal.getType() == TaskSignal.EXECUTION_FAILED) {
if(signal.getTask() instanceof EngineCheck) {
EngineCheck alert = (EngineCheck) signal.getTask();
System.out.println("Execution of alert " + alert.getName() + " on instance " + signal.getEngineGivenName()
+ " failed - status now: " + StatusConverter.convertToString(alert.getStatus()));
} else {
EngineTask task = (EngineTask) signal.getTask();
System.out.println("Execution of Task" + task.getName() + " on instance " + signal.getEngineGivenName()
+ " failed");
}
}
}
Deployment
Simply create a jar file with your code, and dump it in the [dbWatch Server]/server/extensions/catalog.
You should them find it in the Extension View in the dbWatch Monitor.
Post your comment on this topic.