Esempio n. 1
0
int main(int argc, char *argv[]) {
    int result, offset_result;
    double offset=0;
    char *result_line, *perfdata_line;

    setlocale (LC_ALL, "");
    bindtextdomain (PACKAGE, LOCALEDIR);
    textdomain (PACKAGE);

    result = offset_result = STATE_OK;

    /* Parse extra opts if any */
    argv=np_extra_opts (&argc, argv, progname);

    if (process_arguments (argc, argv) == ERROR)
        usage4 (_("Could not parse arguments"));

    set_thresholds(&offset_thresholds, owarn, ocrit);

    /* initialize alarm signal handling */
    signal (SIGALRM, socket_timeout_alarm_handler);

    /* set socket timeout */
    alarm (socket_timeout);

    offset = offset_request(server_address, &offset_result);
    if (offset_result == STATE_UNKNOWN) {
        result = (quiet == 1 ? STATE_UNKNOWN : STATE_CRITICAL);
    } else {
        result = get_status(fabs(offset), offset_thresholds);
    }

    switch (result) {
    case STATE_CRITICAL :
        xasprintf(&result_line, _("NTP CRITICAL:"));
        break;
    case STATE_WARNING :
        xasprintf(&result_line, _("NTP WARNING:"));
        break;
    case STATE_OK :
        xasprintf(&result_line, _("NTP OK:"));
        break;
    default :
        xasprintf(&result_line, _("NTP UNKNOWN:"));
        break;
    }
    if(offset_result == STATE_UNKNOWN) {
        xasprintf(&result_line, "%s %s", result_line, _("Offset unknown"));
        xasprintf(&perfdata_line, "");
    } else {
        xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset);
        xasprintf(&perfdata_line, "%s", perfd_offset(offset));
    }
    printf("%s|%s\n", result_line, perfdata_line);

    if(server_address!=NULL) free(server_address);
    return result;
}
Esempio n. 2
0
int main(int argc, char *argv[]){
	int result, offset_result, jitter_result;
	double offset=0, jitter=0;
	char *result_line, *perfdata_line;

	setlocale (LC_ALL, "");
	bindtextdomain (PACKAGE, LOCALEDIR);
	textdomain (PACKAGE);

	result = offset_result = jitter_result = STATE_OK;

	/* Parse extra opts if any */
	argv=np_extra_opts (&argc, argv, progname);

	if (process_arguments (argc, argv) == ERROR)
		usage4 (_("Could not parse arguments"));

	set_thresholds(&offset_thresholds, owarn, ocrit);
	set_thresholds(&jitter_thresholds, jwarn, jcrit);

	/* initialize alarm signal handling */
	signal (SIGALRM, socket_timeout_alarm_handler);

	/* set socket timeout */
	alarm (socket_timeout);

	offset = offset_request(server_address, &offset_result);
	/* check_ntp used to always return CRITICAL if offset_result == STATE_UNKNOWN.
	 * Now we'll only do that is the offset thresholds were set */
	if (do_offset && offset_result == STATE_UNKNOWN) {
		result = STATE_CRITICAL;
	} else {
		result = get_status(fabs(offset), offset_thresholds);
	}

	/* If not told to check the jitter, we don't even send packets.
	 * jitter is checked using NTP control packets, which not all
	 * servers recognize.  Trying to check the jitter on OpenNTPD
	 * (for example) will result in an error
	 */
	if(do_jitter){
		jitter=jitter_request(server_address, &jitter_result);
		result = max_state_alt(result, get_status(jitter, jitter_thresholds));
		/* -1 indicates that we couldn't calculate the jitter
		 * Only overrides STATE_OK from the offset */
		if(jitter == -1.0 && result == STATE_OK)
			result = STATE_UNKNOWN;
	}
	result = max_state_alt(result, jitter_result);

	switch (result) {
		case STATE_CRITICAL :
			xasprintf(&result_line, _("NTP CRITICAL:"));
			break;
		case STATE_WARNING :
			xasprintf(&result_line, _("NTP WARNING:"));
			break;
		case STATE_OK :
			xasprintf(&result_line, _("NTP OK:"));
			break;
		default :
			xasprintf(&result_line, _("NTP UNKNOWN:"));
			break;
	}
	if(offset_result == STATE_UNKNOWN){
		xasprintf(&result_line, "%s %s", result_line, _("Offset unknown"));
		xasprintf(&perfdata_line, "");
	} else {
		xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset);
		xasprintf(&perfdata_line, "%s", perfd_offset(offset));
	}
	if (do_jitter) {
		xasprintf(&result_line, "%s, jitter=%f", result_line, jitter);
		xasprintf(&perfdata_line, "%s %s", perfdata_line,  perfd_jitter(jitter));
	}
	printf("%s|%s\n", result_line, perfdata_line);

	if(server_address!=NULL) free(server_address);
	return result;
}