[monitoring-plugins] Refactor check_hpjd

Lorenz Kästle git at monitoring-plugins.org
Tue Mar 11 11:50:11 CET 2025


 Module: monitoring-plugins
 Branch: master
 Commit: a2ce9e962d52ebaffc275379c82ce29229a1fba8
 Author: Lorenz Kästle <12514511+RincewindsHat at users.noreply.github.com>
   Date: Tue Mar 11 11:07:26 2025 +0100
    URL: https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=a2ce9e96

Refactor check_hpjd

---

 plugins/Makefile.am           |   1 +
 plugins/check_hpjd.c          | 141 +++++++++++++++++++-----------------------
 plugins/check_hpjd.d/config.h |  25 ++++++++
 3 files changed, 89 insertions(+), 78 deletions(-)

diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index be650089..1b40b91c 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -50,6 +50,7 @@ EXTRA_DIST = t \
 			 tests \
 			 $(np_test_scripts) \
 			 check_swap.d \
+			 check_hpjd.d \
 			 check_game.d \
 			 check_dbi.d \
 			 check_ssh.d \
diff --git a/plugins/check_hpjd.c b/plugins/check_hpjd.c
index 78f55b7f..62417fd6 100644
--- a/plugins/check_hpjd.c
+++ b/plugins/check_hpjd.c
@@ -37,9 +37,10 @@ const char *email = "devel at 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 at 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,10 +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) {
+	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,
@@ -108,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);
@@ -122,11 +105,26 @@ 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;
 
-	line = 0;
-	while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
+	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;
 
+	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') {
 			input_buffer[strlen(input_buffer) - 1] = 0;
@@ -134,18 +132,14 @@ int main(int argc, char **argv) {
 
 		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;
@@ -213,10 +207,9 @@ int main(int argc, char **argv) {
 
 	/* 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... */
@@ -226,7 +219,7 @@ 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"));
@@ -264,30 +257,21 @@ int main(int argc, char **argv) {
 
 	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")) {
 			result = STATE_CRITICAL;
 		}
-	}
-
-	else if (result == STATE_WARNING) {
+	} 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'}, */
@@ -297,37 +281,44 @@ int process_arguments(int argc, char **argv) {
 									   {"help", no_argument, 0, 'h'},
 									   {0, 0, 0, 0}};
 
+	check_hpjd_config_wrapper result = {
+		.errorcode = OK,
+		.config = check_hpjd_config_init(),
+	};
+
 	if (argc < 2) {
-		return ERROR;
+		result.errorcode = ERROR;
+		return result;
 	}
 
-	while (1) {
-		c = getopt_long(argc, argv, "+hVH:C:p:D", longopts, &option);
+	int option = 0;
+	while (true) {
+		int option_index = getopt_long(argc, argv, "+hVH:C:p:D", 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 '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)) {
 				usage2(_("Port must be a positive short integer"), optarg);
 			} else {
-				port = atoi(optarg);
+				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);
@@ -340,32 +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 (result.config.community == NULL) {
 		if (argv[c] != NULL) {
-			community = argv[c];
+			result.config.community = argv[c];
 		} else {
-			community = strdup(DEFAULT_COMMUNITY);
+			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);
 
diff --git a/plugins/check_hpjd.d/config.h b/plugins/check_hpjd.d/config.h
new file mode 100644
index 00000000..e36b7972
--- /dev/null
+++ b/plugins/check_hpjd.d/config.h
@@ -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;
+}



More information about the Commits mailing list