Node Monitor v0.6.5

Show:

Monitor Class

Extends Backbone.Model
Defined in: lib/Monitor.js:17
Module: Monitor

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 optional

      The monitor object id. Set externally.

    • probeClass String

      Class name of the probe this is (or will be) monitoring.

    • [initParams] Object optional

      Initialization parameters passed to the probe during instantiation.

    • [hostName] String optional

      Hostname 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 optional

      Application 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 optional

      Application 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 String

      ID 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

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 String

    Name of the control message.

  • [params] Object optional

    Named input parameters specific to the control message.

  • [callback] Function(error, response) optional

    Function 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]
)
Mixed

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 Mixed

    Object or value to copy

  • [depth=4] Integer optional

    Maximum depth to return. If the depth exceeds this value, the string "[Object]" is returned as the value.

Returns:

Mixed: A depth-limited copy of the value

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:

String: A globally unique ID

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:

Connection: connection The connection object

getRouter

() Router protected static

Get the default router (an application singleton)

This instantiates a Router on first call.

Returns:

Router: The default router.

isConnected

() Boolean

Is the monitor currently connected?

Returns:

Boolean: True if the monitor is currently connected

List

(
  • [items]
)
Backbone.Collection static

Constructor for a list of Monitor objects

var myList = new Monitor.List(initialElements);

Parameters:

  • [items] Array optional

    Initial list items. These can be raw JS objects or Monitor data model objects.

Returns:

Backbone.Collection: Collection of Monitor data model objects

Monitor.generateUniqueCollectionId

(
  • collection
  • [prefix]
)
String

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.Collection

    The collection to generate an ID for

  • [prefix] String optional

    An optional prefix for the id

Returns:

String: id A unique ID with the specified prefix

setLoggerClass

(
  • loggerClass
)
protected

Expose the logger class

Parameters:

  • loggerClass Function

    Logger class to expose

setStatLoggerClass

(
  • statLoggerClass
)
protected

Expose the stat logger class

Parameters:

  • statLoggerClass Function

    Stat logger class to expose

start

(
  • options
  • callback
)
Monitor static

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:

Monitor: monitor - Returns the static Monitor class (for chaining)

stop

(
  • callback
)
static

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]
)
String

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 Mixed

    Object or value to turn into a JSON string

  • [depth=4] Integer optional

    Maximum depth to return. If the depth exceeds this value, the string "[Object]" is returned as the value.

  • [indent=2] Integer optional

    Indent the specified number of spaces (0=no indent)

Returns:

String: A JSON stringified value

toMonitorJSON

(
  • [options]
)
Object

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 optional

    Options to pass onto the model toJSON

Returns:

Object: The monitor attributes

toProbeJSON

(
  • [options]
)
Object

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 optional

    Options to pass onto the model toJSON

Returns:

Object: The probe attributes

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:

String: A string representation of the monitor server

toServerString

(
  • monitorJSON
  • hostName
  • [appName]
  • [appInstance]
)
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

Parameters:

  • monitorJSON Object

    [Object] JSON object containing the following

  • hostName String

    The host to monitor

  • [appName] String optional

    The app name running on the host

  • [appInstance] String optional

    The application instance ID running on the host

Returns:

String: A string representation of the monitor server

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 String

    Reason 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.