summaryrefslogtreecommitdiffstats
path: root/plugins/check_swap.d/swap.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_swap.d/swap.c')
-rw-r--r--plugins/check_swap.d/swap.c122
1 files changed, 61 insertions, 61 deletions
diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c
index 180d5037..58213a3c 100644
--- a/plugins/check_swap.d/swap.c
+++ b/plugins/check_swap.d/swap.c
@@ -52,10 +52,10 @@ swap_result get_swap_data(swap_config config) {
52 } 52 }
53# else // HAVE_SWAP 53# else // HAVE_SWAP
54# ifdef CHECK_SWAP_SWAPCTL_SVR4 54# ifdef CHECK_SWAP_SWAPCTL_SVR4
55 return getSwapFromSwapctl_SRV4(); 55 return getSwapFromSwapctl_SRV4(config);
56# else // CHECK_SWAP_SWAPCTL_SVR4 56# else // CHECK_SWAP_SWAPCTL_SVR4
57# ifdef CHECK_SWAP_SWAPCTL_BSD 57# ifdef CHECK_SWAP_SWAPCTL_BSD
58 return getSwapFromSwapctl_BSD(); 58 return getSwapFromSwapctl_BSD(config);
59# else // CHECK_SWAP_SWAPCTL_BSD 59# else // CHECK_SWAP_SWAPCTL_BSD
60# error No way found to retrieve swap 60# error No way found to retrieve swap
61# endif /* CHECK_SWAP_SWAPCTL_BSD */ 61# endif /* CHECK_SWAP_SWAPCTL_BSD */
@@ -68,7 +68,7 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) {
68 FILE *meminfo_file_ptr; 68 FILE *meminfo_file_ptr;
69 meminfo_file_ptr = fopen(proc_meminfo, "r"); 69 meminfo_file_ptr = fopen(proc_meminfo, "r");
70 70
71 swap_result result = {0}; 71 swap_result result = {};
72 result.errorcode = STATE_UNKNOWN; 72 result.errorcode = STATE_UNKNOWN;
73 73
74 if (meminfo_file_ptr == NULL) { 74 if (meminfo_file_ptr == NULL) {
@@ -78,90 +78,81 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) {
78 return result; 78 return result;
79 } 79 }
80 80
81 uint64_t swap_total = 0; 81 unsigned long swap_total = 0;
82 uint64_t swap_used = 0; 82 unsigned long swap_used = 0;
83 uint64_t swap_free = 0; 83 unsigned long swap_free = 0;
84 84
85 bool found_total = false; 85 bool found_total = false;
86 bool found_used = false;
87 bool found_free = false; 86 bool found_free = false;
88 87
89 char input_buffer[MAX_INPUT_BUFFER]; 88 char input_buffer[MAX_INPUT_BUFFER];
90 char str[32]; 89 char str[32];
91 90
92 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, meminfo_file_ptr)) { 91 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, meminfo_file_ptr)) {
93 uint64_t tmp_KB = 0;
94 92
95 /* 93 /*
96 * The following sscanf call looks for a line looking like: "Swap: 123 94 * The following sscanf call looks for a line looking like: "Swap: 123
97 * 123 123" On which kind of system this format exists, I can not say, 95 * 123 123" which exists on NetBSD (at least),
98 * but I wanted to document this for people who are not adapt with 96 * The unit should be Bytes
99 * sscanf anymore, like me
100 * Also the units used here are unclear and probably wrong
101 */ 97 */
102 if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", &swap_total, &swap_used, &swap_free) == 3) { 98 if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", &swap_total, &swap_used,
103 99 &swap_free) == 3) {
104 result.metrics.total += swap_total;
105 result.metrics.used += swap_used;
106 result.metrics.free += swap_free;
107
108 found_total = true; 100 found_total = true;
109 found_free = true; 101 found_free = true;
110 found_used = true;
111
112 // Set error 102 // Set error
113 result.errorcode = STATE_OK; 103 result.errorcode = STATE_OK;
104 // Break out of fgets here, since both scanf expressions might match (NetBSD for
105 // example)
106 break;
107 }
108
109 /*
110 * The following sscanf call looks for lines looking like:
111 * "SwapTotal: 123" and "SwapFree: 123" This format exists at least
112 * on Debian Linux with a 5.* kernel
113 */
114 unsigned long tmp_KB = 0;
115 int sscanf_result = sscanf(input_buffer,
116 "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu "
117 "%*[k]%*[B]",
118 str, &tmp_KB);
119
120 if (sscanf_result == 2) {
121
122 if (verbose >= 3) {
123 printf("Got %s with %lu\n", str, tmp_KB);
124 }
114 125
115 /* 126 /* I think this part is always in Kb, so convert to bytes */
116 * The following sscanf call looks for lines looking like: 127 if (strcmp("Total", str) == 0) {
117 * "SwapTotal: 123" and "SwapFree: 123" This format exists at least 128 swap_total = tmp_KB * 1000;
118 * on Debian Linux with a 5.* kernel 129 found_total = true;
119 */ 130 } else if (strcmp("Free", str) == 0) {
120 } else { 131 swap_free += tmp_KB * 1000;
121 int sscanf_result = sscanf(input_buffer, 132 found_free = true;
122 "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu " 133 } else if (strcmp("Cached", str) == 0) {
123 "%*[k]%*[B]", 134 swap_free += tmp_KB * 1000;
124 str, &tmp_KB);
125
126 if (sscanf_result == 2) {
127
128 if (verbose >= 3) {
129 printf("Got %s with %lu\n", str, tmp_KB);
130 }
131
132 /* I think this part is always in Kb, so convert to bytes */
133 if (strcmp("Total", str) == 0) {
134 swap_total = tmp_KB * 1000;
135 found_total = true;
136 } else if (strcmp("Free", str) == 0) {
137 swap_free = swap_free + tmp_KB * 1000;
138 found_free = true;
139 found_used = true; // No explicit used metric available
140 } else if (strcmp("Cached", str) == 0) {
141 swap_free = swap_free + tmp_KB * 1000;
142 found_free = true;
143 found_used = true; // No explicit used metric available
144 }
145
146 result.errorcode = STATE_OK;
147 } 135 }
136
137 result.errorcode = STATE_OK;
148 } 138 }
149 } 139 }
150 140
151 fclose(meminfo_file_ptr); 141 fclose(meminfo_file_ptr);
152 142
153 result.metrics.total = swap_total; 143 result.metrics.total = swap_total;
154 result.metrics.used = swap_total - swap_free;
155 result.metrics.free = swap_free; 144 result.metrics.free = swap_free;
145 result.metrics.used = swap_total - swap_free;
156 146
157 if (!found_free || !found_total || !found_used) { 147 if (!found_free || !found_total) {
158 result.errorcode = STATE_UNKNOWN; 148 result.errorcode = STATE_UNKNOWN;
159 } 149 }
160 150
161 return result; 151 return result;
162} 152}
163 153
164swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[], const char swap_format[]) { 154swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[],
155 const char swap_format[]) {
165 swap_result result = {0}; 156 swap_result result = {0};
166 157
167 char *temp_buffer; 158 char *temp_buffer;
@@ -224,7 +215,8 @@ swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[]
224 used_swap_mb = total_swap_mb - free_swap_mb; 215 used_swap_mb = total_swap_mb - free_swap_mb;
225 216
226 if (verbose >= 3) { 217 if (verbose >= 3) {
227 printf(_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb, used_swap_mb, free_swap_mb); 218 printf(_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb, used_swap_mb,
219 free_swap_mb);
228 } 220 }
229 } else { 221 } else {
230 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { 222 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
@@ -297,8 +289,14 @@ struct swapent {
297}; 289};
298 290
299#else 291#else
292
293// Includes for NetBSD
294# include <unistd.h>
295# include <sys/swap.h>
296
300# define bsd_swapctl swapctl 297# define bsd_swapctl swapctl
301#endif 298
299#endif // CHECK_SWAP_SWAPCTL_BSD
302 300
303swap_result getSwapFromSwapctl_BSD(swap_config config) { 301swap_result getSwapFromSwapctl_BSD(swap_config config) {
304 /* get the number of active swap devices */ 302 /* get the number of active swap devices */
@@ -322,8 +320,8 @@ swap_result getSwapFromSwapctl_BSD(swap_config config) {
322 unsigned long long used_swap_mb = 0; 320 unsigned long long used_swap_mb = 0;
323 321
324 for (int i = 0; i < nswaps; i++) { 322 for (int i = 0; i < nswaps; i++) {
325 dsktotal_mb = (float)ent[i].se_nblks / (float)config.conversion_factor; 323 dsktotal_mb = (double)ent[i].se_nblks / (double)config.conversion_factor;
326 dskused_mb = (float)ent[i].se_inuse / (float)config.conversion_factor; 324 dskused_mb = (double)ent[i].se_inuse / (double)config.conversion_factor;
327 dskfree_mb = (dsktotal_mb - dskused_mb); 325 dskfree_mb = (dsktotal_mb - dskused_mb);
328 326
329 if (config.allswaps && dsktotal_mb > 0) { 327 if (config.allswaps && dsktotal_mb > 0) {
@@ -404,7 +402,8 @@ swap_result getSwapFromSwap_SRV4(swap_config config) {
404 } 402 }
405 403
406 /* initialize swap table + entries */ 404 /* initialize swap table + entries */
407 swaptbl_t *tbl = (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * (unsigned long)nswaps)); 405 swaptbl_t *tbl =
406 (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * (unsigned long)nswaps));
408 407
409 if (tbl == NULL) { 408 if (tbl == NULL) {
410 die(STATE_UNKNOWN, _("malloc() failed!\n")); 409 die(STATE_UNKNOWN, _("malloc() failed!\n"));
@@ -439,7 +438,8 @@ swap_result getSwapFromSwap_SRV4(swap_config config) {
439 dskused_mb = (dsktotal_mb - dskfree_mb); 438 dskused_mb = (dsktotal_mb - dskfree_mb);
440 439
441 if (verbose >= 3) { 440 if (verbose >= 3) {
442 printf("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", dsktotal_mb, dskfree_mb, dskused_mb); 441 printf("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", dsktotal_mb, dskfree_mb,
442 dskused_mb);
443 } 443 }
444 444
445 if (config.allswaps && dsktotal_mb > 0) { 445 if (config.allswaps && dsktotal_mb > 0) {