Fork me on GitHub

Activity Reporter

This tutorial presents how Beebotte can be used to monitor and report about CPU and memory usage of a server in real time. This tutorial assumes that you have successfullyregistered to Beebotteand obtained your API and Secret keys.

Tutorial contents:

At the end of this tutorial, you will be able to monitor your server by:

  • Receiving CPU and Memory usage in real time (see the page title)
  • Creating a dashboard to show CPU and Memory usage statistics in graphical charts

Prerequisites

This tutorial assumes you have a linux based server that you want to monitor in real time; it will use JavaScript and Nodejs. You will use your favorite text editor or IDE and a web browser.

Step 1: Install Beebotte SDK

The first step to use Beebotte's API is to install the SDK for your programming language. In this tutorial we will be using nodejs and javascript. Refer to the libraries for more instructions.

Download Beebotte SDK if you haven't already. Create a new javascript file and open it with the editor of your choice and include beebotte library as follows:

//Include the Beebotte SDK for nodejs
var bbt = require('beebotte');

Step 2: Initialize the connector

In order to communicate with Beebotte (use the API), you need to initialize a client connector. For nodejs, this is done as follows:

//Create a Beebotte connector
var bclient = new bbt.Connector({apiKey: 'API_KEY', secretKey: 'SECRET_KEY'});

Step 3: Create channel

In order to report activity statistics, you should create a channel first. In beebotte, a channel is an abstract concept composed of one or multiple resources. Please check concepts and terminology for more information.

This step needs to be performed once before sending any data to Beebotte. Creating a channel named sandbox with 2 resources CPU and memory. The description of the channel in JSON is:

//Define the channel
var sandbox_channel =
{
  name: 'sandbox', description: 'This is a test channel', //channel name and description
  ispublic: true, //everybody has read access to this channel
  resources: [{
    name: 'CPU',
    description: 'CPU activity of the channel',
    vtype: bbt.types.BBT_CPU, //value type
  },
  {
    name: 'Memory',
    description: 'Memory usage of the channel',
    vtype: bbt.types.BBT_Memory, //value type
  }]
};

Note that the access level of the channel is set to public. This grants everybody a read access to the channel data.

To add the channel, use the addChannel API call as follows:

//Add the channel
bclient.addChannel(sandbox_channel, function(err, res) {
  if(err) throw(err);
  console.log(res);
});

If the channel creation succeeds (it will fail if the channel already exists, the format is not valid, or your keys are not valid) you will have the newly created channel in the channels list. The page will show the properties of the created channel and indicate no activity for the resources. This is because we haven't yet sent any activity data.

Step 4: Send activity reports

Beebotte SDK provides utility functions to get the CPU and Memory usage. They will be used in this tutorial to send the usage statistics every 5 minutes.

var cpustats, meminfo;
...
setInterval(function() {
    ...
    //Get the CPU and memory usage
    ...
    bclient.writeResource({
      channel: 'sandbox',
      resource: 'CPU',
      data: cpustats
    }, function(err, res) {
      if(err) console.log(err);
    });
    bclient.writeResource({
      channel: 'sandbox',
      resource: 'Memory',
      data: meminfo
    }, function(err, res) {
      if(err) console.log(err);
    });
}, 5 * 60 * 1000 /* 5 minutes */);

Beebotte provides an API to send bulk messages. This will reduce the global API usage and improve the performance.

var cpustats, meminfo;
...
setInterval(function() {
    ...
    //Get the CPU and memory usage
    ...

    //Send an array of records (Bulk Write), improves performance
    bclient.writeBulk({
        channel: 'sandbox',
        records: [
            { resource: 'CPU', data: cpustats },
            { resource: 'Memory', data: meminfo }
        }, function(err, res) {

      if(err) {
        console.log(err);
        throw(err);
    });
}, 5 * 60 * 1000 /* 5 minutes */);

Get the full code from github

Step 5: Receive data in Web page (real time)

Beebotte provides a javascript library to add real-time communication between a browser (webpage) and Beebotte platform. In this section you will see how a webpage can receive data from Beebotte in real time.

You need first to include the javascript client library in your HTML page.

<script src='socket.io.min.js'></script>
<script src='//cdn.beebotte.com/js/bbt.latest.js' ></script>

Set up the credentials to access the API. As the created channel is public, you only need to the API key to read data.

var bbt = new BBT('API_KEY');

In order to receive messages relative to a specific resource, you need to subscribe to that resource first:

bbt.subscribe( {channel: 'sandbox', resource: 'CPU'}, function(err, message){
  /* work with the data here */
  console.log(message);
});

bbt.subscribe( {channel: 'sandbox', resource: 'Memory'}, function(err, message){
  /* work with the data here */
  console.log(message);
});

Step 6: Read data on server side

You can use the server SDK to read persisted resource from Beebotte.

  • readPublicResource: for reading data from public resources. This API does not require user authentication.
  • readResource: for reading data from private or public resources. This API requires authentication.

Note that from a user point of view, both API functions are similar! Under the hoods, they are drastically different.

//Include the Beebotte SDK for nodejs
var bbt = require('beebotte');

//Create a Beebotte connector
var bclient = new bbt.Connector({apiKey: 'API_KEY', secretKey: 'SECRET_KEY'});

//Read public resource - No authentication
bclient.readPublicResource({owner: 'USERNAME', channel: 'sandbox', resource: 'CPU'}, function(err, res) {
  if(err) console.log(err);
  console.log(res);
});

//Authenticated read
bclient.readResource({channel: 'sandbox', resource: 'Memory', limit: 5/* Retrieves last 5 records . default is 1 */}, function(err, res) {
  if(err) console.log(err);
  console.log(res);
});

Step 7: Create Dashboard

Beebotte provides a simple way to create dashboards based on data written to your resources (see dashboards in your account page). A dashboard is composed of a number of widgets for representing data in different charts.

A user can decide to make a dashboard public. Public dashboards can be explored under this link.

This link shows a server activity monitoring demonstration dashboard.