summaryrefslogtreecommitdiffstats
path: root/contrib/nrpe_check_control.c
blob: 04fa9e0f31bd242e329cb7d3cadb19a1c6f046d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <stdio.h>
#include <string.h>
#include <time.h>

#define MAX_CHARS	1024
#define SERVICE_COUNT	12

#define COMMAND_FILE	"/usr/local/nagios/var/rw/nagios.cmd"
#define SERVICES_FILE	"/usr/local/nagios/etc/services.cfg"

int main(int argc, char *argv[])
{
  char check_name[MAX_CHARS];
  char ent_type[MAX_CHARS];
  char input_buffer[MAX_CHARS];
  char host_name[MAX_CHARS];
  char service_name[MAX_CHARS];
  char state[MAX_CHARS];
  char state_type[MAX_CHARS];
  char temp_input[MAX_CHARS];
  char temp_string[MAX_CHARS];
  char test_host[MAX_CHARS];

  char *temp_var;

  FILE *command_fp;
  FILE *services_fp;

  int attempt;
  int i;

  time_t current_time;

  strcpy(state,argv[1]);
  strcpy(state_type,argv[2]);
  attempt=atoi(argv[3]);
  strcpy(host_name,argv[4]);

  if(strcmp(state,"OK") == 0)
  {
    services_fp=fopen(SERVICES_FILE,"r");
    command_fp=fopen(COMMAND_FILE,"a");
    while((fgets(input_buffer,MAX_CHARS-1,services_fp)) != NULL)
    {
      if(input_buffer[0]=='#' || input_buffer[0]=='\x0' || input_buffer[0]=='\n' || input_buffer[0]=='\r')
      {
        continue;
      }
      else
      {
        strcpy(temp_input,input_buffer);
        strcpy(temp_string,strtok(temp_input,"="));
        strcpy(ent_type,strtok(temp_string,"["));
        if(strcmp(ent_type,"service") == 0)
        {
          strcpy(test_host,strtok(NULL,"]"));
          if(strcmp(test_host,host_name) == 0)
          {
            temp_var=strtok(input_buffer,"=");
            strcpy(service_name,strtok(NULL,";")); 
            for(i=1;i<=SERVICE_COUNT;i++)
            {
              temp_var=strtok(NULL,";");
            }
            strcpy(check_name,strtok(temp_var,"!"));
            if(strcmp(check_name,"check_nrpe") == 0)
            {
              time(&current_time);
              fprintf(command_fp,"[%lu] ENABLE_SVC_CHECK;%s;%s\n",current_time,host_name,service_name);
            }
          }
        }
      }
    }
    fclose(command_fp);
    fclose(services_fp);
  }
  else if(strcmp(state,"CRITICAL") == 0)
  {
    if(attempt == 3)
    {
      services_fp=fopen(SERVICES_FILE,"r");
      command_fp=fopen(COMMAND_FILE,"a");
      while((fgets(input_buffer,MAX_CHARS-1,services_fp)) != NULL)
      {
        if(input_buffer[0]=='#' || input_buffer[0]=='\x0' || input_buffer[0]=='\n' || input_buffer[0]=='\r')
        {
          continue;
        }
        else
        {
          strcpy(temp_input,input_buffer);
          strcpy(temp_string,strtok(temp_input,"="));
          strcpy(ent_type,strtok(temp_string,"["));
          if(strcmp(ent_type,"service") == 0)
          {
            strcpy(test_host,strtok(NULL,"]"));
            if(strcmp(test_host,host_name) == 0)
            {
              temp_var=strtok(input_buffer,"=");
              strcpy(service_name,strtok(NULL,";")); 
              for(i=1;i<=SERVICE_COUNT;i++)
              {
                temp_var=strtok(NULL,";");
              }
              strcpy(check_name,strtok(temp_var,"!"));
              if(strcmp(check_name,"check_nrpe") == 0)
              {
                time(&current_time);
                fprintf(command_fp,"[%lu] DISABLE_SVC_CHECK;%s;%s\n",current_time,host_name,service_name);
              }
            }
          }
        }
      }
      fclose(command_fp);
      fclose(services_fp);
    }
  }
  return 0;
}