Back to index

nagios-nrpe  2.13
nrpe_check_control.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <time.h>
00004 
00005 #define MAX_CHARS    1024
00006 #define SERVICE_COUNT       12
00007 
00008 #define COMMAND_FILE "/usr/local/nagios/var/rw/nagios.cmd"
00009 #define SERVICES_FILE       "/usr/local/nagios/etc/services.cfg"
00010 
00011 int main(int argc, char *argv[])
00012 {
00013   char check_name[MAX_CHARS];
00014   char ent_type[MAX_CHARS];
00015   char input_buffer[MAX_CHARS];
00016   char host_name[MAX_CHARS];
00017   char service_name[MAX_CHARS];
00018   char state[MAX_CHARS];
00019   char state_type[MAX_CHARS];
00020   char temp_input[MAX_CHARS];
00021   char temp_string[MAX_CHARS];
00022   char test_host[MAX_CHARS];
00023 
00024   char *temp_var;
00025 
00026   FILE *command_fp;
00027   FILE *services_fp;
00028 
00029   int attempt;
00030   int i;
00031 
00032   time_t current_time;
00033 
00034   strcpy(state,argv[1]);
00035   strcpy(state_type,argv[2]);
00036   attempt=atoi(argv[3]);
00037   strcpy(host_name,argv[4]);
00038 
00039   if(strcmp(state,"OK") == 0)
00040   {
00041     services_fp=fopen(SERVICES_FILE,"r");
00042     command_fp=fopen(COMMAND_FILE,"a");
00043     while((fgets(input_buffer,MAX_CHARS-1,services_fp)) != NULL)
00044     {
00045       if(input_buffer[0]=='#' || input_buffer[0]=='\x0' || input_buffer[0]=='\n' || input_buffer[0]=='\r')
00046       {
00047         continue;
00048       }
00049       else
00050       {
00051         strcpy(temp_input,input_buffer);
00052         strcpy(temp_string,strtok(temp_input,"="));
00053         strcpy(ent_type,strtok(temp_string,"["));
00054         if(strcmp(ent_type,"service") == 0)
00055         {
00056           strcpy(test_host,strtok(NULL,"]"));
00057           if(strcmp(test_host,host_name) == 0)
00058           {
00059             temp_var=strtok(input_buffer,"=");
00060             strcpy(service_name,strtok(NULL,";")); 
00061             for(i=1;i<=SERVICE_COUNT;i++)
00062             {
00063               temp_var=strtok(NULL,";");
00064             }
00065             strcpy(check_name,strtok(temp_var,"!"));
00066             if(strcmp(check_name,"check_nrpe") == 0)
00067             {
00068               time(&current_time);
00069               fprintf(command_fp,"[%lu] ENABLE_SVC_CHECK;%s;%s\n",current_time,host_name,service_name);
00070             }
00071           }
00072         }
00073       }
00074     }
00075     fclose(command_fp);
00076     fclose(services_fp);
00077   }
00078   else if(strcmp(state,"CRITICAL") == 0)
00079   {
00080     if(attempt == 3)
00081     {
00082       services_fp=fopen(SERVICES_FILE,"r");
00083       command_fp=fopen(COMMAND_FILE,"a");
00084       while((fgets(input_buffer,MAX_CHARS-1,services_fp)) != NULL)
00085       {
00086         if(input_buffer[0]=='#' || input_buffer[0]=='\x0' || input_buffer[0]=='\n' || input_buffer[0]=='\r')
00087         {
00088           continue;
00089         }
00090         else
00091         {
00092           strcpy(temp_input,input_buffer);
00093           strcpy(temp_string,strtok(temp_input,"="));
00094           strcpy(ent_type,strtok(temp_string,"["));
00095           if(strcmp(ent_type,"service") == 0)
00096           {
00097             strcpy(test_host,strtok(NULL,"]"));
00098             if(strcmp(test_host,host_name) == 0)
00099             {
00100               temp_var=strtok(input_buffer,"=");
00101               strcpy(service_name,strtok(NULL,";")); 
00102               for(i=1;i<=SERVICE_COUNT;i++)
00103               {
00104                 temp_var=strtok(NULL,";");
00105               }
00106               strcpy(check_name,strtok(temp_var,"!"));
00107               if(strcmp(check_name,"check_nrpe") == 0)
00108               {
00109                 time(&current_time);
00110                 fprintf(command_fp,"[%lu] DISABLE_SVC_CHECK;%s;%s\n",current_time,host_name,service_name);
00111               }
00112             }
00113           }
00114         }
00115       }
00116       fclose(command_fp);
00117       fclose(services_fp);
00118     }
00119   }
00120   return 0;
00121 }