summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTon Voon <tonvoon@users.sourceforge.net>2004-08-23 21:18:12 (GMT)
committerTon Voon <tonvoon@users.sourceforge.net>2004-08-23 21:18:12 (GMT)
commit33cffe8f87e58de3e38e9146584fb05371eb7de3 (patch)
tree8eb398d18ee7738fb226e305ff476d9aa2dacd55
parentd5ae799b8792708cad68d57c966b562dc442dccd (diff)
downloadmonitoring-plugins-33cffe8f87e58de3e38e9146584fb05371eb7de3.tar.gz
Using swapctl for Solaris, Tru64 and *BSD (Sean Finney)
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@891 f882894a-f735-0410-b71e-b25c423dba1c
-rw-r--r--configure.in115
-rw-r--r--plugins/check_swap.c125
-rw-r--r--plugins/common.h4
3 files changed, 159 insertions, 85 deletions
diff --git a/configure.in b/configure.in
index 3154af0..8e971dd 100644
--- a/configure.in
+++ b/configure.in
@@ -1303,18 +1303,16 @@ then
1303 ac_cv_swap_conv=2048 1303 ac_cv_swap_conv=2048
1304 AC_MSG_RESULT([using IRIX format swap]) 1304 AC_MSG_RESULT([using IRIX format swap])
1305 1305
1306 elif [$PATH_TO_SWAP -l 2>/dev/null | egrep -i "^swapfile +dev +swaplo +blocks +free" >/dev/null]
1307 then
1308 ac_cv_swap_format=["%*s %*[0-9,-] %*d %d %d"]
1309 ac_cv_swap_conv=2048
1310 AC_MSG_RESULT([using Solaris format swap])
1311
1312 elif [$PATH_TO_SWAP -l 2>/dev/null | egrep -i "^path +dev +swaplo +blocks +free" >/dev/null] 1306 elif [$PATH_TO_SWAP -l 2>/dev/null | egrep -i "^path +dev +swaplo +blocks +free" >/dev/null]
1313 then 1307 then
1314 ac_cv_swap_format=["%*s %*[0-9,-] %*d %d %d"] 1308 ac_cv_swap_format=["%*s %*[0-9,-] %*d %d %d"]
1315 ac_cv_swap_conv=2048 1309 ac_cv_swap_conv=2048
1316 AC_MSG_RESULT([using Unixware format swap]) 1310 AC_MSG_RESULT([using Unixware format swap])
1317 1311 else
1312 dnl if we don't know what format swap's output is
1313 dnl we might as well pretend we didn't see it
1314 ac_cv_have_swap=""
1315 ac_cv_swap_command=""
1318 fi 1316 fi
1319fi 1317fi
1320dnl end if for PATH_TO_SWAP 1318dnl end if for PATH_TO_SWAP
@@ -1362,46 +1360,83 @@ fi
1362dnl end if for PATH_TO_SWAPINFO 1360dnl end if for PATH_TO_SWAPINFO
1363fi 1361fi
1364 1362
1365AC_PATH_PROG(PATH_TO_SWAPCTL,swapctl,,[$PATH:/sbin]) 1363dnl
1366if (test -n "$PATH_TO_SWAPCTL") 1364dnl test for swapctl system call, both the 2-arg and 3-arg variants
1367then 1365dnl fwict, the 2-arg is an SVR4 standard, whereas the 3-arg is shared
1368AC_MSG_CHECKING([for $PATH_TO_SWAPCTL format]) 1366dnl in the various BSD's
1369if [$PATH_TO_SWAPCTL -l 2>&1 >/dev/null] 1367dnl
1368
1369AC_CHECK_HEADERS([sys/swap.h sys/stat.h sys/param.h])
1370AC_CHECK_DECLS([swapctl],,,[
1371 #include <unistd.h>
1372 #include <sys/types.h>
1373 #include <sys/stat.h>
1374 #include <sys/swap.h>
1375 ])
1376AC_CHECK_TYPES([swaptbl_t, swapent_t],,,[
1377 #include <sys/types.h>
1378 #include <sys/stat.h>
1379 #include <sys/swap.h>
1380 ])
1381AC_CHECK_MEMBERS([struct swapent.se_nblks],,,[
1382 #include <unistd.h>
1383 #include <sys/types.h>
1384 #include <sys/stat.h>
1385 #include <sys/swap.h>
1386 ])
1387
1388if test "$ac_cv_have_decl_swapctl" = "yes";
1370then 1389then
1371 ac_cv_have_swap=yes 1390 EXTRAS="$EXTRAS check_swap"
1372 ac_cv_swap_command="$PATH_TO_SWAPCTL -l" 1391 AC_MSG_CHECKING([for 2-arg (SVR4) swapctl])
1373 if [$PATH_TO_SWAPCTL -l 2>/dev/null | \ 1392 if test "$ac_cv_type_swaptbl_t" = "yes" -a \
1374 egrep -i "^Device +1K-blocks +Used +Avail +Capacity +Priority" >/dev/null ] 1393 "$ac_cv_type_swapent_t" = "yes";
1375 then 1394 then
1376 ac_cv_swap_format=["%*s %llu %*d %llu %*d"] 1395 AC_MSG_RESULT([yes])
1377 ac_cv_swap_conv=1024 1396 ac_cv_check_swap_swapctl_svr4="1";
1378 AC_MSG_RESULT([using 1K BSD format swapctl]) 1397 AC_DEFINE([CHECK_SWAP_SWAPCTL_SVR4],1,
1379 1398 [Define if 2-argument SVR4 swapctl exists])
1380 elif [$PATH_TO_SWAPCTL -l 2>/dev/null | \ 1399 else
1381 egrep -i "^Device +512-blocks +Used +Avail +Capacity +Priority" >/dev/null ] 1400 AC_MSG_RESULT([no])
1401 AC_MSG_CHECKING([for 3-arg (*BSD) swapctl])
1402 if test "$ac_cv_member_struct_swapent_se_nblks" = "yes";
1403 then
1404 AC_MSG_RESULT([yes])
1405 AC_DEFINE([CHECK_SWAP_SWAPCTL_BSD],1,
1406 [Define if 3-argument BSD swapctl exists])
1407 else
1408 AC_MSG_RESULT([no])
1409 fi
1410 fi
1411 AC_MSG_CHECKING([for whether swapctl uses blocks or pages])
1412 if test "$ac_cv_check_swap_swapctl_svr4" = "1";
1382 then 1413 then
1383 ac_cv_swap_format=["%*s %llu %*d %llu %*d"] 1414 dnl
1415 dnl the SVR4 spec returns values in pages
1416 dnl
1417 AC_MSG_RESULT([page])
1418 AC_CHECK_DECLS([sysconf])
1419 AC_MSG_CHECKING([for system page size])
1420 if test "$ac_cv_have_decl_sysconf" = "yes";
1421 then
1422 AC_MSG_RESULT([determined by sysconf(3)])
1423 ac_cv_swap_conv="(1048576/sysconf(_SC_PAGESIZE))"
1424 else
1425 AC_MSG_WARN([don't know. guessing 4096k])
1426 ac_cv_swap_conv=256
1427 fi
1428 else
1429 dnl
1430 dnl the BSD spec returns values in blocks
1431 dnl
1432 AC_MSG_RESULT([blocks (assuming 512b)])
1384 ac_cv_swap_conv=2048 1433 ac_cv_swap_conv=2048
1385 AC_MSG_RESULT([using 512 byte BSD format swapctl])
1386 fi 1434 fi
1435 AC_DEFINE_UNQUOTED(SWAP_CONVERSION,$ac_cv_swap_conv,
1436 [Conversion factor to MB])
1387fi 1437fi
1388dnl end if for PATH_TO_SWAPCTL
1389fi
1390
1391dnl
1392dnl test for swapctl system call, as found in tru64 and solaris
1393dnl note: the way the ifdef logic in check_swap is right now,
1394dnl this will only affect systems that don't pass one of the
1395dnl earlier tests.
1396dnl
1397AC_CHECK_HEADERS([sys/swap.h sys/stat.h sys/param.h])
1398AC_CHECK_DECLS([swapctl],,,[
1399 #include <sys/types.h>
1400 #include <sys/stat.h>
1401 #include <sys/swap.h>
1402 ])
1403dnl 1438dnl
1404dnl end test for swapctl system call 1439dnl end tests for the swapctl system calls
1405dnl 1440dnl
1406 1441
1407 1442
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),
diff --git a/plugins/common.h b/plugins/common.h
index d878271..fb8ca9c 100644
--- a/plugins/common.h
+++ b/plugins/common.h
@@ -110,6 +110,10 @@
110# endif 110# endif
111#endif 111#endif
112 112
113#ifndef SWAP_CONVERSION
114# define SWAP_CONVERSION 1
115#endif
116
113/* 117/*
114 * 118 *
115 * Missing Functions 119 * Missing Functions