Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/lib/messages.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /* This library was based on and inspired by the gnulib code.
20 */
21
22 #include <fcntl.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include "common.h"
30 #include "messages.h"
31
32 /* This variable is incremented each time 'error' is called. */
33 unsigned int error_message_count;
34
35 /* The calling program should define program_name and set it to the
36 name of the executing program. */
37 extern char *program_name;
38
39 /* Return non-zero if FD is open. */
40 static inline int
41 is_open (int fd)
42 {
43 return 0 <= fcntl (fd, F_GETFL);
44 }
45
46 static inline void
47 flush_stdout (void)
48 {
49 int stdout_fd;
50
51 /* POSIX states that fileno (stdout) after fclose is unspecified. But in
52 practice it is not a problem, because stdout is statically allocated and
53 the fd of a FILE stream is stored as a field in its allocated memory. */
54 stdout_fd = fileno (stdout);
55
56 /* POSIX states that fflush (stdout) after fclose is unspecified; it
57 is safe in glibc, but not on all other platforms. fflush (NULL)
58 is always defined, but too draconian. */
59 if (0 <= stdout_fd && is_open (stdout_fd))
60 fflush (stdout);
61 }
62
63 static void
64 print_errno_message (int errnum)
65 {
66 char const *s;
67
68 s = strerror (errnum);
69 fprintf (stdout, " (%s)", s);
70 }
71
72 /* Print the program name and error message MESSAGE, which is a printf-style
73 format string with optional args.
74 If ERRNUM is nonzero, print its corresponding system error message.
75 Exit with status STATUS if it is nonzero. */
76 void
77 plugin_error (nagstatus status, int errnum, const char *message, ...)
78 {
79 va_list args;
80
81 flush_stdout ();
82
83 fprintf (stdout, "%s: ", program_name);
84
85 va_start (args, message);
86 vfprintf (stdout, message, args);
87 va_end (args);
88
89 ++error_message_count;
90 if (errnum)
91 print_errno_message (errnum);
92
93 fputs ("\n", stdout);
94 fflush (stdout);
95
96 exit (status);
97 }
98
99 const char *
100 state_text (nagstatus result)
101 {
102 switch (result)
103 {
104 case STATE_OK:
105 return "OK";
106 case STATE_WARNING:
107 return "WARNING";
108 case STATE_CRITICAL:
109 return "CRITICAL";
110 case STATE_DEPENDENT:
111 return "DEPENDENT";
112 default:
113 return "UNKNOWN";
114 }
115 }