check_ipxping.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. *****************************************************************************************/
  29. #include "../common/config.h"
  30. #include "../common/common.h"
  31. #include "netutils.h"
  32. /* this should be moved out to the configure script! */
  33. #define IPXPING_COMMAND "/tmp/ipxping/ipxping"
  34. /* these should be moved to the common header file */
  35. #define MAX_IPXNET_ADDRESS_LENGTH 12
  36. #define MAX_IPXHOST_ADDRESS_LENGTH 18
  37. int socket_timeout=DEFAULT_SOCKET_TIMEOUT;
  38. char dest_network[MAX_IPXNET_ADDRESS_LENGTH];
  39. char dest_address[MAX_IPXHOST_ADDRESS_LENGTH];
  40. int wrtt;
  41. int crtt;
  42. int process_arguments(int,char **);
  43. FILE * spopen(const char *);
  44. int spclose(FILE *);
  45. int main(int argc, char **argv){
  46. char command_line[MAX_INPUT_BUFFER];
  47. int rtt;
  48. int bytes_returned;
  49. int result=STATE_OK;
  50. FILE *fp;
  51. char input_buffer[MAX_INPUT_BUFFER];
  52. char *substr;
  53. int current_line;
  54. if(process_arguments(argc,argv)!=OK){
  55. printf("Incorrect arguments supplied\n");
  56. printf("\n");
  57. printf("IPX ping plugin for Nagios\n");
  58. printf("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n");
  59. printf("Last Modified: 09-24-1999\n");
  60. printf("License: GPL\n");
  61. printf("\n");
  62. printf("Usage: %s <dest_network> <dest_address> <wrtt> <crtt> [-to to_sec]\n",argv[0]);
  63. printf("\n");
  64. printf("Options:\n");
  65. printf(" <dest_network> = IPX network that the remote host lies on. (Hex Format - 00:00:00:00)\n");
  66. printf(" <dest_address> = MAC address of the remote host. (Hex Format - 00:00:00:00:00:00)\n");
  67. printf(" <wrtt> = Round trip time in milliseconds necessary to result in a WARNING state\n");
  68. printf(" <crtt> = Round trip time in milliseconds necessary to result in a CRITICAL state\n");
  69. printf(" [to_sec] = Seconds before we should timeout waiting for ping result. Default = %d sec\n",DEFAULT_SOCKET_TIMEOUT);
  70. printf("\n");
  71. printf("Notes:\n");
  72. printf("This plugin will use the /usr/bin/ipxping command to ping the specified host using\n");
  73. printf("the IPX protocol. IPX support must be compiled into the kernel and your host must\n");
  74. printf("be correctly configured to use IPX before this plugin will work! An RPM package of\n");
  75. printf("the ipxping binary can be found at...\n");
  76. printf("http://www.rpmfind.net/linux/RPM/contrib/libc5/i386/ipxping-0.0-2.i386.shtml\n");
  77. printf("\n");
  78. return STATE_UNKNOWN;
  79. }
  80. /* create the command line to use... */
  81. sprintf(command_line,"%s %s %s",IPXPING_COMMAND,dest_network,dest_address);
  82. /* initialize alarm signal handling */
  83. signal(SIGALRM,socket_timeout_alarm_handler);
  84. /* set socket timeout */
  85. alarm(socket_timeout);
  86. /* run the command */
  87. fp = spopen(command_line);
  88. if(fp==NULL){
  89. printf("Unable to open pipe: %s",command_line);
  90. return STATE_UNKNOWN;
  91. }
  92. current_line=0;
  93. while(fgets(input_buffer,MAX_INPUT_BUFFER-1,fp)){
  94. current_line++;
  95. /* skip the first line of the output */
  96. if(current_line==1)
  97. continue;
  98. /* we didn't get the "is alive" */
  99. if(current_line==2 && !strstr(input_buffer,"is alive"))
  100. result=STATE_CRITICAL;
  101. /* get the round trip time */
  102. if(current_line==3){
  103. substr=strtok(input_buffer,":");
  104. substr=strtok(NULL,"\n");
  105. rtt=atoi(substr);
  106. }
  107. /* get the number of bytes returned */
  108. if(current_line==4 && strstr(input_buffer,"bytes returned")){
  109. bytes_returned=atoi(input_buffer);
  110. }
  111. }
  112. /* close the pipe */
  113. spclose(fp);
  114. /* reset the alarm */
  115. alarm(0);
  116. if(current_line==1 || result==STATE_CRITICAL)
  117. printf("IPX Ping problem - No response from host\n");
  118. else{
  119. if(rtt>crtt)
  120. result=STATE_CRITICAL;
  121. else if(rtt>wrtt)
  122. result=STATE_WARNING;
  123. 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);
  124. }
  125. return result;
  126. }
  127. /* process all arguments passed on the command line */
  128. int process_arguments(int argc, char **argv){
  129. int x;
  130. /* no options were supplied */
  131. if(argc<5)
  132. return ERROR;
  133. /* get the destination network address */
  134. strncpy(dest_network,argv[1],sizeof(dest_network)-1);
  135. dest_network[sizeof(dest_network)-1]='\x0';
  136. /* get the destination host address */
  137. strncpy(dest_address,argv[2],sizeof(dest_address)-1);
  138. dest_address[sizeof(dest_address)-1]='\x0';
  139. /* get the round trip time variables */
  140. wrtt=atoi(argv[3]);
  141. crtt=atoi(argv[4]);
  142. /* process remaining arguments */
  143. for(x=6;x<=argc;x++){
  144. /* we got the timeout to use */
  145. if(!strcmp(argv[x-1],"-to")){
  146. if(x<argc){
  147. socket_timeout=atoi(argv[x]);
  148. if(socket_timeout<=0)
  149. return ERROR;
  150. x++;
  151. }
  152. else
  153. return ERROR;
  154. }
  155. /* else we got something else... */
  156. else
  157. return ERROR;
  158. }
  159. return OK;
  160. }