summaryrefslogtreecommitdiffstats
path: root/plugins/check_swap.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_swap.c')
-rw-r--r--plugins/check_swap.c131
1 files changed, 97 insertions, 34 deletions
diff --git a/plugins/check_swap.c b/plugins/check_swap.c
index bc90a90b..4d3b6099 100644
--- a/plugins/check_swap.c
+++ b/plugins/check_swap.c
@@ -28,6 +28,9 @@
28 *****************************************************************************/ 28 *****************************************************************************/
29 29
30#include "common.h" 30#include "common.h"
31#include "output.h"
32#include "states.h"
33#include <limits.h>
31#ifdef HAVE_DECL_SWAPCTL 34#ifdef HAVE_DECL_SWAPCTL
32# ifdef HAVE_SYS_PARAM_H 35# ifdef HAVE_SYS_PARAM_H
33# include <sys/param.h> 36# include <sys/param.h>
@@ -69,8 +72,6 @@ int main(int argc, char **argv) {
69 bindtextdomain(PACKAGE, LOCALEDIR); 72 bindtextdomain(PACKAGE, LOCALEDIR);
70 textdomain(PACKAGE); 73 textdomain(PACKAGE);
71 74
72 char *status = strdup("");
73
74 /* Parse extra opts if any */ 75 /* Parse extra opts if any */
75 argv = np_extra_opts(&argc, argv, progname); 76 argv = np_extra_opts(&argc, argv, progname);
76 77
@@ -90,59 +91,101 @@ int main(int argc, char **argv) {
90 } 91 }
91 92
92 double percent_used; 93 double percent_used;
94 mp_check overall = mp_check_init();
95 if (config.output_format_is_set) {
96 overall.format = config.output_format;
97 }
98 mp_subcheck sc1 = mp_subcheck_init();
99 sc1 = mp_set_subcheck_default_state(sc1, STATE_OK);
100
93 /* if total_swap_mb == 0, let's not divide by 0 */ 101 /* if total_swap_mb == 0, let's not divide by 0 */
94 if (data.metrics.total != 0) { 102 if (data.metrics.total != 0) {
95 percent_used = HUNDRED_PERCENT * ((double)data.metrics.used) / ((double)data.metrics.total); 103 percent_used = HUNDRED_PERCENT * ((double)data.metrics.used) / ((double)data.metrics.total);
96 } else { 104 } else {
97 printf(_("SWAP %s - Swap is either disabled, not present, or of zero " 105 sc1 = mp_set_subcheck_state(sc1, config.no_swap_state);
98 "size."), 106 sc1.output = (char *)_("Swap is either disabled, not present, or of zero size.");
99 state_text(data.statusCode)); 107
100 exit(config.no_swap_state); 108 mp_add_subcheck_to_check(&overall, sc1);
109 mp_exit(overall);
101 } 110 }
102 111
103 if (verbose) { 112 if (verbose) {
104 printf("Computed usage percentage: %g\n", percent_used); 113 printf("Computed usage percentage: %g\n", percent_used);
105 } 114 }
106 115
107 uint64_t warn_print = config.warn.value; 116 mp_perfdata pd = perfdata_init();
108 if (config.warn.is_percentage) { 117 pd.label = "swap";
109 warn_print = config.warn.value * (data.metrics.total / HUNDRED_PERCENT); 118 pd = mp_set_pd_value(pd, data.metrics.free);
119 pd.uom = "B";
120
121 if (config.warn_is_set) {
122 uint64_t warn_print = config.warn.value;
123 if (config.warn.is_percentage) {
124 warn_print = config.warn.value * (data.metrics.total / HUNDRED_PERCENT);
125 }
126
127 mp_perfdata_value warn_pd = mp_create_pd_value(warn_print);
128
129 mp_range warn_range = mp_range_init();
130 warn_range.end_infinity = false;
131 warn_range.end = warn_pd;
132
133 pd.warn = warn_range;
134 pd.warn_present = true;
110 } 135 }
111 136
112 uint64_t crit_print = config.crit.value; 137 if (config.crit_is_set) {
113 if (config.crit.is_percentage) { 138 uint64_t crit_print = config.crit.value;
114 crit_print = config.crit.value * (data.metrics.total / HUNDRED_PERCENT); 139 if (config.crit.is_percentage) {
140 crit_print = config.crit.value * (data.metrics.total / HUNDRED_PERCENT);
141 }
142
143 mp_perfdata_value crit_pd = mp_create_pd_value(crit_print);
144
145 mp_range crit_range = mp_range_init();
146 crit_range.end_infinity = false;
147 crit_range.end = crit_pd;
148
149 pd.crit = crit_range;
150 pd.crit_present = true;
115 } 151 }
116 152
117 char *perfdata = perfdata_uint64("swap", data.metrics.free, "B", config.warn_is_set, warn_print, config.crit_is_set, crit_print, true, 153 mp_perfdata_value max = mp_create_pd_value(data.metrics.total);
118 0, true, data.metrics.total); 154 pd.max = max;
155 pd.max_present = true;
156
157 mp_perfdata_value min = mp_create_pd_value(0);
158 pd.min = min;
159 pd.min_present = true;
160
161 mp_add_perfdata_to_subcheck(&sc1, pd);
162 if (verbose > 1) {
163 printf("Warn threshold value: %" PRIu64 "\n", config.warn.value);
164 }
119 165
120 if (config.warn_is_set) { 166 if (config.warn_is_set) {
121 if (verbose > 1) { 167 if ((config.warn.is_percentage && (percent_used >= (100 - (double)config.warn.value))) || config.warn.value >= data.metrics.free) {
122 printf("Warn threshold value: %" PRIu64 "\n", config.warn.value); 168 sc1 = mp_set_subcheck_state(sc1, STATE_WARNING);
123 } 169 }
170 }
124 171
125 if ((config.warn.is_percentage && (percent_used >= (double)(HUNDRED_PERCENT - config.warn.value))) || 172 if (verbose > 1) {
126 config.warn.value >= data.metrics.free) { 173 printf("Crit threshold value: %" PRIu64 "\n", config.crit.value);
127 data.statusCode = max_state(data.statusCode, STATE_WARNING);
128 }
129 } 174 }
130 175
131 if (config.crit_is_set) { 176 if (config.crit_is_set) {
132 if (verbose > 1) { 177 if ((config.crit.is_percentage && (percent_used >= (100 - (double)config.crit.value))) || config.crit.value >= data.metrics.free) {
133 printf("Crit threshold value: %" PRIu64 "\n", config.crit.value); 178 sc1 = mp_set_subcheck_state(sc1, STATE_CRITICAL);
134 }
135
136 if ((config.crit.is_percentage && (percent_used >= (double)(HUNDRED_PERCENT - config.crit.value))) ||
137 config.crit.value >= data.metrics.free) {
138 data.statusCode = max_state(data.statusCode, STATE_CRITICAL);
139 } 179 }
140 } 180 }
141 181
142 printf(_("SWAP %s - %g%% free (%lluMiB out of %lluMiB) %s|%s\n"), state_text(data.statusCode), (HUNDRED_PERCENT - percent_used), 182 xasprintf(&sc1.output, _("%g%% free (%lluMiB out of %lluMiB)"), (100 - percent_used), data.metrics.free >> 20,
143 BYTES_TO_MiB(data.metrics.free), BYTES_TO_MiB(data.metrics.total), status, perfdata); 183 data.metrics.total >> 20);
184
185 overall.summary = "Swap";
186 mp_add_subcheck_to_check(&overall, sc1);
144 187
145 exit(data.statusCode); 188 mp_exit(overall);
146} 189}
147 190
148int check_swap(float free_swap_mb, float total_swap_mb, swap_config config) { 191int check_swap(float free_swap_mb, float total_swap_mb, swap_config config) {
@@ -172,15 +215,22 @@ int check_swap(float free_swap_mb, float total_swap_mb, swap_config config) {
172 return STATE_OK; 215 return STATE_OK;
173} 216}
174 217
218#define output_format_index CHAR_MAX + 1
219
175/* process command-line arguments */ 220/* process command-line arguments */
176swap_config_wrapper process_arguments(int argc, char **argv) { 221swap_config_wrapper process_arguments(int argc, char **argv) {
177 swap_config_wrapper conf_wrapper = {.errorcode = OK}; 222 swap_config_wrapper conf_wrapper = {.errorcode = OK};
178 conf_wrapper.config = swap_config_init(); 223 conf_wrapper.config = swap_config_init();
179 224
180 static struct option longopts[] = {{"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, 225 static struct option longopts[] = {{"warning", required_argument, 0, 'w'},
181 {"allswaps", no_argument, 0, 'a'}, {"no-swap", required_argument, 0, 'n'}, 226 {"critical", required_argument, 0, 'c'},
182 {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, 227 {"allswaps", no_argument, 0, 'a'},
183 {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; 228 {"no-swap", required_argument, 0, 'n'},
229 {"verbose", no_argument, 0, 'v'},
230 {"version", no_argument, 0, 'V'},
231 {"help", no_argument, 0, 'h'},
232 {"output-format", required_argument, 0, output_format_index},
233 {0, 0, 0, 0}};
184 234
185 while (true) { 235 while (true) {
186 int option = 0; 236 int option = 0;
@@ -263,6 +313,18 @@ swap_config_wrapper process_arguments(int argc, char **argv) {
263 case 'v': /* verbose */ 313 case 'v': /* verbose */
264 verbose++; 314 verbose++;
265 break; 315 break;
316 case output_format_index: {
317 parsed_output_format parser = mp_parse_output_format(optarg);
318 if (!parser.parsing_success) {
319 // TODO List all available formats here, maybe add anothoer usage function
320 printf("Invalid output format: %s\n", optarg);
321 exit(STATE_UNKNOWN);
322 }
323
324 conf_wrapper.config.output_format_is_set = true;
325 conf_wrapper.config.output_format = parser.output_format;
326 break;
327 }
266 case 'V': /* version */ 328 case 'V': /* version */
267 print_revision(progname, NP_VERSION); 329 print_revision(progname, NP_VERSION);
268 exit(STATE_UNKNOWN); 330 exit(STATE_UNKNOWN);
@@ -319,6 +381,7 @@ void print_help(swap_config config) {
319 _("Resulting state when there is no swap regardless of thresholds. " 381 _("Resulting state when there is no swap regardless of thresholds. "
320 "Default:"), 382 "Default:"),
321 state_text(config.no_swap_state)); 383 state_text(config.no_swap_state));
384 printf(UT_OUTPUT_FORMAT);
322 printf(UT_VERBOSE); 385 printf(UT_VERBOSE);
323 386
324 printf("\n"); 387 printf("\n");