Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/plugins/check_filecount.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2022,2024 Davide Madrisan <[email protected]>
5 *
6 * A Nagios plugin that returns the number of files found in one or more
7 * directories.
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24 #include <errno.h>
25 #include <getopt.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include "common.h"
31 #include "files.h"
32 #include "logging.h"
33 #include "messages.h"
34 #include "progname.h"
35 #include "progversion.h"
36 #include "thresholds.h"
37 #include "xstrton.h"
38
39 static const char *program_copyright =
40 "Copyright (C) 2022 Davide Madrisan <" PACKAGE_BUGREPORT ">\n";
41
42 static struct option const longopts[] = {
43 {(char *) "ignore-symlinks", no_argument, NULL, 'l'},
44 {(char *) "ignore-unknown", no_argument, NULL, 'u'},
45 {(char *) "include-hidden", no_argument, NULL, 'H'},
46 {(char *) "name", required_argument, NULL, 'n'},
47 {(char *) "recursive", no_argument, NULL, 'r'},
48 {(char *) "regular-only", no_argument, NULL, 'f'},
49 {(char *) "size", required_argument, NULL, 's'},
50 {(char *) "critical", required_argument, NULL, 'c'},
51 {(char *) "warning", required_argument, NULL, 'w'},
52 {(char *) "verbose", no_argument, NULL, 'v'},
53 {(char *) "help", no_argument, NULL, GETOPT_HELP_CHAR},
54 {(char *) "version", no_argument, NULL, GETOPT_VERSION_CHAR},
55 {NULL, 0, NULL, 0}
56 };
57
58 static _Noreturn void
59 usage (FILE * out)
60 {
61 fprintf (out, "%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
62 fputs ("This plugin returns the number of files found in one or more "
63 "directories.\n", out);
64 fputs (program_copyright, out);
65 fputs (USAGE_HEADER, out);
66 fprintf (out,
67 " %s [-w COUNTER] [-c COUNTER] [-f] [-H] [-l] [-r] [-u] \\\n"
68 "\t[-s SIZE] [-t AGE] [-n PATTERN] DIR [DIR...]\n", program_name);
69 fputs (USAGE_OPTIONS, out);
70 fputs (" -f, --regular-only count regular files only\n", out);
71 fputs (" -H, --include-hidden do not skip the hidden files\n", out);
72 fputs (" -l, --ignore-symlinks ignore symlinks\n", out);
73 fputs (" -n, --name only count files that match PATTERN\n",
74 out);
75 fputs (" -r, --recursive check recursively each subdirectory\n",
76 out);
77 fputs (" -s, --size count only files of a specific size\n",
78 out);
79 fputs (" -t, --time count only files of a specific age\n",
80 out);
81 fputs (" -u, --ignore-unknown ignore file with type unknown\n", out);
82 fputs (" -w, --warning COUNTER warning threshold\n", out);
83 fputs (" -c, --critical COUNTER critical threshold\n", out);
84 fputs (" -v, --verbose show details for command-line debugging "
85 "(Nagios may truncate output)\n", out);
86 fputs (USAGE_HELP, out);
87 fputs (USAGE_VERSION, out);
88 fputs (USAGE_NOTE, out);
89 fputs (" By default, this plugin counts the directories and files"
90 " (of every type)\n"
91 " found in DIR, in a non recursive way."
92 " The hidden files are ignored.\n",
93 out);
94 fputs (" Option \"name\".\n"
95 " Only count files that match PATTERN, where PATTERN is a shell-like"
96 " wildcard\n"
97 " as understood by fnmatch(3). Only the filename is checked against"
98 " the\n"
99 " pattern, not the entire path.\n",
100 out);
101 fputs (" Option \"size\".\n"
102 " When SIZE is a positive number, only files that are at least"
103 " this big\n"
104 " are counted. If SIZE is a negative number, this is inversed,"
105 " i. e.\n"
106 " only files smaller than the absolute value of SIZE are counted.\n"
107 " A \"multiplier\" may be added: b (byte), k (kilobyte),"
108 " m (megabyte),\n"
109 " g (gigabyte), t (terabyte), and p (petabyte). Please note that\n"
110 " there are 1000 bytes in a kilobyte, not 1024.\n",
111 out);
112 fputs (" Option \"time\".\n"
113 " If AGE is greater than zero, only files that haven't been"
114 " touched in the\n"
115 " last AGE seconds are counted. If AGE is a negative number, this"
116 " is\n"
117 " inversed. The number can also be followed by a \"multiplier\" to"
118 " easily\n"
119 " specify a larger timespan. Valid multipliers are s (second),"
120 " m (minute),\n"
121 " h (hour), d (day), w (week), and y (year).\n",
122 out);
123
124 fputs (USAGE_EXAMPLES, out);
125 fprintf (out, " %s -l -r /tmp\n", program_name);
126 fprintf (out, " %s -w 150 -c 200 -f -r /var/log/myapp /tmp/myapp\n",
127 program_name);
128 fprintf (out, " %s -w 10 -c 15 -f -r -s -10.5k /tmp/myapp\n",
129 program_name);
130 fprintf (out, " %s -r -t -1h /tmp/myapp # files modified in the last"
131 " hour\n", program_name);
132 fprintf (out, " %s -f -n \"myapp-202207*.log\" /var/log/myapp\n", program_name);
133
134 exit (out == stderr ? STATE_UNKNOWN : STATE_OK);
135 }
136
137 static _Noreturn void
138 print_version (void)
139 {
140 printf ("%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
141 fputs (program_copyright, stdout);
142 fputs (GPLv3_DISCLAIMER, stdout);
143
144 exit (STATE_OK);
145 }
146
147 int
148 main (int argc, char **argv)
149 {
150 int c, i, ret;
151 bool verbose = false;
152 char *bp, *critical = NULL, *warning = NULL,
153 *errmesg_fage = NULL, *errmesg_fsize = NULL,
154 *pattern = NULL;
155 long long int fileage = 0, filesize = 0;
156 size_t size;
157 unsigned int filecount_flags = FILES_DEFAULT;
158 FILE *perfdata;
159 nagstatus status = STATE_OK;
160 thresholds *my_threshold = NULL;
161
162 set_program_name (argv[0]);
163
164 while ((c = getopt_long (argc, argv,
165 "c:fHln:rs:t:uvw:" GETOPT_HELP_VERSION_STRING,
166 longopts, NULL)) != -1)
167 {
168 switch (c)
169 {
170 default:
171 usage (stderr);
172 case 'c':
173 critical = optarg;
174 break;
175 case 'f':
176 filecount_flags |= FILES_REGULAR_ONLY;
177 break;
178 case 'H':
179 filecount_flags |= FILES_INCLUDE_HIDDEN;
180 break;
181 case 'l':
182 filecount_flags |= FILES_IGNORE_SYMLINKS;
183 break;
184 case 'n':
185 pattern = optarg;
186 break;
187 case 'r':
188 filecount_flags |= FILES_RECURSIVE;
189 break;
190 case 's':
191 ret = sizetollint (optarg, &filesize, &errmesg_fsize);
192 if (ret < 0)
193 plugin_error (STATE_UNKNOWN, errno
194 , "failed to parse file size argument: %s"
195 , errmesg_fsize);
196 break;
197 case 't':
198 ret = agetollint (optarg, &fileage, &errmesg_fage);
199 if (ret < 0)
200 plugin_error (STATE_UNKNOWN, errno
201 , "failed to parse file size argument: %s"
202 , errmesg_fage);
203 break;
204 case 'u':
205 filecount_flags |= FILES_IGNORE_UNKNOWN;
206 break;
207 case 'w':
208 warning = optarg;
209 break;
210 case 'v':
211 verbose = true;
212 break;
213
214 case_GETOPT_HELP_CHAR
215 case_GETOPT_VERSION_CHAR
216
217 }
218 }
219
220 if (argc <= optind)
221 usage (stderr);
222
223 struct files_types *filecount;
224 perfdata = open_memstream (&bp, &size);
225 int64_t total = 0;
226
227 for (i = optind; i < argc; ++i)
228 {
229 if (verbose)
230 printf("checking directory %s with flags %u ...\n", argv[i],
231 filecount_flags);
232
233 if (fileage != 0)
234 dbg ("looking for files that were touched %s %u seconds ago...\n"
235 , (fileage < 0) ? "less than" : "more than"
236 , (fileage < 0) ? (unsigned int)(-fileage) : (unsigned int)fileage);
237 if (filesize != 0)
238 dbg ("looking for files with size %s than %lld bytes...\n"
239 , (filesize < 0) ? "less" : "greater"
240 , (filesize < 0) ? -filesize : filesize);
241 if (pattern != NULL)
242 dbg ("looking for files that math the pattern `%s'...\n", pattern);
243
244 filecount = NULL;
245 ret = files_filecount (argv[i], filecount_flags,
246 fileage, filesize, pattern, &filecount);
247 if (ret < 0)
248 plugin_error (STATE_UNKNOWN, errno, "Cannot open %s", argv[i]);
249
250 fprintf (perfdata, "%s_total=%lu ", argv[i],
251 (unsigned long)filecount->total);
252
253 if (!(filecount_flags & FILES_REGULAR_ONLY))
254 fprintf (perfdata, "%s_directory=%lu ", argv[i],
255 (unsigned long)filecount->directory);
256
257 if (filecount_flags & FILES_INCLUDE_HIDDEN)
258 fprintf (perfdata, "%s_hidden=%lu ", argv[i],
259 (unsigned long)filecount->hidden);
260
261 fprintf (perfdata, "%s_regular=%lu ", argv[i],
262 (unsigned long)filecount->regular_file);
263
264 if (!(filecount_flags & FILES_REGULAR_ONLY))
265 fprintf (perfdata, "%s_special=%lu ",
266 argv[i], (unsigned long)filecount->special_file);
267
268 if (!(filecount_flags & (FILES_IGNORE_SYMLINKS | FILES_REGULAR_ONLY)))
269 fprintf (perfdata, "%s_symlink=%lu ", argv[i],
270 (unsigned long)filecount->symlink);
271
272 fprintf (perfdata, "%s_unknown=%lu ", argv[i],
273 (unsigned long)filecount->unknown);
274
275 total += filecount->total;
276 free (filecount);
277 }
278
279 fclose (perfdata);
280
281 status = set_thresholds (&my_threshold, warning, critical);
282 if (status == NP_RANGE_UNPARSEABLE)
283 usage (stderr);
284
285 status = get_status (total, my_threshold);
286 free (my_threshold);
287
288 printf ("%s %s - total number of files: %lu | %s\n",
289 program_name_short, state_text (status), (unsigned long)total, bp);
290
291 return status;
292 }