Exemplo n.º 1
0
int process_arguments (int argc, char **argv) {
    int c;
    int option = 0;

    static struct option longopts[] = {
        MP_LONGOPTS_DEFAULT,
        MP_LONGOPTS_WC,
        MP_LONGOPTS_END,
    };

    /* Set default */
    setWarn(&usage_thresholds, "90", 0);
    setCrit(&usage_thresholds, "95", 0);

    while (1) {
        c = mp_getopt(&argc, &argv, MP_OPTSTR_DEFAULT"c:w:", longopts, &option);

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

        getopt_wc(c, optarg, &usage_thresholds);

    }

    /* Check requirements */

    return(OK);
}
Exemplo n.º 2
0
AdvancedOptionsWidget::AdvancedOptionsWidget( QWidget *parent ) :
	OptionsDialogPage( parent ),
	ui( new Ui::AdvancedOptionsWidget ) {
	ui->setupUi( this );

	connect( ui->labelTrace, SIGNAL( clicked() ), this, SLOT( setTrace() ) );
	connect( ui->labelDebug, SIGNAL( clicked() ), this, SLOT( setDebug() ) );
	connect( ui->labelInfo, SIGNAL( clicked() ), this, SLOT( setInfo() ) );
	connect( ui->labelWarn, SIGNAL( clicked() ), this, SLOT( setWarn() ) );
	connect( ui->labelError, SIGNAL( clicked() ), this, SLOT( setError() ) );
	connect( ui->labelFatal, SIGNAL( clicked() ), this, SLOT( setFatal() ) );

	ui->loggingLevel->setValue( QsLogging::Logger::instance().loggingLevel() );
}
Exemplo 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,
        {"packets", required_argument, 0, 'p'},
        {"quick", no_argument, 0, 'q'},
        {"interval", required_argument, 0, 'i'},
        {"interface", required_argument, 0, 'I'},
        {"ttl", required_argument, 0, 'T'},
        MP_LONGOPTS_WC,
        MP_LONGOPTS_END
    };

    if (argc < 2) {
        print_help();
        exit(STATE_OK);
    }

    /* Set default */
    setWarn(&rta_thresholds, "~:90", NOEXT);
    setWarn(&lost_thresholds, "80", NOEXT);
    setCrit(&rta_thresholds, "~:100", NOEXT);
    setCrit(&lost_thresholds, "90", NOEXT);

    while (1) {
        c = mp_getopt(&argc, &argv, MP_OPTSTR_DEFAULT"H:46p:qi:I:T:w:c:", longopts, &option);

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

        getopt_46(c, &ipv);

        switch (c) {
        /* Hostname opt */
        case 'H':
            getopt_host(optarg, &hostname);
            break;
        /* Packets opt */
        case 'p':
            if (!is_integer(optarg))
                usage("Illegal packets value '%s'.", optarg);
            packets = (int)strtol(optarg, NULL, 10);
            break;
        /* Quick opt */
        case 'q':
            quick = 1;
            break;
        /* Interface opt */
        case 'I':
            interface = optarg;
            break;
        /* Interval opt */
        case 'i': {
            double val;
            val = strtod(optarg, NULL);
            if (val <= 0)
                usage("Illegal interval value '%s'.", optarg);
            interval.tv_sec = (int)val;
            interval.tv_nsec = ((int)(val*1000000000))%1000000000;
        }
        break;
        /* TTL opt */
        case 'T':
            if (!is_integer(optarg))
                usage("Illegal ttl value '%s'.", optarg);
            ttl = (int)strtol(optarg, NULL, 10);
            if (ttl < 1 || ttl > 255)
                usage("Illegal ttl value '%s'.", optarg);
            break;
        /* Warn/Crit opt */
        case 'w':
        case 'c': {
            char *rta, *pl;
            pl = optarg;
            rta = strsep(&pl, ",");
            if (rta == NULL || pl == NULL) {
                usage("Waring/Critical threshold is <rta>,<pl>%%.");
            }
            if (c == 'w') {
                if (setWarn(&rta_thresholds, rta, NOEXT) == ERROR)
                    usage("Illegal -w tra threshold '%s'.", rta);
                if (setWarn(&lost_thresholds, pl, NOEXT) == ERROR)
                    usage("Illegal -w pl threshold '%s'.", pl);
            } else {
                if (setCrit(&rta_thresholds, rta, NOEXT) == ERROR)
                    usage("Illegal -c tra threshold '%s'.", rta);
                if (setCrit(&lost_thresholds, pl, NOEXT) == ERROR)
                    usage("Illegal -c pl threshold '%s'.", pl);
            }
        }
        break;
        }
    }

    /* Check requirements */
    if (!hostname)
        usage("Hostname is mandatory.");

    return(OK);
}
Exemplo n.º 4
0
int main (int argc, char **argv) {
    /* Local Vars */
    int             state = STATE_OK;
    char            *ups_ident              = NULL;
    long            ups_battery_status      = LONG_MIN;
    long            ups_seconds_on_battery  = LONG_MIN;
    long            ups_remaining_runtime   = LONG_MIN;
    long            ups_remaining_charge    = LONG_MIN;
    long            ups_battery_voltage     = LONG_MIN;
    long            ups_battery_current     = LONG_MIN;
    long            ups_battery_temperature = LONG_MIN;
    long            ups_input_line_bads     = LONG_MIN;
    long            ups_input_lines         = LONG_MIN;
    long            ups_output_source       = LONG_MIN;
    long            ups_output_frequency    = LONG_MIN;
    long            ups_output_lines        = LONG_MIN;
    long            ups_alarms_present      = LONG_MIN;
    char            *output = NULL;
    char            buf[64];
    netsnmp_session *snmp_session;

    mp_snmp_query_cmd snmpcmd[] = {
        {{1,3,6,1,2,1,33,1,1,5,0}, 11, ASN_OCTET_STR,
         (void *)&ups_ident, 0},
        {{1,3,6,1,2,1,33,1,2,1,0}, 11, ASN_INTEGER,
         (void *)&ups_battery_status, sizeof(long int)},
        {{1,3,6,1,2,1,33,1,2,2,0}, 11, ASN_INTEGER,
         (void *)&ups_seconds_on_battery, sizeof(long int)},
        {{1,3,6,1,2,1,33,1,2,3,0}, 11, ASN_INTEGER,
         (void *)&ups_remaining_runtime, sizeof(long int)},
        {{1,3,6,1,2,1,33,1,2,4,0}, 11, ASN_INTEGER,
         (void *)&ups_remaining_charge, sizeof(long int)},
        {{1,3,6,1,2,1,33,1,2,5,0}, 11, ASN_INTEGER,
         (void *)&ups_battery_voltage, sizeof(long int)},     /* 0.1 volts DC */
        {{1,3,6,1,2,1,33,1,2,6,0}, 11, ASN_INTEGER,
         (void *)&ups_battery_current, sizeof(long int)},     /* 0.1 amps DC */
        {{1,3,6,1,2,1,33,1,2,7,0}, 11, ASN_INTEGER,
         (void *)&ups_battery_temperature, sizeof(long int)}, /* deg C */
        {{1,3,6,1,2,1,33,1,3,1,0}, 11, ASN_COUNTER,
         (void *)&ups_input_line_bads, sizeof(long int)},
        {{1,3,6,1,2,1,33,1,3,2,0}, 11, ASN_INTEGER,
         (void *)&ups_input_lines, sizeof(long int)},
        {{1,3,6,1,2,1,33,1,4,1,0}, 11, ASN_INTEGER,
         (void *)&ups_output_source, sizeof(long int)},
        {{1,3,6,1,2,1,33,1,4,2,0}, 11, ASN_INTEGER,
         (void *)&ups_output_frequency, sizeof(long int)}, /* 0.1 RMS */
        {{1,3,6,1,2,1,33,1,4,3,0}, 11, ASN_INTEGER,
         (void *)&ups_output_lines, sizeof(long int)},
        {{1,3,6,1,2,1,33,1,6,1,0}, 11, ASN_GAUGE,
         (void *)&ups_alarms_present, sizeof(long int)},
        {{0}, 0, 0, NULL},
    };

    /* set threshold defaults */
    setWarn(&threshold_charge, DEFAULT_CHARGE_WARNING, NOEXT);
    setCrit(&threshold_charge, DEFAULT_CHARGE_CRITICAL, NOEXT);
    setWarn(&threshold_runtime, DEFAULT_RUNTIME_WARNING, NOEXT);
    setCrit(&threshold_runtime, DEFAULT_RUNTIME_CRITICAL, NOEXT);

    /* 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);

    snmp_session = mp_snmp_init();
    mp_snmp_query(snmp_session, snmpcmd);
    mp_snmp_deinit();

    if (mp_verbose > 1) {
        printf("battery status: %ld\n", ups_battery_status);
        printf("output source: %ld\n", ups_output_source);
        printf("alarms present: %ld\n", ups_alarms_present);
        printf("input lines: %ld\n", ups_input_lines);
        printf("output lines: %ld\n", ups_output_lines);
        printf("input line bads: %ld\n", ups_input_line_bads);
    }

    if (ups_ident) {
        if (*ups_ident != '\0') {
            mp_snprintf((char *)&buf, sizeof(buf), "[%s] ", ups_ident);
            mp_strcat(&output, buf);
        }
        free(ups_ident);
    }

    /* always warning, if on battery */
    if ((ups_seconds_on_battery > 0) ||
        (ups_output_source == OUTPUT_BATTERY)) {
        state = STATE_WARNING;
        if (ups_seconds_on_battery > 0) {
            mp_snprintf((char *) &buf, sizeof(buf),
                        "ON BATTERY (since %d sec%s), ",
                        ups_seconds_on_battery,
                        ((ups_seconds_on_battery != 1) ? "s" : ""));
            mp_strcat(&output, buf);
        } else {
            mp_strcat(&output, "ON BATTERY, ");
        }
    }

    /* check alarms */
    if ((ups_alarms_present != LONG_MIN) && (ups_alarms_present > 0)) {
        if (state != STATE_CRITICAL)
            state = STATE_WARNING;

        if (ups_alarms_present == 1)
            mp_strcat(&output, "UPS REPORTS AN ALARM, ");
        else
            mp_strcat(&output, "UPS REPORTS MULTIPLE ALARMS, ");
    }

    /* check remaining runtime threshold */
    if (ups_remaining_runtime > LONG_MIN) {
        switch (get_status(ups_remaining_runtime, threshold_runtime)) {
        case STATE_CRITICAL:
            state = STATE_CRITICAL;
            mp_strcat(&output, "RUNTIME CRITICAL, ");
            break;
        case STATE_WARNING:
            if (state == STATE_OK)
                state = STATE_WARNING;
            mp_strcat(&output, "RUNTIME WARNING, ");
            break;
        } /* switch */
    }

    /* check remaining charge threshold */
    if (ups_remaining_charge > LONG_MIN) {
        switch (get_status(ups_remaining_charge, threshold_charge)) {
        case STATE_CRITICAL:
            state = STATE_CRITICAL;
            mp_strcat(&output, "CHARGE CRITICAL, ");
            break;
        case STATE_WARNING:
            if (state == STATE_OK)
                state = STATE_WARNING;
            mp_strcat(&output, "CHARGE WARNING, ");
            break;
        } /* switch */
    }

    /* check battery status */
    switch (ups_battery_status) {
    case BATTERY_NORMAL:
        break;
    case BATTERY_LOW:
        state = STATE_CRITICAL;
        break;
    case BATTERY_DEPLETED:
        state = STATE_CRITICAL;
        break;
    case BATTERY_UNKNOWN:
        /* FALL-THROUGH */
    default:
        if (state == STATE_OK)
            state = STATE_UNKNOWN;
        break;
    } /* switch */

    /* check output source */
    switch (ups_output_source) {
    case OUTPUT_NORMAL:
        /* DO NOTHING */
        break;
    case OUTPUT_OTHER:
        /* FALL-THROUGH */
    case OUTPUT_BYPASS:
        /* FALL-THROUGH */
    case OUTPUT_BOOSTER:
        /* FALL-THROUGH */
    case OUTPUT_REDUCER:
        /* FALL-THROUGH */
    case OUTPUT_BATTERY:
        /* FALL-THROUGH */
        if (state != STATE_CRITICAL)
            state = STATE_WARNING;
        break;
    case OUTPUT_NONE:
        state = STATE_CRITICAL;
        break;
    default:
        if (state == STATE_OK)
            state = STATE_UNKNOWN;
        break;
    } /* switch */

    /*
     * status line
     */
    mp_snprintf((char *) &buf, sizeof(buf), "battery: %s",
                battery_status_to_string(ups_battery_status));
    mp_strcat(&output, buf);

    if ((ups_remaining_runtime > LONG_MIN) &&
        (ups_remaining_charge > LONG_MIN)) {
        mp_snprintf((char *) &buf, sizeof(buf),
                    "remaining: %ld min%s [%d%%]",
                    ups_remaining_runtime,
                    ((ups_remaining_runtime != 1) ? "s" : ""),
                    (int) ups_remaining_charge);
        mp_strcat_comma(&output, buf);
    } else if (ups_remaining_runtime > LONG_MIN) {
        mp_snprintf((char *) &buf, sizeof(buf),
                    "remaining: %ld min%s",
                    ups_remaining_runtime,
                    ((ups_remaining_runtime != 1) ? "s" : ""));
        mp_strcat_comma(&output, buf);
    } else if (ups_remaining_charge > LONG_MIN) {
        mp_snprintf((char *) &buf, sizeof(buf),
                    "remaining: %d%%",
                    (int) ups_remaining_charge);
        mp_strcat_comma(&output, buf);
    }

    mp_snprintf((char *) &buf, sizeof(buf), "source: %s",
                output_source_to_string(ups_output_source));
    mp_strcat_comma(&output, buf);

    if (extended_status) {
        if (ups_battery_voltage > LONG_MIN) {
            mp_snprintf((char *) &buf, sizeof(buf),
                        "voltage: %1.0f V",
                        (ups_battery_voltage * 0.1f));
            mp_strcat_comma(&output, buf);
        }
        if (ups_battery_current > LONG_MIN) {
            mp_snprintf((char *) &buf, sizeof(buf),
                        "current: %1.1f A",
                        (ups_battery_current * 0.1f));
            mp_strcat_comma(&output, buf);
        }
        if (ups_battery_temperature > LONG_MIN) {
            mp_snprintf((char *) &buf, sizeof(buf),
                        "temperature: %ld C",
                        ups_battery_temperature);
            mp_strcat_comma(&output, buf);
        }
        if (ups_output_frequency > LONG_MIN) {
            mp_snprintf((char *) &buf, sizeof(buf),
                        "output frequency: %3.1f Hz",
                        (ups_output_frequency * 0.1f));
            mp_strcat_comma(&output, buf);
        }
        if (ups_input_line_bads > LONG_MIN) {
            mp_snprintf((char *) &buf, sizeof(buf),
                        "input line bads: %ld",
                        ups_input_line_bads);
            mp_strcat_comma(&output, buf);
        }
    }

    /*
     * performance data
     */
    if (mp_showperfdata) {
        if (ups_remaining_charge > LONG_MIN) {
            mp_perfdata_int("remaining_charge",
                            ups_remaining_charge,
                            "%", threshold_charge);
        }
        if (ups_remaining_runtime > LONG_MIN) {
            mp_perfdata_int("remaining_runtime",
                            ups_remaining_charge,
                            "minutes", threshold_runtime);
        }
        if (ups_battery_voltage > LONG_MIN) {
            mp_perfdata_float("battery_voltage",
                              ups_battery_voltage * 0.1f,
                              "V", NULL);
        }
        if (ups_battery_current > LONG_MIN) {
            mp_perfdata_float("battery_current",
                              ups_battery_current * 0.1f,
                              "A", NULL);
        }
        if (ups_battery_temperature > LONG_MIN) {
            mp_perfdata_int("battery_temperature",
                            ups_battery_temperature,
                            "C", NULL);
        }
        if (ups_output_frequency > LONG_MIN) {
            mp_perfdata_float("output_frequency",
                              ups_output_frequency * 0.1f,
                              "Hz", NULL);
        }
        if (ups_input_line_bads > LONG_MIN) {
            mp_perfdata_int("input_line_bads",
                            ups_input_line_bads,
                            "", NULL);
        }
    }

    switch (state) {
        case STATE_OK:
            ok("UPS: %s", output);
            break;
        case STATE_WARNING:
            warning("UPS: %s", output);
            break;
        case STATE_CRITICAL:
            critical("UPS: %s", output);
            break;
        default:
            unknown("UPS: %s", output);
            break;
    }
}