Monitor Class
Monitor a remote probe
Monitor objects are the local interface to a remote Probe. The probe may be running in this process or on a remote server.
In a disconnected state, the monitor object contains information about the type, attributes, and location of the probe it will monitor.
In a connected state, the monitor object contains the data attributes of the probe it is monitoring, and emits change events as the probe changes state.
Many monitors may be attached to a single probe. When the probe data model changes, changes are broadcast to the connected monitors.
Probes can be remotely controlled using the control() method. The control() method acts an RPC in that it accepts input arguments and returns results to the monitor initiating the request.
Example:
// Connecting a monitor to a probe
var processMonitor = new Monitor({
probeClass: 'Process'
});
processMonitor.connect();
// Monitoring the probe
processMonitor.on('change', function(){
console.log('Changes:', processMonitor.getChangedAttributes());
});
// Remote control
processMonitor.control('ping', function(error, response) {
console.log('Ping response: ', response);
});
Monitoring a probe on a remote server requires the hostName
parameter
to be set.
// Connecting to a remote monitor
var processMonitor = new Monitor({
probeClass: 'Process',
hostName: 'remote-server1'
});
processMonitor.connect();
Additional parameters can be set to identify a specific server if many
servers are running on the specified hostName
.
Constructor
Monitor
-
model
Parameters:
-
model
Object- Initial data model. Can be a JS object or another Model.
-
[id]
String optionalThe monitor object id. Set externally.
-
probeClass
StringClass name of the probe this is (or will be) monitoring.
-
[initParams]
Object optionalInitialization parameters passed to the probe during instantiation.
-
[hostName]
String optionalHostname the probe is (or will) run on. If not set, the Router will connect with the first host capable of running this probe.
-
[appName]
String optionalApplication name the probe is (or will) run within. If not set, the Router will disregard the appName of the process it is connecting with.
-
[appInstance]
String optionalApplication instance ID the probe is (or will) run within. If not set, the Router will disregard the appInstance of the process it is connecting with. Application instances can (should) set the $NODEAPPINSTANCE environment variable prior to running to uniquely identify their unique instance within a server or network. If this variable is not set prior to running the app, node-monitor will assign a unique ID among other running apps on the host.
-
probeId
StringID of the probe this is monitoring (once connected). READONLY
-
PROBE_PARAMS...
(defined by the probe)... all other
model
parameters are READONLY parameters of the connected probe
Item Index
Methods
Events
Methods
connect
-
callback
Connect the monitor to the remote probe
Upon connection, the monitor data model is a proxy of the current state of the probe.
Parameters:
-
callback
Function(error)Called when the probe is connected (or error)
control
-
name
-
[params]
-
[callback]
Send a control message to the probe.
Monitors can use this method to send a message and receive a response from a connected probe.
The probe must implement the specified control method. All probes are derived from the base Probe class, which offers a ping control.
To send a ping message to a probe and log the results:
var myMonitor.control('ping', console.log);
Parameters:
-
name
StringName of the control message.
-
[params]
Object optionalNamed input parameters specific to the control message.
-
[callback]
Function(error, response) optionalFunction to call upon return.
- error (Any) - An object describing an error (null if no errors)
- response (Any) - Response parameters specific to the control message.
deepCopy
-
value
-
[depth=4]
Produce a depth-limited copy of the specified object
Functions are copied for visual inspection purposes - the fact that they are a function, and any prototype members. This is so a JSON.stringify of the result will show the functions (normally JSON.stringify doesn't output functions).
This method is mostly for debugging - for producing a human-readable stream representation of the object. It is an exact copy, except for elements of type function.
Parameters:
-
value
MixedObject or value to copy
-
[depth=4]
Integer optionalMaximum depth to return. If the depth exceeds this value, the string "[Object]" is returned as the value.
Returns:
disconnect
-
callback
Disconnect from the remote probe
This should be called when the monitor is no longer needed. It releases resources associated with monitoring the probe.
If this was the last object monitoring the probe, the probe will be stopped, releasing resources associated with running the probe.
Parameters:
-
callback
Function(error)Called when disconnected (or error)
generateUniqueId
()
String
protected
static
Generate a unique UUID-v4 style string
This is a cross-platform UUID implementation used to uniquely identify model instances. It is a random number based UUID, and as such can't be guaranteed unique.
Returns:
getConnection
()
Connection
Get the connection to the remote probe
This method returns the Connection object that represents the remote server used for communicating with the connected probe.
If the probe is running internally or the monitor isn't currently connected, this will return null.
Returns:
getRouter
()
Router
protected
static
Get the default router (an application singleton)
This instantiates a Router on first call.
Returns:
isConnected
()
Boolean
Is the monitor currently connected?
Returns:
List
-
[items]
Constructor for a list of Monitor objects
var myList = new Monitor.List(initialElements);
Parameters:
-
[items]
Array optionalInitial list items. These can be raw JS objects or Monitor data model objects.
Returns:
Monitor.generateUniqueCollectionId
-
collection
-
[prefix]
Generate a unique ID for a collection
This generates an ID to be used for new elements of the collection, assuring they don't clash with other elements in the collection.
Parameters:
-
collection
Backbone.CollectionThe collection to generate an ID for
-
[prefix]
String optionalAn optional prefix for the id
Returns:
setLoggerClass
-
loggerClass
Expose the logger class
Parameters:
-
loggerClass
FunctionLogger class to expose
setStatLoggerClass
-
statLoggerClass
Expose the stat logger class
Parameters:
-
statLoggerClass
FunctionStat logger class to expose
start
-
options
-
callback
Start a monitor server in this process
This is a shortand for the following:
var Monitor = require('monitor');
var server = new Monitor.Server();
server.start();
It can be chained like this:
var Monitor = require('monitor').start(),
log = Monitor.getLogger('my-app');
For more fine-tuned starting, see the Server api.
Parameters:
-
options
Object- Server.start() options. OPTIONAL
-
port
Integer- Port to attempt listening on if server isn't specified. Default: 42000
-
callback
Function(error)- Called when the server is accepting connections.
Returns:
stop
-
callback
Stop a started monitor server in this process
Parameters:
-
callback
Function(error)- Called when the server is accepting connections.
stringify
-
value
-
[depth=4]
-
[indent=2]
Produce a recursion-safe JSON string.
This method recurses the specified object to a maximum specified depth (default 4).
It also indents sub-objects for debugging output. The indent level can be specified, or set to 0 for no indentation.
This is mostly useful in debugging when the standard JSON.stringify returns an error.
Parameters:
-
value
MixedObject or value to turn into a JSON string
-
[depth=4]
Integer optionalMaximum depth to return. If the depth exceeds this value, the string "[Object]" is returned as the value.
-
[indent=2]
Integer optionalIndent the specified number of spaces (0=no indent)
Returns:
toMonitorJSON
-
[options]
Produce an object with the monitor only attributes.
A Monitor object contains a union of the connection attributes required for a Monitor, and the additional attributes defined by the probe it's monitoring.
This method produces an object containing only the monitor portion of those attributes.
Parameters:
-
[options]
Object optionalOptions to pass onto the model toJSON
Returns:
toProbeJSON
-
[options]
Produce an object without monitor attributes
A Monitor object contains a union of the connection attributes required for a Monitor, and the additional attributes defined by the probe it's monitoring.
This method produces an object containing only the probe portion of those attributes.
The id attribute of the returned JSON is set to the probeId from the monitor.
Parameters:
-
[options]
Object optionalOptions to pass onto the model toJSON
Returns:
toServerString
()
String
Produce a server string representation of the hostName:appName:appInstance
Depending on the presence of the appName and appInstance, this will produce one of the following:
hostName
hostName:appName
hostName:appName:appInstance
Returns:
toServerString
-
monitorJSON
-
hostName
-
[appName]
-
[appInstance]
Produce a server string representation of the hostName:appName:appInstance
Depending on the presence of the appName and appInstance, this will produce one of the following:
hostName
hostName:appName
hostName:appName:appInstance
Parameters:
Returns:
Events
change
Receive real time notifications from the probe
When the probe data model changes, all changed attributes are forwarded to monitors, triggering this event.
All probe attributes are available in the monitor, and the getChangedAttributes() method returns the list of attributes changed since the last change event.
myMonitor.on('change', function(){
console.log('Changes:', myMonitor.getChangedAttributes());
});
connect
The monitor has successfully connected with the probe
disconnect
The monitor has disconnected from the probe
Event Payload:
-
reason
StringReason specified for the disconnect
- Known Reasons:
- manual_disconnect - A manual call to disconnect() was made.
- connect_failed - Underlying transport connection problem.
- remote_disconnect - Underlying transport disconnected.