#include <ndbapi/NdbApi.hpp>
#include <mgmapi/mgmapi.h>
#include <mgmapi/mgmapi_debug.h>

#include <stdio.h>
#include <unistd.h>
#include <time.h>

/*
g++ -o minimgm -I/usr/local/drop6p15/include/mysql -I/usr/local/drop6p15/include/mysql/storage/ndb -I/usr/local/drop6p15/include/mysql/storage/ndb/ndbapi -L/usr/local/drop6p15/lib/mysql -lmysqlclient -lz -lcrypt -lnsl -lm -lndbclient -lmysys -lmystrings minimgm.cpp
*/

void p(char *msg)
{
	char buf[30];
	time_t now;
	struct tm *t;

	now = time(NULL);
	t = localtime(&now);
	strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", t);
	printf("%s [-] %s", buf, msg);
}

int main(int argc, char **argv)
{
	NdbMgmHandle handle= ndb_mgm_create_handle();
	ndb_mgm_connect(handle,0,0,0);
	struct ndb_mgm_cluster_state *state;
	struct ndb_mgm_node_state *node_state;

	for(int t=0; t < 5000; t++) {
		state = ndb_mgm_get_status(handle);
		if( !state ) {
			p("Could not get status\n");
		} else {
			p("All status: ");
			for(int i=0; i < state->no_of_nodes; i++) {
				node_state = &state->node_states[i];
				printf("%d=", node_state->node_id);
				  
				if(node_state->version != 0)
					printf("OK");
				else
					printf("KO");
				printf(",");
			}
			printf("\n");
		}
		//sleep(1);
	}

	free((void*)state);
	ndb_mgm_destroy_handle(&handle);

	return 0;
}
