dnxNebMain.c File Reference

Implements the DNX Nagios plugin-based server module. More...

#include "dnxNebMain.h"
#include "dnxError.h"
#include "dnxDebug.h"
#include "dnxLogging.h"
#include "dnxCollector.h"
#include "dnxDispatcher.h"
#include "dnxRegistrar.h"
#include "dnxJobList.h"
#include "dnxCfgParser.h"
#include "dnxStats.h"
#include "dnxAgent.h"
#include "nagios.h"
#include "objects.h"
#include "nebmodules.h"
#include "nebstructs.h"
#include "nebcallbacks.h"
#include "neberrors.h"
#include "broker.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <regex.h>

Go to the source code of this file.

Data Structures

struct  DnxJobData
 The internal structure of a new job payload object. More...
struct  DnxServerCfg
 The internal server module configuration data structure. More...

Defines

#define VERSION   "<unknown>"
#define SYSCONFDIR   "/etc"
#define SYSLOGDIR   "/var/log"
#define elemcount(x)   (sizeof(x)/sizeof(*(x)))
#define DNX_DEFAULT_SERVER_CONFIG_FILE   SYSCONFDIR "/dnxServer.cfg"
#define DNX_DEFAULT_LOG   SYSLOGDIR "/dnxsrv.log"
#define DNX_DEFAULT_DBGLOG   SYSLOGDIR "/dnxsrv.dbg.log"

Functions

 NEB_API_VERSION (CURRENT_NEB_API_VERSION)
static void releaseConfig (void)
 Cleanup the config file parser.
static int validateCfg (DnxCfgDict *dict, void **vptrs, void *passthru)
 Validate a configuration data structure in context.
static int initConfig (char *cfgfile)
 Read and parse the dnxServer configuration file.
static int nagiosGetServiceCount (void)
 Return the number of services configured in Nagios.
int dnxPostResult (void *data, time_t start_time, unsigned delta, int early_timeout, int res_code, char *res_data)
 Post a completed service request to the Nagios service result buffer.
static int dnxCalculateJobListSize (void)
 Calculate the optimal size of the job list.
static int dnxPostNewJob (DnxJobList *joblist, unsigned long serial, DnxJobData *jdp, nebstruct_service_check_data *ds, DnxNodeRequest *pNode)
 Post a new job from Nagios to the dnxServer job queue.
static int ehSvcCheck (int event_type, void *data)
 Service Check Event Handler.
static int ehProcessData (int event_type, void *data)
 Process Data Event Handler.
static int dnxServerDeInit (void)
 Deinitialize the dnx server.
static int dnxServerInit (void)
 Initialize the dnxServer.
static int launchScript (char *script)
 Launches an external command and waits for it to return a status code.
char * versionText (void)
 Format version text to an allocated string buffer.
void dnxJobCleanup (DnxNewJob *pJob)
 Release all resources associated with a job object.
int dnxAuditJob (DnxNewJob *pJob, char *action)
 Send an audit message to the dnx server audit log.
int nebmodule_deinit (int flags, int reason)
 The main NEB module deinitialization routine.
int nebmodule_init (int flags, char *args, nebmodule *handle)
 The main NEB module initialization routine.

Variables

static DnxServerCfg cfg
 The server configuration parameters.
static DnxCfgParserparser
 The system configuration parser.
static DnxJobListjoblist
 The master job list.
static DnxRegistrarregistrar
 The client node registrar.
static DnxDispatcherdispatcher
 The job list dispatcher.
static DnxCollectorcollector
 The job list results collector.
static time_t start_time
 The module start time.
static int s_shutdown
 The agent shutdown flag.
static DnxChannelagent
 The agent comm channel.
static pthread_t agentTid
 The agent thread id.
static void * myHandle
 Private NEB module handle.
static regex_t regEx
 Compiled regular expression structure.


Detailed Description

Implements the DNX Nagios plugin-based server module.

This version of the DNX Nagios plugin implements the entire DNX server within the Nagios Event Broker (NEB) plugin module. It intercepts all service checks and dispatches them to distributed worker nodes.

The most significant problem with this approach is that this modules must start several threads, and Nagios (especially Nagios 3.x) avoids threads in favor of processes (fork/exec).

Author:
Robert W. Ingraham (dnx-devel@lists.sourceforge.net)

John Calcote (dnx-devel@lists.sourceforge.net)

Attention:
Please submit patches to http://dnx.sourceforge.net

Definition in file dnxNebMain.c.


Define Documentation

#define DNX_DEFAULT_DBGLOG   SYSLOGDIR "/dnxsrv.dbg.log"

Definition at line 101 of file dnxNebMain.c.

#define DNX_DEFAULT_LOG   SYSLOGDIR "/dnxsrv.log"

Definition at line 100 of file dnxNebMain.c.

#define DNX_DEFAULT_SERVER_CONFIG_FILE   SYSCONFDIR "/dnxServer.cfg"

Definition at line 99 of file dnxNebMain.c.

#define elemcount (  )     (sizeof(x)/sizeof(*(x)))

Definition at line 97 of file dnxNebMain.c.

#define SYSCONFDIR   "/etc"

Definition at line 60 of file dnxNebMain.c.

#define SYSLOGDIR   "/var/log"

Definition at line 64 of file dnxNebMain.c.

#define VERSION   "<unknown>"

Definition at line 56 of file dnxNebMain.c.


Function Documentation

int dnxAuditJob ( DnxNewJob pJob,
char *  action 
)

Send an audit message to the dnx server audit log.

Parameters:
[in] pJob - the job to be audited.
[in] action - the audit action that we're logging.
Returns:
Zero on success or a non-zero error value.

Todo:
This conversion should take place in the dnxUdpRead function and the resultant address string stored in the DnxNewJob structure. This would have two benefits:
1. Encapsulates conversion at the protocol level. 2. Saves some time during logging.

Definition at line 1190 of file dnxNebMain.c.

References DnxNodeRequest::address, DnxNodeRequest::addrstr, DnxCfgData::auditFilePath, DnxServerCfg::auditFilePath, DnxNewJob::cmd, DNX_OK, dnxAudit(), DnxXID::objSerial, DnxXID::objSlot, DnxNewJob::pNode, DnxNodeRequest::xid, and DnxNewJob::xid.

static int dnxCalculateJobListSize ( void   )  [static]

Calculate the optimal size of the job list.

Assumes the caller will actually use the returned value to allocate the job list. Based on this assumption, this routine logs messages indicating when various configuration overrides have taken effect.

Returns:
The calculated size of the job list.

Definition at line 801 of file dnxNebMain.c.

References dnxLog(), DnxServerCfg::maxNodeRequests, DnxServerCfg::minServiceSlots, and nagiosGetServiceCount().

void dnxJobCleanup ( DnxNewJob pJob  ) 

Release all resources associated with a job object.

Parameters:
[in] pJob - the job to be freed.

Definition at line 1178 of file dnxNebMain.c.

References DnxNewJob::cmd, DnxNewJob::payload, DnxNewJob::pNode, and xfree.

static int dnxPostNewJob ( DnxJobList joblist,
unsigned long  serial,
DnxJobData jdp,
nebstruct_service_check_data *  ds,
DnxNodeRequest pNode 
) [static]

Post a new job from Nagios to the dnxServer job queue.

Parameters:
[in] joblist - the job list to which the new job should be posted.
[in] serial - the serial number of the new job.
[in] jdp - a pointer to a job data structure.
[in] ds - a pointer to the nagios job that's being posted.
[in] pNode - a dnxClient node request structure that is being posted with this job. The dispatcher thread will send the job to the associated node.
Returns:
Zero on success, or a non-zero error value.

Definition at line 847 of file dnxNebMain.c.

References DnxNewJob::cmd, DNX_OBJ_JOB, DNX_OK, dnxAuditJob(), dnxDebug(), dnxJobListAdd(), dnxLog(), dnxMakeXID(), dnxStatsInc(), DnxNewJob::expires, JOBS_HANDLED, JOBS_REJECTED_NO_SLOTS, DnxXID::objSerial, DnxNewJob::payload, DnxNewJob::pNode, DnxNewJob::start_time, DnxNewJob::timeout, DnxNewJob::xid, and xstrdup.

int dnxPostResult ( void *  data,
time_t  start_time,
unsigned  delta,
int  early_timeout,
int  res_code,
char *  res_data 
)

Post a completed service request to the Nagios service result buffer.

If early_timeout is true (1), then res_code will be reset to STATE_UNKNOWN, so you might as well pass zero for res_code.

Parameters:
[in] data - An opaque pointer to the nagios service structure and other relevant results data.
[in] start_time - the nagios service object start time.
[in] delta - the running time of the nagios service object in seconds.
[in] early_timeout - boolean; true means the job DID time out.
[in] res_code - the result code of this job.
[in] res_data - the resulting STDOUT output text of this job.
Returns:
Zero on success, or a non-zero error code.

Todo:
Nagios 3.x: Collect a better value for exited_ok.

Todo:
Nagios 3.x: Collect a better value for check_type.

Definition at line 746 of file dnxNebMain.c.

References DnxJobData::chkopts, dnxStatsInc(), POST_RESULTS_FAILED, POST_RESULTS_OK, DnxJobData::reschedule, RESULTS_FAILED, RESULTS_OK, RESULTS_TIMED_OUT, DnxJobData::schedule, and DnxJobData::svc.

static int dnxServerDeInit ( void   )  [static]

static int dnxServerInit ( void   )  [static]

static int ehProcessData ( int  event_type,
void *  data 
) [static]

Process Data Event Handler.

Parameters:
[in] event_type - the event regarding which we were called by Nagios.
[in] data - an opaque pointer to an event-specific data structure.
Returns:
Zero if all is okay, but we want nagios to handle this event; non-zero if there's a problem of some sort.

Definition at line 1113 of file dnxNebMain.c.

References dnxDebug(), dnxLog(), dnxServerDeInit(), dnxServerInit(), launchScript(), and DnxServerCfg::syncScript.

static int ehSvcCheck ( int  event_type,
void *  data 
) [static]

Service Check Event Handler.

Parameters:
[in] event_type - the event type for which we're being called.
[in] data - an opaque pointer to nagios event-specific data.
Returns:
Zero if we want Nagios to handle the event; NEBERROR_CALLBACKOVERRIDE indicates that we want to handle the event ourselves; any other non-zero value represents an error.

Definition at line 892 of file dnxNebMain.c.

References DnxJobData::chkopts, DNX_OK, dnxDebug(), dnxErrorString(), dnxGetNodeRequest(), dnxLog(), dnxPostNewJob(), DnxServerCfg::localCheckPattern, regEx, DnxJobData::reschedule, DnxJobData::schedule, DnxJobData::svc, xfree, and xmalloc.

static int initConfig ( char *  cfgfile  )  [static]

static int launchScript ( char *  script  )  [static]

Launches an external command and waits for it to return a status code.

Parameters:
[in] script - the command line to be launched.
Returns:
Zero on success, or a non-zero error value.

Definition at line 1083 of file dnxNebMain.c.

References DNX_ERR_INVALID, DNX_OK, and dnxLog().

static int nagiosGetServiceCount ( void   )  [static]

Return the number of services configured in Nagios.

Returns:
The number of services configured in Nagios.
Todo:
This routine should be in nagios code.

Definition at line 290 of file dnxNebMain.c.

References service_list.

NEB_API_VERSION ( CURRENT_NEB_API_VERSION   ) 

int nebmodule_deinit ( int  flags,
int  reason 
)

The main NEB module deinitialization routine.

This function gets called when the module is unloaded by the event broker.

Parameters:
[in] flags - nagios NEB module flags - not used.
[in] reason - nagios reason code - not used.
Returns:
Always returns zero.

Definition at line 1213 of file dnxNebMain.c.

References dnxLog(), dnxServerDeInit(), releaseConfig(), and xheapchk.

int nebmodule_init ( int  flags,
char *  args,
nebmodule *  handle 
)

The main NEB module initialization routine.

This function gets called when the module is loaded by the event broker.

Parameters:
[in] flags - module flags - not used
[in] args - module arguments. These come from the nagios configuration file, and are passed through to the module as it loads.
[in] handle - our module handle - passed from the OS to nagios as nagios loaded us.
Returns:
Zero on success, or a non-zero error value.

Definition at line 1239 of file dnxNebMain.c.

References DnxServerCfg::agentUrl, DnxServerCfg::auditFilePath, DnxServerCfg::collectorUrl, DnxServerCfg::debugFilePath, DnxServerCfg::debugLevel, DnxServerCfg::dispatcherUrl, DNX_DEFAULT_SERVER_CONFIG_FILE, dnxErrorString(), dnxLog(), dnxLogInit(), ehProcessData(), initConfig(), DnxServerCfg::logFilePath, myHandle, neb_register_callback(), releaseConfig(), and VERSION.

static void releaseConfig ( void   )  [static]

Cleanup the config file parser.

Definition at line 152 of file dnxNebMain.c.

References dnxCfgParserDestroy(), DnxServerCfg::localCheckPattern, and regEx.

static int validateCfg ( DnxCfgDict dict,
void **  vptrs,
void *  passthru 
) [static]

Validate a configuration data structure in context.

Parameters:
[in] dict - the dictionary used by the DnxCfgParser.
[in] vptrs - an array of opaque objects (either pointers or values) to be checked.
[in] passthru - an opaque pointer passed through from dnxCfgParserCreate. In this routine, it's the regex_t object into which we should parse the regular expression if one is given.
Returns:
Zero on success, or a non-zero error value. This error value is passed back through dnxCfgParserParse.

Definition at line 174 of file dnxNebMain.c.

References DnxServerCfg::agentUrl, DnxServerCfg::auditFilePath, DnxServerCfg::authWorkerNodes, DnxServerCfg::collectorUrl, DnxServerCfg::debugFilePath, DnxServerCfg::debugLevel, DnxServerCfg::dispatcherUrl, DNX_ERR_INVALID, dnxLog(), DnxServerCfg::expirePollInterval, DnxServerCfg::localCheckPattern, DnxServerCfg::logFilePath, DnxServerCfg::maxNodeRequests, DnxServerCfg::minServiceSlots, and DnxServerCfg::syncScript.

char* versionText ( void   ) 

Format version text to an allocated string buffer.

Caller is responsible to free memory buffer returned, using xfree.

Returns:
An allocated string buffer containing version text.

Definition at line 1155 of file dnxNebMain.c.

References DNX_DEFAULT_DBGLOG, DNX_DEFAULT_LOG, DNX_DEFAULT_SERVER_CONFIG_FILE, PACKAGE_BUGREPORT, s_progname, VERSION, versionText(), and xstrdup.


Variable Documentation

DnxChannel* agent [static]

The agent comm channel.

Definition at line 142 of file dnxNebMain.c.

pthread_t agentTid [static]

The agent thread id.

Definition at line 143 of file dnxNebMain.c.

DnxServerCfg cfg [static]

The server configuration parameters.

Definition at line 134 of file dnxNebMain.c.

The job list results collector.

Definition at line 139 of file dnxNebMain.c.

The job list dispatcher.

Definition at line 138 of file dnxNebMain.c.

DnxJobList* joblist [static]

The master job list.

Definition at line 136 of file dnxNebMain.c.

void* myHandle [static]

Private NEB module handle.

Definition at line 144 of file dnxNebMain.c.

DnxCfgParser* parser [static]

The system configuration parser.

Definition at line 135 of file dnxNebMain.c.

regex_t regEx [static]

Compiled regular expression structure.

Definition at line 145 of file dnxNebMain.c.

The client node registrar.

Definition at line 137 of file dnxNebMain.c.

int s_shutdown [static]

The agent shutdown flag.

Definition at line 141 of file dnxNebMain.c.

time_t start_time [static]

The module start time.

Definition at line 140 of file dnxNebMain.c.


Generated on Tue Apr 13 15:48:08 2010 for DNX by  doxygen 1.5.6