Icinga-core 1.4.0
next gen monitoring
module/helloworld.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002  *
00003  * HELLOWORLD.C - Example of a simple NEB module
00004  *
00005  * Copyright (c) 1999-2009 Ethan Galstad (egalstad@nagios.org)
00006  * Copyright (c) 2009-2011 Icinga Development Team (http://www.icinga.org)
00007  *
00008  * Description:
00009  *
00010  * This is an example of a very basic module.  It does nothing useful other
00011  * than logging some messages to the main Icinga log file when it is initialized
00012  * (loaded), when it is closed (unloaded), and when aggregated status updates
00013  * occur.  I would not call that too useful, but hopefully it will serve as a
00014  * very basic example of how to write a NEB module...
00015  *
00016  * Instructions:
00017  * 
00018  * Compile with the following command:
00019  *
00020  *     gcc -shared -o helloworld.o helloworld.c
00021  *
00022  *****************************************************************************/
00023 
00024 /* include (minimum required) event broker header files */
00025 #include "../include/nebmodules.h"
00026 #include "../include/nebcallbacks.h"
00027 
00028 /* include other event broker header files that we need for our work */
00029 #include "../include/nebstructs.h"
00030 #include "../include/broker.h"
00031 
00032 /* include some Icinga stuff as well */
00033 #include "../include/config.h"
00034 #include "../include/common.h"
00035 #include "../include/icinga.h"
00036 
00037 /* specify event broker API version (required) */
00038 NEB_API_VERSION(CURRENT_NEB_API_VERSION);
00039 
00040 
00041 void *helloworld_module_handle=NULL;
00042 
00043 void helloworld_reminder_message(char *);
00044 int helloworld_handle_data(int,void *);
00045 
00046 
00047 /* this function gets called when the module is loaded by the event broker */
00048 int nebmodule_init(int flags, char *args, nebmodule *handle){
00049         char temp_buffer[1024];
00050         time_t current_time;
00051         unsigned long interval;
00052 
00053         /* save our handle */
00054         helloworld_module_handle=handle;
00055 
00056         /* set some info - this is completely optional, as Icinga doesn't do anything with this data */
00057         neb_set_module_info(helloworld_module_handle,NEBMODULE_MODINFO_TITLE,"helloworld");
00058         neb_set_module_info(helloworld_module_handle,NEBMODULE_MODINFO_AUTHOR,"Ethan Galstad");
00059         neb_set_module_info(helloworld_module_handle,NEBMODULE_MODINFO_TITLE,"Copyright (c) 2003-2007 Ethan Galstad");
00060         neb_set_module_info(helloworld_module_handle,NEBMODULE_MODINFO_VERSION,"noversion");
00061         neb_set_module_info(helloworld_module_handle,NEBMODULE_MODINFO_LICENSE,"GPL v2");
00062         neb_set_module_info(helloworld_module_handle,NEBMODULE_MODINFO_DESC,"A simple example to get you started with Icinga Event Broker modules.");
00063 
00064         /* log module info to the Icinga log file */
00065         write_to_all_logs("helloworld: Copyright (c) 2003-2007 Ethan Galstad (egalstad@nagios.org)",NSLOG_INFO_MESSAGE);
00066         
00067         /* log a message to the Icinga log file */
00068         snprintf(temp_buffer,sizeof(temp_buffer)-1,"helloworld: Hello world!\n");
00069         temp_buffer[sizeof(temp_buffer)-1]='\x0';
00070         write_to_all_logs(temp_buffer,NSLOG_INFO_MESSAGE);
00071 
00072         /* log a reminder message every 15 minutes (how's that for annoying? :-)) */
00073         time(&current_time);
00074         interval=900;
00075         schedule_new_event(EVENT_USER_FUNCTION,TRUE,current_time+interval,TRUE,interval,NULL,TRUE,(void *)helloworld_reminder_message,"How about you?",0);
00076 
00077         /* register to be notified of certain events... */
00078         neb_register_callback(NEBCALLBACK_AGGREGATED_STATUS_DATA,helloworld_module_handle,0,helloworld_handle_data);
00079 
00080         return 0;
00081         }
00082 
00083 
00084 /* this function gets called when the module is unloaded by the event broker */
00085 int nebmodule_deinit(int flags, int reason){
00086         char temp_buffer[1024];
00087 
00088         /* deregister for all events we previously registered for... */
00089         neb_deregister_callback(NEBCALLBACK_AGGREGATED_STATUS_DATA,helloworld_handle_data);
00090 
00091         /* log a message to the Icinga log file */
00092         snprintf(temp_buffer,sizeof(temp_buffer)-1,"helloworld: Goodbye world!\n");
00093         temp_buffer[sizeof(temp_buffer)-1]='\x0';
00094         write_to_all_logs(temp_buffer,NSLOG_INFO_MESSAGE);
00095 
00096         return 0;
00097         }
00098 
00099 
00100 /* gets called every X minutes by an event in the scheduling queue */
00101 void helloworld_reminder_message(char *message){
00102         char temp_buffer[1024];
00103         
00104         /* log a message to the Icinga log file */
00105         snprintf(temp_buffer,sizeof(temp_buffer)-1,"helloworld: I'm still here! %s",message);
00106         temp_buffer[sizeof(temp_buffer)-1]='\x0';
00107         write_to_all_logs(temp_buffer,NSLOG_INFO_MESSAGE);
00108 
00109         return;
00110         }
00111 
00112 
00113 /* handle data from Icinga daemon */
00114 int helloworld_handle_data(int event_type, void *data){
00115         nebstruct_aggregated_status_data *agsdata=NULL;
00116         char temp_buffer[1024];
00117 
00118         /* what type of event/data do we have? */
00119         switch(event_type){
00120 
00121         case NEBCALLBACK_AGGREGATED_STATUS_DATA:
00122 
00123                 /* an aggregated status data dump just started or ended... */
00124                 if((agsdata=(nebstruct_aggregated_status_data *)data)){
00125 
00126                         /* log a message to the Icinga log file */
00127                         snprintf(temp_buffer,sizeof(temp_buffer)-1,"helloworld: An aggregated status update just %s.",(agsdata->type==NEBTYPE_AGGREGATEDSTATUS_STARTDUMP)?"started":"finished");
00128                         temp_buffer[sizeof(temp_buffer)-1]='\x0';
00129                         write_to_all_logs(temp_buffer,NSLOG_INFO_MESSAGE);
00130                         }
00131 
00132                 break;
00133 
00134         default:
00135                 break;
00136                 }
00137 
00138         return 0;
00139         }
00140 
 All Data Structures Files Functions Variables Typedefs Defines