diff options
Diffstat (limited to 'plugins/check_swap.d/swap.c')
-rw-r--r-- | plugins/check_swap.d/swap.c | 104 |
1 files changed, 50 insertions, 54 deletions
diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c index 2fe4544f..634f80d9 100644 --- a/plugins/check_swap.d/swap.c +++ b/plugins/check_swap.d/swap.c | |||
@@ -14,6 +14,8 @@ swap_config swap_config_init(void) { | |||
14 | tmp.warn_is_set = false; | 14 | tmp.warn_is_set = false; |
15 | tmp.crit_is_set = false; | 15 | tmp.crit_is_set = false; |
16 | 16 | ||
17 | tmp.output_format_is_set = false; | ||
18 | |||
17 | #ifdef _AIX | 19 | #ifdef _AIX |
18 | tmp.on_aix = true; | 20 | tmp.on_aix = true; |
19 | #else | 21 | #else |
@@ -66,7 +68,7 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { | |||
66 | FILE *meminfo_file_ptr; | 68 | FILE *meminfo_file_ptr; |
67 | meminfo_file_ptr = fopen(proc_meminfo, "r"); | 69 | meminfo_file_ptr = fopen(proc_meminfo, "r"); |
68 | 70 | ||
69 | swap_result result = {0}; | 71 | swap_result result = {}; |
70 | result.errorcode = STATE_UNKNOWN; | 72 | result.errorcode = STATE_UNKNOWN; |
71 | 73 | ||
72 | if (meminfo_file_ptr == NULL) { | 74 | if (meminfo_file_ptr == NULL) { |
@@ -76,83 +78,71 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { | |||
76 | return result; | 78 | return result; |
77 | } | 79 | } |
78 | 80 | ||
79 | uint64_t swap_total = 0; | 81 | unsigned long swap_total = 0; |
80 | uint64_t swap_used = 0; | 82 | unsigned long swap_used = 0; |
81 | uint64_t swap_free = 0; | 83 | unsigned long swap_free = 0; |
82 | 84 | ||
83 | bool found_total = false; | 85 | bool found_total = false; |
84 | bool found_used = false; | ||
85 | bool found_free = false; | 86 | bool found_free = false; |
86 | 87 | ||
87 | char input_buffer[MAX_INPUT_BUFFER]; | 88 | char input_buffer[MAX_INPUT_BUFFER]; |
88 | char str[32]; | 89 | char str[32]; |
89 | 90 | ||
90 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, meminfo_file_ptr)) { | 91 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, meminfo_file_ptr)) { |
91 | uint64_t tmp_KB = 0; | ||
92 | 92 | ||
93 | /* | 93 | /* |
94 | * 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 |
95 | * 123 123" On which kind of system this format exists, I can not say, | 95 | * 123 123" which exists on NetBSD (at least), |
96 | * but I wanted to document this for people who are not adapt with | 96 | * The unit should be Bytes |
97 | * sscanf anymore, like me | ||
98 | * Also the units used here are unclear and probably wrong | ||
99 | */ | 97 | */ |
100 | 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, &swap_free) == 3) { |
101 | |||
102 | result.metrics.total += swap_total; | ||
103 | result.metrics.used += swap_used; | ||
104 | result.metrics.free += swap_free; | ||
105 | |||
106 | found_total = true; | 99 | found_total = true; |
107 | found_free = true; | 100 | found_free = true; |
108 | found_used = true; | ||
109 | |||
110 | // Set error | 101 | // Set error |
111 | result.errorcode = STATE_OK; | 102 | result.errorcode = STATE_OK; |
103 | // Break out of fgets here, since both scanf expressions might match (NetBSD for example) | ||
104 | break; | ||
105 | } | ||
112 | 106 | ||
113 | /* | 107 | /* |
114 | * The following sscanf call looks for lines looking like: | 108 | * The following sscanf call looks for lines looking like: |
115 | * "SwapTotal: 123" and "SwapFree: 123" This format exists at least | 109 | * "SwapTotal: 123" and "SwapFree: 123" This format exists at least |
116 | * on Debian Linux with a 5.* kernel | 110 | * on Debian Linux with a 5.* kernel |
117 | */ | 111 | */ |
118 | } else { | 112 | unsigned long tmp_KB = 0; |
119 | int sscanf_result = sscanf(input_buffer, | 113 | int sscanf_result = sscanf(input_buffer, |
120 | "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu " | 114 | "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu " |
121 | "%*[k]%*[B]", | 115 | "%*[k]%*[B]", |
122 | str, &tmp_KB); | 116 | str, &tmp_KB); |
123 | 117 | ||
124 | if (sscanf_result == 2) { | 118 | if (sscanf_result == 2) { |
125 | 119 | ||
126 | if (verbose >= 3) { | 120 | if (verbose >= 3) { |
127 | printf("Got %s with %lu\n", str, tmp_KB); | 121 | printf("Got %s with %lu\n", str, tmp_KB); |
128 | } | ||
129 | |||
130 | /* I think this part is always in Kb, so convert to bytes */ | ||
131 | if (strcmp("Total", str) == 0) { | ||
132 | swap_total = tmp_KB * 1000; | ||
133 | found_total = true; | ||
134 | } else if (strcmp("Free", str) == 0) { | ||
135 | swap_free = swap_free + tmp_KB * 1000; | ||
136 | found_free = true; | ||
137 | found_used = true; // No explicit used metric available | ||
138 | } else if (strcmp("Cached", str) == 0) { | ||
139 | swap_free = swap_free + tmp_KB * 1000; | ||
140 | found_free = true; | ||
141 | found_used = true; // No explicit used metric available | ||
142 | } | ||
143 | |||
144 | result.errorcode = STATE_OK; | ||
145 | } | 122 | } |
123 | |||
124 | /* I think this part is always in Kb, so convert to bytes */ | ||
125 | if (strcmp("Total", str) == 0) { | ||
126 | swap_total = tmp_KB * 1000; | ||
127 | found_total = true; | ||
128 | } else if (strcmp("Free", str) == 0) { | ||
129 | swap_free += tmp_KB * 1000; | ||
130 | found_free = true; | ||
131 | } else if (strcmp("Cached", str) == 0) { | ||
132 | swap_free += tmp_KB * 1000; | ||
133 | } | ||
134 | |||
135 | result.errorcode = STATE_OK; | ||
146 | } | 136 | } |
147 | } | 137 | } |
148 | 138 | ||
149 | fclose(meminfo_file_ptr); | 139 | fclose(meminfo_file_ptr); |
150 | 140 | ||
151 | result.metrics.total = swap_total; | 141 | result.metrics.total = swap_total; |
152 | result.metrics.used = swap_total - swap_free; | ||
153 | result.metrics.free = swap_free; | 142 | result.metrics.free = swap_free; |
143 | result.metrics.used = swap_total - swap_free; | ||
154 | 144 | ||
155 | if (!found_free || !found_total || !found_used) { | 145 | if (!found_free || !found_total) { |
156 | result.errorcode = STATE_UNKNOWN; | 146 | result.errorcode = STATE_UNKNOWN; |
157 | } | 147 | } |
158 | 148 | ||
@@ -295,8 +285,14 @@ struct swapent { | |||
295 | }; | 285 | }; |
296 | 286 | ||
297 | #else | 287 | #else |
288 | |||
289 | // Includes for NetBSD | ||
290 | # include <unistd.h> | ||
291 | # include <sys/swap.h> | ||
292 | |||
298 | # define bsd_swapctl swapctl | 293 | # define bsd_swapctl swapctl |
299 | #endif | 294 | |
295 | #endif // CHECK_SWAP_SWAPCTL_BSD | ||
300 | 296 | ||
301 | swap_result getSwapFromSwapctl_BSD(swap_config config) { | 297 | swap_result getSwapFromSwapctl_BSD(swap_config config) { |
302 | /* get the number of active swap devices */ | 298 | /* get the number of active swap devices */ |
@@ -320,8 +316,8 @@ swap_result getSwapFromSwapctl_BSD(swap_config config) { | |||
320 | unsigned long long used_swap_mb = 0; | 316 | unsigned long long used_swap_mb = 0; |
321 | 317 | ||
322 | for (int i = 0; i < nswaps; i++) { | 318 | for (int i = 0; i < nswaps; i++) { |
323 | dsktotal_mb = (float)ent[i].se_nblks / (float)config.conversion_factor; | 319 | dsktotal_mb = (double)ent[i].se_nblks / (double)config.conversion_factor; |
324 | dskused_mb = (float)ent[i].se_inuse / (float)config.conversion_factor; | 320 | dskused_mb = (double)ent[i].se_inuse / (double)config.conversion_factor; |
325 | dskfree_mb = (dsktotal_mb - dskused_mb); | 321 | dskfree_mb = (dsktotal_mb - dskused_mb); |
326 | 322 | ||
327 | if (config.allswaps && dsktotal_mb > 0) { | 323 | if (config.allswaps && dsktotal_mb > 0) { |