check_nrpe.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /********************************************************************************************
  2. *
  3. * CHECK_NRPE.C
  4. *
  5. * Program: NRPE plugin for Nagios
  6. * License: GPL
  7. * Copyright (c) 1999-2001 Ethan Galstad (nagios@nagios.org)
  8. *
  9. * Version: 1.3
  10. * Last Modified: 06-23-2001
  11. *
  12. * Command line: CHECK_NRPE <host_address> [-p port] [-c command] [-wt warn_time] \
  13. * [-ct crit_time] [-to to_sec]
  14. *
  15. * Description:
  16. *
  17. * This plugin will attempt to connect to the Nagios remote plugin executor daemon on the
  18. * specified server and port. The daemon will attempt to run the command defined as
  19. * [command]. Program output and return code are sent back from the daemon and displayed
  20. * as this plugin's own output and return code.
  21. *
  22. ********************************************************************************************/
  23. #include "../common/common.h"
  24. #include "../common/config.h"
  25. #include "netutils.h"
  26. #define PROGRAM_VERSION "1.3"
  27. #define MODIFICATION_DATE "06-23-2001"
  28. #define DEFAULT_NRPE_COMMAND "_NRPE_CHECK" /* check version of NRPE daemon */
  29. time_t start_time,end_time;
  30. int server_port=DEFAULT_SERVER_PORT;
  31. char server_name[MAX_HOST_ADDRESS_LENGTH];
  32. char query_string[MAX_PACKETBUFFER_LENGTH]=DEFAULT_NRPE_COMMAND;;
  33. int socket_timeout=DEFAULT_SOCKET_TIMEOUT;
  34. int warning_time=0;
  35. int check_warning_time=FALSE;
  36. int critical_time=0;
  37. int check_critical_time=FALSE;
  38. int process_arguments(int,char **);
  39. void alarm_handler(int);
  40. int main(int argc, char **argv){
  41. int sd;
  42. int rc;
  43. int result;
  44. packet send_packet;
  45. packet receive_packet;
  46. result=process_arguments(argc,argv);
  47. if(result!=OK){
  48. printf("Incorrect command line arguments supplied\n");
  49. printf("\n");
  50. printf("NRPE Plugin for Nagios\n");
  51. printf("Copyright (c) 1999-2001 Ethan Galstad (nagios@nagios.org)\n");
  52. printf("Version: %s\n",PROGRAM_VERSION);
  53. printf("Last Modified: %s\n",MODIFICATION_DATE);
  54. printf("License: GPL\n");
  55. printf("\n");
  56. printf("Usage: %s <host_address> [-p port] [-c command] [-wt warn_time]\n",argv[0]);
  57. printf(" [-ct crit_time] [-to to_sec]\n");
  58. printf("\n");
  59. printf("Options:\n");
  60. printf(" <host_address> = The IP address of the host running the NRPE daemon\n");
  61. printf(" [port] = The port on which the daemon is running - default is %d\n",DEFAULT_SERVER_PORT);
  62. printf(" [command] = The name of the command that the remote daemon should run\n");
  63. printf(" [warn_time] = Response time in seconds necessary to result in a warning\n");
  64. printf(" status\n");
  65. printf(" [crit_time] = Response time in seconds necessary to result in a critical\n");
  66. printf(" status\n");
  67. printf(" [to_sec] = Number of seconds before connection attempt times out.\n");
  68. printf(" Default timeout is %d seconds\n",DEFAULT_SOCKET_TIMEOUT);
  69. printf("\n");
  70. printf("Note:\n");
  71. printf("This plugin requires that you have the NRPE daemon running on the remote host.\n");
  72. printf("You must also have configured the daemon to associate a specific plugin command\n");
  73. printf("with the [command] option you are specifying here. Upon receipt of the\n");
  74. printf("[command] argument, the NRPE daemon will run the appropriate plugin command and\n");
  75. printf("send the plugin output and return code back to *this* plugin. This allows you\n");
  76. printf("to execute plugins on remote hosts and 'fake' the results to make Nagios think\n");
  77. printf("the plugin is being run locally.\n");
  78. printf("\n");
  79. return STATE_UNKNOWN;
  80. }
  81. /* initialize alarm signal handling */
  82. signal(SIGALRM,alarm_handler);
  83. /* set socket timeout */
  84. alarm(socket_timeout);
  85. time(&start_time);
  86. /* try to connect to the host at the given port number */
  87. result=my_tcp_connect(server_name,server_port,&sd);
  88. /* we connected, so close connection before exiting */
  89. if(result==STATE_OK){
  90. /* send the query packet */
  91. bzero(&send_packet,sizeof(send_packet));
  92. send_packet.packet_type=htonl(QUERY_PACKET);
  93. send_packet.packet_version=htonl(NRPE_PACKET_VERSION_1);
  94. send_packet.buffer_length=htonl(strlen(query_string));
  95. strcpy(&send_packet.buffer[0],query_string);
  96. rc=send(sd,(void *)&send_packet,sizeof(send_packet),0);
  97. if(rc==-1){
  98. printf("CHECK_NRPE: Error sending query to host\n");
  99. close(sd);
  100. return STATE_UNKNOWN;
  101. }
  102. /* wait for the response packet */
  103. bzero(&receive_packet,sizeof(receive_packet));
  104. rc=recv(sd,(void *)&receive_packet,sizeof(receive_packet),0);
  105. if(rc==-1){
  106. printf("CHECK_NRPE: Error receiving data from host\n");
  107. close(sd);
  108. return STATE_UNKNOWN;
  109. }
  110. else if(rc==0){
  111. printf("CHECK_NRPE: Received 0 bytes. Is this host authorized to connect with nrpe daemon?\n");
  112. close(sd);
  113. return STATE_UNKNOWN;
  114. }
  115. else if(rc<sizeof(receive_packet)){
  116. printf("CHECK_NRPE: Receive underflow. Only %d bytes received (%d expected).\n",rc,sizeof(receive_packet));
  117. close(sd);
  118. return STATE_UNKNOWN;
  119. }
  120. time(&end_time);
  121. result=STATE_OK;
  122. if(check_critical_time==TRUE && (end_time-start_time)>critical_time)
  123. result=STATE_CRITICAL;
  124. else if(check_warning_time==TRUE && (end_time-start_time)>warning_time)
  125. result=STATE_WARNING;
  126. if(result!=STATE_OK)
  127. printf("CHECK_NRPE problem - %d second response time: ",(int)(end_time-start_time));
  128. /* get the return code from the remote plugin */
  129. result=ntohl(receive_packet.result_code);
  130. /* make sure there is something in the plugin output buffer */
  131. if(!strcmp(receive_packet.buffer,""))
  132. printf("CHECK_NRPE: No output returned from NRPE daemon.\n");
  133. else
  134. printf("%s\n",receive_packet.buffer);
  135. /* close the connection */
  136. close(sd);
  137. }
  138. /* reset the alarm */
  139. alarm(0);
  140. return result;
  141. }
  142. /* process command line arguments */
  143. int process_arguments(int argc, char **argv){
  144. int x;
  145. /* no options were supplied */
  146. if(argc<2)
  147. return ERROR;
  148. /* first option is always the server name/address */
  149. strncpy(server_name,argv[1],sizeof(server_name)-1);
  150. server_name[sizeof(server_name)-1]='\x0';
  151. /* process all remaining arguments */
  152. for(x=3;x<=argc;x++){
  153. if(!strcmp(argv[x-1],"-c")){
  154. if(x<argc){
  155. strncpy(query_string,argv[x],sizeof(query_string)-1);
  156. query_string[sizeof(query_string)-1]='\x0';
  157. x++;
  158. }
  159. else
  160. return ERROR;
  161. }
  162. else if(!strcmp(argv[x-1],"-p")){
  163. if(x<argc){
  164. server_port=atoi(argv[x]);
  165. x++;
  166. }
  167. else
  168. return ERROR;
  169. }
  170. else if(!strcmp(argv[x-1],"-to")){
  171. if(x<argc){
  172. socket_timeout=atoi(argv[x]);
  173. if(socket_timeout<=0)
  174. return ERROR;
  175. x++;
  176. }
  177. else
  178. return ERROR;
  179. }
  180. else if(!strcmp(argv[x-1],"-wt")){
  181. if(x<argc){
  182. warning_time=atoi(argv[x]);
  183. check_warning_time=TRUE;
  184. x++;
  185. }
  186. else
  187. return ERROR;
  188. }
  189. else if(!strcmp(argv[x-1],"-ct")){
  190. if(x<argc){
  191. critical_time=atoi(argv[x]);
  192. check_critical_time=TRUE;
  193. x++;
  194. }
  195. else
  196. return ERROR;
  197. }
  198. else
  199. return ERROR;
  200. }
  201. return OK;
  202. }
  203. void alarm_handler(int sig){
  204. printf("CHECK_NRPE: Socket timeout after %d seconds\n",socket_timeout);
  205. exit(STATE_CRITICAL);
  206. }