Stop being lazy and write
an event broker module already
           Dave Josephsen

             dave@dbg.com
WHY?!


  The NEB interface is AWESOME
  Interpreted automata is expensive
        Especially via global event handlers and/or
         performance-data hooks
  You're more likely to make something I can use
  too!




                          2012                        2
How we're going to do this

  Function pointers (and YOU)
  What is the event broker?
  How do we use it?
  A few words of advice
  Dissect a couple existing plugins
        Nagfs
        DNX




                          2012        3
Function Pointers are weird, but they make
callbacks possible

                                                                  Normal Function Declarations
  void main() {

      int c2f ( int c ) { return (9/5) *c+32; }
      int f2c ( int f ) { return (5/9) *(f-32); }
                                                               Argument

      int convert (int input, int (*fPointer)(int)) {
        return fPointer(input);

      }
                                                                  wha??
      void Go() {

          int result=convert('72',&f2c);

      }
                                                    Oh.. ok.
  }




                                                       2012                                      4
What is the event broker?

                                      Event Broker added
           Nagios Event loop
           (kinda)




                               2012                    5
How do we use it?
 Write an init function
       int nebmodule_init( int flags, char * args, nebmodule * handle)
 Subscribe to the events you want
       neb_register_callback(STUFF, handle, '1', &myhandler);
 Write handler functions for each event you
 subscribed to
       int myhandler(int neb_event_type, nebstruct_stuff_data *ds)
 Write a de-init function
       int nebmodule_deinit(int flags, int reason){ return 0 }




                                2012                                 6
How do we use it? (cont)
 include/nebcallbacks.h
NEBCALLBACK_RAW_DATA
NEBCALLBACK_NEB_DATA
NEBCALLBACK_PROCESS_DATA
NEBCALLBACK_TIMED_EVENT_DATA            neb_register_callback(NEBCALLBACK_TIMED_EVENT_DATA,           handle, '1', &handler);
NEBCALLBACK_LOG_DATA
NEBCALLBACK_SYSTEM_COMMAND_DATA
NEBCALLBACK_EVENT_HANDLER_DATA
NEBCALLBACK_NOTIFICATION_DATA
NEBCALLBACK_SERVICE_CHECK_DATA                                  base/broker.c
NEBCALLBACK_HOST_CHECK_DATA
NEBCALLBACK_COMMENT_DATA                           /* fill struct with relevant data */
NEBCALLBACK_DOWNTIME_DATA
NEBCALLBACK_FLAPPING_DATA                             ds.type=type;
NEBCALLBACK_PROGRAM_STATUS_DATA                       ds.flags=flags;
NEBCALLBACK_HOST_STATUS_DATA                          ds.attr=attr;
NEBCALLBACK_SERVICE_STATUS_DATA
NEBCALLBACK_ADAPTIVE_PROGRAM_DATA                     ds.timestamp=get_broker_timestamp(timestamp);
NEBCALLBACK_ADAPTIVE_HOST_DATA
NEBCALLBACK_ADAPTIVE_SERVICE_DATA                     ds.event_type=event->event_type;
NEBCALLBACK_EXTERNAL_COMMAND_DATA
NEBCALLBACK_AGGREGATED_STATUS_DATA                    ds.recurring=event->recurring;
NEBCALLBACK_RETENTION_DATA                            ds.run_time=event->run_time;
NEBCALLBACK_CONTACT_NOTIFICATION_DATA                 ds.event_data=event->event_data;
NEBCALLBACK_CONTACT_NOTIFICATION_METHOD_DATA
NEBCALLBACK_ACKNOWLEDGEMENT_DATA                      ds.event_ptr=(void *)event;
NEBCALLBACK_STATE_CHANGE_DATA
NEBCALLBACK_CONTACT_STATUS_DATA                       /* make callbacks */
NEBCALLBACK_ADAPTIVE_CONTACT_DATA
                                                 neb_make_callbacks(NEBCALLBACK_TIMED_EVENT_DATA,(void *)&ds);


  include/broker.h
#define NEBTYPE_TIMEDEVENT_ADD
#define NEBTYPE_TIMEDEVENT_REMOVE
#define NEBTYPE_TIMEDEVENT_EXECUTE               int handler(int neb_event_type, nebstruct_timed_event_data *ds)
#define NEBTYPE_TIMEDEVENT_DELAY
#define NEBTYPE_TIMEDEVENT_SKIP
#define NEBTYPE_TIMEDEVENT_SLEEP


                                                         2012                                                             7
How do we use it? (cont)

 Everything is a struct


 Every struct is globally scoped


 The sky is the limit
      enumerate/add/remove events from the queue
      reconfigure hosts/services post-runtime
      export/import data from/to whatever
      process check data, inject/delete check results
      GO CRAZY!
                              2012                      8
Some words of advice


 There's a “hello world” module in contrib
 Write your thingy as a stand-alone thingy first, and
 then convert it to an EH function
 Start simple, and then build up
 Avoid Threads
      (run them in a separate process)
 The DNX guys have pre-packaged the headers for
 various versions of Nagios (zomgthankyou)


                          2012                          9
Nagfs
DNX
●External process IPC
●Service check preemption

●Check result injection
OK BYE!
       http://old.nagios.org/developerinfo/
http://nagios.larsmichelsen.com/event_broker_options/
        http://dnx.sourceforge.net/



             (and speaking of “buy”...)

Nagios Conference 2012 - Dave Josephsen - Stop Being Lazy

  • 1.
    Stop being lazyand write an event broker module already Dave Josephsen dave@dbg.com
  • 2.
    WHY?! TheNEB interface is AWESOME Interpreted automata is expensive Especially via global event handlers and/or performance-data hooks You're more likely to make something I can use too! 2012 2
  • 3.
    How we're goingto do this Function pointers (and YOU) What is the event broker? How do we use it? A few words of advice Dissect a couple existing plugins Nagfs DNX 2012 3
  • 4.
    Function Pointers areweird, but they make callbacks possible Normal Function Declarations void main() { int c2f ( int c ) { return (9/5) *c+32; } int f2c ( int f ) { return (5/9) *(f-32); } Argument int convert (int input, int (*fPointer)(int)) { return fPointer(input); } wha?? void Go() { int result=convert('72',&f2c); } Oh.. ok. } 2012 4
  • 5.
    What is theevent broker? Event Broker added Nagios Event loop (kinda) 2012 5
  • 6.
    How do weuse it? Write an init function int nebmodule_init( int flags, char * args, nebmodule * handle) Subscribe to the events you want neb_register_callback(STUFF, handle, '1', &myhandler); Write handler functions for each event you subscribed to int myhandler(int neb_event_type, nebstruct_stuff_data *ds) Write a de-init function int nebmodule_deinit(int flags, int reason){ return 0 } 2012 6
  • 7.
    How do weuse it? (cont) include/nebcallbacks.h NEBCALLBACK_RAW_DATA NEBCALLBACK_NEB_DATA NEBCALLBACK_PROCESS_DATA NEBCALLBACK_TIMED_EVENT_DATA neb_register_callback(NEBCALLBACK_TIMED_EVENT_DATA, handle, '1', &handler); NEBCALLBACK_LOG_DATA NEBCALLBACK_SYSTEM_COMMAND_DATA NEBCALLBACK_EVENT_HANDLER_DATA NEBCALLBACK_NOTIFICATION_DATA NEBCALLBACK_SERVICE_CHECK_DATA base/broker.c NEBCALLBACK_HOST_CHECK_DATA NEBCALLBACK_COMMENT_DATA /* fill struct with relevant data */ NEBCALLBACK_DOWNTIME_DATA NEBCALLBACK_FLAPPING_DATA ds.type=type; NEBCALLBACK_PROGRAM_STATUS_DATA ds.flags=flags; NEBCALLBACK_HOST_STATUS_DATA ds.attr=attr; NEBCALLBACK_SERVICE_STATUS_DATA NEBCALLBACK_ADAPTIVE_PROGRAM_DATA ds.timestamp=get_broker_timestamp(timestamp); NEBCALLBACK_ADAPTIVE_HOST_DATA NEBCALLBACK_ADAPTIVE_SERVICE_DATA ds.event_type=event->event_type; NEBCALLBACK_EXTERNAL_COMMAND_DATA NEBCALLBACK_AGGREGATED_STATUS_DATA ds.recurring=event->recurring; NEBCALLBACK_RETENTION_DATA ds.run_time=event->run_time; NEBCALLBACK_CONTACT_NOTIFICATION_DATA ds.event_data=event->event_data; NEBCALLBACK_CONTACT_NOTIFICATION_METHOD_DATA NEBCALLBACK_ACKNOWLEDGEMENT_DATA ds.event_ptr=(void *)event; NEBCALLBACK_STATE_CHANGE_DATA NEBCALLBACK_CONTACT_STATUS_DATA /* make callbacks */ NEBCALLBACK_ADAPTIVE_CONTACT_DATA neb_make_callbacks(NEBCALLBACK_TIMED_EVENT_DATA,(void *)&ds); include/broker.h #define NEBTYPE_TIMEDEVENT_ADD #define NEBTYPE_TIMEDEVENT_REMOVE #define NEBTYPE_TIMEDEVENT_EXECUTE int handler(int neb_event_type, nebstruct_timed_event_data *ds) #define NEBTYPE_TIMEDEVENT_DELAY #define NEBTYPE_TIMEDEVENT_SKIP #define NEBTYPE_TIMEDEVENT_SLEEP 2012 7
  • 8.
    How do weuse it? (cont) Everything is a struct Every struct is globally scoped The sky is the limit enumerate/add/remove events from the queue reconfigure hosts/services post-runtime export/import data from/to whatever process check data, inject/delete check results GO CRAZY! 2012 8
  • 9.
    Some words ofadvice There's a “hello world” module in contrib Write your thingy as a stand-alone thingy first, and then convert it to an EH function Start simple, and then build up Avoid Threads (run them in a separate process) The DNX guys have pre-packaged the headers for various versions of Nagios (zomgthankyou) 2012 9
  • 10.
  • 11.
    DNX ●External process IPC ●Servicecheck preemption ●Check result injection
  • 12.
    OK BYE! http://old.nagios.org/developerinfo/ http://nagios.larsmichelsen.com/event_broker_options/ http://dnx.sourceforge.net/ (and speaking of “buy”...)