MySQL Cluster: Listen and know what's going on

You’re not scared of writing clusterious code and eavesdropping is your favorite pastime at work? You want to know what’s going on in your MySQL Cluster but were afraid asking? The MySQL Cluster Management API can help you!

Below you’ll find example C-code that will get you started with MGM API. It’s rather dull at first, but imagine you, instead of printing the event information, taking action. Imagine you starting another thread where you run some procedure which tells a monitoring system: “Hey! Some error happend!”. Imagine you spending countless hours of clusterious fun with this API!

The example listens for events in 4 categories (Statistic, Info, Error and Checkpoint) with the greatest level of 15. The code only prints out what event was received, and it also shows how to interprete the event.category, which proves to be a bit tricky.

#include <stdlib.h>
#include <unistd.h>
#include <mgmapi/mgmapi.h>

int main()
{
  NdbMgmHandle handle = ndb_mgm_create_handle();

  NdbLogEventHandle logevent;
  struct ndb_logevent event;

  int res, category;
  int timeout = 1000; // in ms
  int filter[] = {
    15, NDB_MGM_EVENT_CATEGORY_STATISTIC,
    15, NDB_MGM_EVENT_CATEGORY_INFO,
    15, NDB_MGM_EVENT_CATEGORY_ERROR,
    15, NDB_MGM_EVENT_CATEGORY_CHECKPOINT,
    0
  };

  ndb_mgm_set_connectstring(handle,"localhost");
  ndb_mgm_connect(handle,0,0,0);

  logevent = ndb_mgm_create_logevent_handle(handle, filter);
  while(true) {
    res = ndb_logevent_get_next(logevent,&event,timeout);
    category = event.category + CFG_MIN_LOGLEVEL;
    if (category == NDB_MGM_EVENT_CATEGORY_CHECKPOINT) {
      printf("We got a checkpoint event!\n");
    }
    printf("Event type %d category %d\n",
       event.type, event.category);
    usleep(500); // temporize!
  }

  ndb_mgm_destroy_handle(&handle);
}

Note that the above is very bare bone without error handling, but it works. Also, run it on the machine which runs your Management Node (ndb_mgmd), or change the connection string in the code.

Compiling can be a bit of a chore, unless you read “Want to compile a MySQL Cluster MGM API application?”.

Comments

LenZ
Hi Geert! Nice writeup, thanks for that! So when will we see a Python wrapper for this? ;)
Matthew Boehm
Excellent!