Debian Codegraph @

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

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2014,2015 Davide Madrisan <[email protected]>
5 *
6 * A Nagios plugin that returns the number of seconds elapsed between
7 * local time and Nagios time.
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 <stdio.h>
27 #include <stdlib.h>
28 #include <time.h>
29
30 #include "common.h"
31 #include "messages.h"
32 #include "progname.h"
33 #include "progversion.h"
34 #include "thresholds.h"
35 #include "xstrton.h"
36
37 static const char *program_copyright =
38 "Copyright (C) 2014,2015 Davide Madrisan <" PACKAGE_BUGREPORT ">\n";
39
40 static struct option const longopts[] = {
41 {(char *) "refclock", required_argument, NULL, 'r'},
42 {(char *) "critical", required_argument, NULL, 'c'},
43 {(char *) "warning", required_argument, NULL, 'w'},
44 {(char *) "verbose", no_argument, NULL, 'v'},
45 {(char *) "help", no_argument, NULL, GETOPT_HELP_CHAR},
46 {(char *) "version", no_argument, NULL, GETOPT_VERSION_CHAR},
47 {NULL, 0, NULL, 0}
48 };
49
50 static _Noreturn void
51 usage (FILE * out)
52 {
53 fprintf (out, "%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
54 fputs ("This plugin returns the number of seconds elapsed between\n", out);
55 fputs ("the host local time and Nagios time.\n", out);
56 fputs (program_copyright, out);
57 fputs (USAGE_HEADER, out);
58 fprintf (out, " %s [-w COUNTER] [-c COUNTER] --refclock TIME\n",
59 program_name);
60 fputs (USAGE_OPTIONS, out);
61 fputs (" -r, --refclock COUNTER the clock reference "
62 "(in seconds since the Epoch)\n",
63 out);
64 fputs (" -w, --warning COUNTER warning threshold\n", out);
65 fputs (" -c, --critical COUNTER critical threshold\n", out);
66 fputs (" -v, --verbose show details for command-line debugging "
67 "(Nagios may truncate output)\n", out);
68 fputs (USAGE_HELP, out);
69 fputs (USAGE_VERSION, out);
70 fputs (USAGE_EXAMPLES, out);
71 fprintf (out, " %s -w 60 -c 120 --refclock $ARG1$\n",
72 program_name);
73 fputs (" # where $ARG1$ is the number of seconds since the Epoch: "
74 "\"$(date '+%s')\"\n", out);
75 fputs (" # provided by the Nagios poller\n", out);
76
77 exit (out == stderr ? STATE_UNKNOWN : STATE_OK);
78 }
79
80 static _Noreturn void
81 print_version (void)
82 {
83 printf ("%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
84 fputs (program_copyright, stdout);
85 fputs (GPLv3_DISCLAIMER, stdout);
86
87 exit (STATE_OK);
88 }
89
90 static int
91 get_timedelta (unsigned long refclock, bool verbose)
92 {
93 struct tm tminfo;
94 time_t rawtime;
95 char outstr[32];
96 char *end = NULL;
97 long timedelta;
98
99 rawtime = time (NULL);
100 localtime_r (&rawtime, &tminfo);
101 if (strftime (outstr, sizeof (outstr), "%s", &tminfo) == 0)
102 plugin_error (STATE_UNKNOWN, errno, "strftime() failed");
103
104 timedelta = (strtol (outstr, &end, 10) - refclock);
105
106 if (verbose)
107 {
108 printf ("Seconds since the Epoch: %s\n", outstr);
109 printf ("Refclock: %lu --> Delta: %ld\n", refclock, timedelta);
110 }
111
112 return timedelta;
113 }
114
115 #ifndef NPL_TESTING
116 int
117 main (int argc, char **argv)
118 {
119 int c;
120 bool verbose = false;
121 char *critical = NULL, *warning = NULL;
122 nagstatus status = STATE_OK;
123 thresholds *my_threshold = NULL;
124
125 unsigned long refclock = ~0UL;
126 long timedelta;
127
128 set_program_name (argv[0]);
129
130 while ((c = getopt_long (argc, argv,
131 "r:c:w:v" GETOPT_HELP_VERSION_STRING,
132 longopts, NULL)) != -1)
133 {
134 switch (c)
135 {
136 default:
137 usage (stderr);
138 case 'r':
139 refclock = strtol_or_err (optarg,
140 "the option '-s' requires an integer");
141 break;
142 case 'c':
143 critical = optarg;
144 break;
145 case 'w':
146 warning = optarg;
147 break;
148 case 'v':
149 verbose = true;
150 break;
151
152 case_GETOPT_HELP_CHAR
153 case_GETOPT_VERSION_CHAR
154
155 }
156 }
157
158 if (refclock == ~0UL)
159 usage (stderr);
160
161 status = set_thresholds (&my_threshold, warning, critical);
162 if (status == NP_RANGE_UNPARSEABLE)
163 usage (stderr);
164
165 timedelta = get_timedelta (refclock, verbose);
166
167 status = get_status (labs (timedelta), my_threshold);
168 free (my_threshold);
169
170 printf ("%s %s - time delta %lds | clock_delta=%ld\n",
171 program_name_short, state_text (status), timedelta, timedelta);
172
173 return status;
174 }
175 #endif /* NPL_TESTING */