Debian Codegraph @

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

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2010-2019 Davide Madrisan <[email protected]>
5 *
6 * A Nagios plugin to check how long the system has been running.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <getopt.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #if HAVE_SYS_SYSINFO_H
28 # include <sys/sysinfo.h>
29 #endif
30 #include <time.h>
31 #include <unistd.h>
32
33 #include "common.h"
34 #include "messages.h"
35 #include "progname.h"
36 #include "progversion.h"
37 #include "thresholds.h"
38 #include "xasprintf.h"
39
40 static const char *program_copyright =
41 "Copyright (C) 2010-2019 Davide Madrisan <" PACKAGE_BUGREPORT ">\n";
42
43 #define BUFSIZE 0x80
44 static char buf[BUFSIZE + 1];
45
46 static struct option const longopts[] = {
47 #if HAVE_CLOCK_GETTIME_MONOTONIC
48 {(char *) "clock-monotonic", no_argument, NULL, 'm'},
49 #endif
50 {(char *) "critical", required_argument, NULL, 'c'},
51 {(char *) "warning", required_argument, NULL, 'w'},
52 {(char *) "help", no_argument, NULL, GETOPT_HELP_CHAR},
53 {(char *) "version", no_argument, NULL, GETOPT_VERSION_CHAR},
54 {NULL, 0, NULL, 0}
55 };
56
57 static _Noreturn void
58 usage (FILE * out)
59 {
60 fprintf (out, "%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
61 fputs ("This plugin checks how long the system has been running.\n", out);
62 fputs (program_copyright, out);
63 fputs (USAGE_HEADER, out);
64 fprintf (out, " %s [OPTION]\n", program_name);
65 fputs (USAGE_OPTIONS, out);
66 #if HAVE_CLOCK_GETTIME_MONOTONIC
67 fputs (" -m, --clock-monotonic "
68 "use the monotonic clock for retrieving the time\n", out);
69 #endif
70 fputs (" -w, --warning MINUTES warning threshold\n", out);
71 fputs (" -c, --critical MINUTES critical threshold\n", out);
72 fputs (USAGE_HELP, out);
73 fputs (USAGE_VERSION, out);
74 fputs (USAGE_EXAMPLES, out);
75 fprintf (out, " %s\n", program_name);
76 fprintf (out, " %s --critical 15: --warning 30:\n", program_name);
77 #if HAVE_CLOCK_GETTIME_MONOTONIC
78 fprintf (out, " %s --clock-monotonic -c 15: -w 30:\n", program_name);
79 #endif
80 fputs (USAGE_SEPARATOR, out);
81 fputs (USAGE_THRESHOLDS, out);
82
83 exit (out == stderr ? STATE_UNKNOWN : STATE_OK);
84 }
85
86 static _Noreturn void
87 print_version (void)
88 {
89 printf ("%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
90 fputs (program_copyright, stdout);
91 fputs (GPLv3_DISCLAIMER, stdout);
92
93 exit (STATE_OK);
94 }
95
96 static double
97 uptime_sysinfo ()
98 {
99 struct sysinfo info;
100
101 if (0 != sysinfo (&info))
102 plugin_error (STATE_UNKNOWN, errno, "cannot get the system uptime");
103
104 return info.uptime;
105 }
106
107 #if HAVE_CLOCK_GETTIME_MONOTONIC
108 static double
109 uptime_clock_monotonic ()
110 {
111 struct timespec ts;
112
113 if (0 != clock_gettime (CLOCK_MONOTONIC, &ts))
114 plugin_error (STATE_UNKNOWN, errno, "cannot get the system uptime");
115
116 return ts.tv_sec;
117 }
118 #endif
119
120 static char *
121 sprint_uptime (double uptime_secs)
122 {
123 int upminutes, uphours, updays;
124 int pos = 0;
125
126 updays = (int) uptime_secs / (60 * 60 * 24);
127 if (updays)
128 pos +=
129 snprintf (buf, BUFSIZE, "%d day%s ", updays, (updays != 1) ? "s" : "");
130 upminutes = (int) uptime_secs / 60;
131 uphours = upminutes / 60;
132 uphours = uphours % 24;
133 upminutes = upminutes % 60;
134
135 if (uphours)
136 snprintf (buf + pos, BUFSIZE - pos, "%d hour%s %d min", uphours,
137 (uphours != 1) ? "s" : "", upminutes);
138 else
139 snprintf (buf + pos, BUFSIZE - pos, "%d min", upminutes);
140
141 return buf;
142 }
143
144 #ifndef NPL_TESTING
145 int
146 main (int argc, char **argv)
147 {
148 int c, uptime_mins, status;
149 char *critical = NULL, *warning = NULL;
150 char *status_msg, *perfdata_msg;
151 double uptime_secs;
152 thresholds *my_threshold = NULL;
153 double (*uptime) ();
154
155 set_program_name (argv[0]);
156 uptime = uptime_sysinfo;
157
158 while ((c = getopt_long (argc, argv, "mc:w:" GETOPT_HELP_VERSION_STRING,
159 longopts, NULL)) != -1)
160 {
161 switch (c)
162 {
163 default:
164 usage (stderr);
165 #if HAVE_CLOCK_GETTIME_MONOTONIC
166 case 'm':
167 uptime = uptime_clock_monotonic;
168 break;
169 #endif
170 case 'c':
171 critical = optarg;
172 break;
173 case 'w':
174 warning = optarg;
175 break;
176
177 case_GETOPT_HELP_CHAR
178 case_GETOPT_VERSION_CHAR
179
180 }
181 }
182
183 status = set_thresholds (&my_threshold, warning, critical);
184 if (status == NP_RANGE_UNPARSEABLE)
185 usage (stderr);
186
187 uptime_secs = uptime ();
188
189 uptime_mins = (int) uptime_secs / 60;
190 status = get_status (uptime_mins, my_threshold);
191 free (my_threshold);
192
193 status_msg =
194 xasprintf ("%s %s: %s", program_name_short,
195 state_text (status), sprint_uptime (uptime_secs));
196 perfdata_msg =
197 xasprintf ("uptime=%d;%s;%s;0;", uptime_mins,
198 warning ? warning : "", critical ? critical : "");
199 printf ("%s | %s\n", status_msg, perfdata_msg);
200
201 return status;
202 }
203 #endif /* NPL_TESTING */