Code

git-notify: Optionally omit the author name
[nagiosplug.git] / contrib / check_ipxping.c
1 /******************************************************************************************
2  *
3  * CHECK_IPXPING.C
4  *
5  * Program: IPX ping plugin for Nagios
6  * License: GPL
7  * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
8  *
9  * Last Modified: 09-24-1999
10  *
11  * Command line: CHECK_IPXPING <dest_network> <dest_address> <wrtt> <crtt>
12  *
13  * Description:
14  *
15  * This plugin will use the /usr/bin/ipxping command to ping the specified host using the
16  * IPX protocol.  Note: Linux users must have IPX support compiled into the kernerl and
17  * must have IPX configured correctly in order for this plugin to work.
18  * If the round trip time value is above the <wrtt> level, a STATE_WARNING is
19  * returned.  If it exceeds the <crtt> level, a STATE_CRITICAL is returned.
20  *
21  *
22  *
23  * IMPORTANT!!
24  *
25  * This plugin will only work with the ipxping command that has been ported to Linux.
26  * The version for Sun takes different command line arguments and differs in its output.
27  *
28  *****************************************************************************************/
30 #include "config.h"
31 #include "common.h"
32 #include "netutils.h"
33 #include "popen.h"
35 /* this should be moved out to the configure script! */
36 #define IPXPING_COMMAND "/tmp/ipxping/ipxping"
38 /* these should be moved to the common header file */
39 #define MAX_IPXNET_ADDRESS_LENGTH       12
40 #define MAX_IPXHOST_ADDRESS_LENGTH      18
42 int socket_timeout=DEFAULT_SOCKET_TIMEOUT;
43 char dest_network[MAX_IPXNET_ADDRESS_LENGTH];
44 char dest_address[MAX_IPXHOST_ADDRESS_LENGTH];
45 int wrtt;
46 int crtt;
48 int process_arguments(int,char **);
50 FILE * spopen(const char *);
51 int spclose(FILE *);
53 int main(int argc, char **argv){
54         char command_line[MAX_INPUT_BUFFER];
55         int rtt;
56         int bytes_returned;
57         int result=STATE_OK;
58         FILE *fp;
59         char input_buffer[MAX_INPUT_BUFFER];
60         char *substr;
61         int current_line;
63         if(process_arguments(argc,argv)!=OK){
64                 printf("Incorrect arguments supplied\n");
65                 printf("\n");
66                 printf("IPX ping plugin for Nagios\n");
67                 printf("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n");
68                 printf("Last Modified: 09-24-1999\n");
69                 printf("License: GPL\n");
70                 printf("\n");
71                 printf("Usage: %s <dest_network> <dest_address> <wrtt> <crtt> [-to to_sec]\n",argv[0]);
72                 printf("\n");
73                 printf("Options:\n");
74                 printf(" <dest_network> = IPX network that the remote host lies on.  (Hex Format - 00:00:00:00)\n");
75                 printf(" <dest_address> = MAC address of the remote host.  (Hex Format - 00:00:00:00:00:00)\n");
76                 printf(" <wrtt>         = Round trip time in milliseconds necessary to result in a WARNING state\n");
77                 printf(" <crtt>         = Round trip time in milliseconds necessary to result in a CRITICAL state\n");
78                 printf(" [to_sec]       = Seconds before we should timeout waiting for ping result.  Default = %d sec\n",DEFAULT_SOCKET_TIMEOUT);
79                 printf("\n");
80                 printf("Notes:\n");
81                 printf("This plugin will use the /usr/bin/ipxping command to ping the specified host using\n");
82                 printf("the IPX protocol.  IPX support must be compiled into the kernel and your host must\n");
83                 printf("be correctly configured to use IPX before this plugin will work! An RPM package of\n");
84                 printf("the ipxping binary can be found at...\n");
85                 printf("http://www.rpmfind.net/linux/RPM/contrib/libc5/i386/ipxping-0.0-2.i386.shtml\n");
86                 printf("\n");
87                 return STATE_UNKNOWN;
88                 }
89   
90         /* create the command line to use... */
91         sprintf(command_line,"%s %s %s",IPXPING_COMMAND,dest_network,dest_address);
93         /* initialize alarm signal handling */
94         signal(SIGALRM,socket_timeout_alarm_handler);
96         /* set socket timeout */
97         alarm(socket_timeout);
99         /* run the command */
100         fp = spopen(command_line);
101         if(fp==NULL){
102                 printf("Unable to open pipe: %s",command_line);
103                 return STATE_UNKNOWN;
104                 }
106         current_line=0;
107         while(fgets(input_buffer,MAX_INPUT_BUFFER-1,fp)){
109                 current_line++;
111                 /* skip the first line of the output */
112                 if(current_line==1)
113                         continue;
115                 /* we didn't get the "is alive" */
116                 if(current_line==2 && !strstr(input_buffer,"is alive"))
117                         result=STATE_CRITICAL;
119                 /* get the round trip time */
120                 if(current_line==3){
121                         substr=strtok(input_buffer,":");
122                         substr=strtok(NULL,"\n");
123                         rtt=atoi(substr);
124                         }
126                 /* get the number of bytes returned */
127                 if(current_line==4 && strstr(input_buffer,"bytes returned")){
128                         bytes_returned=atoi(input_buffer);
129                         }
130                 }
132         /* close the pipe */
133         spclose(fp);
135         /* reset the alarm */
136         alarm(0);
138         if(current_line==1 || result==STATE_CRITICAL)
139                 printf("IPX Ping problem - No response from host\n");
140         else{
142                 if(rtt>crtt)
143                         result=STATE_CRITICAL;
144                 else if(rtt>wrtt)
145                         result=STATE_WARNING;
147                 printf("IPX Ping %s - RTT = %d ms, %d bytes returned from %s %s\n",(result==STATE_OK)?"ok":"problem",rtt,bytes_returned,dest_network,dest_address);
148                 }
149         
151         return result;
152         }
156 /* process all arguments passed on the command line */
157 int process_arguments(int argc, char **argv){
158         int x;
160         /* no options were supplied */
161         if(argc<5)
162                 return ERROR;
164         /* get the destination network address */
165         strncpy(dest_network,argv[1],sizeof(dest_network)-1);
166         dest_network[sizeof(dest_network)-1]='\x0';
168         /* get the destination host address */
169         strncpy(dest_address,argv[2],sizeof(dest_address)-1);
170         dest_address[sizeof(dest_address)-1]='\x0';
172         /* get the round trip time variables */
173         wrtt=atoi(argv[3]);
174         crtt=atoi(argv[4]);
176         /* process remaining arguments */
177         for(x=6;x<=argc;x++){
179                 /* we got the timeout to use */
180                 if(!strcmp(argv[x-1],"-to")){
181                         if(x<argc){
182                                 socket_timeout=atoi(argv[x]);
183                                 if(socket_timeout<=0)
184                                         return ERROR;
185                                 x++;
186                                 }
187                         else
188                                 return ERROR;
189                         }
191                 /* else we got something else... */
192                 else
193                         return ERROR;
194                 }
196         return OK;
197         }