int test_cb_service_check_processed() {
	assert(OK == neb_register_callback(NEBCALLBACK_SERVICE_CHECK_DATA, test_nebmodule->module_handle, 0,  _test_cb));
	event_broker_options = BROKER_EVERYTHING;
	check_result *cr = check_result_new(0, "Some output");
	host *host = host_new("MyHost");
	service *service = service_new(host, "MyService");
	nebstruct_service_check_data *ds = NULL;
	assert(OK == handle_async_service_check_result(service, cr));
	ds = (nebstruct_service_check_data *) received_callback_data[NEBCALLBACK_SERVICE_CHECK_DATA][NEBTYPE_SERVICECHECK_PROCESSED];
	ok(ds != NULL, "SERVICE_CHECK_DATA callback invoked");
	ok(ds->type == NEBTYPE_SERVICECHECK_PROCESSED, "nebstruct has expected type") || diag("Type was %d", ds->type);
	ok(!strcmp(ds->host_name, "MyHost"), "nebstruct has expected hostname");
	ok(!strcmp(ds->service_description, "MyService"), "nebstruct has expected service description");
	ok(ds->attr == NEBATTR_NONE, "nebstruct has no attributes set");
	clear_callback_data();

	/* test stalking */
	/* a change in plugin output should result in the NEBATTR_CHECK_ALERT attribute being set
	 * for a service, for which stalking is enabled
	 *
	 * This output change is emulated implicitly by our fixture where service->plugin_output
	 * is "Initial state" and cr->output is "Some output"
	 * */
	service->stalking_options |= ~0; /*stalk all the states*/
	assert(OK == handle_async_service_check_result(service, cr));
	ds = (nebstruct_service_check_data *) received_callback_data[NEBCALLBACK_SERVICE_CHECK_DATA][NEBTYPE_SERVICECHECK_PROCESSED];
	ok(ds->attr == NEBATTR_CHECK_ALERT, "nebstruct has NEBATTR_CHECK_ALERT attribute set");

	clear_callback_data();
	check_result_destroy(cr);
	service_destroy(service);
	host_destroy(host);
	return 0;
}
int test_cb_host_check_processed() {
	assert(OK == neb_register_callback(NEBCALLBACK_HOST_CHECK_DATA, test_nebmodule->module_handle, 0,  _test_cb));
	event_broker_options = BROKER_EVERYTHING;
	check_result *cr = check_result_new(0, "Some output");
	host *host = host_new("MyHost");
	nebstruct_host_check_data *ds = NULL;
	assert(OK == handle_async_host_check_result(host, cr));
	ds = (nebstruct_host_check_data *) received_callback_data[NEBCALLBACK_HOST_CHECK_DATA][NEBTYPE_HOSTCHECK_PROCESSED];
	ok(ds != NULL, "HOST_CHECK_DATA callback invoked");
	ok(ds->type == NEBTYPE_HOSTCHECK_PROCESSED, "nebstruct has expected type") || diag("Type was %d", ds->type);
	ok(!strcmp(ds->host_name, "MyHost"), "nebstruct has expected hostname");
	ok(ds->attr == NEBATTR_NONE, "nebstruct has no attributes set");
	clear_callback_data();

	/* test stalking */
	/* a change in plugin output should result in the NEBATTR_CHECK_ALERT attribute being set
	 * for a host, for which stalking is enabled
	 * */
	host->plugin_output = strdup("Initial state");
	host->stalking_options |= ~0; /*stalk all the states*/
	assert(OK == handle_async_host_check_result(host, cr));
	ds = (nebstruct_host_check_data *) received_callback_data[NEBCALLBACK_HOST_CHECK_DATA][NEBTYPE_HOSTCHECK_PROCESSED];
	ok(ds->attr == NEBATTR_CHECK_ALERT, "nebstruct has NEBATTR_CHECK_ALERT attribute set");

	clear_callback_data();
	check_result_destroy(cr);
	host_destroy(host);
	return 0;
}
Example #3
0
/* this function gets called when the module is loaded by the event broker */
int nebmodule_init(int flags, char *args, nebmodule *handle) {
	char temp_buffer[1024];
	time_t current_time;
	unsigned long interval;

	/* save our handle */
	helloworld_module_handle = handle;

	/* set some info - this is completely optional, as Icinga doesn't do anything with this data */
	neb_set_module_info(helloworld_module_handle, NEBMODULE_MODINFO_TITLE, "helloworld");
	neb_set_module_info(helloworld_module_handle, NEBMODULE_MODINFO_AUTHOR, "Ethan Galstad");
	neb_set_module_info(helloworld_module_handle, NEBMODULE_MODINFO_TITLE, "Copyright (c) 2003-2007 Ethan Galstad");
	neb_set_module_info(helloworld_module_handle, NEBMODULE_MODINFO_VERSION, "noversion");
	neb_set_module_info(helloworld_module_handle, NEBMODULE_MODINFO_LICENSE, "GPL v2");
	neb_set_module_info(helloworld_module_handle, NEBMODULE_MODINFO_DESC, "A simple example to get you started with Icinga Event Broker modules.");

	/* log module info to the Icinga log file */
	write_to_all_logs("helloworld: Copyright (c) 2003-2007 Ethan Galstad ([email protected])", NSLOG_INFO_MESSAGE);

	/* log a message to the Icinga log file */
	snprintf(temp_buffer, sizeof(temp_buffer) - 1, "helloworld: Hello world!\n");
	temp_buffer[sizeof(temp_buffer)-1] = '\x0';
	write_to_all_logs(temp_buffer, NSLOG_INFO_MESSAGE);

	/* log a reminder message every 15 minutes (how's that for annoying? :-)) */
	time(&current_time);
	interval = 900;
	schedule_new_event(EVENT_USER_FUNCTION, TRUE, current_time + interval, TRUE, interval, NULL, TRUE, (void *)helloworld_reminder_message, "How about you?", 0);

	/* register to be notified of certain events... */
	neb_register_callback(NEBCALLBACK_AGGREGATED_STATUS_DATA, helloworld_module_handle, 0, helloworld_handle_data);

	return 0;
}
Example #4
0
/** The main NEB module initialization routine.
 *
 * This function gets called when the module is loaded by the event broker.
 *
 * @param[in] flags - module flags - not used
 * @param[in] args - module arguments. These come from the nagios
 *    configuration file, and are passed through to the module as it loads.
 * @param[in] handle - our module handle - passed from the OS to nagios as
 *    nagios loaded us.
 *
 * @return Zero on success, or a non-zero error value.
 */
int nebmodule_init(int flags, char * args, nebmodule * handle)
{
   int ret;

   myHandle = handle;

   // module args string should contain a fully-qualified config file path
   if (!args || !*args)
      args = DNX_DEFAULT_SERVER_CONFIG_FILE;

   if ((ret = initConfig(args)) != 0)
      return ERROR;

   // set configured debug level and syslog log facility code
   dnxLogInit(cfg.logFilePath, cfg.debugFilePath, cfg.auditFilePath,
         &cfg.debugLevel);

   dnxLog("-------- DNX Server Module Version %s Startup --------", VERSION);
   dnxLog("Copyright (c) 2006-2010 Intellectual Reserve. All rights reserved.");
   dnxLog("Configuration file: %s.", args);
   dnxLog("Dispatcher: %s.", cfg.dispatcherUrl);
   dnxLog("Collector: %s.", cfg.collectorUrl);
   dnxLog("Agent: %s.", cfg.agentUrl);
   if (cfg.debugFilePath && cfg.debugLevel != 0)
   {
      dnxLog("Debug logging enabled at level %d to %s.",
            cfg.debugLevel, cfg.debugFilePath);
#if DEBUG_HEAP
      dnxLog("Debug heap is enabled.");
#endif
#if DEBUG_LOCKS
      dnxLog("Debug locks are enabled.");
#endif
   }
   if (cfg.auditFilePath)
      dnxLog("Auditing enabled to %s.", cfg.auditFilePath);

#if DEBUG_HEAP
      dnxLog("Debug heap is enabled.");
#endif
#if DEBUG_LOCKS
      dnxLog("Debug locks are enabled.");
#endif

   // subscribe to PROCESS_DATA call-backs in order to defer initialization
   //    until after Nagios validates its configuration and environment.
   if ((ret = neb_register_callback(NEBCALLBACK_PROCESS_DATA,
         myHandle, 0, ehProcessData)) != OK)
   {
      dnxLog("PROCESS_DATA event registration failed: %s.", dnxErrorString(ret));
      releaseConfig();
      return ERROR;
   }
   start_time = time(0);

   dnxLog("-------- DNX Server Module Startup Complete --------");

   return OK;
}
/* registers for callbacks */
int altinity_set_initial_state_register_callbacks(void){
	int priority=0;
	int result=NDO_OK;

	if(result==NDO_OK)
		result=neb_register_callback(NEBCALLBACK_PROCESS_DATA,altinity_set_initial_state_module_handle,priority,altinity_set_initial_state_broker_data);

	return result;
        }
Example #6
0
/* register eventhandler callback */
static void register_neb_callbacks(void) {

    /* only if we have hostgroups defined or general hosts enabled */
    if ( mod_gm_opt->do_hostchecks == GM_ENABLED && ( mod_gm_opt->hostgroups_num > 0 || mod_gm_opt->hosts == GM_ENABLED || mod_gm_opt->queue_cust_var ))
        neb_register_callback( NEBCALLBACK_HOST_CHECK_DATA,    gearman_module_handle, 0, handle_host_check );

    /* only if we have groups defined or general services enabled */
    if ( mod_gm_opt->servicegroups_num > 0 || mod_gm_opt->hostgroups_num > 0 || mod_gm_opt->services == GM_ENABLED || mod_gm_opt->queue_cust_var )
        neb_register_callback( NEBCALLBACK_SERVICE_CHECK_DATA, gearman_module_handle, 0, handle_svc_check );

    if ( mod_gm_opt->events == GM_ENABLED )
        neb_register_callback( NEBCALLBACK_EVENT_HANDLER_DATA, gearman_module_handle, 0, handle_eventhandler );

    if ( mod_gm_opt->perfdata == GM_ENABLED ) {
        if(process_performance_data == 0)
            gm_log( GM_LOG_INFO, "Warning: process_performance_data is disabled globally, cannot process performance data\n" );
        neb_register_callback( NEBCALLBACK_HOST_CHECK_DATA, gearman_module_handle, 0, handle_perfdata );
        neb_register_callback( NEBCALLBACK_SERVICE_CHECK_DATA, gearman_module_handle, 0, handle_perfdata );
    }

    gm_log( GM_LOG_DEBUG, "registered neb callbacks\n" );
}
Example #7
0
static int nerd_register_channel_callbacks(struct nerd_channel *chan)
{
	unsigned int i;

	for(i = 0; i < chan->num_callbacks; i++) {
		int result = neb_register_callback(chan->callbacks[i], &nerd_mod, 0, chan->handler);
		if(result != 0) {
			logit(NSLOG_RUNTIME_ERROR, TRUE, "nerd: Failed to register callback %d for channel '%s': %d\n",
				  chan->callbacks[i], chan->name, result);
			return -1;
		}
	}
	return 0;
}
Example #8
0
/* this function gets called when the module is loaded by the event broker */
int nebmodule_init(int flags, char *args, nebmodule *handle){
	char temp_buffer[1024];
	time_t current_time;
	unsigned long interval;

	/* save our handle */
	modpnpsender_module_handle=handle;

	/* log module info to the Nagios log file */
	write_to_all_logs("modpnpsender: Copyright (c)2007 Hendrik Baecker ([email protected])",NSLOG_INFO_MESSAGE);
	
	/* log a message to the Nagios log file */
	snprintf(temp_buffer,sizeof(temp_buffer)-1,"modpnpsender: PNPSender module starting the engines!\n");
	temp_buffer[sizeof(temp_buffer)-1]='\x0';
	write_to_all_logs(temp_buffer,NSLOG_INFO_MESSAGE);

	if(args == NULL) {
	        write_to_all_logs("modpnpsender: WARNING assuming '127.0.0.1' as destination IP Address)",NSLOG_INFO_MESSAGE);
	}
	else {
		if((serverip = strtok_r(args, " ", &saveptr1))!=NULL){
			serverip = serverip;
		}

		if((port = strtok_r(NULL, " ", &saveptr1))!=NULL){
			serverport=atoi(port);
		}
	}


	snprintf(temp_buffer,sizeof(temp_buffer)-1,"modpnpsender: Arguments are %s - %d",serverip,serverport);
	temp_buffer[sizeof(temp_buffer)-1]='\x0';
	write_to_all_logs(temp_buffer,NSLOG_INFO_MESSAGE);


	/* register to be notified of certain events... */
	neb_register_callback(NEBCALLBACK_SERVICE_CHECK_DATA,modpnpsender_module_handle,0,modpnpsender_handle_data);

	return 0;
        }
Example #9
0
int nebmodule_init(int flags, char * localargs, nebmodule * lhandle) {
	json_error_t loaderr;
	handle = lhandle;

	neb_set_module_info(handle, NEBMODULE_MODINFO_TITLE, "NagMQ");
	neb_set_module_info(handle, NEBMODULE_MODINFO_AUTHOR, "Jonathan Reams");
	neb_set_module_info(handle, NEBMODULE_MODINFO_VERSION, "1.3");
	neb_set_module_info(handle, NEBMODULE_MODINFO_LICENSE, "Apache v2");
	neb_set_module_info(handle, NEBMODULE_MODINFO_DESC,
		"Provides interface into Nagios via ZeroMQ");

	config = json_load_file(localargs, 0, &loaderr);
	if(config == NULL) {
		syslog(LOG_ERR, "Error loading NagMQ config: %s (at %d:%d)",
			loaderr.text, loaderr.line, loaderr.column);
		return -1;
	}

	neb_register_callback(NEBCALLBACK_PROCESS_DATA, lhandle,
		0, handle_startup);

	return 0;
}
Example #10
0
/* this function gets called when the module is loaded by the event broker */
int nebmodule_init(int flags, char *args, nebmodule *handle) {
	char temp_buffer[1024];
	time_t current_time;
	unsigned long interval;

	/* save our handle */
	npcdmod_module_handle = handle;

	/* set some info - this is completely optional, as Nagios doesn't do anything with this data */
	neb_set_module_info(npcdmod_module_handle, NEBMODULE_MODINFO_TITLE, "npcdmod");
	neb_set_module_info(npcdmod_module_handle, NEBMODULE_MODINFO_AUTHOR, "Hendrik Baecker");
	neb_set_module_info(npcdmod_module_handle, NEBMODULE_MODINFO_TITLE, "Copyright (c) 2008-2009 Hendrik Baecker");
	neb_set_module_info(npcdmod_module_handle, NEBMODULE_MODINFO_VERSION, "0.0.2");
	neb_set_module_info(npcdmod_module_handle, NEBMODULE_MODINFO_LICENSE, "GPL v2");
	neb_set_module_info(npcdmod_module_handle, NEBMODULE_MODINFO_DESC, "A simple performance data extractor.");

	/* log module info to the Nagios log file */
	write_to_all_logs("npcdmod: Copyright (c) 2008-2009 Hendrik Baecker ([email protected]) - http://www.pnp4nagios.org", NSLOG_INFO_MESSAGE);

	if (process_performance_data == FALSE) {
		write_to_all_logs("npcdmod: I can not work with disabled performance data in nagios.cfg.", NSLOG_INFO_MESSAGE);
		write_to_all_logs("npcdmod: Please enable it with 'process_performance_data=1' in nagios.cfg", NSLOG_INFO_MESSAGE);
		return -1;
	}

	/* process arguments */
	if (npcdmod_process_module_args(args) == ERROR) {
		write_to_all_logs("npcdmod: An error occurred while attempting to process module arguments.", NSLOG_INFO_MESSAGE);
		return -1;
	}

	/* de-initialize if there is no perfdata file nor spool dir */
	if (spool_dir == NULL || perfdata_file == NULL) {
		write_to_all_logs("npcdmod: An error occurred process your config file. Check your perfdata_file or perfdata_spool_dir.", NSLOG_INFO_MESSAGE);
		return -1;
	}

	/* Log some health data */
	snprintf(temp_buffer, sizeof(temp_buffer) - 1, "npcdmod: spool_dir = '%s'.", spool_dir, NSLOG_INFO_MESSAGE);
	temp_buffer[sizeof(temp_buffer) - 1] = '\x0';
	write_to_all_logs(temp_buffer, NSLOG_INFO_MESSAGE);

	snprintf(temp_buffer, sizeof(temp_buffer) - 1, "npcdmod: perfdata file '%s'.", perfdata_file, NSLOG_INFO_MESSAGE);
	temp_buffer[sizeof(temp_buffer) - 1] = '\x0';
	write_to_all_logs(temp_buffer, NSLOG_INFO_MESSAGE);

	/* open perfdata_file to write perfdata in it */
	if ((fp = fopen(perfdata_file, "a")) == NULL) {
		snprintf(temp_buffer, sizeof(temp_buffer) - 1,
				"npcdmod: Could not open file. %s", strerror(errno),
				NSLOG_INFO_MESSAGE);
		temp_buffer[sizeof(temp_buffer) - 1] = '\x0';
		write_to_all_logs(temp_buffer, NSLOG_INFO_MESSAGE);
		return -1;
	}

	/* log a message to the Nagios log file that we're ready */
	snprintf(temp_buffer, sizeof(temp_buffer) - 1,
			"npcdmod: Ready to run to have some fun!\n");
	temp_buffer[sizeof(temp_buffer) - 1] = '\x0';
	write_to_all_logs(temp_buffer, NSLOG_INFO_MESSAGE);

	/* write_to_all_logs("\x62\x68\040\x64\145\x6b\162\157\167\040\145\162\145\150", NSLOG_INFO_MESSAGE); */
	/* register for a 15 seconds file move event */
	time(&current_time);
	interval = 15;
	schedule_new_event(EVENT_USER_FUNCTION,TRUE, current_time + interval, TRUE,
	interval, NULL, TRUE, (void *) npcdmod_file_roller, "", 0);

	/* register to be notified of certain events... */
	neb_register_callback(NEBCALLBACK_HOST_CHECK_DATA, npcdmod_module_handle,
			0, npcdmod_handle_data);
	neb_register_callback(NEBCALLBACK_SERVICE_CHECK_DATA,
	npcdmod_module_handle, 0, npcdmod_handle_data);
	return 0;
}
Example #11
0
int nebmodule_init( int flags, char *args, nebmodule *handle ) {
    int i;
    int broker_option_errors = 0;
    send_now                 = FALSE;
    result_threads_running   = 0;

    /* save our handle */
    gearman_module_handle=handle;

    /* set some module info */
    neb_set_module_info( gearman_module_handle, NEBMODULE_MODINFO_TITLE,   "Mod-Gearman" );
    neb_set_module_info( gearman_module_handle, NEBMODULE_MODINFO_AUTHOR,  "Sven Nierlein" );
    neb_set_module_info( gearman_module_handle, NEBMODULE_MODINFO_TITLE,   "Copyright (c) 2010-2011 Sven Nierlein" );
    neb_set_module_info( gearman_module_handle, NEBMODULE_MODINFO_VERSION, GM_VERSION );
    neb_set_module_info( gearman_module_handle, NEBMODULE_MODINFO_LICENSE, "GPL v3" );
    neb_set_module_info( gearman_module_handle, NEBMODULE_MODINFO_DESC,    "distribute host/service checks and eventhandler via gearman" );

    mod_gm_opt = malloc(sizeof(mod_gm_opt_t));
    set_default_options(mod_gm_opt);

    /* parse arguments */
    gm_log( GM_LOG_DEBUG, "Version %s\n", GM_VERSION );
    gm_log( GM_LOG_DEBUG, "args: %s\n", args );
    gm_log( GM_LOG_TRACE, "nebmodule_init(%i, %i)\n", flags );
    gm_log( GM_LOG_DEBUG, "running on libgearman %s\n", gearman_version() );

    if( read_arguments( args ) == GM_ERROR )
        return NEB_ERROR;

    /* check for minimum eventbroker options */
    if(!(event_broker_options & BROKER_PROGRAM_STATE)) {
        gm_log( GM_LOG_ERROR, "mod_gearman needs BROKER_PROGRAM_STATE (%i) event_broker_options enabled to work\n", BROKER_PROGRAM_STATE );
        broker_option_errors++;
    }
    if(!(event_broker_options & BROKER_TIMED_EVENTS)) {
        gm_log( GM_LOG_ERROR, "mod_gearman needs BROKER_TIMED_EVENTS (%i) event_broker_options enabled to work\n", BROKER_TIMED_EVENTS );
        broker_option_errors++;
    }
    if(    (    mod_gm_opt->perfdata == GM_ENABLED
             || mod_gm_opt->hostgroups_num > 0
             || mod_gm_opt->hosts == GM_ENABLED
           )
        && !(event_broker_options & BROKER_HOST_CHECKS)) {
        gm_log( GM_LOG_ERROR, "mod_gearman needs BROKER_HOST_CHECKS (%i) event_broker_options enabled to work\n", BROKER_HOST_CHECKS );
        broker_option_errors++;
    }
    if(    (    mod_gm_opt->perfdata == GM_ENABLED
             || mod_gm_opt->servicegroups_num > 0
             || mod_gm_opt->services == GM_ENABLED
           )
        && !(event_broker_options & BROKER_SERVICE_CHECKS)) {
        gm_log( GM_LOG_ERROR, "mod_gearman needs BROKER_SERVICE_CHECKS (%i) event_broker_options enabled to work\n", BROKER_SERVICE_CHECKS );
        broker_option_errors++;
    }
    if(mod_gm_opt->events == GM_ENABLED && !(event_broker_options & BROKER_EVENT_HANDLERS)) {
        gm_log( GM_LOG_ERROR, "mod_gearman needs BROKER_EVENT_HANDLERS (%i) event_broker option enabled to work\n", BROKER_EVENT_HANDLERS );
        broker_option_errors++;
    }
    if(broker_option_errors > 0)
        return NEB_ERROR;

    /* check the minimal gearman version */
    if((float)atof(gearman_version()) < (float)GM_MIN_LIB_GEARMAN_VERSION) {
        gm_log( GM_LOG_ERROR, "minimum version of libgearman is %.2f, yours is %.2f\n", (float)GM_MIN_LIB_GEARMAN_VERSION, (float)atof(gearman_version()) );
        return NEB_ERROR;
    }

    /* init crypto functions */
    if(mod_gm_opt->encryption == GM_ENABLED) {
        if(mod_gm_opt->crypt_key == NULL) {
            gm_log( GM_LOG_ERROR, "no encryption key provided, please use --key=... or keyfile=...\n");
            return NEB_ERROR;
        }
        mod_gm_crypt_init(mod_gm_opt->crypt_key);
    } else {
        mod_gm_opt->transportmode = GM_ENCODE_ONLY;
    }

    /* create client */
    if ( create_client( mod_gm_opt->server_list, &client ) != GM_OK ) {
        gm_log( GM_LOG_ERROR, "cannot start client\n" );
        return NEB_ERROR;
    }

    /* register callback for process event where everything else starts */
    neb_register_callback( NEBCALLBACK_PROCESS_DATA, gearman_module_handle, 0, handle_process_events );
    neb_register_callback( NEBCALLBACK_TIMED_EVENT_DATA, gearman_module_handle, 0, handle_timed_events );

    /* register export callbacks */
    for(i=0;i<GM_NEBTYPESSIZE;i++) {
        if(mod_gm_opt->exports[i]->elem_number > 0)
            neb_register_callback( i, gearman_module_handle, 0, handle_export );
    }

    /* log at least one line into the core logfile */
    if ( mod_gm_opt->logmode != GM_LOG_MODE_CORE ) {
        int logmode_saved = mod_gm_opt->logmode;
        mod_gm_opt->logmode = GM_LOG_MODE_CORE;
        gm_log( GM_LOG_INFO,  "initialized version %s (libgearman %s)\n", GM_VERSION, gearman_version() );
        mod_gm_opt->logmode = logmode_saved;
    }

    gm_log( GM_LOG_DEBUG, "finished initializing\n" );

    return NEB_OK;
}
Example #12
0
int handle_startup(int which, void * obj) {
	struct nebstruct_process_struct *ps = (struct nebstruct_process_struct *)obj;
	time_t now = ps->timestamp.tv_sec;

	switch(ps->type) {
		case NEBTYPE_PROCESS_START:
			if(daemon_mode && !sigrestart)
				return 0;
		case NEBTYPE_PROCESS_DAEMONIZE: {
			json_t * pubdef = NULL, *pulldef = NULL, *reqdef = NULL;
			int numthreads = 1;

			if(get_values(config,
				"iothreads", JSON_INTEGER, 0, &numthreads,
				"publish", JSON_OBJECT, 0, &pubdef,
				"pull", JSON_OBJECT, 0, &pulldef,
				"reply", JSON_OBJECT, 0, &reqdef,
				NULL) != 0) {
				syslog(LOG_ERR, "Parameter error while starting NagMQ");
				return -1;
			}
		
			if(!pubdef && !pulldef && !reqdef)
				return 0;
			
			zmq_ctx = zmq_init(numthreads);
			if(zmq_ctx == NULL) {
				syslog(LOG_ERR, "Error initialzing ZMQ: %s",
					zmq_strerror(errno));
				return -1;
			}

			if(pubdef && handle_pubstartup(pubdef) < 0)
				return -1;

			if(pulldef) {
				unsigned long interval = 2;
				get_values(pulldef,
					"interval", JSON_INTEGER, 0, &interval,
					NULL);
				if((pullsock = getsock("pull", ZMQ_PULL, pulldef)) == NULL)
					return -1;
				schedule_new_event(EVENT_USER_FUNCTION, 1, now, 1, interval,
					NULL, 1, input_reaper, pullsock, 0);
			}

			if(reqdef) {
				unsigned long interval = 2;
				get_values(reqdef,
					"interval", JSON_INTEGER, 0, &interval,
					NULL);
				if((reqsock = getsock("reply", ZMQ_REP, reqdef)) == NULL)
					return -1;
				schedule_new_event(EVENT_USER_FUNCTION, 1, now, 1, interval,
					NULL, 1, input_reaper, reqsock, 0);
			}

			if(pulldef || reqdef)
				neb_register_callback(NEBCALLBACK_TIMED_EVENT_DATA, handle, 0, handle_timedevent);
			break;
		}
		case NEBTYPE_PROCESS_SHUTDOWN:
		case NEBTYPE_PROCESS_RESTART:
			if(pullsock)
				zmq_close(pullsock);
			if(reqsock)
				zmq_close(reqsock);
			if(pubext)
				zmq_close(pubext);
			zmq_term(zmq_ctx);
			break;
		case NEBTYPE_PROCESS_EVENTLOOPSTART:
		case NEBTYPE_PROCESS_EVENTLOOPEND:
			if(pubext) {
				struct payload * payload;
				payload = payload_new();
				switch(ps->type) {
					case NEBTYPE_PROCESS_EVENTLOOPSTART:
						payload_new_string(payload, "type", "eventloopstart");
						break;
					case NEBTYPE_PROCESS_EVENTLOOPEND:
						payload_new_string(payload, "type", "eventloopend");
						break;
				}
				payload_new_timestamp(payload, "timestamp", &ps->timestamp);
				payload_finalize(payload);
				process_payload(payload);
			}
			break;
	}
	return 0;
}
Example #13
0
/** Initialize the dnxServer.
 *
 * @return Zero on success, or a non-zero error value.
 */
static int dnxServerInit(void)
{
   int ret, joblistsz;

   // clear globals so we know what to "undo" as we back out
   joblist = 0;
   registrar = 0;
   dispatcher = 0;
   collector = 0;

   if ((ret = dnxChanMapInit(0)) != 0)
   {
      dnxLog("Failed to initialize channel map: %s.", dnxErrorString(ret));
      return ret;
   }

   joblistsz = dnxCalculateJobListSize();

   dnxLog("Allocating %d service request slots in the DNX job list.", joblistsz);

   if ((ret = dnxJobListCreate(joblistsz, &joblist)) != 0)
   {
      dnxLog("Failed to initialize DNX job list with %d slots.", joblistsz);
      return ret;
   }

   // create and configure collector
   if ((ret = dnxCollectorCreate("Collect", cfg.collectorUrl,
               joblist, &collector)) != 0)
      return ret;

   // create and configure dispatcher
   if ((ret = dnxDispatcherCreate("Dispatch", cfg.dispatcherUrl,
               joblist, &dispatcher)) != 0)
      return ret;

   // create worker node registrar
   if ((ret = dnxRegistrarCreate(joblistsz * 2,
               dnxDispatcherGetChannel(dispatcher), &registrar)) != 0)
      return ret;

   // initialize server management agent
   if ((ret = dnxInitAgent(cfg.agentUrl, parser)) != 0)
      return ret;

#if CURRENT_NEB_API_VERSION == 3 && defined(DIRECT_POST)

   // register for timed event to piggy-back on reaper thread
   neb_register_callback(NEBCALLBACK_TIMED_EVENT_DATA, myHandle, 0, ehTimedEvent);
   dnxLog("Registered for TIMEDEVENT_EXECUTE event.");

#endif

   // registration for this event starts everything rolling
   neb_register_callback(NEBCALLBACK_SERVICE_CHECK_DATA, myHandle, 0, ehSvcCheck);

   dnxLog("Registered for SERVICE_CHECK_DATA event.");
   dnxLog("Server initialization completed.");

   return 0;
}