Merge pull request #2021 from monitoring-plugins/check_disk_static_fixes
[monitoring-plugins.git] / plugins / utils.h
blobf939e337fa1927b3d6327f1e59f6d33055b5e236
1 #ifndef NP_UTILS_H
2 #define NP_UTILS_H
3 /* Header file for Monitoring Plugins utils.c */
5 /* This file should be included in all plugins */
7 /* The purpose of this package is to provide safer alternatives to C
8 functions that might otherwise be vulnerable to hacking. This
9 currently includes a standard suite of validation routines to be sure
10 that an string argument actually converts to its intended type and a
11 suite of string handling routine that do their own memory management
12 in order to resist overflow attacks. In addition, a few functions are
13 provided to standardize version and error reporting across the entire
14 suite of plugins. */
16 /* now some functions etc are being defined in ../lib/utils_base.c */
17 #include "utils_base.h"
19 #include <stdbool.h>
22 #ifdef NP_EXTRA_OPTS
23 /* Include extra-opts functions if compiled in */
24 #include "extra_opts.h"
25 #else
26 /* else, fake np_extra_opts */
27 #define np_extra_opts(acptr,av,pr) av
28 #endif
30 /* Standardize version information, termination */
32 void support (void);
33 void print_revision (const char *, const char *);
35 extern time_t start_time, end_time;
37 /* Test input types */
39 bool is_integer (char *);
40 bool is_intpos (char *);
41 bool is_intneg (char *);
42 bool is_intnonneg (char *);
43 bool is_intpercent (char *);
44 bool is_uint64(char *number, uint64_t *target);
45 bool is_int64(char *number, int64_t *target);
47 bool is_numeric (char *);
48 bool is_positive (char *);
49 bool is_negative (char *);
50 bool is_nonnegative (char *);
51 bool is_percentage (char *);
52 bool is_percentage_expression (const char[]);
54 bool is_option (char *);
56 /* Generalized timer that will do milliseconds if available */
57 #ifndef HAVE_STRUCT_TIMEVAL
58 struct timeval {
59 long tv_sec; /* seconds */
60 long tv_usec; /* microseconds */
62 #endif
64 #ifndef HAVE_GETTIMEOFDAY
65 int gettimeofday(struct timeval *, struct timezone *);
66 #endif
68 double delta_time (struct timeval tv);
69 long deltime (struct timeval tv);
71 /* Handle strings safely */
73 void strip (char *);
74 char *strscpy (char *, const char *);
75 char *strnl (char *);
76 char *strpcpy (char *, const char *, const char *);
77 char *strpcat (char *, const char *, const char *);
78 int xvasprintf (char **strp, const char *fmt, va_list ap);
79 int xasprintf (char **strp, const char *fmt, ...);
81 int max_state (int a, int b);
82 int max_state_alt (int a, int b);
84 void usage (const char *) __attribute__((noreturn));
85 void usage2(const char *, const char *) __attribute__((noreturn));
86 void usage3(const char *, int) __attribute__((noreturn));
87 void usage4(const char *) __attribute__((noreturn));
88 void usage5(void) __attribute__((noreturn));
89 void usage_va(const char *fmt, ...) __attribute__((noreturn));
91 #define max(a,b) (((a)>(b))?(a):(b))
92 #define min(a,b) (((a)<(b))?(a):(b))
94 char *perfdata (const char *, long int, const char *, int, long int,
95 int, long int, int, long int, int, long int);
97 char *perfdata_uint64 (const char *, uint64_t , const char *, int, uint64_t,
98 int, uint64_t, int, uint64_t, int, uint64_t);
100 char *perfdata_int64 (const char *, int64_t, const char *, int, int64_t,
101 int, int64_t, int, int64_t, int, int64_t);
103 char *fperfdata (const char *, double, const char *, int, double,
104 int, double, int, double, int, double);
106 char *sperfdata (const char *, double, const char *, char *, char *,
107 int, double, int, double);
109 char *sperfdata_int (const char *, int, const char *, char *, char *,
110 int, int, int, int);
112 /* The idea here is that, although not every plugin will use all of these,
113 most will or should. Therefore, for consistency, these very common
114 options should have only these meanings throughout the overall suite */
116 #define STD_LONG_OPTS \
117 {"version",no_argument,0,'V'},\
118 {"verbose",no_argument,0,'v'},\
119 {"help",no_argument,0,'h'},\
120 {"timeout",required_argument,0,'t'},\
121 {"critical",required_argument,0,'c'},\
122 {"warning",required_argument,0,'w'},\
123 {"hostname",required_argument,0,'H'}
125 #define COPYRIGHT "Copyright (c) %s Monitoring Plugins Development Team\n\
126 \t<%s>\n\n"
128 #define UT_HLP_VRS _("\
129 %s (-h | --help) for detailed help\n\
130 %s (-V | --version) for version information\n")
132 #define UT_HELP_VRSN _("\
133 \nOptions:\n\
134 -h, --help\n\
135 Print detailed help screen\n\
136 -V, --version\n\
137 Print version information\n")
139 #define UT_HOST_PORT _("\
140 -H, --hostname=ADDRESS\n\
141 Host name, IP Address, or unix socket (must be an absolute path)\n\
142 -%c, --port=INTEGER\n\
143 Port number (default: %s)\n")
145 #define UT_IPv46 _("\
146 -4, --use-ipv4\n\
147 Use IPv4 connection\n\
148 -6, --use-ipv6\n\
149 Use IPv6 connection\n")
151 #define UT_VERBOSE _("\
152 -v, --verbose\n\
153 Show details for command-line debugging (output may be truncated by\n\
154 the monitoring system)\n")
156 #define UT_WARN_CRIT _("\
157 -w, --warning=DOUBLE\n\
158 Response time to result in warning status (seconds)\n\
159 -c, --critical=DOUBLE\n\
160 Response time to result in critical status (seconds)\n")
162 #define UT_WARN_CRIT_RANGE _("\
163 -w, --warning=RANGE\n\
164 Warning range (format: start:end). Alert if outside this range\n\
165 -c, --critical=RANGE\n\
166 Critical range\n")
168 #define UT_CONN_TIMEOUT _("\
169 -t, --timeout=INTEGER\n\
170 Seconds before connection times out (default: %d)\n")
172 #define UT_PLUG_TIMEOUT _("\
173 -t, --timeout=INTEGER\n\
174 Seconds before plugin times out (default: %d)\n")
176 #ifdef NP_EXTRA_OPTS
177 #define UT_EXTRA_OPTS _("\
178 --extra-opts=[section][@file]\n\
179 Read options from an ini file. See\n\
180 https://www.monitoring-plugins.org/doc/extra-opts.html\n\
181 for usage and examples.\n")
182 #else
183 #define UT_EXTRA_OPTS " \b"
184 #endif
186 #define UT_THRESHOLDS_NOTES _("\
187 See:\n\
188 https://www.monitoring-plugins.org/doc/guidelines.html#THRESHOLDFORMAT\n\
189 for THRESHOLD format and examples.\n")
191 #define UT_SUPPORT _("\n\
192 Send email to help@monitoring-plugins.org if you have questions regarding\n\
193 use of this software. To submit patches or suggest improvements, send email\n\
194 to devel@monitoring-plugins.org\n\n")
196 #define UT_NOWARRANTY _("\n\
197 The Monitoring Plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n\
198 copies of the plugins under the terms of the GNU General Public License.\n\
199 For more information about these matters, see the file named COPYING.\n")
201 #endif /* NP_UTILS_H */