summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'plugins')
-rw-r--r--plugins/check_swap.c171
1 files changed, 91 insertions, 80 deletions
diff --git a/plugins/check_swap.c b/plugins/check_swap.c
index ad5eb25..63af4cc 100644
--- a/plugins/check_swap.c
+++ b/plugins/check_swap.c
@@ -48,16 +48,23 @@ long unsigned int crit_size = 0;
48int verbose; 48int verbose;
49int allswaps; 49int allswaps;
50 50
51#if !defined(sun)
52int sun = 0; /* defined by compiler if it is a sun solaris system */
53#endif
54
51int 55int
52main (int argc, char **argv) 56main (int argc, char **argv)
53{ 57{
54 int percent_used, percent; 58 int percent_used, percent;
55 long unsigned int total_swap = 0, used_swap = 0, free_swap = 0; 59 long unsigned int total_swap = 0, used_swap = 0, free_swap = 0;
56 long unsigned int total, used, free; 60 long unsigned int total, used, free;
61 int conv_factor; /* Convert to MBs */
57 int result = STATE_OK; 62 int result = STATE_OK;
58 char input_buffer[MAX_INPUT_BUFFER]; 63 char input_buffer[MAX_INPUT_BUFFER];
59#ifdef HAVE_SWAP 64#ifdef HAVE_SWAP
60 char *temp_buffer; 65 char *temp_buffer;
66 char *swap_command;
67 char *swap_format;
61#endif 68#endif
62#ifdef HAVE_PROC_MEMINFO 69#ifdef HAVE_PROC_MEMINFO
63 FILE *fp; 70 FILE *fp;
@@ -70,48 +77,40 @@ main (int argc, char **argv)
70 77
71#ifdef HAVE_PROC_MEMINFO 78#ifdef HAVE_PROC_MEMINFO
72 fp = fopen (PROC_MEMINFO, "r"); 79 fp = fopen (PROC_MEMINFO, "r");
73 asprintf (&status, "%s", "Swap used:");
74 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) { 80 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
75 if (sscanf (input_buffer, " %s %lu %lu %lu", str, &total, &used, &free) == 4 && 81 if (sscanf (input_buffer, " %s %lu %lu %lu", str, &total, &used, &free) == 4 &&
76 strstr (str, "Swap")) { 82 strstr (str, "Swap")) {
77 total_swap += total; 83#endif
78 used_swap += used;
79 free_swap += free;
80 if (allswaps) {
81 percent = 100 * (((double) used) / ((double) total));
82 if (percent >= crit_percent || free <= crit_size)
83 result = max_state (STATE_CRITICAL, result);
84 else if (percent >= warn_percent || free <= warn_size)
85 result = max_state (STATE_WARNING, result);
86 if (verbose)
87 asprintf (&status, "%s [%lu/%lu]", status, used, total);
88 }
89 }
90 }
91 percent_used = 100 * (((double) used_swap) / ((double) total_swap));
92 if (percent_used >= crit_percent || free_swap <= crit_size)
93 result = max_state (STATE_CRITICAL, result);
94 else if (percent_used >= warn_percent || free_swap <= warn_size)
95 result = max_state (STATE_WARNING, result);
96 asprintf (&status, "%s %2d%% (%lu out of %lu)", status, percent_used,
97 used_swap, total_swap);
98 fclose (fp);
99#else
100#ifdef HAVE_SWAP 84#ifdef HAVE_SWAP
101 child_process = spopen (SWAP_COMMAND); 85 if (!allswaps && sun) {
86 asprintf(&swap_command, "%s", "/usr/sbin/swap -s");
87 asprintf(&swap_format, "%s", "%*s %*dk %*s %*s + %*dk %*s = %dk %*s %dk %*s");
88 conv_factor = 2048;
89 } else {
90 asprintf(&swap_command, "%s", SWAP_COMMAND);
91 asprintf(&swap_format, "%s", SWAP_FORMAT);
92 conv_factor = SWAP_CONVERSION;
93 }
94
95 if (verbose >= 2)
96 printf ("Command: %s\n", swap_command);
97 if (verbose >= 3)
98 printf ("Format: %s\n", swap_format);
99
100 child_process = spopen (swap_command);
102 if (child_process == NULL) { 101 if (child_process == NULL) {
103 printf ("Could not open pipe: %s\n", SWAP_COMMAND); 102 printf ("Could not open pipe: %s\n", swap_command);
104 return STATE_UNKNOWN; 103 return STATE_UNKNOWN;
105 } 104 }
106 105
107 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); 106 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
108 if (child_stderr == NULL) 107 if (child_stderr == NULL)
109 printf ("Could not open stderr for %s\n", SWAP_COMMAND); 108 printf ("Could not open stderr for %s\n", swap_command);
110 109
111 sprintf (str, "%s", ""); 110 sprintf (str, "%s", "");
112 /* read 1st line */ 111 /* read 1st line */
113 fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process); 112 fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
114 if (strcmp (SWAP_FORMAT, "") == 0) { 113 if (strcmp (swap_format, "") == 0) {
115 temp_buffer = strtok (input_buffer, " \n"); 114 temp_buffer = strtok (input_buffer, " \n");
116 while (temp_buffer) { 115 while (temp_buffer) {
117 if (strstr (temp_buffer, "blocks")) 116 if (strstr (temp_buffer, "blocks"))
@@ -124,31 +123,42 @@ main (int argc, char **argv)
124 } 123 }
125 } 124 }
126 125
127 asprintf (&status, "%s", "Swap used:"); 126 if (!allswaps && sun) {
128 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { 127 sscanf (input_buffer, swap_format, &used_swap, &free_swap);
129 sscanf (input_buffer, SWAP_FORMAT, &total, &free); 128 used_swap = used_swap / 1024;
130 used = total - free; 129 free_swap = free_swap / 1024;
131 total_swap += total; 130 total_swap = used_swap + free_swap;
132 used_swap += used; 131 } else {
133 free_swap += free; 132 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
134 if (allswaps) { 133 sscanf (input_buffer, swap_format, &total, &free);
135 percent = 100 * (((double) used) / ((double) total)); 134
136 if (percent >= crit_percent || free <= crit_size) 135 total = total / conv_factor;
137 result = max_state (STATE_CRITICAL, result); 136 free = free / conv_factor;
138 else if (percent >= warn_percent || free <= warn_size) 137 if (verbose >= 3)
139 result = max_state (STATE_WARNING, result); 138 printf ("total=%d, free=%d\n", total, free);
140 if (verbose) 139
141 asprintf (&status, "%s [%lu/%lu]", status, used, total); 140 used = total - free;
141#endif
142 total_swap += total;
143 used_swap += used;
144 free_swap += free;
145 if (allswaps) {
146 percent = 100 * (((double) used) / ((double) total));
147 result = max_state (result, check_swap (percent, free));
148 if (verbose)
149 asprintf (&status, "%s [%lu (%d%%)]", status, free, 100 - percent);
150 }
142 } 151 }
143 } 152 }
144 percent_used = 100 * ((double) used_swap) / ((double) total_swap); 153 percent_used = 100 * ((double) used_swap) / ((double) total_swap);
145 if (percent_used >= crit_percent || free_swap <= crit_size) 154 result = max_state (result, check_swap (percent_used, free_swap));
146 result = max_state (STATE_CRITICAL, result); 155 asprintf (&status, " %d%% free (%lu MB out of %lu MB)%s",
147 else if (percent_used >= warn_percent || free_swap <= warn_size) 156 (100 - percent_used), free_swap, total_swap, status);
148 result = max_state (STATE_WARNING, result);
149 asprintf (&status, "%s %2d%% (%lu out of %lu)",
150 status, percent_used, used_swap, total_swap);
151 157
158#ifdef HAVE_PROC_MEMINFO
159 fclose(fp);
160#endif
161#ifdef HAVE_SWAP
152 /* If we get anything on STDERR, at least set warning */ 162 /* If we get anything on STDERR, at least set warning */
153 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) 163 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
154 result = max_state (result, STATE_WARNING); 164 result = max_state (result, STATE_WARNING);
@@ -160,34 +170,29 @@ main (int argc, char **argv)
160 if (spclose (child_process)) 170 if (spclose (child_process))
161 result = max_state (result, STATE_WARNING); 171 result = max_state (result, STATE_WARNING);
162#endif 172#endif
163#endif
164 173
165#ifndef SWAP_COMMAND 174 terminate (result, "SWAP %s:%s\n", state_text (result), status);
166#ifndef SWAP_FILE
167#ifndef HAVE_PROC_MEMINFO
168 return STATE_UNKNOWN;
169#endif
170#endif
171#endif
172
173 if (result == STATE_OK)
174 printf ("Swap ok - %s\n", status);
175 else if (result == STATE_CRITICAL)
176 printf ("CRITICAL - %s\n", status);
177 else if (result == STATE_WARNING)
178 printf ("WARNING - %s\n", status);
179 else if (result == STATE_UNKNOWN)
180 printf ("Unable to read output\n");
181 else {
182 result = STATE_UNKNOWN;
183 printf ("UNKNOWN - %s\n", status);
184 }
185
186 return result;
187} 175}
188 176
189 177
190 178
179
180int
181check_swap (int usp, int free_swap)
182{
183 int result = STATE_UNKNOWN;
184 if (usp >= 0 && usp >= (100.0 - crit_percent))
185 result = STATE_CRITICAL;
186 else if (crit_size >= 0 && free_swap <= crit_size)
187 result = STATE_CRITICAL;
188 else if (usp >= 0 && usp >= (100.0 - warn_percent))
189 result = STATE_WARNING;
190 else if (warn_size >= 0 && free_swap <= warn_size)
191 result = STATE_WARNING;
192 else if (usp >= 0.0)
193 result = STATE_OK;
194 return result;
195}
191 196
192 197
193/* process command-line arguments */ 198/* process command-line arguments */
@@ -255,11 +260,11 @@ process_arguments (int argc, char **argv)
255 usage ("Critical threshold must be integer or percentage!\n"); 260 usage ("Critical threshold must be integer or percentage!\n");
256 } 261 }
257 cc++; 262 cc++;
258 case 'a': /* verbose */ 263 case 'a': /* all swap */
259 allswaps = TRUE; 264 allswaps = TRUE;
260 break; 265 break;
261 case 'v': /* verbose */ 266 case 'v': /* verbose */
262 verbose = TRUE; 267 verbose++;
263 break; 268 break;
264 case 'V': /* version */ 269 case 'V': /* version */
265 print_revision (progname, "$Revision$"); 270 print_revision (progname, "$Revision$");
@@ -307,13 +312,13 @@ validate_arguments (void)
307 && crit_size < 0) { 312 && crit_size < 0) {
308 return ERROR; 313 return ERROR;
309 } 314 }
310 else if (warn_percent > crit_percent) { 315 else if (warn_percent < crit_percent) {
311 usage 316 usage
312 ("Warning percentage should not be less than critical percentage\n"); 317 ("Warning percentage should be more than critical percentage\n");
313 } 318 }
314 else if (warn_size < crit_size) { 319 else if (warn_size < crit_size) {
315 usage 320 usage
316 ("Warning free space should not be more than critical free space\n"); 321 ("Warning free space should be more than critical free space\n");
317 } 322 }
318 return OK; 323 return OK;
319} 324}
@@ -350,15 +355,21 @@ print_help (void)
350 " -w, --warning=INTEGER\n" 355 " -w, --warning=INTEGER\n"
351 " Exit with WARNING status if less than INTEGER bytes of swap space are free\n" 356 " Exit with WARNING status if less than INTEGER bytes of swap space are free\n"
352 " -w, --warning=PERCENT%%\n" 357 " -w, --warning=PERCENT%%\n"
353 " Exit with WARNING status if more than PERCENT of swap space has been used\n" 358 " Exit with WARNING status if less than PERCENT of swap space has been used\n"
354 " -c, --critical=INTEGER\n" 359 " -c, --critical=INTEGER\n"
355 " Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n" 360 " Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n"
356 " -c, --critical=PERCENT%%\n" 361 " -c, --critical=PERCENT%%\n"
357 " Exit with CRITCAL status if more than PERCENT of swap space has been used\n" 362 " Exit with CRITCAL status if less than PERCENT of swap space has been used\n"
358 " -a, --allswaps\n" 363 " -a, --allswaps\n"
359 " Conduct comparisons for all swap partitions, one by one\n" 364 " Conduct comparisons for all swap partitions, one by one\n"
360 " -h, --help\n" 365 " -h, --help\n"
361 " Print detailed help screen\n" 366 " Print detailed help screen\n"
362 " -V, --version\n" " Print version information\n\n"); 367 " -V, --version\n" " Print version information\n"
368#ifdef sun
369 "\nOn Solaris, if -a specified, uses swap -l, otherwise uses swap -s.\n"
370 "Will be discrepencies because swap -s counts allocated swap and includes real memory\n"
371#endif
372 "\n"
373 );
363 support (); 374 support ();
364} 375}