1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
Index: check_swap.c
===================================================================
RCS file: /cvsroot/nagiosplug/nagiosplug/plugins/check_swap.c,v
retrieving revision 1.9
diff -u -r1.9 check_swap.c
--- check_swap.c 18 Feb 2003 03:46:15 -0000 1.9
+++ check_swap.c 27 Feb 2005 23:58:43 -0000
@@ -36,6 +36,18 @@
#define EMAIL "kdebisschop@users.sourceforge.net"
#define SUMMARY "Check swap space on local server.\n"
+#ifdef HAVE_LONGLONG
+typedef long long unsigned int swapsize_t;
+#define SWAPSIZEFORMAT "%llu"
+#define SWAPSIZEFORMAT_OUT SWAPSIZEFORMAT
+#define atos atoll
+#else
+typedef float swapsize_t;
+#define SWAPSIZEFORMAT "%f"
+#define SWAPSIZEFORMAT_OUT "%.0f"
+#define atos (float)atof
+#endif
+
int process_arguments (int argc, char **argv);
int validate_arguments (void);
void print_usage (void);
@@ -43,8 +55,8 @@
int warn_percent = 200;
int crit_percent = 200;
-long unsigned int warn_size = 0;
-long unsigned int crit_size = 0;
+swapsize_t warn_size = 0;
+swapsize_t crit_size = 0;
int verbose;
int allswaps;
@@ -52,8 +64,8 @@
main (int argc, char **argv)
{
int percent_used, percent;
- long unsigned int total_swap = 0, used_swap = 0, free_swap = 0;
- long unsigned int total, used, free;
+ swapsize_t total_swap = 0, used_swap = 0, free_swap = 0;
+ swapsize_t total, used, free;
int result = STATE_OK;
char input_buffer[MAX_INPUT_BUFFER];
#ifdef HAVE_SWAP
@@ -61,6 +73,7 @@
#endif
#ifdef HAVE_PROC_MEMINFO
FILE *fp;
+ char str_format[32];
#endif
char str[32];
char *status = "";
@@ -70,9 +83,10 @@
#ifdef HAVE_PROC_MEMINFO
fp = fopen (PROC_MEMINFO, "r");
+ sprintf (str_format, " %%s %s %s %s", SWAPSIZEFORMAT, SWAPSIZEFORMAT, SWAPSIZEFORMAT);
asprintf (&status, "%s", "Swap used:");
while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
- if (sscanf (input_buffer, " %s %lu %lu %lu", str, &total, &used, &free) == 4 &&
+ if (sscanf (input_buffer, str_format, str, &total, &used, &free) == 4 &&
strstr (str, "Swap")) {
total_swap += total;
used_swap += used;
@@ -83,8 +97,10 @@
result = max_state (STATE_CRITICAL, result);
else if (percent >= warn_percent || free <= warn_size)
result = max_state (STATE_WARNING, result);
- if (verbose)
- asprintf (&status, "%s [%lu/%lu]", status, used, total);
+ if (verbose) {
+ sprintf (str_format, "%%s [%s/%s]", SWAPSIZEFORMAT_OUT, SWAPSIZEFORMAT_OUT);
+ asprintf (&status, str_format, status, used, total);
+ }
}
}
}
@@ -93,7 +109,8 @@
result = max_state (STATE_CRITICAL, result);
else if (percent_used >= warn_percent || free_swap <= warn_size)
result = max_state (STATE_WARNING, result);
- asprintf (&status, "%s %2d%% (%lu out of %lu)", status, percent_used,
+ sprintf (str_format, "%%s %%2d%%%% (%s out of %s)", SWAPSIZEFORMAT_OUT, SWAPSIZEFORMAT_OUT);
+ asprintf (&status, str_format, status, percent_used,
used_swap, total_swap);
fclose (fp);
#else
@@ -137,8 +154,9 @@
result = max_state (STATE_CRITICAL, result);
else if (percent >= warn_percent || free <= warn_size)
result = max_state (STATE_WARNING, result);
- if (verbose)
- asprintf (&status, "%s [%lu/%lu]", status, used, total);
+ if (verbose) {
+ sprintf (str_format, "%%s [%s/%s]", SWAPSIZEFORMAT_OUT, SWAPSIZEFORMAT_OUT);
+ asprintf (&status, str_format, status, used, total);
}
}
percent_used = 100 * ((double) used_swap) / ((double) total_swap);
@@ -146,8 +164,9 @@
result = max_state (STATE_CRITICAL, result);
else if (percent_used >= warn_percent || free_swap <= warn_size)
result = max_state (STATE_WARNING, result);
- asprintf (&status, "%s %2d%% (%lu out of %lu)",
- status, percent_used, used_swap, total_swap);
+ sprintf (str_format, "%%s %%2d%%%% (%s out of %s)", SWAPSIZEFORMAT_OUT, SWAPSIZEFORMAT_OUT)
+ asprintf (&status, str_format,
+ status, percent_used, used_swap, total_swap);
/* If we get anything on STDERR, at least set warning */
while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
@@ -198,6 +217,8 @@
int wc = 0; /* warning counter */
int cc = 0; /* critical counter */
+ char str_format[32];
+
#ifdef HAVE_GETOPT_H
int option_index = 0;
static struct option long_options[] = {
@@ -224,15 +245,16 @@
if (c == -1 || c == EOF)
break;
+ sprintf (str_format, "%s,%%d%%%%", SWAPSIZEFORMAT);
switch (c) {
case 'w': /* warning time threshold */
if (is_intnonneg (optarg)) {
- warn_size = atoi (optarg);
+ warn_size = atos (optarg);
break;
}
else if (strstr (optarg, ",") &&
strstr (optarg, "%") &&
- sscanf (optarg, "%lu,%d%%", &warn_size, &warn_percent) == 2) {
+ sscanf (optarg, str_format, &warn_size, &warn_percent) == 2) {
break;
}
else if (strstr (optarg, "%") &&
@@ -245,12 +267,12 @@
wc++;
case 'c': /* critical time threshold */
if (is_intnonneg (optarg)) {
- crit_size = atoi (optarg);
+ crit_size = atos (optarg);
break;
}
else if (strstr (optarg, ",") &&
strstr (optarg, "%") &&
- sscanf (optarg, "%lu,%d%%", &crit_size, &crit_percent) == 2) {
+ sscanf (optarg, str_format, &crit_size, &crit_percent) == 2) {
break;
}
else if (strstr (optarg, "%") &&
@@ -292,12 +314,12 @@
if (c == argc)
return validate_arguments ();
if (warn_size < 0 && is_intnonneg (argv[c]))
- warn_size = atoi (argv[c++]);
+ warn_size = atos (argv[c++]);
if (c == argc)
return validate_arguments ();
if (crit_size < 0 && is_intnonneg (argv[c]))
- crit_size = atoi (argv[c++]);
+ crit_size = atos (argv[c++]);
return validate_arguments ();
}
|