Merge branch 'master' into refactor/check_ldap

This commit is contained in:
Lorenz Kästle 2025-03-11 12:14:41 +01:00
commit 8b25eaa0d1
5 changed files with 318 additions and 231 deletions

View file

@ -51,6 +51,7 @@ EXTRA_DIST = t \
$(np_test_scripts) \
check_swap.d \
check_ldap.d \
check_hpjd.d \
check_game.d \
check_dbi.d \
check_ssh.d \
@ -59,7 +60,8 @@ EXTRA_DIST = t \
check_by_ssh.d \
check_smtp.d \
check_dig.d \
check_cluster.d
check_cluster.d \
check_fping.d
PLUGINHDRS = common.h

View file

@ -38,52 +38,29 @@ const char *email = "devel@monitoring-plugins.org";
#include "netutils.h"
#include "utils.h"
#include <stdbool.h>
#include "check_fping.d/config.h"
#include "states.h"
enum {
PACKET_COUNT = 1,
PACKET_SIZE = 56,
PL = 0,
RTA = 1
};
static int textscan(char *buf);
static int process_arguments(int /*argc*/, char ** /*argv*/);
static mp_state_enum textscan(char *buf, const char * /*server_name*/, bool /*crta_p*/, double /*crta*/, bool /*wrta_p*/, double /*wrta*/,
bool /*cpl_p*/, int /*cpl*/, bool /*wpl_p*/, int /*wpl*/, bool /*alive_p*/);
typedef struct {
int errorcode;
check_fping_config config;
} check_fping_config_wrapper;
static check_fping_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
static int get_threshold(char *arg, char *rv[2]);
static void print_help(void);
void print_usage(void);
static char *server_name = NULL;
static char *sourceip = NULL;
static char *sourceif = NULL;
static int packet_size = PACKET_SIZE;
static int packet_count = PACKET_COUNT;
static int target_timeout = 0;
static int packet_interval = 0;
static bool verbose = false;
static bool dontfrag = false;
static bool randomize_packet_data = false;
static int cpl;
static int wpl;
static double crta;
static double wrta;
static bool cpl_p = false;
static bool wpl_p = false;
static bool alive_p = false;
static bool crta_p = false;
static bool wrta_p = false;
int main(int argc, char **argv) {
/* normally should be int result = STATE_UNKNOWN; */
int status = STATE_UNKNOWN;
int result = 0;
char *fping_prog = NULL;
char *server = NULL;
char *command_line = NULL;
char *input_buffer = NULL;
char *option_string = "";
input_buffer = malloc(MAX_INPUT_BUFFER);
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
@ -91,39 +68,54 @@ int main(int argc, char **argv) {
/* Parse extra opts if any */
argv = np_extra_opts(&argc, argv, progname);
if (process_arguments(argc, argv) == ERROR)
check_fping_config_wrapper tmp_config = process_arguments(argc, argv);
if (tmp_config.errorcode == ERROR) {
usage4(_("Could not parse arguments"));
}
server = strscpy(server, server_name);
const check_fping_config config = tmp_config.config;
char *server = NULL;
server = strscpy(server, config.server_name);
char *option_string = "";
/* compose the command */
if (target_timeout)
xasprintf(&option_string, "%s-t %d ", option_string, target_timeout);
if (packet_interval)
xasprintf(&option_string, "%s-p %d ", option_string, packet_interval);
if (sourceip)
xasprintf(&option_string, "%s-S %s ", option_string, sourceip);
if (sourceif)
xasprintf(&option_string, "%s-I %s ", option_string, sourceif);
if (dontfrag)
if (config.target_timeout) {
xasprintf(&option_string, "%s-t %d ", option_string, config.target_timeout);
}
if (config.packet_interval) {
xasprintf(&option_string, "%s-p %d ", option_string, config.packet_interval);
}
if (config.sourceip) {
xasprintf(&option_string, "%s-S %s ", option_string, config.sourceip);
}
if (config.sourceif) {
xasprintf(&option_string, "%s-I %s ", option_string, config.sourceif);
}
if (config.dontfrag) {
xasprintf(&option_string, "%s-M ", option_string);
if (randomize_packet_data)
}
if (config.randomize_packet_data) {
xasprintf(&option_string, "%s-R ", option_string);
}
char *fping_prog = NULL;
#ifdef PATH_TO_FPING6
if (address_family != AF_INET && is_inet6_addr(server))
if (address_family != AF_INET && is_inet6_addr(server)) {
fping_prog = strdup(PATH_TO_FPING6);
else
} else {
fping_prog = strdup(PATH_TO_FPING);
}
#else
fping_prog = strdup(PATH_TO_FPING);
#endif
xasprintf(&command_line, "%s %s-b %d -c %d %s", fping_prog, option_string, packet_size, packet_count, server);
char *command_line = NULL;
xasprintf(&command_line, "%s %s-b %d -c %d %s", fping_prog, option_string, config.packet_size, config.packet_count, server);
if (verbose)
if (verbose) {
printf("%s\n", command_line);
}
/* run the command */
child_process = spopen(command_line);
@ -137,23 +129,29 @@ int main(int argc, char **argv) {
printf(_("Could not open stderr for %s\n"), command_line);
}
char *input_buffer = malloc(MAX_INPUT_BUFFER);
mp_state_enum status = STATE_UNKNOWN;
while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
if (verbose)
if (verbose) {
printf("%s", input_buffer);
status = max_state(status, textscan(input_buffer));
}
status = max_state(status, textscan(input_buffer, config.server_name, config.crta_p, config.crta, config.wrta_p, config.wrta,
config.cpl_p, config.cpl, config.wpl_p, config.wpl, config.alive_p));
}
/* If we get anything on STDERR, at least set warning */
while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
status = max_state(status, STATE_WARNING);
if (verbose)
if (verbose) {
printf("%s", input_buffer);
status = max_state(status, textscan(input_buffer));
}
status = max_state(status, textscan(input_buffer, config.server_name, config.crta_p, config.crta, config.wrta_p, config.wrta,
config.cpl_p, config.cpl, config.wpl_p, config.wpl, config.alive_p));
}
(void)fclose(child_stderr);
/* close the pipe */
result = spclose(child_process);
int result = spclose(child_process);
if (result) {
/* need to use max_state not max */
status = max_state(status, STATE_WARNING);
@ -172,21 +170,17 @@ int main(int argc, char **argv) {
}
}
printf("FPING %s - %s\n", state_text(status), server_name);
printf("FPING %s - %s\n", state_text(status), config.server_name);
return status;
}
int textscan(char *buf) {
char *rtastr = NULL;
char *losstr = NULL;
char *xmtstr = NULL;
double loss;
double rta;
double xmt;
int status = STATE_UNKNOWN;
mp_state_enum textscan(char *buf, const char *server_name, bool crta_p, double crta, bool wrta_p, double wrta, bool cpl_p, int cpl,
bool wpl_p, int wpl, bool alive_p) {
/* stops testing after the first successful reply. */
double rta;
double loss;
char *rtastr = NULL;
if (alive_p && strstr(buf, "avg, 0% loss)")) {
rtastr = strstr(buf, "ms (");
rtastr = 1 + index(rtastr, '(');
@ -198,6 +192,10 @@ int textscan(char *buf) {
fperfdata("rta", rta / 1.0e3, "s", wrta_p, wrta / 1.0e3, crta_p, crta / 1.0e3, true, 0, false, 0));
}
mp_state_enum status = STATE_UNKNOWN;
char *xmtstr = NULL;
double xmt;
char *losstr = NULL;
if (strstr(buf, "not found")) {
die(STATE_CRITICAL, _("FPING UNKNOWN - %s not found\n"), server_name);
@ -221,18 +219,19 @@ int textscan(char *buf) {
rtastr = 1 + index(rtastr, '/');
loss = strtod(losstr, NULL);
rta = strtod(rtastr, NULL);
if (cpl_p && loss > cpl)
if (cpl_p && loss > cpl) {
status = STATE_CRITICAL;
else if (crta_p && rta > crta)
} else if (crta_p && rta > crta) {
status = STATE_CRITICAL;
else if (wpl_p && loss > wpl)
} else if (wpl_p && loss > wpl) {
status = STATE_WARNING;
else if (wrta_p && rta > wrta)
} else if (wrta_p && rta > wrta) {
status = STATE_WARNING;
else
} else {
status = STATE_OK;
}
die(status, _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"), state_text(status), server_name, loss, rta,
perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, true, 0, true, 100),
perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, false, 0, false, 0),
fperfdata("rta", rta / 1.0e3, "s", wrta_p, wrta / 1.0e3, crta_p, crta / 1.0e3, true, 0, false, 0));
} else if (strstr(buf, "xmt/rcv/%loss")) {
@ -241,22 +240,24 @@ int textscan(char *buf) {
losstr = strstr(buf, "=");
xmtstr = 1 + losstr;
xmt = strtod(xmtstr, NULL);
if (xmt == 0)
if (xmt == 0) {
die(STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name);
}
losstr = 1 + strstr(losstr, "/");
losstr = 1 + strstr(losstr, "/");
loss = strtod(losstr, NULL);
if (atoi(losstr) == 100)
if (atoi(losstr) == 100) {
status = STATE_CRITICAL;
else if (cpl_p && loss > cpl)
} else if (cpl_p && loss > cpl) {
status = STATE_CRITICAL;
else if (wpl_p && loss > wpl)
} else if (wpl_p && loss > wpl) {
status = STATE_WARNING;
else
} else {
status = STATE_OK;
}
/* loss=%.0f%%;%d;%d;0;100 */
die(status, _("FPING %s - %s (loss=%.0f%% )|%s\n"), state_text(status), server_name, loss,
perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, true, 0, true, 100));
perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, false, 0, false, 0));
} else {
status = max_state(status, STATE_WARNING);
@ -266,54 +267,50 @@ int textscan(char *buf) {
}
/* process command-line arguments */
int process_arguments(int argc, char **argv) {
int c;
check_fping_config_wrapper process_arguments(int argc, char **argv) {
static struct option longopts[] = {
{"hostname", required_argument, 0, 'H'}, {"sourceip", required_argument, 0, 'S'}, {"sourceif", required_argument, 0, 'I'},
{"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, {"alive", no_argument, 0, 'a'},
{"bytes", required_argument, 0, 'b'}, {"number", required_argument, 0, 'n'}, {"target-timeout", required_argument, 0, 'T'},
{"interval", required_argument, 0, 'i'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'},
{"help", no_argument, 0, 'h'}, {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'},
{"dontfrag", no_argument, 0, 'M'}, {"random", no_argument, 0, 'R'}, {0, 0, 0, 0}};
char *rv[2];
int option = 0;
static struct option longopts[] = {{"hostname", required_argument, 0, 'H'},
{"sourceip", required_argument, 0, 'S'},
{"sourceif", required_argument, 0, 'I'},
{"critical", required_argument, 0, 'c'},
{"warning", required_argument, 0, 'w'},
{"alive", no_argument, 0, 'a'},
{"bytes", required_argument, 0, 'b'},
{"number", required_argument, 0, 'n'},
{"target-timeout", required_argument, 0, 'T'},
{"interval", required_argument, 0, 'i'},
{"verbose", no_argument, 0, 'v'},
{"version", no_argument, 0, 'V'},
{"help", no_argument, 0, 'h'},
{"use-ipv4", no_argument, 0, '4'},
{"use-ipv6", no_argument, 0, '6'},
{"dontfrag", no_argument, 0, 'M'},
{"random", no_argument, 0, 'R'},
{0, 0, 0, 0}};
rv[PL] = NULL;
rv[RTA] = NULL;
if (argc < 2)
return ERROR;
int option = 0;
check_fping_config_wrapper result = {
.errorcode = OK,
.config = check_fping_config_init(),
};
if (argc < 2) {
result.errorcode = ERROR;
return result;
}
if (!is_option(argv[1])) {
server_name = argv[1];
result.config.server_name = argv[1];
argv[1] = argv[0];
argv = &argv[1];
argc--;
}
while (1) {
c = getopt_long(argc, argv, "+hVvaH:S:c:w:b:n:T:i:I:M:R:46", longopts, &option);
int option_index = getopt_long(argc, argv, "+hVvaH:S:c:w:b:n:T:i:I:M:R:46", longopts, &option);
if (c == -1 || c == EOF || c == 1)
if (option_index == -1 || option_index == EOF || option_index == 1) {
break;
}
switch (c) {
switch (option_index) {
case '?': /* print short usage statement if args not parsable */
usage5();
case 'a': /* host alive mode */
alive_p = true;
result.config.alive_p = true;
break;
case 'h': /* help */
print_help();
@ -325,19 +322,19 @@ int process_arguments(int argc, char **argv) {
verbose = true;
break;
case 'H': /* hostname */
if (is_host(optarg) == false) {
if (!is_host(optarg)) {
usage2(_("Invalid hostname/address"), optarg);
}
server_name = strscpy(server_name, optarg);
result.config.server_name = optarg;
break;
case 'S': /* sourceip */
if (is_host(optarg) == false) {
if (!is_host(optarg)) {
usage2(_("Invalid hostname/address"), optarg);
}
sourceip = strscpy(sourceip, optarg);
result.config.sourceip = optarg;
break;
case 'I': /* sourceip */
sourceif = strscpy(sourceif, optarg);
result.config.sourceif = optarg;
break;
case '4': /* IPv4 only */
address_family = AF_INET;
@ -352,82 +349,89 @@ int process_arguments(int argc, char **argv) {
case 'c':
get_threshold(optarg, rv);
if (rv[RTA]) {
crta = strtod(rv[RTA], NULL);
crta_p = true;
result.config.crta = strtod(rv[RTA], NULL);
result.config.crta_p = true;
rv[RTA] = NULL;
}
if (rv[PL]) {
cpl = atoi(rv[PL]);
cpl_p = true;
result.config.cpl = atoi(rv[PL]);
result.config.cpl_p = true;
rv[PL] = NULL;
}
break;
case 'w':
get_threshold(optarg, rv);
if (rv[RTA]) {
wrta = strtod(rv[RTA], NULL);
wrta_p = true;
result.config.wrta = strtod(rv[RTA], NULL);
result.config.wrta_p = true;
rv[RTA] = NULL;
}
if (rv[PL]) {
wpl = atoi(rv[PL]);
wpl_p = true;
result.config.wpl = atoi(rv[PL]);
result.config.wpl_p = true;
rv[PL] = NULL;
}
break;
case 'b': /* bytes per packet */
if (is_intpos(optarg))
packet_size = atoi(optarg);
else
if (is_intpos(optarg)) {
result.config.packet_size = atoi(optarg);
} else {
usage(_("Packet size must be a positive integer"));
}
break;
case 'n': /* number of packets */
if (is_intpos(optarg))
packet_count = atoi(optarg);
else
if (is_intpos(optarg)) {
result.config.packet_count = atoi(optarg);
} else {
usage(_("Packet count must be a positive integer"));
}
break;
case 'T': /* timeout in msec */
if (is_intpos(optarg))
target_timeout = atoi(optarg);
else
if (is_intpos(optarg)) {
result.config.target_timeout = atoi(optarg);
} else {
usage(_("Target timeout must be a positive integer"));
}
break;
case 'i': /* interval in msec */
if (is_intpos(optarg))
packet_interval = atoi(optarg);
else
if (is_intpos(optarg)) {
result.config.packet_interval = atoi(optarg);
} else {
usage(_("Interval must be a positive integer"));
}
break;
case 'R':
randomize_packet_data = true;
result.config.randomize_packet_data = true;
break;
case 'M':
dontfrag = true;
result.config.dontfrag = true;
break;
}
}
if (server_name == NULL)
if (result.config.server_name == NULL) {
usage4(_("Hostname was not supplied"));
}
return OK;
return result;
}
int get_threshold(char *arg, char *rv[2]) {
char *arg1 = NULL;
char *arg2 = NULL;
arg1 = strscpy(arg1, arg);
if (strpbrk(arg1, ",:"))
char *arg1 = strdup(arg);
if (strpbrk(arg1, ",:")) {
arg2 = 1 + strpbrk(arg1, ",:");
}
if (arg2) {
arg1[strcspn(arg1, ",:")] = 0;
if (strstr(arg1, "%") && strstr(arg2, "%"))
if (strstr(arg1, "%") && strstr(arg2, "%")) {
die(STATE_UNKNOWN, _("%s: Only one threshold may be packet loss (%s)\n"), progname, arg);
if (!strstr(arg1, "%") && !strstr(arg2, "%"))
}
if (!strstr(arg1, "%") && !strstr(arg2, "%")) {
die(STATE_UNKNOWN, _("%s: Only one threshold must be packet loss (%s)\n"), progname, arg);
}
}
if (arg2 && strstr(arg2, "%")) {

View file

@ -0,0 +1,58 @@
#pragma once
#include "../../config.h"
#include <stddef.h>
enum {
PACKET_SIZE = 56,
PACKET_COUNT = 1,
};
typedef struct {
char *server_name;
char *sourceip;
char *sourceif;
int packet_size;
int packet_count;
int target_timeout;
int packet_interval;
bool randomize_packet_data;
bool dontfrag;
bool alive_p;
double crta;
bool crta_p;
double wrta;
bool wrta_p;
int cpl;
bool cpl_p;
int wpl;
bool wpl_p;
} check_fping_config;
check_fping_config check_fping_config_init() {
check_fping_config tmp = {
.server_name = NULL,
.sourceip = NULL,
.sourceif = NULL,
.packet_size = PACKET_SIZE,
.packet_count = PACKET_COUNT,
.target_timeout = 0,
.packet_interval = 0,
.randomize_packet_data = false,
.dontfrag = false,
.alive_p = false,
.crta = 0,
.crta_p = false,
.wrta = 0,
.wrta_p = false,
.cpl = 0,
.cpl_p = false,
.wpl = 0,
.wpl_p = false,
};
return tmp;
}

View file

@ -37,9 +37,10 @@ const char *email = "devel@monitoring-plugins.org";
#include "popen.h"
#include "utils.h"
#include "netutils.h"
#include "states.h"
#include "check_hpjd.d/config.h"
#define DEFAULT_COMMUNITY "public"
#define DEFAULT_PORT "161"
#define HPJD_LINE_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.1"
#define HPJD_PAPER_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.2"
@ -57,39 +58,15 @@ const char *email = "devel@monitoring-plugins.org";
#define ONLINE 0
#define OFFLINE 1
static int process_arguments(int /*argc*/, char ** /*argv*/);
static int validate_arguments(void);
typedef struct {
int errorcode;
check_hpjd_config config;
} check_hpjd_config_wrapper;
static check_hpjd_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
static void print_help(void);
void print_usage(void);
static char *community = NULL;
static char *address = NULL;
static unsigned int port = 0;
static int check_paper_out = 1;
int main(int argc, char **argv) {
char command_line[1024];
int result = STATE_UNKNOWN;
int line;
char input_buffer[MAX_INPUT_BUFFER];
char query_string[512];
char *errmsg;
char *temp_buffer;
int line_status = ONLINE;
int paper_status = 0;
int intervention_required = 0;
int peripheral_error = 0;
int paper_jam = 0;
int paper_out = 0;
int toner_low = 0;
int page_punt = 0;
int memory_out = 0;
int door_open = 0;
int paper_output = 0;
char display_message[MAX_INPUT_BUFFER];
errmsg = malloc(MAX_INPUT_BUFFER);
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
@ -97,9 +74,15 @@ int main(int argc, char **argv) {
/* Parse extra opts if any */
argv = np_extra_opts(&argc, argv, progname);
if (process_arguments(argc, argv) == ERROR)
usage4(_("Could not parse arguments"));
check_hpjd_config_wrapper tmp_config = process_arguments(argc, argv);
if (tmp_config.errorcode == ERROR) {
usage4(_("Could not parse arguments"));
}
const check_hpjd_config config = tmp_config.config;
char query_string[512];
/* removed ' 2>1' at end of command 10/27/1999 - EG */
/* create the query string */
sprintf(query_string, "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0", HPJD_LINE_STATUS, HPJD_PAPER_STATUS,
@ -107,7 +90,8 @@ int main(int argc, char **argv) {
HPJD_GD_PAGE_PUNT, HPJD_GD_MEMORY_OUT, HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY);
/* get the command to run */
sprintf(command_line, "%s -OQa -m : -v 1 -c %s %s:%u %s", PATH_TO_SNMPGET, community, address, port, query_string);
char command_line[1024];
sprintf(command_line, "%s -OQa -m : -v 1 -c %s %s:%u %s", PATH_TO_SNMPGET, config.community, config.address, config.port, query_string);
/* run the command */
child_process = spopen(command_line);
@ -121,29 +105,41 @@ int main(int argc, char **argv) {
printf(_("Could not open stderr for %s\n"), command_line);
}
result = STATE_OK;
mp_state_enum result = STATE_OK;
int line_status = ONLINE;
int paper_status = 0;
int intervention_required = 0;
int peripheral_error = 0;
int paper_jam = 0;
int paper_out = 0;
int toner_low = 0;
int page_punt = 0;
int memory_out = 0;
int door_open = 0;
int paper_output = 0;
char display_message[MAX_INPUT_BUFFER];
char input_buffer[MAX_INPUT_BUFFER];
char *errmsg = malloc(MAX_INPUT_BUFFER);
int line = 0;
line = 0;
while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
/* strip the newline character from the end of the input */
if (input_buffer[strlen(input_buffer) - 1] == '\n')
if (input_buffer[strlen(input_buffer) - 1] == '\n') {
input_buffer[strlen(input_buffer) - 1] = 0;
}
line++;
temp_buffer = strtok(input_buffer, "=");
char *temp_buffer = strtok(input_buffer, "=");
temp_buffer = strtok(NULL, "=");
if (temp_buffer == NULL && line < 13) {
result = STATE_UNKNOWN;
strcpy(errmsg, input_buffer);
} else {
switch (line) {
case 1: /* 1st line should contain the line status */
line_status = atoi(temp_buffer);
break;
@ -186,16 +182,18 @@ int main(int argc, char **argv) {
}
/* break out of the read loop if we encounter an error */
if (result != STATE_OK)
if (result != STATE_OK) {
break;
}
}
/* WARNING if output found on stderr */
if (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
result = max_state(result, STATE_WARNING);
/* remove CRLF */
if (input_buffer[strlen(input_buffer) - 1] == '\n')
if (input_buffer[strlen(input_buffer) - 1] == '\n') {
input_buffer[strlen(input_buffer) - 1] = 0;
}
sprintf(errmsg, "%s", input_buffer);
}
@ -203,15 +201,15 @@ int main(int argc, char **argv) {
(void)fclose(child_stderr);
/* close the pipe */
if (spclose(child_process))
if (spclose(child_process)) {
result = max_state(result, STATE_WARNING);
}
/* if there wasn't any output, display an error */
if (line == 0) {
/* might not be the problem, but most likely is. */
result = STATE_UNKNOWN;
xasprintf(&errmsg, "%s : Timeout from host %s\n", errmsg, address);
xasprintf(&errmsg, "%s : Timeout from host %s\n", errmsg, config.address);
}
/* if we had no read errors, check the printer status results... */
@ -221,8 +219,9 @@ int main(int argc, char **argv) {
result = STATE_WARNING;
strcpy(errmsg, _("Paper Jam"));
} else if (paper_out) {
if (check_paper_out)
if (config.check_paper_out) {
result = STATE_WARNING;
}
strcpy(errmsg, _("Out of Paper"));
} else if (line_status == OFFLINE) {
if (strcmp(errmsg, "POWERSAVE ON") != 0) {
@ -256,29 +255,23 @@ int main(int argc, char **argv) {
}
}
if (result == STATE_OK)
if (result == STATE_OK) {
printf(_("Printer ok - (%s)\n"), display_message);
else if (result == STATE_UNKNOWN) {
} else if (result == STATE_UNKNOWN) {
printf("%s\n", errmsg);
/* if printer could not be reached, escalate to critical */
if (strstr(errmsg, "Timeout"))
if (strstr(errmsg, "Timeout")) {
result = STATE_CRITICAL;
}
} else if (result == STATE_WARNING) {
printf("%s (%s)\n", errmsg, display_message);
}
else if (result == STATE_WARNING)
printf("%s (%s)\n", errmsg, display_message);
return result;
exit(result);
}
/* process command-line arguments */
int process_arguments(int argc, char **argv) {
int c;
int option = 0;
check_hpjd_config_wrapper process_arguments(int argc, char **argv) {
static struct option longopts[] = {{"hostname", required_argument, 0, 'H'},
{"community", required_argument, 0, 'C'},
/* {"critical", required_argument,0,'c'}, */
@ -288,34 +281,44 @@ int process_arguments(int argc, char **argv) {
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}};
if (argc < 2)
return ERROR;
check_hpjd_config_wrapper result = {
.errorcode = OK,
.config = check_hpjd_config_init(),
};
while (1) {
c = getopt_long(argc, argv, "+hVH:C:p:D", longopts, &option);
if (argc < 2) {
result.errorcode = ERROR;
return result;
}
if (c == -1 || c == EOF || c == 1)
int option = 0;
while (true) {
int option_index = getopt_long(argc, argv, "+hVH:C:p:D", longopts, &option);
if (option_index == -1 || option_index == EOF || option_index == 1) {
break;
}
switch (c) {
switch (option_index) {
case 'H': /* hostname */
if (is_host(optarg)) {
address = strscpy(address, optarg);
result.config.address = strscpy(result.config.address, optarg);
} else {
usage2(_("Invalid hostname/address"), optarg);
}
break;
case 'C': /* community */
community = strscpy(community, optarg);
result.config.community = strscpy(result.config.community, optarg);
break;
case 'p':
if (!is_intpos(optarg))
if (!is_intpos(optarg)) {
usage2(_("Port must be a positive short integer"), optarg);
else
port = atoi(optarg);
} else {
result.config.port = atoi(optarg);
}
break;
case 'D': /* disable paper out check*/
check_paper_out = 0;
result.config.check_paper_out = false;
break;
case 'V': /* version */
print_revision(progname, NP_VERSION);
@ -328,31 +331,26 @@ int process_arguments(int argc, char **argv) {
}
}
c = optind;
if (address == NULL) {
int c = optind;
if (result.config.address == NULL) {
if (is_host(argv[c])) {
address = argv[c++];
result.config.address = argv[c++];
} else {
usage2(_("Invalid hostname/address"), argv[c]);
}
}
if (community == NULL) {
if (argv[c] != NULL)
community = argv[c];
else
community = strdup(DEFAULT_COMMUNITY);
if (result.config.community == NULL) {
if (argv[c] != NULL) {
result.config.community = argv[c];
} else {
result.config.community = strdup(DEFAULT_COMMUNITY);
}
}
if (port == 0) {
port = atoi(DEFAULT_PORT);
}
return validate_arguments();
return result;
}
int validate_arguments(void) { return OK; }
void print_help(void) {
print_revision(progname, NP_VERSION);

View file

@ -0,0 +1,25 @@
#pragma once
#include "../../config.h"
#include <stddef.h>
#include <stdlib.h>
#define DEFAULT_PORT "161"
typedef struct {
char *address;
char *community;
unsigned int port;
bool check_paper_out;
} check_hpjd_config;
check_hpjd_config check_hpjd_config_init() {
check_hpjd_config tmp = {
.address = NULL,
.community = NULL,
.port = (unsigned int)atoi(DEFAULT_PORT),
.check_paper_out = true,
};
return tmp;
}