Ejemplo n.º 1
0
int
main (int argc, char *argv[])
{

	LDAP *ld;
	LDAPMessage *result;

	/* should be 	int result = STATE_UNKNOWN; */

	int status = STATE_UNKNOWN;
	long microsec;
	double elapsed_time;

	/* for ldap tls */

	int tls;
	int version=3;

	/* for entry counting */

	LDAPMessage *next_entry;
	int status_entries = STATE_OK;
	int num_entries = 0;

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

	if (strstr(argv[0],"check_ldaps")) {
		xasprintf (&progname, "check_ldaps");
 	}

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

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

	if (strstr(argv[0],"check_ldaps") && ! starttls && ! ssl_on_connect)
		starttls = TRUE;

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

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

	/* get the start time */
	gettimeofday (&tv, NULL);

	/* initialize ldap */
	if (ld_uri != NULL)
	{
#ifdef HAVE_LDAP_INITIALIZE
		int result = ldap_initialize(&ld, ld_uri);
		if (result != LDAP_SUCCESS)
		{
			printf ("Failed to connect to LDAP server at %s: %s\n",
				ld_uri, ldap_err2string(result));
			return STATE_CRITICAL;
		}
#else
		printf ("Sorry, this version of %s was compiled without URI support!\n",
			argv[0]);
		return STATE_CRITICAL;
#endif
	}
#ifdef HAVE_LDAP_INIT
	else if (!(ld = ldap_init (ld_host, ld_port))) {
		printf ("Could not connect to the server at port %i\n", ld_port);
		return STATE_CRITICAL;
	}
#else
	else if (!(ld = ldap_open (ld_host, ld_port))) {
		if (verbose)
			ldap_perror(ld, "ldap_open");
		printf (_("Could not connect to the server at port %i\n"), ld_port);
		return STATE_CRITICAL;
	}
#endif /* HAVE_LDAP_INIT */

#ifdef HAVE_LDAP_SET_OPTION
	/* set ldap options */
	if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ld_protocol) !=
			LDAP_OPT_SUCCESS ) {
		printf(_("Could not set protocol version %d\n"), ld_protocol);
		return STATE_CRITICAL;
	}
#endif

	if (ld_port == LDAPS_PORT || ssl_on_connect) {
		xasprintf (&SERVICE, "LDAPS");
#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS)
		/* ldaps: set option tls */
		tls = LDAP_OPT_X_TLS_HARD;

		if (ldap_set_option (ld, LDAP_OPT_X_TLS, &tls) != LDAP_SUCCESS)
		{
			if (verbose)
				ldap_perror(ld, "ldaps_option");
			printf (_("Could not init TLS at port %i!\n"), ld_port);
			return STATE_CRITICAL;
		}
#else
		printf (_("TLS not supported by the libraries!\n"));
		return STATE_CRITICAL;
#endif /* LDAP_OPT_X_TLS */
	} else if (starttls) {
		xasprintf (&SERVICE, "LDAP-TLS");
#if defined(HAVE_LDAP_SET_OPTION) && defined(HAVE_LDAP_START_TLS_S)
		/* ldap with startTLS: set option version */
		if (ldap_get_option(ld,LDAP_OPT_PROTOCOL_VERSION, &version) == LDAP_OPT_SUCCESS )
		{
			if (version < LDAP_VERSION3)
			{
				version = LDAP_VERSION3;
				ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version);
			}
		}
		/* call start_tls */
		if (ldap_start_tls_s(ld, NULL, NULL) != LDAP_SUCCESS)
		{
			if (verbose)
				ldap_perror(ld, "ldap_start_tls");
			printf (_("Could not init startTLS at port %i!\n"), ld_port);
			return STATE_CRITICAL;
		}
#else
		printf (_("startTLS not supported by the library, needs LDAPv3!\n"));
		return STATE_CRITICAL;
#endif /* HAVE_LDAP_START_TLS_S */
	}

	/* bind to the ldap server */
	if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
			LDAP_SUCCESS) {
		if (verbose)
			ldap_perror(ld, "ldap_bind");
		printf (_("Could not bind to the LDAP server\n"));
		return STATE_CRITICAL;
	}

	/* do a search of all objectclasses in the base dn */
	if (ldap_search_s (ld, ld_base, (crit_entries!=NULL || warn_entries!=NULL) ? LDAP_SCOPE_SUBTREE : LDAP_SCOPE_BASE, ld_attr, NULL, 0, &result)
			!= LDAP_SUCCESS) {
		if (verbose)
			ldap_perror(ld, "ldap_search");
		printf (_("Could not search/find objectclasses in %s\n"), ld_base);
		return STATE_CRITICAL;
	} else if (crit_entries!=NULL || warn_entries!=NULL) {
		num_entries = ldap_count_entries(ld, result);
	}

	/* unbind from the ldap server */
	ldap_unbind (ld);

	/* reset the alarm handler */
	alarm (0);

	/* calcutate the elapsed time and compare to thresholds */

	microsec = deltime (tv);
	elapsed_time = (double)microsec / 1.0e6;

	if (crit_time!=UNDEFINED && elapsed_time>crit_time)
		status = STATE_CRITICAL;
	else if (warn_time!=UNDEFINED && elapsed_time>warn_time)
		status = STATE_WARNING;
	else
		status = STATE_OK;

	if(entries_thresholds != NULL) {
		if (verbose) {
			printf ("entries found: %d\n", num_entries);
			print_thresholds("entry threasholds", entries_thresholds);
		}
		status_entries = get_status(num_entries, entries_thresholds);
		if (status_entries == STATE_CRITICAL) {
			status = STATE_CRITICAL;
		} else if (status != STATE_CRITICAL) {
			status = status_entries;
		}
	}

	/* print out the result */
	if (crit_entries!=NULL || warn_entries!=NULL) {
		printf (_("LDAP %s - found %d entries in %.3f seconds|%s %s\n"),
			state_text (status),
			num_entries,
			elapsed_time,
			fperfdata ("time", elapsed_time, "s",
				(int)warn_time, warn_time,
				(int)crit_time, crit_time,
				TRUE, 0, FALSE, 0),
			sperfdata ("entries", (double)num_entries, "",
				warn_entries,
				crit_entries,
				TRUE, 0.0, FALSE, 0.0));
	} else {
		printf (_("LDAP %s - %.3f seconds response time|%s\n"),
			state_text (status),
			elapsed_time,
			fperfdata ("time", elapsed_time, "s",
				(int)warn_time, warn_time,
				(int)crit_time, crit_time,
				TRUE, 0, FALSE, 0));
	}

	return status;
}
Ejemplo n.º 2
0
int main(int argc, char **argv){
	char *ptr;
	int data_val;
	int return_code=STATE_OK;
	thresholds *thresholds = NULL;

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

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

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

	/* Initialize the thresholds */
	set_thresholds(&thresholds, warn_threshold, crit_threshold);
	if(verbose)
		print_thresholds("check_cluster", thresholds);

	/* check the data values */
	for(ptr=strtok(data_vals,",");ptr!=NULL;ptr=strtok(NULL,",")){

		data_val=atoi(ptr);

		if(check_type==CHECK_SERVICES){
			switch(data_val){
			case 0:
				total_services_ok++;
				break;
			case 1:
				total_services_warning++;
				break;
			case 2:
				total_services_critical++;
				break;
			case 3:
				total_services_unknown++;
				break;
			default:
				break;
		        }
	        }
		else{
			switch(data_val){
			case 0:
				total_hosts_up++;
				break;
			case 1:
				total_hosts_down++;
				break;
			case 2:
				total_hosts_unreachable++;
				break;
			default:
				break;
		        }
	        }
        }


	/* return the status of the cluster */
	if(check_type==CHECK_SERVICES){
		return_code=get_status(total_services_warning+total_services_unknown+total_services_critical, thresholds);
		printf("CLUSTER %s: %s: %d ok, %d warning, %d unknown, %d critical\n",
			state_text(return_code), (label==NULL)?"Service cluster":label,
			total_services_ok,total_services_warning,
			total_services_unknown,total_services_critical);
	}
	else{
		return_code=get_status(total_hosts_down+total_hosts_unreachable, thresholds);
		printf("CLUSTER %s: %s: %d up, %d down, %d unreachable\n",
			state_text(return_code), (label==NULL)?"Host cluster":label,
			total_hosts_up,total_hosts_down,total_hosts_unreachable);
	}

	return return_code;
}
Ejemplo n.º 3
0
int process_arguments (int argc, char **argv) {
    int c;
    int option = 0;

    static struct option longopts[] = {
            MP_LONGOPTS_DEFAULT,
            MP_LONGOPTS_HOST,
            MP_LONGOPTS_PORT,
            {"extended-status", no_argument, NULL, (int) 'e'},
            SNMP_LONGOPTS,
            MP_LONGOPTS_END
    };

    if (argc < 3) {
       print_help();
       exit(STATE_UNKNOWN);
    }

    mp_snmp_version = SNMP_VERSION_1;

    while (1) {
        c = mp_getopt(&argc, &argv, MP_OPTSTR_DEFAULT"H:P:ew:c:W:Z:"SNMP_OPTSTR,
                      longopts, &option);

        if (c == -1 || c == EOF)
            break;

        getopt_snmp(c);

        switch (c) {
            /* Hostname opt */
            case 'H':
                getopt_host(optarg, &hostname);
                break;
            /* Port opt */
            case 'P':
                getopt_port(optarg, &port);
                break;
            /* Plugin opt */
            case 'e':
                extended_status = 1;
                break;
            case 'w':
                getopt_wc_at(c, optarg, &threshold_charge);
                break;
            case 'c':
                getopt_wc_at(c, optarg, &threshold_charge);
                break;
            case 'W':
                getopt_wc_at('w', optarg, &threshold_runtime);
                break;
            case 'Z':
                getopt_wc_at('c', optarg, &threshold_runtime);
                break;
        }
    }

    if (mp_verbose > 0) {
        print_thresholds("charge", threshold_charge);
        print_thresholds("runtime", threshold_runtime);
    }

    /* sanity check: charge threshold */
    if ((threshold_charge->warning->end < 0) ||
        (threshold_charge->warning->end > 100)) {
        usage("Warning charge threshold must be between 0 and 100.");
    }
    if ((threshold_charge->critical->end < 0) ||
        (threshold_charge->critical->end > 100)) {
        usage("Critical charge threshold must be between 0 and 100.");
    }
    if (threshold_charge->critical->end > threshold_charge->warning->end) {
        usage("Warning charge threshold (%ld) must be larger than "
              "critical charge threshold (%ld).",
              (long) threshold_charge->warning->end,
              (long) threshold_charge->critical->end);
    }

    /* sanity check: runtime threshold */
    if (threshold_runtime->warning->end < 0) {
        usage("Warning runtime threshold must be larher or equal to 0.");
    }
    if (threshold_runtime->critical->end < 0) {
        usage("Critical runtime threshold must be larher or equal to 0.");
    }
    if (threshold_runtime->critical->end > threshold_runtime->warning->end) {
        usage("Warning runtime threshold (%ld) must be larger than "
              "runtime charge threshold (%ld).",
              (long) threshold_runtime->warning->end,
              (long) threshold_runtime->critical->end);
    }

    return(OK);
}
Ejemplo n.º 4
0
int main (int argc, char **argv) {
    /* Local Vars */
    CURL        *curl;
    char        *url;
    double      size;
    double      time;

    /* Set signal handling and alarm */
    if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR)
        critical("Setup SIGALRM trap failed!");

    /* Process check arguments */
    if (process_arguments(argc, argv) != OK)
        unknown("Parsing arguments failed!");

    /* Start plugin timeout */
    alarm(mp_timeout);

    /* Magik */

    size_t urllen = strlen(hostname)+strlen(filename) + 9;

    url = mp_malloc(urllen);

    snprintf(url, urllen, "tftp://%s/%s", hostname, filename);

    if (mp_verbose > 0) {
        printf("CURL Version: %s\n", curl_version());
        printf("Try fetch %s\n", url);
        print_thresholds("fetch_thresholds", fetch_thresholds);
    }

    /* Init libcurl */
    curl = mp_curl_init();

    /* Setup request */
    if (curl_easy_setopt(curl, CURLOPT_PROTOCOLS, CURLPROTO_TFTP) == CURLE_UNSUPPORTED_PROTOCOL)
        unknown("libcurl don't support tftp.");
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, mp_curl_recv_blackhole);
    if (port != 0)
        curl_easy_setopt(curl, CURLOPT_LOCALPORT, port);

    /* Perform Request */
    mp_curl_perform(curl);

    /* Get metric */
    curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &time);
    curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD , &size);

    /* Clenup libcurl */
    curl_easy_cleanup(curl);
    curl_global_cleanup();

    mp_perfdata_float("time", (float)time, "s", fetch_thresholds);
    free(url);

    switch(get_status(time, fetch_thresholds)) {
    case STATE_OK:
        free_threshold(fetch_thresholds);
        ok("Received %'.0fbyte in %fs.", size, time);
        break;
    case STATE_WARNING:
        free_threshold(fetch_thresholds);
        warning("Received %'.0fbyte in %fs.", size, time);
        break;
    case STATE_CRITICAL:
        free_threshold(fetch_thresholds);
        critical("Received %'.0fbyte in %fs.", size, time);
        break;
    }
    free_threshold(fetch_thresholds);

    critical("You should never reach this point.");
}
int main (int argc, char **argv) {
    /* Local Vars */
    CURL                *curl;
    struct mp_curl_data answer;
    long int            code;
    int                 sb_wait = 0;
    int                 sb_start = 0;
    int                 sb_recv = 0;
    int                 sb_send = 0;
    int                 sb_keep = 0;
    int                 sb_dns = 0;
    int                 sb_close = 0;
    int                 sb_log = 0;
    int                 sb_grace = 0;
    int                 sb_idle = 0;
    int                 sb_open = 0;
    char                *buf = NULL, *key, *val;
    char                *server = NULL;

    /* Set signal handling and alarm */
    if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR)
        critical("Setup SIGALRM trap failed!");

    /* Process check arguments */
    if (process_arguments(argc, argv) != OK)
        unknown("Parsing arguments failed!");

    /* Start plugin timeout */
    alarm(mp_timeout);

    /* Build query */
    if (mp_verbose > 0) {
        printf("CURL Version: %s\n", curl_version());
        printf("Url: %s\n", url);
        print_thresholds("open_thresholds", open_thresholds);
    }

    /* Headers */
    struct mp_curl_header headers[] = {
        {"Server", &server}, {NULL, NULL}
    };

    /* Init libcurl */
    curl = mp_curl_init();
    answer.data = NULL;
    answer.size = 0;

    /* Setup request */
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, mp_curl_recv_data);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&answer);
    curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, mp_curl_recv_header);
    curl_easy_setopt(curl, CURLOPT_HEADERDATA, (void *)headers);

    /* Get url */
    code = mp_curl_perform(curl);

    /* Cleanup libcurl */
    curl_easy_cleanup(curl);
    curl_global_cleanup();

    if (mp_verbose > 1) {
        printf("Code: %ld\n", code);
        printf("Server: %s\n", server);
        if (mp_verbose > 2) {
            printf("Answer: '%s'\n", answer.data);
        }
    }

    /* Parse Answer */
    buf = answer.data;

    while(*buf) {
        val = strsep(&buf, "\n");
        key = strsep(&val, ": ");

        if (mp_verbose > 1) {
            printf("%s => %s\n", key, val);
        }

        if(strcmp(key, "Scoreboard") == 0) {
            for(; *val; val++) {
                switch (*val) {
                    case '.':
                        sb_open++;
                        break;
                    case '_':
                        sb_wait++;
                        break;
                    case 'S':
                        sb_start++;
                        break;
                    case 'R':
                        sb_recv++;
                        break;
                    case 'W':
                        sb_send++;
                        break;
                    case 'K':
                        sb_keep++;
                        break;
                    case 'D':
                        sb_dns++;
                        break;
                    case 'C':
                        sb_close++;
                        break;
                    case 'L':
                        sb_log++;
                        break;
                    case 'G':
                        sb_grace++;
                        break;
                    case 'I':
                        sb_idle++;
                        break;
                }
            }
        }
    }

    mp_perfdata_int("open", (long int)sb_open, "c", open_thresholds);
    mp_perfdata_int("start", (long int)sb_start, "c", NULL);
    mp_perfdata_int("read", (long int)sb_recv, "c", NULL);
    mp_perfdata_int("write", (long int)sb_send, "c", NULL);
    mp_perfdata_int("keep", (long int)sb_keep, "c", NULL);
    mp_perfdata_int("dns", (long int)sb_dns, "c", NULL);
    mp_perfdata_int("close", (long int)sb_close, "c", NULL);
    mp_perfdata_int("log", (long int)sb_log, "c", NULL);
    mp_perfdata_int("grace", (long int)sb_grace, "c", NULL);
    mp_perfdata_int("idle", (long int)sb_idle, "c", NULL);
    mp_perfdata_int("open", (long int)sb_open, "c", NULL);

    /* free */
    free(answer.data);

    if (open_thresholds) {
        switch(get_status(sb_open, open_thresholds)) {
        case STATE_OK:
            free_threshold(open_thresholds);
            ok("Apache HTTPD status - %s", server);
            break;
        case STATE_WARNING:
            free_threshold(open_thresholds);
            warning("Apache HTTPD low on open slots - %s", server);
            break;
        case STATE_CRITICAL:
            free_threshold(open_thresholds);
            critical("Apache HTTPD not enough open slots - %s", server);
            break;
        }
    }
    ok("Apache HTTPD status - %s", server);
}