#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 DnxCfgParser * | parser |
The system configuration parser. | |
static DnxJobList * | joblist |
The master job list. | |
static DnxRegistrar * | registrar |
The client node registrar. | |
static DnxDispatcher * | dispatcher |
The job list dispatcher. | |
static DnxCollector * | collector |
The job list results collector. | |
static time_t | start_time |
The module start time. | |
static int | s_shutdown |
The agent shutdown flag. | |
static DnxChannel * | agent |
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. |
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).
Definition in file dnxNebMain.c.
#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 | ( | x | ) | (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.
int dnxAuditJob | ( | DnxNewJob * | pJob, | |
char * | action | |||
) |
Send an audit message to the dnx server audit log.
[in] | pJob | - the job to be audited. |
[in] | action | - the audit action that we're 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.
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.
[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.
[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. |
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
.
[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. |
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] |
Deinitialize the dnx server.
Definition at line 974 of file dnxNebMain.c.
References dnxChanMapRelease(), dnxCollectorDestroy(), dnxDispatcherDestroy(), dnxJobListDestroy(), dnxRegistrarDestroy(), dnxReleaseAgent(), dnxStatsCleanup(), ehProcessData(), ehSvcCheck(), and neb_deregister_callback().
static int dnxServerInit | ( | void | ) | [static] |
Initialize the dnxServer.
Definition at line 1013 of file dnxNebMain.c.
References DnxServerCfg::agentUrl, DnxServerCfg::collectorUrl, DnxServerCfg::dispatcherUrl, dnxCalculateJobListSize(), dnxChanMapInit(), dnxCollectorCreate(), dnxDispatcherCreate(), dnxDispatcherGetChannel(), dnxErrorString(), dnxInitAgent(), dnxJobListCreate(), dnxLog(), dnxRegistrarCreate(), ehSvcCheck(), myHandle, and neb_register_callback().
static int ehProcessData | ( | int | event_type, | |
void * | data | |||
) | [static] |
Process Data Event Handler.
[in] | event_type | - the event regarding which we were called by Nagios. |
[in] | data | - an opaque pointer to an event-specific data structure. |
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.
[in] | event_type | - the event type for which we're being called. |
[in] | data | - an opaque pointer to nagios event-specific data. |
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] |
Read and parse the dnxServer configuration file.
[in] | cfgfile | - the configuration file to be read. |
Definition at line 233 of file dnxNebMain.c.
References DnxServerCfg::agentUrl, DnxServerCfg::auditFilePath, DnxServerCfg::authWorkerNodes, DnxServerCfg::collectorUrl, DnxServerCfg::debugFilePath, DnxServerCfg::debugLevel, DnxServerCfg::dispatcherUrl, DNX_CFG_FSPATH, DNX_CFG_STRING, DNX_CFG_UNSIGNED, DNX_CFG_URL, DNX_DEFAULT_DBGLOG, DNX_DEFAULT_LOG, dnxCfgParserCreate(), dnxCfgParserDestroy(), dnxCfgParserParse(), DnxServerCfg::expirePollInterval, DnxServerCfg::localCheckPattern, DnxServerCfg::logFilePath, DnxServerCfg::maxNodeRequests, DnxServerCfg::minServiceSlots, regEx, DnxServerCfg::syncScript, and validateCfg().
static int launchScript | ( | char * | script | ) | [static] |
Launches an external command and waits for it to return a status code.
[in] | script | - the command line to be launched. |
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.
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.
[in] | flags | - nagios NEB module flags - not used. |
[in] | reason | - nagios reason code - not used. |
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.
[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. |
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.
[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. |
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.
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.
DnxChannel* agent [static] |
pthread_t agentTid [static] |
DnxServerCfg cfg [static] |
DnxCollector* collector [static] |
DnxDispatcher* dispatcher [static] |
DnxJobList* joblist [static] |
void* myHandle [static] |
DnxCfgParser* parser [static] |
regex_t regEx [static] |
DnxRegistrar* registrar [static] |
int s_shutdown [static] |
time_t start_time [static] |