| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- /********************************************************************************************
- *
- * CHECK_NRPE.C
- *
- * Program: NRPE plugin for Nagios
- * License: GPL
- * Copyright (c) 1999-2001 Ethan Galstad (nagios@nagios.org)
- *
- * Version: 1.3
- * Last Modified: 06-23-2001
- *
- * Command line: CHECK_NRPE <host_address> [-p port] [-c command] [-wt warn_time] \
- * [-ct crit_time] [-to to_sec]
- *
- * Description:
- *
- * This plugin will attempt to connect to the Nagios remote plugin executor daemon on the
- * specified server and port. The daemon will attempt to run the command defined as
- * [command]. Program output and return code are sent back from the daemon and displayed
- * as this plugin's own output and return code.
- *
- ********************************************************************************************/
- #include "../common/common.h"
- #include "../common/config.h"
- #include "netutils.h"
- #define PROGRAM_VERSION "1.3"
- #define MODIFICATION_DATE "06-23-2001"
- #define DEFAULT_NRPE_COMMAND "_NRPE_CHECK" /* check version of NRPE daemon */
- time_t start_time,end_time;
- int server_port=DEFAULT_SERVER_PORT;
- char server_name[MAX_HOST_ADDRESS_LENGTH];
- char query_string[MAX_PACKETBUFFER_LENGTH]=DEFAULT_NRPE_COMMAND;;
- int socket_timeout=DEFAULT_SOCKET_TIMEOUT;
- int warning_time=0;
- int check_warning_time=FALSE;
- int critical_time=0;
- int check_critical_time=FALSE;
- int process_arguments(int,char **);
- void alarm_handler(int);
- int main(int argc, char **argv){
- int sd;
- int rc;
- int result;
- packet send_packet;
- packet receive_packet;
- result=process_arguments(argc,argv);
- if(result!=OK){
- printf("Incorrect command line arguments supplied\n");
- printf("\n");
- printf("NRPE Plugin for Nagios\n");
- printf("Copyright (c) 1999-2001 Ethan Galstad (nagios@nagios.org)\n");
- printf("Version: %s\n",PROGRAM_VERSION);
- printf("Last Modified: %s\n",MODIFICATION_DATE);
- printf("License: GPL\n");
- printf("\n");
- printf("Usage: %s <host_address> [-p port] [-c command] [-wt warn_time]\n",argv[0]);
- printf(" [-ct crit_time] [-to to_sec]\n");
- printf("\n");
- printf("Options:\n");
- printf(" <host_address> = The IP address of the host running the NRPE daemon\n");
- printf(" [port] = The port on which the daemon is running - default is %d\n",DEFAULT_SERVER_PORT);
- printf(" [command] = The name of the command that the remote daemon should run\n");
- printf(" [warn_time] = Response time in seconds necessary to result in a warning\n");
- printf(" status\n");
- printf(" [crit_time] = Response time in seconds necessary to result in a critical\n");
- printf(" status\n");
- printf(" [to_sec] = Number of seconds before connection attempt times out.\n");
- printf(" Default timeout is %d seconds\n",DEFAULT_SOCKET_TIMEOUT);
- printf("\n");
- printf("Note:\n");
- printf("This plugin requires that you have the NRPE daemon running on the remote host.\n");
- printf("You must also have configured the daemon to associate a specific plugin command\n");
- printf("with the [command] option you are specifying here. Upon receipt of the\n");
- printf("[command] argument, the NRPE daemon will run the appropriate plugin command and\n");
- printf("send the plugin output and return code back to *this* plugin. This allows you\n");
- printf("to execute plugins on remote hosts and 'fake' the results to make Nagios think\n");
- printf("the plugin is being run locally.\n");
- printf("\n");
- return STATE_UNKNOWN;
- }
- /* initialize alarm signal handling */
- signal(SIGALRM,alarm_handler);
- /* set socket timeout */
- alarm(socket_timeout);
- time(&start_time);
- /* try to connect to the host at the given port number */
- result=my_tcp_connect(server_name,server_port,&sd);
- /* we connected, so close connection before exiting */
- if(result==STATE_OK){
- /* send the query packet */
- bzero(&send_packet,sizeof(send_packet));
- send_packet.packet_type=htonl(QUERY_PACKET);
- send_packet.packet_version=htonl(NRPE_PACKET_VERSION_1);
- send_packet.buffer_length=htonl(strlen(query_string));
- strcpy(&send_packet.buffer[0],query_string);
- rc=send(sd,(void *)&send_packet,sizeof(send_packet),0);
- if(rc==-1){
- printf("CHECK_NRPE: Error sending query to host\n");
- close(sd);
- return STATE_UNKNOWN;
- }
- /* wait for the response packet */
- bzero(&receive_packet,sizeof(receive_packet));
- rc=recv(sd,(void *)&receive_packet,sizeof(receive_packet),0);
- if(rc==-1){
- printf("CHECK_NRPE: Error receiving data from host\n");
- close(sd);
- return STATE_UNKNOWN;
- }
- else if(rc==0){
- printf("CHECK_NRPE: Received 0 bytes. Is this host authorized to connect with nrpe daemon?\n");
- close(sd);
- return STATE_UNKNOWN;
- }
- else if(rc<sizeof(receive_packet)){
- printf("CHECK_NRPE: Receive underflow. Only %d bytes received (%d expected).\n",rc,sizeof(receive_packet));
- close(sd);
- return STATE_UNKNOWN;
- }
- time(&end_time);
- result=STATE_OK;
- if(check_critical_time==TRUE && (end_time-start_time)>critical_time)
- result=STATE_CRITICAL;
- else if(check_warning_time==TRUE && (end_time-start_time)>warning_time)
- result=STATE_WARNING;
- if(result!=STATE_OK)
- printf("CHECK_NRPE problem - %d second response time: ",(int)(end_time-start_time));
- /* get the return code from the remote plugin */
- result=ntohl(receive_packet.result_code);
- /* make sure there is something in the plugin output buffer */
- if(!strcmp(receive_packet.buffer,""))
- printf("CHECK_NRPE: No output returned from NRPE daemon.\n");
- else
- printf("%s\n",receive_packet.buffer);
- /* close the connection */
- close(sd);
- }
- /* reset the alarm */
- alarm(0);
- return result;
- }
- /* process command line arguments */
- int process_arguments(int argc, char **argv){
- int x;
- /* no options were supplied */
- if(argc<2)
- return ERROR;
- /* first option is always the server name/address */
- strncpy(server_name,argv[1],sizeof(server_name)-1);
- server_name[sizeof(server_name)-1]='\x0';
- /* process all remaining arguments */
- for(x=3;x<=argc;x++){
- if(!strcmp(argv[x-1],"-c")){
- if(x<argc){
- strncpy(query_string,argv[x],sizeof(query_string)-1);
- query_string[sizeof(query_string)-1]='\x0';
- x++;
- }
- else
- return ERROR;
- }
- else if(!strcmp(argv[x-1],"-p")){
- if(x<argc){
- server_port=atoi(argv[x]);
- x++;
- }
- else
- return ERROR;
- }
- else if(!strcmp(argv[x-1],"-to")){
- if(x<argc){
- socket_timeout=atoi(argv[x]);
- if(socket_timeout<=0)
- return ERROR;
- x++;
- }
- else
- return ERROR;
- }
- else if(!strcmp(argv[x-1],"-wt")){
- if(x<argc){
- warning_time=atoi(argv[x]);
- check_warning_time=TRUE;
- x++;
- }
- else
- return ERROR;
- }
- else if(!strcmp(argv[x-1],"-ct")){
- if(x<argc){
- critical_time=atoi(argv[x]);
- check_critical_time=TRUE;
- x++;
- }
- else
- return ERROR;
- }
- else
- return ERROR;
- }
- return OK;
- }
- void alarm_handler(int sig){
- printf("CHECK_NRPE: Socket timeout after %d seconds\n",socket_timeout);
- exit(STATE_CRITICAL);
- }
|