4bbe33d0fa351a1b9fba23f41bd2ac42a3f3287a
1 #ifndef NP_UTILS_H
2 #define NP_UTILS_H
3 /* Header file for nagios 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 acually 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 /* Standardize version information, termination */
18 /* $Id$ */
20 void support (void);
21 char *clean_revstring (const char *);
22 void print_revision (const char *, const char *);
23 void die (int, const char *, ...) __attribute__((noreturn,format(printf, 2, 3)));
25 /* Handle timeouts */
27 #ifdef LOCAL_TIMEOUT_ALARM_HANDLER
28 extern unsigned int timeout_interval;
29 RETSIGTYPE timeout_alarm_handler (int);
30 #else
31 unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT;
32 extern RETSIGTYPE timeout_alarm_handler (int);
33 #endif
35 time_t start_time, end_time;
37 /* Test input types */
39 int is_integer (char *);
40 int is_intpos (char *);
41 int is_intneg (char *);
42 int is_intnonneg (char *);
43 int is_intpercent (char *);
45 int is_numeric (char *);
46 int is_positive (char *);
47 int is_negative (char *);
48 int is_nonnegative (char *);
49 int is_percentage (char *);
51 int is_option (char *);
53 /* Generalized timer that will do milliseconds if available */
54 #ifndef HAVE_STRUCT_TIMEVAL
55 struct timeval {
56 long tv_sec; /* seconds */
57 long tv_usec; /* microseconds */
58 };
59 #endif
61 #define OUTSIDE 0
62 #define INSIDE 1
64 typedef struct range_struct {
65 double start;
66 int start_infinity; /* FALSE (default) or TRUE */
67 double end;
68 int end_infinity;
69 int alert_on; /* OUTSIDE (default) or INSIDE */
70 } range;
72 typedef struct thresholds_struct {
73 range *warning;
74 range *critical;
75 } thresholds;
77 range *parse_range_string (char *);
78 int _set_thresholds(thresholds **, char *, char *);
79 void set_thresholds(thresholds **, char *, char *);
80 int check_range(double, range *);
81 int get_status(double, thresholds *);
83 #ifndef HAVE_GETTIMEOFDAY
84 int gettimeofday(struct timeval *, struct timezone *);
85 #endif
87 double delta_time (struct timeval tv);
88 long deltime (struct timeval tv);
90 /* Handle strings safely */
92 void strip (char *);
93 char *strscpy (char *, const char *);
94 char *strnl (char *);
95 char *strpcpy (char *, const char *, const char *);
96 char *strpcat (char *, const char *, const char *);
98 int max_state (int a, int b);
100 void usage (const char *) __attribute__((noreturn));
101 void usage2(const char *, const char *) __attribute__((noreturn));
102 void usage3(const char *, int) __attribute__((noreturn));
103 void usage4(const char *);
104 void usage_va(const char *fmt, ...);
106 const char *state_text (int);
108 #define max(a,b) (((a)>(b))?(a):(b))
109 #define min(a,b) (((a)<(b))?(a):(b))
111 char *perfdata (const char *,
112 long int,
113 const char *,
114 int,
115 long int,
116 int,
117 long int,
118 int,
119 long int,
120 int,
121 long int);
123 char *fperfdata (const char *,
124 double,
125 const char *,
126 int,
127 double,
128 int,
129 double,
130 int,
131 double,
132 int,
133 double);
135 char *np_escaped_string (const char *);
137 /* The idea here is that, although not every plugin will use all of these,
138 most will or should. Therefore, for consistency, these very common
139 options should have only these meanings throughout the overall suite */
141 #define STD_LONG_OPTS \
142 {"version",no_argument,0,'V'},\
143 {"verbose",no_argument,0,'v'},\
144 {"help",no_argument,0,'h'},\
145 {"timeout",required_argument,0,'t'},\
146 {"critical",required_argument,0,'c'},\
147 {"warning",required_argument,0,'w'},\
148 {"hostname",required_argument,0,'H'}
150 #define COPYRIGHT "Copyright (c) %s Nagios Plugin Development Team\n\
151 \t<%s>\n\n"
153 #define UT_HLP_VRS "\
154 %s (-h | --help) for detailed help\n\
155 %s (-V | --version) for version information\n"
157 #define UT_HELP_VRSN "\
158 \nOptions:\n\
159 -h, --help\n\
160 Print detailed help screen\n\
161 -V, --version\n\
162 Print version information\n"
164 #define UT_HOST_PORT "\
165 -H, --hostname=ADDRESS\n\
166 Host name, IP Address, or unix socket (must be an absolute path)\n\
167 -%c, --port=INTEGER\n\
168 Port number (default: %s)\n"
170 #define UT_IPv46 "\
171 -4, --use-ipv4\n\
172 Use IPv4 connection\n\
173 -6, --use-ipv6\n\
174 Use IPv6 connection\n"
176 #define UT_VERBOSE "\
177 -v, --verbose\n\
178 Show details for command-line debugging (Nagios may truncate output)\n"
180 #define UT_WARN_CRIT "\
181 -w, --warning=DOUBLE\n\
182 Response time to result in warning status (seconds)\n\
183 -c, --critical=DOUBLE\n\
184 Response time to result in critical status (seconds)\n"
186 #define UT_WARN_CRIT_RANGE "\
187 -w, --warning=RANGE\n\
188 Warning range (format: start:end). Alert if outside this range\n\
189 -c, --critical=RANGE\n\
190 Critical range\n"
192 #define UT_TIMEOUT "\
193 -t, --timeout=INTEGER\n\
194 Seconds before connection times out (default: %d)\n"
196 #define UT_SUPPORT "\n\
197 Send email to nagios-users@lists.sourceforge.net if you have questions\n\
198 regarding use of this software. To submit patches or suggest improvements,\n\
199 send email to nagiosplug-devel@lists.sourceforge.net\n"
201 #define UT_NOWARRANTY "\n\
202 The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n\
203 copies of the plugins under the terms of the GNU General Public License.\n\
204 For more information about these matters, see the file named COPYING.\n"
206 #endif /* NP_UTILS_H */