Code

The "-e" option now accepts a comma-delimited list of expected status
[nagiosplug.git] / plugins / check_ntp_peer.c
index 5ed97db24f514fd30569a7cd6d96328e68f25422..00c8e97d14e001919ea0a772e7c14a763d0637bb 100644 (file)
@@ -175,23 +175,56 @@ void print_ntp_control_message(const ntp_control_message *p){
        }
 }
 
+/*
+ * Extract the value from NTP key/value pairs, or return NULL.
+ * The value returned can be free()ed.
+ */
 char *extract_value(const char *varlist, const char *name){
-       char *tmpvarlist=NULL, *tmpkey=NULL, *value=NULL;
-       int last=0;
-
-       /* The following code require a non-empty varlist */
-       if(strlen(varlist) == 0)
-               return NULL;
-
-       tmpvarlist = strdup(varlist);
-       tmpkey = strtok(tmpvarlist, "=");
+       char *tmp=NULL, *value=NULL;
+       int i;
 
-       do {
-               if(strstr(tmpkey, name) != NULL) {
-               value = strtok(NULL, ",");
-                       last = 1;
+       while (1) {
+               /* Strip any leading space */
+               for (varlist; isspace(varlist[0]); varlist++);
+
+               if (strncmp(name, varlist, strlen(name)) == 0) {
+                       varlist += strlen(name);
+                       /* strip trailing spaces */
+                       for (varlist; isspace(varlist[0]); varlist++);
+
+                       if (varlist[0] == '=') {
+                               /* We matched the key, go past the = sign */
+                               varlist++;
+                               /* strip leading spaces */
+                               for (varlist; isspace(varlist[0]); varlist++);
+
+                               if (tmp = index(varlist, ',')) {
+                                       /* Value is delimited by a comma */
+                                       if (tmp-varlist == 0) continue;
+                                       value = (char *)malloc(tmp-varlist+1);
+                                       strncpy(value, varlist, tmp-varlist);
+                                       value[tmp-varlist] = '\0';
+                               } else {
+                                       /* Value is delimited by a \0 */
+                                       if (strlen(varlist) == 0) continue;
+                                       value = (char *)malloc(strlen(varlist) + 1);
+                                       strncpy(value, varlist, strlen(varlist));
+                                       value[strlen(varlist)] = '\0';
+                               }
+                               break;
+                       }
+               }
+               if (tmp = index(varlist, ',')) {
+                       /* More keys, keep going... */
+                       varlist = tmp + 1;
+               } else {
+                       /* We're done */
+                       break;
                }
-       } while (last == 0 && (tmpkey = strtok(NULL, "=")));
+       }
+
+       /* Clean-up trailing spaces/newlines */
+       if (value) for (i=strlen(value)-1; isspace(value[i]); i--) value[i] = '\0';
 
        return value;
 }
@@ -552,6 +585,9 @@ int main(int argc, char *argv[]){
        bindtextdomain (PACKAGE, LOCALEDIR);
        textdomain (PACKAGE);
 
+       /* Parse extra opts if any */
+       argv=np_extra_opts (&argc, argv, progname);
+
        if (process_arguments (argc, argv) == ERROR)
                usage4 (_("Could not parse arguments"));
 
@@ -638,6 +674,7 @@ void print_help(void){
 
        print_usage();
        printf (_(UT_HELP_VRSN));
+       printf (_(UT_EXTRA_OPTS));
        printf (_(UT_HOST_PORT), 'p', "123");
        printf (" %s\n", "-q, --quiet");
        printf ("    %s\n", _("Returns UNKNOWN instead of CRITICAL or WARNING if server isn't synchronized"));
@@ -664,17 +701,23 @@ void print_help(void){
        printf(" %s\n", _("Use this plugin to check the health of an NTP server. It supports"));
        printf(" %s\n", _("checking the offset with the sync peer, the jitter and stratum. This"));
        printf(" %s\n", _("plugin will not check the clock offset between the local host and NTP"));
-       printf(" %s\n\n", _("server; please use check_ntp_time for that purpose."));
-
+       printf(" %s\n", _("server; please use check_ntp_time for that purpose."));
+       printf("\n");
        printf(_(UT_THRESHOLDS_NOTES));
+#ifdef NP_EXTRA_OPTS
+       printf("\n");
+       printf(_(UT_EXTRA_OPTS_NOTES));
+#endif
 
        printf("\n");
        printf("%s\n", _("Examples:"));
        printf(" %s\n", _("Simple NTP server check:"));
        printf("  %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1"));
+       printf("\n");
        printf(" %s\n", _("Check jitter too, avoiding critical notifications if jitter isn't available"));
        printf(" %s\n", _("(See Notes above for more details on thresholds formats):"));
        printf("  %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200"));
+       printf("\n");
        printf(" %s\n", _("Check only stratum:"));
        printf("  %s\n", ("./check_ntp_peer -H ntpserv -W 4 -C 6"));