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.c139
1 files changed, 105 insertions, 34 deletions
diff --git a/plugins/check_swap.c b/plugins/check_swap.c
index bc90a90b..435a104e 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
@@ -89,60 +90,110 @@ int main(int argc, char **argv) {
89 exit(STATE_UNKNOWN); 90 exit(STATE_UNKNOWN);
90 } 91 }
91 92
93 if (verbose) {
94 printf("Swap retrieval result:\n"
95 "\tFree: %llu\n"
96 "\tUsed: %llu\n"
97 "\tTotal: %llu\n",
98 data.metrics.free, data.metrics.used, data.metrics.total);
99 }
100
92 double percent_used; 101 double percent_used;
102 mp_check overall = mp_check_init();
103 if (config.output_format_is_set) {
104 mp_set_format(config.output_format);
105 }
106 mp_subcheck sc1 = mp_subcheck_init();
107 sc1 = mp_set_subcheck_default_state(sc1, STATE_OK);
108
93 /* if total_swap_mb == 0, let's not divide by 0 */ 109 /* if total_swap_mb == 0, let's not divide by 0 */
94 if (data.metrics.total != 0) { 110 if (data.metrics.total != 0) {
95 percent_used = HUNDRED_PERCENT * ((double)data.metrics.used) / ((double)data.metrics.total); 111 percent_used = HUNDRED_PERCENT * ((double)data.metrics.used) / ((double)data.metrics.total);
96 } else { 112 } else {
97 printf(_("SWAP %s - Swap is either disabled, not present, or of zero " 113 sc1 = mp_set_subcheck_state(sc1, config.no_swap_state);
98 "size."), 114 sc1.output = (char *)_("Swap is either disabled, not present, or of zero size.");
99 state_text(data.statusCode)); 115
100 exit(config.no_swap_state); 116 mp_add_subcheck_to_check(&overall, sc1);
117 mp_exit(overall);
101 } 118 }
102 119
103 if (verbose) { 120 if (verbose) {
104 printf("Computed usage percentage: %g\n", percent_used); 121 printf("Computed usage percentage: %g\n", percent_used);
105 } 122 }
106 123
107 uint64_t warn_print = config.warn.value; 124 mp_perfdata pd = perfdata_init();
108 if (config.warn.is_percentage) { 125 pd.label = "swap";
109 warn_print = config.warn.value * (data.metrics.total / HUNDRED_PERCENT); 126 pd = mp_set_pd_value(pd, data.metrics.free);
127 pd.uom = "B";
128
129 if (config.warn_is_set) {
130 uint64_t warn_print = config.warn.value;
131 if (config.warn.is_percentage) {
132 warn_print = config.warn.value * (data.metrics.total / HUNDRED_PERCENT);
133 }
134
135 mp_perfdata_value warn_pd = mp_create_pd_value(warn_print);
136
137 mp_range warn_range = mp_range_init();
138 warn_range.end_infinity = false;
139 warn_range.end = warn_pd;
140
141 pd.warn = warn_range;
142 pd.warn_present = true;
110 } 143 }
111 144
112 uint64_t crit_print = config.crit.value; 145 if (config.crit_is_set) {
113 if (config.crit.is_percentage) { 146 uint64_t crit_print = config.crit.value;
114 crit_print = config.crit.value * (data.metrics.total / HUNDRED_PERCENT); 147 if (config.crit.is_percentage) {
148 crit_print = config.crit.value * (data.metrics.total / HUNDRED_PERCENT);
149 }
150
151 mp_perfdata_value crit_pd = mp_create_pd_value(crit_print);
152
153 mp_range crit_range = mp_range_init();
154 crit_range.end_infinity = false;
155 crit_range.end = crit_pd;
156
157 pd.crit = crit_range;
158 pd.crit_present = true;
115 } 159 }
116 160
117 char *perfdata = perfdata_uint64("swap", data.metrics.free, "B", config.warn_is_set, warn_print, config.crit_is_set, crit_print, true, 161 mp_perfdata_value max = mp_create_pd_value(data.metrics.total);
118 0, true, data.metrics.total); 162 pd.max = max;
163 pd.max_present = true;
164
165 mp_perfdata_value min = mp_create_pd_value(0);
166 pd.min = min;
167 pd.min_present = true;
168
169 mp_add_perfdata_to_subcheck(&sc1, pd);
170 if (verbose > 1) {
171 printf("Warn threshold value: %" PRIu64 "\n", config.warn.value);
172 }
119 173
120 if (config.warn_is_set) { 174 if (config.warn_is_set) {
121 if (verbose > 1) { 175 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); 176 sc1 = mp_set_subcheck_state(sc1, STATE_WARNING);
123 } 177 }
178 }
124 179
125 if ((config.warn.is_percentage && (percent_used >= (double)(HUNDRED_PERCENT - config.warn.value))) || 180 if (verbose > 1) {
126 config.warn.value >= data.metrics.free) { 181 printf("Crit threshold value: %" PRIu64 "\n", config.crit.value);
127 data.statusCode = max_state(data.statusCode, STATE_WARNING);
128 }
129 } 182 }
130 183
131 if (config.crit_is_set) { 184 if (config.crit_is_set) {
132 if (verbose > 1) { 185 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); 186 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 } 187 }
140 } 188 }
141 189
142 printf(_("SWAP %s - %g%% free (%lluMiB out of %lluMiB) %s|%s\n"), state_text(data.statusCode), (HUNDRED_PERCENT - percent_used), 190 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); 191 data.metrics.total >> 20);
192
193 overall.summary = "Swap";
194 mp_add_subcheck_to_check(&overall, sc1);
144 195
145 exit(data.statusCode); 196 mp_exit(overall);
146} 197}
147 198
148int check_swap(float free_swap_mb, float total_swap_mb, swap_config config) { 199int check_swap(float free_swap_mb, float total_swap_mb, swap_config config) {
@@ -172,15 +223,22 @@ int check_swap(float free_swap_mb, float total_swap_mb, swap_config config) {
172 return STATE_OK; 223 return STATE_OK;
173} 224}
174 225
226#define output_format_index CHAR_MAX + 1
227
175/* process command-line arguments */ 228/* process command-line arguments */
176swap_config_wrapper process_arguments(int argc, char **argv) { 229swap_config_wrapper process_arguments(int argc, char **argv) {
177 swap_config_wrapper conf_wrapper = {.errorcode = OK}; 230 swap_config_wrapper conf_wrapper = {.errorcode = OK};
178 conf_wrapper.config = swap_config_init(); 231 conf_wrapper.config = swap_config_init();
179 232
180 static struct option longopts[] = {{"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, 233 static struct option longopts[] = {{"warning", required_argument, 0, 'w'},
181 {"allswaps", no_argument, 0, 'a'}, {"no-swap", required_argument, 0, 'n'}, 234 {"critical", required_argument, 0, 'c'},
182 {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, 235 {"allswaps", no_argument, 0, 'a'},
183 {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; 236 {"no-swap", required_argument, 0, 'n'},
237 {"verbose", no_argument, 0, 'v'},
238 {"version", no_argument, 0, 'V'},
239 {"help", no_argument, 0, 'h'},
240 {"output-format", required_argument, 0, output_format_index},
241 {0, 0, 0, 0}};
184 242
185 while (true) { 243 while (true) {
186 int option = 0; 244 int option = 0;
@@ -263,6 +321,18 @@ swap_config_wrapper process_arguments(int argc, char **argv) {
263 case 'v': /* verbose */ 321 case 'v': /* verbose */
264 verbose++; 322 verbose++;
265 break; 323 break;
324 case output_format_index: {
325 parsed_output_format parser = mp_parse_output_format(optarg);
326 if (!parser.parsing_success) {
327 // TODO List all available formats here, maybe add anothoer usage function
328 printf("Invalid output format: %s\n", optarg);
329 exit(STATE_UNKNOWN);
330 }
331
332 conf_wrapper.config.output_format_is_set = true;
333 conf_wrapper.config.output_format = parser.output_format;
334 break;
335 }
266 case 'V': /* version */ 336 case 'V': /* version */
267 print_revision(progname, NP_VERSION); 337 print_revision(progname, NP_VERSION);
268 exit(STATE_UNKNOWN); 338 exit(STATE_UNKNOWN);
@@ -319,6 +389,7 @@ void print_help(swap_config config) {
319 _("Resulting state when there is no swap regardless of thresholds. " 389 _("Resulting state when there is no swap regardless of thresholds. "
320 "Default:"), 390 "Default:"),
321 state_text(config.no_swap_state)); 391 state_text(config.no_swap_state));
392 printf(UT_OUTPUT_FORMAT);
322 printf(UT_VERBOSE); 393 printf(UT_VERBOSE);
323 394
324 printf("\n"); 395 printf("\n");