summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-08-30 14:16:36 +0200
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-08-30 14:16:36 +0200
commit77a5db04d7ccc954096c2d86c21fa7b05948072d (patch)
treecd5cac928fb29d1c50bdc13bbf4cce4188c382f1 /plugins
parent1ca5a6040a01cb5fc4f1ceb61b133826b22e1f7b (diff)
downloadmonitoring-plugins-77a5db04d7ccc954096c2d86c21fa7b05948072d.tar.gz
check_snmp: implement output format setting
Diffstat (limited to 'plugins')
-rw-r--r--plugins/check_snmp.c58
-rw-r--r--plugins/check_snmp.d/config.h3
2 files changed, 46 insertions, 15 deletions
diff --git a/plugins/check_snmp.c b/plugins/check_snmp.c
index 0c1e8ebe..a939f078 100644
--- a/plugins/check_snmp.c
+++ b/plugins/check_snmp.c
@@ -75,12 +75,6 @@ const char DEFAULT_MIBLIST[] = "ALL";
75# define DEFAULT_PRIV_PROTOCOL "AES" 75# define DEFAULT_PRIV_PROTOCOL "AES"
76#endif 76#endif
77 77
78/* Longopts only arguments */
79#define L_INVERT_SEARCH CHAR_MAX + 3
80#define L_OFFSET CHAR_MAX + 4
81#define L_IGNORE_MIB_PARSING_ERRORS CHAR_MAX + 5
82#define L_CONNECTION_PREFIX CHAR_MAX + 6
83
84typedef struct proces_arguments_wrapper { 78typedef struct proces_arguments_wrapper {
85 int errorcode; 79 int errorcode;
86 check_snmp_config config; 80 check_snmp_config config;
@@ -121,6 +115,10 @@ int main(int argc, char **argv) {
121 115
122 check_snmp_config config = paw_tmp.config; 116 check_snmp_config config = paw_tmp.config;
123 117
118 if (config.output_format_is_set) {
119 mp_set_format(config.output_format);
120 }
121
124 if (config.ignore_mib_parsing_errors) { 122 if (config.ignore_mib_parsing_errors) {
125 char *opt_toggle_res = snmp_mib_toggle_options("e"); 123 char *opt_toggle_res = snmp_mib_toggle_options("e");
126 if (opt_toggle_res != NULL) { 124 if (opt_toggle_res != NULL) {
@@ -406,6 +404,15 @@ int main(int argc, char **argv) {
406 404
407/* process command-line arguments */ 405/* process command-line arguments */
408static process_arguments_wrapper process_arguments(int argc, char **argv) { 406static process_arguments_wrapper process_arguments(int argc, char **argv) {
407 enum {
408 /* Longopts only arguments */
409 invert_search_index = CHAR_MAX + 1,
410 offset_index,
411 ignore_mib_parsing_errors_index,
412 connection_prefix_index,
413 output_format_index,
414 };
415
409 static struct option longopts[] = { 416 static struct option longopts[] = {
410 STD_LONG_OPTS, 417 STD_LONG_OPTS,
411 {"community", required_argument, 0, 'C'}, 418 {"community", required_argument, 0, 'C'},
@@ -433,14 +440,15 @@ static process_arguments_wrapper process_arguments(int argc, char **argv) {
433 {"authpasswd", required_argument, 0, 'A'}, 440 {"authpasswd", required_argument, 0, 'A'},
434 {"privpasswd", required_argument, 0, 'X'}, 441 {"privpasswd", required_argument, 0, 'X'},
435 {"next", no_argument, 0, 'n'}, 442 {"next", no_argument, 0, 'n'},
436 {"offset", required_argument, 0, L_OFFSET}, 443 {"offset", required_argument, 0, offset_index},
437 {"invert-search", no_argument, 0, L_INVERT_SEARCH}, 444 {"invert-search", no_argument, 0, invert_search_index},
438 {"perf-oids", no_argument, 0, 'O'}, 445 {"perf-oids", no_argument, 0, 'O'},
439 {"ipv4", no_argument, 0, '4'}, 446 {"ipv4", no_argument, 0, '4'},
440 {"ipv6", no_argument, 0, '6'}, 447 {"ipv6", no_argument, 0, '6'},
441 {"multiplier", required_argument, 0, 'M'}, 448 {"multiplier", required_argument, 0, 'M'},
442 {"ignore-mib-parsing-errors", no_argument, 0, L_IGNORE_MIB_PARSING_ERRORS}, 449 {"ignore-mib-parsing-errors", no_argument, 0, ignore_mib_parsing_errors_index},
443 {"connection-prefix", required_argument, 0, L_CONNECTION_PREFIX}, 450 {"connection-prefix", required_argument, 0, connection_prefix_index},
451 {"output-format", required_argument, 0, output_format_index},
444 {0, 0, 0, 0}}; 452 {0, 0, 0, 0}};
445 453
446 if (argc < 2) { 454 if (argc < 2) {
@@ -780,10 +788,11 @@ static process_arguments_wrapper process_arguments(int argc, char **argv) {
780 } 788 }
781 unitv_counter++; 789 unitv_counter++;
782 } break; 790 } break;
783 case L_OFFSET: 791 case offset_index:
784 config.offset = strtod(optarg, NULL); 792 config.offset = strtod(optarg, NULL);
793 config.offset_set = true;
785 break; 794 break;
786 case L_INVERT_SEARCH: 795 case invert_search_index:
787 config.invert_search = false; 796 config.invert_search = false;
788 break; 797 break;
789 case 'O': 798 case 'O':
@@ -796,16 +805,34 @@ static process_arguments_wrapper process_arguments(int argc, char **argv) {
796 case '6': 805 case '6':
797 connection_prefix = "udp6"; 806 connection_prefix = "udp6";
798 break; 807 break;
799 case L_CONNECTION_PREFIX: 808 case connection_prefix_index:
800 connection_prefix = optarg; 809 connection_prefix = optarg;
801 break; 810 break;
802 case 'M': 811 case 'M':
803 if (strspn(optarg, "0123456789.,") == strlen(optarg)) { 812 if (strspn(optarg, "0123456789.,") == strlen(optarg)) {
804 config.multiplier = strtod(optarg, NULL); 813 config.multiplier = strtod(optarg, NULL);
814 config.multiplier_set = true;
805 } 815 }
806 break; 816 break;
807 case L_IGNORE_MIB_PARSING_ERRORS: 817 case ignore_mib_parsing_errors_index:
808 config.ignore_mib_parsing_errors = true; 818 config.ignore_mib_parsing_errors = true;
819 break;
820 case 'f': // Deprecated format option for floating point values
821 break;
822 case output_format_index: {
823 parsed_output_format parser = mp_parse_output_format(optarg);
824 if (!parser.parsing_success) {
825 // TODO List all available formats here, maybe add anothoer usage function
826 printf("Invalid output format: %s\n", optarg);
827 exit(STATE_UNKNOWN);
828 }
829
830 config.output_format_is_set = true;
831 config.output_format = parser.output_format;
832 break;
833 }
834 default:
835 die(STATE_UNKNOWN, "Unknown option");
809 } 836 }
810 } 837 }
811 838
@@ -1068,6 +1095,7 @@ void print_help(void) {
1068 printf(" %s\n", _("Units label(s) for output data (e.g., 'sec.').")); 1095 printf(" %s\n", _("Units label(s) for output data (e.g., 'sec.')."));
1069 printf(" %s\n", "-M, --multiplier=FLOAT"); 1096 printf(" %s\n", "-M, --multiplier=FLOAT");
1070 printf(" %s\n", _("Multiplies current value, 0 < n < 1 works as divider, defaults to 1")); 1097 printf(" %s\n", _("Multiplies current value, 0 < n < 1 works as divider, defaults to 1"));
1098 printf(UT_OUTPUT_FORMAT);
1071 1099
1072 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); 1100 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
1073 printf(" %s\n", _("NOTE the final timeout value is calculated using this formula: " 1101 printf(" %s\n", _("NOTE the final timeout value is calculated using this formula: "
@@ -1115,5 +1143,5 @@ void print_usage(void) {
1115 printf("[-l label] [-u units] [-p port-number] [-d delimiter] [-D output-delimiter]\n"); 1143 printf("[-l label] [-u units] [-p port-number] [-d delimiter] [-D output-delimiter]\n");
1116 printf("[-m miblist] [-P snmp version] [-N context] [-L seclevel] [-U secname]\n"); 1144 printf("[-m miblist] [-P snmp version] [-N context] [-L seclevel] [-U secname]\n");
1117 printf("[-a authproto] [-A authpasswd] [-x privproto] [-X privpasswd] [-4|6]\n"); 1145 printf("[-a authproto] [-A authpasswd] [-x privproto] [-X privpasswd] [-4|6]\n");
1118 printf("[-M multiplier [-f format]]\n"); 1146 printf("[-M multiplier]\n");
1119} 1147}
diff --git a/plugins/check_snmp.d/config.h b/plugins/check_snmp.d/config.h
index ca624a81..e96dff5c 100644
--- a/plugins/check_snmp.d/config.h
+++ b/plugins/check_snmp.d/config.h
@@ -62,4 +62,7 @@ typedef struct check_snmp_config {
62 62
63 // Modify output 63 // Modify output
64 bool use_perf_data_labels_from_input; 64 bool use_perf_data_labels_from_input;
65
66 mp_output_format output_format;
67 bool output_format_is_set;
65} check_snmp_config; 68} check_snmp_config;