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.c125
1 files changed, 80 insertions, 45 deletions
diff --git a/plugins/check_swap.c b/plugins/check_swap.c
index f6bbd33..ddf298a 100644
--- a/plugins/check_swap.c
+++ b/plugins/check_swap.c
@@ -56,29 +56,37 @@ main (int argc, char **argv)
56 int result = STATE_OK; 56 int result = STATE_OK;
57 char input_buffer[MAX_INPUT_BUFFER]; 57 char input_buffer[MAX_INPUT_BUFFER];
58 char *perf; 58 char *perf;
59 int conv_factor = SWAP_CONVERSION;
59#ifdef HAVE_PROC_MEMINFO 60#ifdef HAVE_PROC_MEMINFO
60 FILE *fp; 61 FILE *fp;
61#else 62#else
62# ifdef HAVE_SWAP 63# ifdef HAVE_SWAP
63 int conv_factor; /* Convert to MBs */
64 char *temp_buffer; 64 char *temp_buffer;
65 char *swap_command; 65 char *swap_command;
66 char *swap_format; 66 char *swap_format;
67# else 67# else
68# ifdef HAVE_DECL_SWAPCTL 68# ifdef HAVE_DECL_SWAPCTL
69 int i=0, nswaps=0; 69 int i=0, nswaps=0, swapctl_res=0;
70 swaptbl_t tbl; 70# ifdef CHECK_SWAP_SWAPCTL_SVR4
71 swaptbl_t *tbl=NULL;
72 swapent_t *ent=NULL;
73# else
74# ifdef CHECK_SWAP_SWAPCTL_BSD
75 struct swapent *ent;
76# endif /* CHECK_SWAP_SWAPCTL_BSD */
77# endif /* CHECK_SWAP_SWAPCTL_SVR4 */
71# endif /* HAVE_DECL_SWAPCTL */ 78# endif /* HAVE_DECL_SWAPCTL */
72# endif 79# endif
73#endif 80#endif
74 char str[32]; 81 char str[32];
75 char *status; 82 char *status, *tmp_status;
76 83
77 setlocale (LC_ALL, ""); 84 setlocale (LC_ALL, "");
78 bindtextdomain (PACKAGE, LOCALEDIR); 85 bindtextdomain (PACKAGE, LOCALEDIR);
79 textdomain (PACKAGE); 86 textdomain (PACKAGE);
80 87
81 status = strdup (""); 88 status = strdup ("");
89 tmp_status = strdup ("");
82 perf = strdup (""); 90 perf = strdup ("");
83 91
84 if (process_arguments (argc, argv) != OK) 92 if (process_arguments (argc, argv) != OK)
@@ -122,7 +130,6 @@ main (int argc, char **argv)
122# ifdef HAVE_SWAP 130# ifdef HAVE_SWAP
123 asprintf(&swap_command, "%s", SWAP_COMMAND); 131 asprintf(&swap_command, "%s", SWAP_COMMAND);
124 asprintf(&swap_format, "%s", SWAP_FORMAT); 132 asprintf(&swap_format, "%s", SWAP_FORMAT);
125 conv_factor = SWAP_CONVERSION;
126 133
127/* These override the command used if a summary (and thus ! allswaps) is required */ 134/* These override the command used if a summary (and thus ! allswaps) is required */
128/* The summary flag returns more accurate information about swap usage on these OSes */ 135/* The summary flag returns more accurate information about swap usage on these OSes */
@@ -132,14 +139,6 @@ main (int argc, char **argv)
132 asprintf(&swap_format, "%s", "%d%*s %d"); 139 asprintf(&swap_format, "%s", "%d%*s %d");
133 conv_factor = 1; 140 conv_factor = 1;
134 } 141 }
135# else
136# ifdef sun
137 if (!allswaps) {
138 asprintf(&swap_command, "%s", "/usr/sbin/swap -s");
139 asprintf(&swap_format, "%s", "%*s %*dk %*s %*s + %*dk %*s = %dk %*s %dk %*s");
140 conv_factor = 2048;
141 }
142# endif
143# endif 142# endif
144 143
145 if (verbose >= 2) 144 if (verbose >= 2)
@@ -183,15 +182,6 @@ main (int argc, char **argv)
183 if (verbose >= 3) 182 if (verbose >= 3)
184 printf (_("total=%d, used=%d, free=%d\n"), total_swap, used_swap, free_swap); 183 printf (_("total=%d, used=%d, free=%d\n"), total_swap, used_swap, free_swap);
185 } else { 184 } else {
186# else
187# ifdef sun
188 if (!allswaps) {
189 sscanf (input_buffer, swap_format, &used_swap, &free_swap);
190 used_swap = used_swap / 1024;
191 free_swap = free_swap / 1024;
192 total_swap = used_swap + free_swap;
193 } else {
194# endif
195# endif 185# endif
196 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { 186 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
197 sscanf (input_buffer, swap_format, &dsktotal, &dskfree); 187 sscanf (input_buffer, swap_format, &dsktotal, &dskfree);
@@ -219,10 +209,6 @@ main (int argc, char **argv)
219 } 209 }
220# ifdef _AIX 210# ifdef _AIX
221 } 211 }
222# else
223# ifdef sun
224 }
225# endif
226# endif 212# endif
227 213
228 /* If we get anything on STDERR, at least set warning */ 214 /* If we get anything on STDERR, at least set warning */
@@ -236,31 +222,38 @@ main (int argc, char **argv)
236 if (spclose (child_process)) 222 if (spclose (child_process))
237 result = max_state (result, STATE_WARNING); 223 result = max_state (result, STATE_WARNING);
238# else 224# else
239# ifdef HAVE_DECL_SWAPCTL 225# ifdef CHECK_SWAP_SWAPCTL_SVR4
240
241 /* initialize swap table entries */
242 memset(&tbl, 0, sizeof(swaptbl_t));
243 tbl.swt_ent[0].ste_path=(char*)malloc(sizeof(char)*(MAXPATHLEN+1));
244 memset(tbl.swt_ent[0].ste_path, 0, sizeof(char)*(MAXPATHLEN+1));
245 tbl.swt_n=1;
246 226
247 /* get the number of active swap devices */ 227 /* get the number of active swap devices */
248 nswaps=swapctl(SC_GETNSWP, NULL); 228 nswaps=swapctl(SC_GETNSWP, NULL);
249 229
230 /* initialize swap table + entries */
231 tbl=(swaptbl_t*)malloc(sizeof(swaptbl_t)+(sizeof(swapent_t)*nswaps));
232 memset(tbl, 0, sizeof(swaptbl_t)+(sizeof(swapent_t)*nswaps));
233 tbl->swt_n=nswaps;
234 for(i=0;i<nswaps;i++){
235 ent=&tbl->swt_ent[i];
236 ent->ste_path=(char*)malloc(sizeof(char)*MAXPATHLEN);
237 }
238
250 /* and now, tally 'em up */ 239 /* and now, tally 'em up */
240 swapctl_res=swapctl(SC_LIST, tbl);
241 if(swapctl_res < 0){
242 perror("swapctl failed: ");
243 result = STATE_WARNING;
244 }
245
251 for(i=0;i<nswaps;i++){ 246 for(i=0;i<nswaps;i++){
252 swapctl(SC_LIST, &tbl); 247 dsktotal = tbl->swt_ent[i].ste_pages / SWAP_CONVERSION;
253 /* on tru64, swap is stored in 8k pages. i'd 248 dskfree = tbl->swt_ent[i].ste_free / SWAP_CONVERSION;
254 use conv_factor or SWAP_CONVERSION, but they're 249 dskused = ( dsktotal - dskfree );
255 both buried under a bunch of ifdef's. ideally
256 all functions could call getpagesize(2)... */
257 dsktotal = tbl.swt_ent[0].ste_pages / 128;
258 dskfree = tbl.swt_ent[0].ste_free / 128;
259 dskused = ( total_swap - free_swap );
260 250
261 if(allswaps && dsktotal > 0){ 251 if(allswaps && dsktotal > 0){
262 percent = 100 * (((double) dskused) / ((double) dsktotal)); 252 percent = 100 * (((double) dskused) / ((double) dsktotal));
263 result = max_state (result, check_swap (percent, dskfree)); 253 result = max_state (result, check_swap (percent, dskfree));
254 if (verbose) {
255 asprintf (&status, "%s [%d (%d%%)]", status, (int)dskfree, 100 - percent);
256 }
264 } 257 }
265 258
266 total_swap += dsktotal; 259 total_swap += dsktotal;
@@ -269,16 +262,58 @@ main (int argc, char **argv)
269 } 262 }
270 263
271 /* and clean up after ourselves */ 264 /* and clean up after ourselves */
272 free(tbl.swt_ent[0].ste_path); 265 for(i=0;i<nswaps;i++){
266 free(tbl->swt_ent[i].ste_path);
267 }
268 free(tbl);
269# else
270# ifdef CHECK_SWAP_SWAPCTL_BSD
273 271
274# endif /* HAVE_DECL_SWAPCTL */ 272 /* get the number of active swap devices */
273 nswaps=swapctl(SWAP_NSWAP, NULL, 0);
274
275 /* initialize swap table + entries */
276 ent=(struct swapent*)malloc(sizeof(struct swapent)*nswaps);
277
278 /* and now, tally 'em up */
279 swapctl_res=swapctl(SWAP_STATS, ent, nswaps);
280 if(swapctl_res < 0){
281 perror("swapctl failed: ");
282 result = STATE_WARNING;
283 }
284
285 for(i=0;i<nswaps;i++){
286 dsktotal = ent->se_nblks / conv_factor;
287 dskused = ent->se_inuse / conv_factor;
288 dskfree = ( dsktotal - dskused );
289
290 if(allswaps && dsktotal > 0){
291 percent = 100 * (((double) dskused) / ((double) dsktotal));
292 result = max_state (result, check_swap (percent, dskfree));
293 if (verbose) {
294 asprintf (&status, "%s [%d (%d%%)]", status, (int)dskfree, 100 - percent);
295 }
296 }
297
298 total_swap += dsktotal;
299 free_swap += dskfree;
300 used_swap += dskused;
301 }
302
303 /* and clean up after ourselves */
304 free(ent);
305
306# endif /* CHECK_SWAP_SWAPCTL_BSD */
307# endif /* CHECK_SWAP_SWAPCTL_SVR4 */
275# endif /* HAVE_SWAP */ 308# endif /* HAVE_SWAP */
276#endif /* HAVE_PROC_MEMINFO */ 309#endif /* HAVE_PROC_MEMINFO */
277 310
278 percent_used = 100 * ((double) used_swap) / ((double) total_swap); 311 percent_used = 100 * ((double) used_swap) / ((double) total_swap);
279 result = max_state (result, check_swap (percent_used, free_swap)); 312 result = max_state (result, check_swap (percent_used, free_swap));
280 asprintf (&status, _(" %d%% free (%llu MB out of %llu MB)%s"), 313 /* broken into two steps because of funkiness with builtin asprintf */
281 (100 - percent_used), free_swap, total_swap, status); 314 asprintf (&tmp_status, _(" %d%% free (%llu MB out of %llu MB)"),
315 (100 - percent_used), free_swap, total_swap);
316 asprintf (&status, "%s%s", tmp_status, status);
282 317
283 asprintf (&perf, "%s", perfdata ("swap", (long) free_swap, "MB", 318 asprintf (&perf, "%s", perfdata ("swap", (long) free_swap, "MB",
284 TRUE, (long) max (warn_size/1024, warn_percent/100.0*total_swap), 319 TRUE, (long) max (warn_size/1024, warn_percent/100.0*total_swap),