Back to index

nagios-plugins  1.4.16
check_nt.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * 
00003 * Nagios check_nt plugin
00004 * 
00005 * License: GPL
00006 * Copyright (c) 2000-2002 Yves Rubin (rubiyz@yahoo.com)
00007 * Copyright (c) 2003-2007 Nagios Plugins Development Team
00008 * 
00009 * Description:
00010 * 
00011 * This file contains the check_nt plugin
00012 * 
00013 * This plugin collects data from the NSClient service running on a
00014 * Windows NT/2000/XP/2003 server.
00015 * This plugin requires NSClient software to run on NT
00016 * (http://nsclient.ready2run.nl/)
00017 * 
00018 * 
00019 * This program is free software: you can redistribute it and/or modify
00020 * it under the terms of the GNU General Public License as published by
00021 * the Free Software Foundation, either version 3 of the License, or
00022 * (at your option) any later version.
00023 * 
00024 * This program is distributed in the hope that it will be useful,
00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00027 * GNU General Public License for more details.
00028 * 
00029 * You should have received a copy of the GNU General Public License
00030 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00031 * 
00032 * 
00033 *****************************************************************************/
00034 
00035 const char *progname = "check_nt";
00036 const char *copyright = "2000-2007";
00037 const char *email = "nagiosplug-devel@lists.sourceforge.net";
00038 
00039 #include "common.h"
00040 #include "netutils.h"
00041 #include "utils.h"
00042 
00043 enum checkvars {
00044        CHECK_NONE,
00045        CHECK_CLIENTVERSION,
00046        CHECK_CPULOAD,
00047        CHECK_UPTIME,
00048        CHECK_USEDDISKSPACE,
00049        CHECK_SERVICESTATE,
00050        CHECK_PROCSTATE,
00051        CHECK_MEMUSE,
00052        CHECK_COUNTER,
00053        CHECK_FILEAGE,
00054        CHECK_INSTANCES
00055 };
00056 
00057 enum {
00058        MAX_VALUE_LIST = 30,
00059        PORT = 1248
00060 };
00061 
00062 char *server_address=NULL;
00063 char *volume_name=NULL;
00064 int server_port=PORT;
00065 char *value_list=NULL;
00066 char *req_password=NULL;
00067 unsigned long lvalue_list[MAX_VALUE_LIST];
00068 unsigned long warning_value=0L;
00069 unsigned long critical_value=0L;
00070 int check_warning_value=FALSE;
00071 int check_critical_value=FALSE;
00072 enum checkvars vars_to_check = CHECK_NONE;
00073 int show_all=FALSE;
00074 
00075 char recv_buffer[MAX_INPUT_BUFFER];
00076 
00077 void fetch_data (const char* address, int port, const char* sendb);
00078 int process_arguments(int, char **);
00079 void preparelist(char *string);
00080 int strtoularray(unsigned long *array, char *string, const char *delim);
00081 void print_help(void);
00082 void print_usage(void);
00083 
00084 int main(int argc, char **argv){
00085 
00086 /* should be  int result = STATE_UNKNOWN; */
00087 
00088        int return_code = STATE_UNKNOWN;
00089        char *send_buffer=NULL;
00090        char *output_message=NULL;
00091        char *perfdata=NULL;
00092        char *temp_string=NULL;
00093        char *temp_string_perf=NULL;
00094        char *description=NULL,*counter_unit = NULL;
00095        char *minval = NULL, *maxval = NULL, *errcvt = NULL;
00096        char *fds=NULL, *tds=NULL;
00097 
00098        double total_disk_space=0;
00099        double free_disk_space=0;
00100        double percent_used_space=0;
00101        double warning_used_space=0;
00102        double critical_used_space=0;
00103        double mem_commitLimit=0;
00104        double mem_commitByte=0;
00105        double fminval = 0, fmaxval = 0;
00106        unsigned long utilization;
00107        unsigned long uptime;
00108        unsigned long age_in_minutes;
00109        double counter_value = 0.0;
00110        int offset=0;
00111        int updays=0;
00112        int uphours=0;
00113        int upminutes=0;
00114 
00115        int isPercent = FALSE;
00116        int allRight = FALSE;
00117 
00118        setlocale (LC_ALL, "");
00119        bindtextdomain (PACKAGE, LOCALEDIR);
00120        textdomain (PACKAGE);
00121 
00122        /* Parse extra opts if any */
00123        argv=np_extra_opts (&argc, argv, progname);
00124 
00125        if(process_arguments(argc,argv) == ERROR)
00126               usage4 (_("Could not parse arguments"));
00127 
00128        /* initialize alarm signal handling */
00129        signal(SIGALRM,socket_timeout_alarm_handler);
00130 
00131        /* set socket timeout */
00132        alarm(socket_timeout);
00133 
00134        switch (vars_to_check) {
00135 
00136        case CHECK_CLIENTVERSION:
00137 
00138               asprintf(&send_buffer, "%s&1", req_password);
00139               fetch_data (server_address, server_port, send_buffer);
00140               if (value_list != NULL && strcmp(recv_buffer, value_list) != 0) {
00141                      asprintf (&output_message, _("Wrong client version - running: %s, required: %s"), recv_buffer, value_list);
00142                      return_code = STATE_WARNING;
00143               } else {
00144                      asprintf (&output_message, "%s", recv_buffer);
00145                      return_code = STATE_OK;
00146               }
00147               break;
00148 
00149        case CHECK_CPULOAD:
00150 
00151               if (value_list==NULL)
00152                      output_message = strdup (_("missing -l parameters"));
00153               else if (strtoularray(lvalue_list,value_list,",")==FALSE)
00154                      output_message = strdup (_("wrong -l parameter."));
00155               else {
00156                      /* -l parameters is present with only integers */
00157                      return_code=STATE_OK;
00158                      temp_string = strdup (_("CPU Load"));
00159                      temp_string_perf = strdup (" ");
00160 
00161                      /* loop until one of the parameters is wrong or not present */
00162                      while (lvalue_list[0+offset]> (unsigned long)0 &&
00163                                            lvalue_list[0+offset]<=(unsigned long)17280 &&
00164                                            lvalue_list[1+offset]> (unsigned long)0 &&
00165                                            lvalue_list[1+offset]<=(unsigned long)100 &&
00166                                            lvalue_list[2+offset]> (unsigned long)0 &&
00167                                            lvalue_list[2+offset]<=(unsigned long)100) {
00168 
00169                             /* Send request and retrieve data */
00170                             asprintf(&send_buffer,"%s&2&%lu",req_password,lvalue_list[0+offset]);
00171                             fetch_data (server_address, server_port, send_buffer);
00172 
00173                             utilization=strtoul(recv_buffer,NULL,10);
00174 
00175                             /* Check if any of the request is in a warning or critical state */
00176                             if(utilization >= lvalue_list[2+offset])
00177                                    return_code=STATE_CRITICAL;
00178                             else if(utilization >= lvalue_list[1+offset] && return_code<STATE_WARNING)
00179                                    return_code=STATE_WARNING;
00180 
00181                             asprintf(&output_message,_(" %lu%% (%lu min average)"), utilization, lvalue_list[0+offset]);
00182                             asprintf(&temp_string,"%s%s",temp_string,output_message);
00183                             asprintf(&perfdata,_(" '%lu min avg Load'=%lu%%;%lu;%lu;0;100"), lvalue_list[0+offset], utilization,
00184                               lvalue_list[1+offset], lvalue_list[2+offset]);
00185                             asprintf(&temp_string_perf,"%s%s",temp_string_perf,perfdata);
00186                             offset+=3;    /* move across the array */
00187                      }
00188 
00189                      if (strlen(temp_string)>10) {  /* we had at least one loop */
00190                             output_message = strdup (temp_string);
00191                             perfdata = temp_string_perf;
00192                      } else
00193                             output_message = strdup (_("not enough values for -l parameters"));
00194               }
00195               break;
00196 
00197        case CHECK_UPTIME:
00198 
00199               asprintf(&send_buffer, "%s&3", req_password);
00200               fetch_data (server_address, server_port, send_buffer);
00201               uptime=strtoul(recv_buffer,NULL,10);
00202               updays = uptime / 86400;
00203               uphours = (uptime % 86400) / 3600;
00204               upminutes = ((uptime % 86400) % 3600) / 60;
00205               asprintf(&output_message,_("System Uptime - %u day(s) %u hour(s) %u minute(s)"),updays,uphours, upminutes);
00206               if (check_critical_value==TRUE && uptime <= critical_value)
00207                      return_code=STATE_CRITICAL;
00208               else if (check_warning_value==TRUE && uptime <= warning_value)
00209                      return_code=STATE_WARNING;
00210               else
00211                      return_code=STATE_OK;
00212               break;
00213 
00214        case CHECK_USEDDISKSPACE:
00215 
00216               if (value_list==NULL)
00217                      output_message = strdup (_("missing -l parameters"));
00218               else if (strlen(value_list)!=1)
00219                      output_message = strdup (_("wrong -l argument"));
00220               else {
00221                      asprintf(&send_buffer,"%s&4&%s", req_password, value_list);
00222                      fetch_data (server_address, server_port, send_buffer);
00223                      fds=strtok(recv_buffer,"&");
00224                      tds=strtok(NULL,"&");
00225                      if(fds!=NULL)
00226                             free_disk_space=atof(fds);
00227                      if(tds!=NULL)
00228                             total_disk_space=atof(tds);
00229 
00230                      if (total_disk_space>0 && free_disk_space>=0) {
00231                             percent_used_space = ((total_disk_space - free_disk_space) / total_disk_space) * 100;
00232                             warning_used_space = ((float)warning_value / 100) * total_disk_space;
00233                             critical_used_space = ((float)critical_value / 100) * total_disk_space;
00234 
00235                             asprintf(&temp_string,_("%s:\\ - total: %.2f Gb - used: %.2f Gb (%.0f%%) - free %.2f Gb (%.0f%%)"),
00236                               value_list, total_disk_space / 1073741824, (total_disk_space - free_disk_space) / 1073741824,
00237                               percent_used_space, free_disk_space / 1073741824, (free_disk_space / total_disk_space)*100);
00238                             asprintf(&temp_string_perf,_("'%s:\\ Used Space'=%.2fGb;%.2f;%.2f;0.00;%.2f"), value_list,
00239                               (total_disk_space - free_disk_space) / 1073741824, warning_used_space / 1073741824,
00240                               critical_used_space / 1073741824, total_disk_space / 1073741824);
00241 
00242                             if(check_critical_value==TRUE && percent_used_space >= critical_value)
00243                                    return_code=STATE_CRITICAL;
00244                             else if (check_warning_value==TRUE && percent_used_space >= warning_value)
00245                                    return_code=STATE_WARNING;
00246                             else
00247                                    return_code=STATE_OK;
00248 
00249                             output_message = strdup (temp_string);
00250                             perfdata = temp_string_perf;
00251                      } else {
00252                             output_message = strdup (_("Free disk space : Invalid drive"));
00253                             return_code=STATE_UNKNOWN;
00254                      }
00255               }
00256               break;
00257 
00258        case CHECK_SERVICESTATE:
00259        case CHECK_PROCSTATE:
00260 
00261               if (value_list==NULL)
00262                      output_message = strdup (_("No service/process specified"));
00263               else {
00264                      preparelist(value_list);           /* replace , between services with & to send the request */
00265                      asprintf(&send_buffer,"%s&%u&%s&%s", req_password,(vars_to_check==CHECK_SERVICESTATE)?5:6,
00266                                                   (show_all==TRUE) ? "ShowAll" : "ShowFail",value_list);
00267                      fetch_data (server_address, server_port, send_buffer);
00268                      return_code=atoi(strtok(recv_buffer,"&"));
00269                      temp_string=strtok(NULL,"&");
00270                      output_message = strdup (temp_string);
00271               }
00272               break;
00273 
00274        case CHECK_MEMUSE:
00275 
00276               asprintf(&send_buffer,"%s&7", req_password);
00277               fetch_data (server_address, server_port, send_buffer);
00278               mem_commitLimit=atof(strtok(recv_buffer,"&"));
00279               mem_commitByte=atof(strtok(NULL,"&"));
00280               percent_used_space = (mem_commitByte / mem_commitLimit) * 100;
00281               warning_used_space = ((float)warning_value / 100) * mem_commitLimit;
00282               critical_used_space = ((float)critical_value / 100) * mem_commitLimit;
00283 
00284               /* Divisor should be 1048567, not 3044515, as we are measuring "Commit Charge" here,
00285               which equals RAM + Pagefiles. */
00286               asprintf(&output_message,_("Memory usage: total:%.2f Mb - used: %.2f Mb (%.0f%%) - free: %.2f Mb (%.0f%%)"),
00287                 mem_commitLimit / 1048567, mem_commitByte / 1048567, percent_used_space,
00288                 (mem_commitLimit - mem_commitByte) / 1048567, (mem_commitLimit - mem_commitByte) / mem_commitLimit * 100);
00289               asprintf(&perfdata,_("'Memory usage'=%.2fMb;%.2f;%.2f;0.00;%.2f"), mem_commitByte / 1048567,
00290                 warning_used_space / 1048567, critical_used_space / 1048567, mem_commitLimit / 1048567);
00291 
00292               return_code=STATE_OK;
00293               if(check_critical_value==TRUE && percent_used_space >= critical_value)
00294                      return_code=STATE_CRITICAL;
00295               else if (check_warning_value==TRUE && percent_used_space >= warning_value)
00296                      return_code=STATE_WARNING;
00297 
00298               break;
00299 
00300        case CHECK_COUNTER:
00301 
00302 
00303               /*
00304               CHECK_COUNTER has been modified to provide extensive perfdata information.
00305               In order to do this, some modifications have been done to the code
00306               and some constraints have been introduced.
00307 
00308               1) For the sake of simplicity of the code, perfdata information will only be
00309                provided when the "description" field is added.
00310 
00311               2) If the counter you're going to measure is percent-based, the code will detect
00312                the percent sign in its name and will attribute minimum (0%) and maximum (100%)
00313                values automagically, as well the %" sign to graph units.
00314 
00315               3) OTOH, if the counter is "absolute", you'll have to provide the following
00316                the counter unit - that is, the dimensions of the counter you're getting. Examples:
00317                pages/s, packets transferred, etc.
00318 
00319               4) If you want, you may provide the minimum and maximum values to expect. They aren't mandatory,
00320                but once specified they MUST have the same order of magnitude and units of -w and -c; otherwise.
00321                strange things will happen when you make graphs of your data.
00322               */
00323 
00324               if (value_list == NULL)
00325                      output_message = strdup (_("No counter specified"));
00326               else
00327               {
00328                      preparelist (value_list);   /* replace , between services with & to send the request */
00329                      isPercent = (strchr (value_list, '%') != NULL);
00330 
00331                      strtok (value_list, "&");   /* burn the first parameters */
00332                      description = strtok (NULL, "&");
00333                      counter_unit = strtok (NULL, "&");
00334                      asprintf (&send_buffer, "%s&8&%s", req_password, value_list);
00335                      fetch_data (server_address, server_port, send_buffer);
00336                      counter_value = atof (recv_buffer);
00337 
00338                      if (description == NULL)
00339                      asprintf (&output_message, "%.f", counter_value);
00340                      else if (isPercent)
00341                      {
00342                             counter_unit = strdup ("%");
00343                             allRight = TRUE;
00344                      }
00345 
00346                      if ((counter_unit != NULL) && (!allRight))
00347                      {
00348                             minval = strtok (NULL, "&");
00349                             maxval = strtok (NULL, "&");
00350 
00351                             /* All parameters specified. Let's check the numbers */
00352 
00353                             fminval = (minval != NULL) ? strtod (minval, &errcvt) : -1;
00354                             fmaxval = (minval != NULL) ? strtod (maxval, &errcvt) : -1;
00355 
00356                             if ((fminval == 0) && (minval == errcvt))
00357                                    output_message = strdup (_("Minimum value contains non-numbers"));
00358                             else
00359                             {
00360                                    if ((fmaxval == 0) && (maxval == errcvt))
00361                                           output_message = strdup (_("Maximum value contains non-numbers"));
00362                                    else
00363                                           allRight = TRUE;     /* Everything is OK. */
00364 
00365                             }
00366                      }
00367                      else if ((counter_unit == NULL) && (description != NULL))
00368                             output_message = strdup (_("No unit counter specified"));
00369 
00370                      if (allRight)
00371                      {
00372                             /* Let's format the output string, finally... */
00373                                    if (strstr(description, "%") == NULL) {
00374                                           asprintf (&output_message, "%s = %.2f %s", description, counter_value, counter_unit);
00375                                    } else {
00376                                           /* has formatting, will segv if wrong */
00377                                           asprintf (&output_message, description, counter_value);
00378                                    }
00379                                    asprintf (&output_message, "%s |", output_message);
00380                                    asprintf (&output_message,"%s %s", output_message,
00381                                           fperfdata (description, counter_value,
00382                                                  counter_unit, 1, warning_value, 1, critical_value,
00383                                                  (!(isPercent) && (minval != NULL)), fminval,
00384                                                  (!(isPercent) && (minval != NULL)), fmaxval));
00385                      }
00386               }
00387 
00388               if (critical_value > warning_value)
00389               {                    /* Normal thresholds */
00390                      if (check_critical_value == TRUE && counter_value >= critical_value)
00391                             return_code = STATE_CRITICAL;
00392                      else if (check_warning_value == TRUE && counter_value >= warning_value)
00393                             return_code = STATE_WARNING;
00394                      else
00395                             return_code = STATE_OK;
00396               }
00397               else
00398               {                    /* inverse thresholds */
00399                      return_code = STATE_OK;
00400                      if (check_critical_value == TRUE && counter_value <= critical_value)
00401                             return_code = STATE_CRITICAL;
00402                      else if (check_warning_value == TRUE && counter_value <= warning_value)
00403                             return_code = STATE_WARNING;
00404               }
00405        break;
00406 
00407        case CHECK_FILEAGE:
00408 
00409               if (value_list==NULL)
00410                      output_message = strdup (_("No counter specified"));
00411               else {
00412                      preparelist(value_list);           /* replace , between services with & to send the request */
00413                      asprintf(&send_buffer,"%s&9&%s", req_password,value_list);
00414                      fetch_data (server_address, server_port, send_buffer);
00415                      age_in_minutes = atoi(strtok(recv_buffer,"&"));
00416                      description = strtok(NULL,"&");
00417                      output_message = strdup (description);
00418 
00419                      if (critical_value > warning_value) {        /* Normal thresholds */
00420                             if(check_critical_value==TRUE && age_in_minutes >= critical_value)
00421                                    return_code=STATE_CRITICAL;
00422                             else if (check_warning_value==TRUE && age_in_minutes >= warning_value)
00423                                    return_code=STATE_WARNING;
00424                             else
00425                                    return_code=STATE_OK;
00426                      }
00427                      else {                                       /* inverse thresholds */
00428                             if(check_critical_value==TRUE && age_in_minutes <= critical_value)
00429                                    return_code=STATE_CRITICAL;
00430                             else if (check_warning_value==TRUE && age_in_minutes <= warning_value)
00431                                    return_code=STATE_WARNING;
00432                             else
00433                                    return_code=STATE_OK;
00434                      }
00435               }
00436               break;
00437 
00438        case CHECK_INSTANCES:
00439               if (value_list==NULL)
00440                      output_message = strdup (_("No counter specified"));
00441               else {
00442                      asprintf(&send_buffer,"%s&10&%s", req_password,value_list);
00443                      fetch_data (server_address, server_port, send_buffer);
00444                      if (!strncmp(recv_buffer,"ERROR",5)) {
00445                             printf("NSClient - %s\n",recv_buffer);
00446                             exit(STATE_UNKNOWN);
00447                      }
00448                      asprintf(&output_message,"%s",recv_buffer);
00449                      return_code=STATE_OK;
00450               }
00451               break;
00452 
00453        case CHECK_NONE:
00454        default:
00455               usage4 (_("Please specify a variable to check"));
00456               break;
00457 
00458        }
00459 
00460        /* reset timeout */
00461        alarm(0);
00462 
00463        if (perfdata==NULL)
00464               printf("%s\n",output_message);
00465        else
00466               printf("%s | %s\n",output_message,perfdata);
00467        return return_code;
00468 }
00469 
00470 
00471 
00472 /* process command-line arguments */
00473 int process_arguments(int argc, char **argv){
00474        int c;
00475 
00476        int option = 0;
00477        static struct option longopts[] =
00478        {
00479               {"port",     required_argument,0,'p'},
00480               {"timeout",  required_argument,0,'t'},
00481               {"critical", required_argument,0,'c'},
00482               {"warning",  required_argument,0,'w'},
00483               {"variable", required_argument,0,'v'},
00484               {"hostname", required_argument,0,'H'},
00485               {"params",   required_argument,0,'l'},
00486               {"secret",   required_argument,0,'s'},
00487               {"display",  required_argument,0,'d'},
00488               {"unknown-timeout", no_argument, 0, 'u'},
00489               {"version",  no_argument,      0,'V'},
00490               {"help",     no_argument,      0,'h'},
00491               {0,0,0,0}
00492        };
00493 
00494        /* no options were supplied */
00495        if(argc<2) return ERROR;
00496 
00497        /* backwards compatibility */
00498        if (! is_option(argv[1])) {
00499               server_address = strdup(argv[1]);
00500               argv[1]=argv[0];
00501               argv=&argv[1];
00502               argc--;
00503        }
00504 
00505        for (c=1;c<argc;c++) {
00506               if(strcmp("-to",argv[c])==0)
00507                      strcpy(argv[c],"-t");
00508               else if (strcmp("-wv",argv[c])==0)
00509                      strcpy(argv[c],"-w");
00510               else if (strcmp("-cv",argv[c])==0)
00511                      strcpy(argv[c],"-c");
00512        }
00513 
00514        while (1) {
00515               c = getopt_long(argc,argv,"+hVH:t:c:w:p:v:l:s:d:u",longopts,&option);
00516 
00517               if (c==-1||c==EOF||c==1)
00518                      break;
00519 
00520               switch (c) {
00521                      case '?': /* print short usage statement if args not parsable */
00522                      usage5 ();
00523                      case 'h': /* help */
00524                             print_help();
00525                             exit(STATE_OK);
00526                      case 'V': /* version */
00527                             print_revision(progname, NP_VERSION);
00528                             exit(STATE_OK);
00529                      case 'H': /* hostname */
00530                             server_address = optarg;
00531                             break;
00532                      case 's': /* password */
00533                             req_password = optarg;
00534                             break;
00535                      case 'p': /* port */
00536                             if (is_intnonneg(optarg))
00537                                    server_port=atoi(optarg);
00538                             else
00539                                    die(STATE_UNKNOWN,_("Server port must be an integer\n"));
00540                             break;
00541                      case 'v':
00542                             if(strlen(optarg)<4)
00543                                    return ERROR;
00544                             if(!strcmp(optarg,"CLIENTVERSION"))
00545                                    vars_to_check=CHECK_CLIENTVERSION;
00546                             else if(!strcmp(optarg,"CPULOAD"))
00547                                    vars_to_check=CHECK_CPULOAD;
00548                             else if(!strcmp(optarg,"UPTIME"))
00549                                    vars_to_check=CHECK_UPTIME;
00550                             else if(!strcmp(optarg,"USEDDISKSPACE"))
00551                                    vars_to_check=CHECK_USEDDISKSPACE;
00552                             else if(!strcmp(optarg,"SERVICESTATE"))
00553                                    vars_to_check=CHECK_SERVICESTATE;
00554                             else if(!strcmp(optarg,"PROCSTATE"))
00555                                    vars_to_check=CHECK_PROCSTATE;
00556                             else if(!strcmp(optarg,"MEMUSE"))
00557                                    vars_to_check=CHECK_MEMUSE;
00558                             else if(!strcmp(optarg,"COUNTER"))
00559                                    vars_to_check=CHECK_COUNTER;
00560                             else if(!strcmp(optarg,"FILEAGE"))
00561                                    vars_to_check=CHECK_FILEAGE;
00562                             else if(!strcmp(optarg,"INSTANCES"))
00563                                    vars_to_check=CHECK_INSTANCES;
00564                             else
00565                                    return ERROR;
00566                             break;
00567                      case 'l': /* value list */
00568                             value_list = optarg;
00569                             break;
00570                      case 'w': /* warning threshold */
00571                             warning_value=strtoul(optarg,NULL,10);
00572                             check_warning_value=TRUE;
00573                             break;
00574                      case 'c': /* critical threshold */
00575                             critical_value=strtoul(optarg,NULL,10);
00576                             check_critical_value=TRUE;
00577                             break;
00578                      case 'd': /* Display select for services */
00579                             if (!strcmp(optarg,"SHOWALL"))
00580                                    show_all = TRUE;
00581                             break;
00582                      case 'u':
00583                             socket_timeout_state=STATE_UNKNOWN;
00584                             break;
00585                      case 't': /* timeout */
00586                             socket_timeout=atoi(optarg);
00587                             if(socket_timeout<=0)
00588                                    return ERROR;
00589                      }
00590 
00591        }
00592        if (server_address == NULL)
00593               usage4 (_("You must provide a server address or host name"));
00594 
00595        if (vars_to_check==CHECK_NONE)
00596               return ERROR;
00597 
00598        if (req_password == NULL)
00599               req_password = strdup (_("None"));
00600 
00601        return OK;
00602 }
00603 
00604 
00605 
00606 void fetch_data (const char *address, int port, const char *sendb) {
00607        int result;
00608 
00609        result=process_tcp_request(address, port, sendb, recv_buffer,sizeof(recv_buffer));
00610 
00611        if(result!=STATE_OK)
00612               die (result, _("could not fetch information from server\n"));
00613 
00614        if (!strncmp(recv_buffer,"ERROR",5))
00615               die (STATE_UNKNOWN, "NSClient - %s\n",recv_buffer);
00616 }
00617 
00618 int strtoularray(unsigned long *array, char *string, const char *delim) {
00619        /* split a <delim> delimited string into a long array */
00620        int idx=0;
00621        char *t1;
00622 
00623        for (idx=0;idx<MAX_VALUE_LIST;idx++)
00624               array[idx]=0;
00625 
00626        idx=0;
00627        for(t1 = strtok(string,delim);t1 != NULL; t1 = strtok(NULL, delim)) {
00628               if (is_numeric(t1) && idx<MAX_VALUE_LIST) {
00629                      array[idx]=strtoul(t1,NULL,10);
00630                      idx++;
00631               } else
00632                      return FALSE;
00633        }
00634        return TRUE;
00635 }
00636 
00637 void preparelist(char *string) {
00638        /* Replace all , with & which is the delimiter for the request */
00639        int i;
00640 
00641        for (i = 0; (size_t)i < strlen(string); i++)
00642               if (string[i] == ',') {
00643                      string[i]='&';
00644               }
00645 }
00646 
00647 
00648 
00649 void print_help(void)
00650 {
00651        print_revision(progname, NP_VERSION);
00652 
00653        printf ("Copyright (c) 2000 Yves Rubin (rubiyz@yahoo.com)\n");
00654        printf (COPYRIGHT, copyright, email);
00655 
00656        printf ("%s\n", _("This plugin collects data from the NSClient service running on a"));
00657        printf ("%s\n", _("Windows NT/2000/XP/2003 server."));
00658 
00659        printf ("\n\n");
00660 
00661        print_usage();
00662 
00663        printf (UT_HELP_VRSN);
00664        printf (UT_EXTRA_OPTS);
00665 
00666        printf ("%s\n", _("Options:"));
00667        printf (" %s\n", "-H, --hostname=HOST");
00668        printf ("   %s\n", _("Name of the host to check"));
00669        printf (" %s\n", "-p, --port=INTEGER");
00670        printf ("   %s", _("Optional port number (default: "));
00671        printf ("%d)\n", PORT);
00672        printf (" %s\n", "-s, --secret=<password>");
00673        printf ("   %s\n", _("Password needed for the request"));
00674        printf (" %s\n", "-w, --warning=INTEGER");
00675        printf ("   %s\n", _("Threshold which will result in a warning status"));
00676        printf (" %s\n", "-c, --critical=INTEGER");
00677        printf ("   %s\n", _("Threshold which will result in a critical status"));
00678        printf (" %s\n", "-t, --timeout=INTEGER");
00679        printf ("   %s", _("Seconds before connection attempt times out (default: "));
00680        printf (" %s\n", "-l, --params=<parameters>");
00681        printf ("   %s", _("Parameters passed to specified check (see below)"));
00682        printf (" %s\n", "-d, --display={SHOWALL}");
00683        printf ("   %s", _("Display options (currently only SHOWALL works)"));
00684        printf (" %s\n", "-u, --unknown-timeout");
00685        printf ("   %s", _("Return UNKNOWN on timeouts"));
00686        printf ("%d)\n", DEFAULT_SOCKET_TIMEOUT);
00687        printf (" %s\n", "-h, --help");
00688        printf ("   %s\n", _("Print this help screen"));
00689        printf (" %s\n", "-V, --version");
00690        printf ("   %s\n", _("Print version information"));
00691        printf (" %s\n", "-v, --variable=STRING");
00692        printf ("   %s\n\n", _("Variable to check"));
00693        printf ("%s\n", _("Valid variables are:"));
00694        printf (" %s", "CLIENTVERSION =");
00695        printf (" %s\n", _("Get the NSClient version"));
00696        printf ("  %s\n", _("If -l <version> is specified, will return warning if versions differ."));
00697        printf (" %s\n", "CPULOAD =");
00698        printf ("  %s\n", _("Average CPU load on last x minutes."));
00699        printf ("  %s\n", _("Request a -l parameter with the following syntax:"));
00700        printf ("  %s\n", _("-l <minutes range>,<warning threshold>,<critical threshold>."));
00701        printf ("  %s\n", _("<minute range> should be less than 24*60."));
00702        printf ("  %s\n", _("Thresholds are percentage and up to 10 requests can be done in one shot."));
00703        printf ("  %s\n", "ie: -l 60,90,95,120,90,95");
00704        printf (" %s\n", "UPTIME =");
00705        printf ("  %s\n", _("Get the uptime of the machine."));
00706        printf ("  %s\n", _("No specific parameters. No warning or critical threshold"));
00707        printf (" %s\n", "USEDDISKSPACE =");
00708        printf ("  %s\n", _("Size and percentage of disk use."));
00709        printf ("  %s\n", _("Request a -l parameter containing the drive letter only."));
00710        printf ("  %s\n", _("Warning and critical thresholds can be specified with -w and -c."));
00711        printf (" %s\n", "MEMUSE =");
00712        printf ("  %s\n", _("Memory use."));
00713        printf ("  %s\n", _("Warning and critical thresholds can be specified with -w and -c."));
00714        printf (" %s\n", "SERVICESTATE =");
00715        printf ("  %s\n", _("Check the state of one or several services."));
00716        printf ("  %s\n", _("Request a -l parameters with the following syntax:"));
00717        printf ("  %s\n", _("-l <service1>,<service2>,<service3>,..."));
00718        printf ("  %s\n", _("You can specify -d SHOWALL in case you want to see working services"));
00719        printf ("  %s\n", _("in the returned string."));
00720        printf (" %s\n", "PROCSTATE =");
00721        printf ("  %s\n", _("Check if one or several process are running."));
00722        printf ("  %s\n", _("Same syntax as SERVICESTATE."));
00723        printf (" %s\n", "COUNTER =");
00724        printf ("  %s\n", _("Check any performance counter of Windows NT/2000."));
00725        printf ("     %s\n", _("Request a -l parameters with the following syntax:"));
00726        printf ("     %s\n", _("-l \"\\\\<performance object>\\\\counter\",\"<description>"));
00727        printf ("     %s\n", _("The <description> parameter is optional and is given to a printf "));
00728        printf ("  %s\n", _("output command which requires a float parameter."));
00729        printf ("  %s\n", _("If <description> does not include \"%%\", it is used as a label."));
00730        printf ("  %s\n", _("Some examples:"));
00731        printf ("  %s\n", "\"Paging file usage is %%.2f %%%%\"");
00732        printf ("  %s\n", "\"%%.f %%%% paging file used.\"");
00733        printf (" %s\n", "INSTANCES =");
00734        printf ("  %s\n", _("Check any performance counter object of Windows NT/2000."));
00735        printf ("  %s\n", _("Syntax: check_nt -H <hostname> -p <port> -v INSTANCES -l <counter object>"));
00736        printf ("  %s\n", _("<counter object> is a Windows Perfmon Counter object (eg. Process),"));
00737        printf ("  %s\n", _("if it is two words, it should be enclosed in quotes"));
00738        printf ("  %s\n", _("The returned results will be a comma-separated list of instances on "));
00739        printf ("  %s\n", _(" the selected computer for that object."));
00740        printf ("  %s\n", _("The purpose of this is to be run from command line to determine what instances"));
00741        printf ("  %s\n", _(" are available for monitoring without having to log onto the Windows server"));
00742        printf ("  %s\n", _("  to run Perfmon directly."));
00743        printf ("  %s\n", _("It can also be used in scripts that automatically create Nagios service"));
00744        printf ("  %s\n", _(" configuration files."));
00745        printf ("  %s\n", _("Some examples:"));
00746        printf ("  %s\n\n", _("check_nt -H 192.168.1.1 -p 1248 -v INSTANCES -l Process"));
00747 
00748        printf ("%s\n", _("Notes:"));
00749        printf (" %s\n", _("- The NSClient service should be running on the server to get any information"));
00750        printf ("   %s\n", "(http://nsclient.ready2run.nl).");
00751        printf (" %s\n", _("- Critical thresholds should be lower than warning thresholds"));
00752        printf (" %s\n", _("- Default port 1248 is sometimes in use by other services. The error"));
00753        printf ("   %s\n", _("output when this happens contains \"Cannot map xxxxx to protocol number\"."));
00754        printf ("   %s\n", _("One fix for this is to change the port to something else on check_nt "));
00755        printf ("   %s\n", _("and on the client service it\'s connecting to."));
00756 
00757        printf (UT_SUPPORT);
00758 }
00759 
00760 
00761 
00762 void print_usage(void)
00763 {
00764        printf ("%s\n", _("Usage:"));
00765        printf ("%s -H host -v variable [-p port] [-w warning] [-c critical]\n",progname);
00766        printf ("[-l params] [-d SHOWALL] [-u] [-t timeout]\n");
00767 }
00768