~xnox/ubuntu/quantal/nagios-nrpe/merge

« back to all changes in this revision

Viewing changes to src/check_nrpe.c

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2008-07-12 01:09:21 UTC
  • mfrom: (4.1.1 gutsy)
  • Revision ID: james.westby@ubuntu.com-20080712010921-23irea456g0jvx9n
Tags: 2.8.1-1.1
* Non-maintainer upload.
* Fix bashism in debian/rules (Closes: #484412)
* Bump Standards-Version to 3.8.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/********************************************************************************************
2
2
 *
3
3
 * CHECK_NRPE.C - NRPE Plugin For Nagios
4
 
 * Copyright (c) 1999-2006 Ethan Galstad (nagios@nagios.org)
 
4
 * Copyright (c) 1999-2007 Ethan Galstad (nagios@nagios.org)
5
5
 * License: GPL
6
6
 *
7
 
 * Last Modified: 04-09-2006
 
7
 * Last Modified: 05-10-2007
8
8
 *
9
9
 * Command line: CHECK_NRPE -H <host_address> [-p port] [-c command] [-to to_sec]
10
10
 *
28
28
char *server_name=NULL;
29
29
char *command_name=NULL;
30
30
int socket_timeout=DEFAULT_SOCKET_TIMEOUT;
 
31
int timeout_return_code=STATE_CRITICAL;
31
32
int sd;
32
33
 
33
34
char query[MAX_INPUT_BUFFER]="";
48
49
 
49
50
int process_arguments(int,char **);
50
51
void alarm_handler(int);
 
52
int graceful_close(int,int);
51
53
 
52
54
 
53
55
 
70
72
                        printf("Incorrect command line arguments supplied\n");
71
73
                printf("\n");
72
74
                printf("NRPE Plugin for Nagios\n");
73
 
                printf("Copyright (c) 1999-2006 Ethan Galstad (nagios@nagios.org)\n");
 
75
                printf("Copyright (c) 1999-2007 Ethan Galstad (nagios@nagios.org)\n");
74
76
                printf("Version: %s\n",PROGRAM_VERSION);
75
77
                printf("Last Modified: %s\n",MODIFICATION_DATE);
76
78
                printf("License: GPL v2 with exemptions (-l for more info)\n");
82
84
 
83
85
        if(result!=OK || show_help==TRUE){
84
86
 
85
 
                printf("Usage: check_nrpe -H <host> [-n] [-p <port>] [-t <timeout>] [-c <command>] [-a <arglist...>]\n");
 
87
                printf("Usage: check_nrpe -H <host> [-n] [-u] [-p <port>] [-t <timeout>] [-c <command>] [-a <arglist...>]\n");
86
88
                printf("\n");
87
89
                printf("Options:\n");
88
90
                printf(" -n         = Do no use SSL\n");
 
91
                printf(" -u         = Make socket timeouts return an UNKNOWN state instead of CRITICAL\n");
89
92
                printf(" <host>     = The address of the host running the NRPE daemon\n");
90
93
                printf(" [port]     = The port on which the daemon is running (default=%d)\n",DEFAULT_SERVER_PORT);
91
94
                printf(" [timeout]  = Number of seconds before connection times out (default=%d)\n",DEFAULT_SOCKET_TIMEOUT);
238
241
                        SSL_CTX_free(ctx);
239
242
                        }
240
243
#endif
241
 
                close(sd);
 
244
                graceful_close(sd,1000);
242
245
 
243
246
                /* recv() error */
244
247
                if(rc<0){
320
323
                {"command", required_argument, 0, 'c'},
321
324
                {"args", required_argument, 0, 'a'},
322
325
                {"no-ssl", no_argument, 0, 'n'},
 
326
                {"unknown-timeout", no_argument, 0, 'u'},
323
327
                {"timeout", required_argument, 0, 't'},
324
328
                {"port", required_argument, 0, 'p'},
325
329
                {"help", no_argument, 0, 'h'},
332
336
        if(argc<2)
333
337
                return ERROR;
334
338
 
335
 
        snprintf(optchars,MAX_INPUT_BUFFER,"H:c:a:t:p:nhl");
 
339
        snprintf(optchars,MAX_INPUT_BUFFER,"H:c:a:t:p:nuhl");
336
340
 
337
341
        while(1){
338
342
#ifdef HAVE_GETOPT_LONG
378
382
                case 'n':
379
383
                        use_ssl=FALSE;
380
384
                        break;
 
385
                case 'u':
 
386
                        timeout_return_code=STATE_UNKNOWN;
 
387
                        break;
381
388
                default:
382
389
                        return ERROR;
383
390
                        break;
417
424
 
418
425
        printf("CHECK_NRPE: Socket timeout after %d seconds.\n",socket_timeout);
419
426
 
420
 
        exit(STATE_CRITICAL);
 
427
        exit(timeout_return_code);
421
428
        }
422
429
 
 
430
 
 
431
/* submitted by Mark Plaksin 08/31/2006 */
 
432
int graceful_close(int sd, int timeout){
 
433
        fd_set in;
 
434
        struct timeval tv;
 
435
        char buf[1000];
 
436
 
 
437
        /* send FIN packet */
 
438
        shutdown(sd,SHUT_WR);  
 
439
        for(;;){
 
440
 
 
441
                FD_ZERO(&in);
 
442
                FD_SET(sd,&in);
 
443
                tv.tv_sec=timeout/1000;
 
444
                tv.tv_usec=(timeout % 1000)*1000;
 
445
 
 
446
                /* timeout or error */
 
447
                if(1!=select(sd+1,&in,NULL,NULL,&tv))
 
448
                        break;   
 
449
 
 
450
                /* no more data (FIN or RST) */
 
451
                if(0>=recv(sd,buf,sizeof(buf),0))
 
452
                        break;
 
453
                }
 
454
 
 
455
#ifdef HAVE_CLOSESOCKET
 
456
        closesocket(sd);
 
457
#else
 
458
        close(sd);
 
459
#endif
 
460
 
 
461
        return OK;
 
462
        }