1 /************************************************************************ 2 * 3 * MACROS.H - Common macro functions 4 * Written By: Ethan Galstad (egalstad@nagios.org) 5 * 6 * License: 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 version 2 as 10 * published by the Free Software Foundation. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 ************************************************************************/ 21 22 #ifndef NAGIOS_MACROS_H_INCLUDED 23 #define NAGIOS_MACROS_H_INCLUDED 24 25 #include "common.h" 26 #include "objects.h" 27 28 29 30 /****************** LENGTH LIMITATIONS ****************/ 31 32 #define MAX_COMMAND_ARGUMENTS 32 /* maximum number of $ARGx$ macros */ 33 34 35 /****************** MACRO DEFINITIONS *****************/ 36 37 #define MACRO_ENV_VAR_PREFIX "NAGIOS_" 38 39 #define MAX_USER_MACROS 256 /* maximum number of $USERx$ macros */ 40 41 #define MACRO_X_COUNT 163 /* size of macro_x[] array */ 42 43 NAGIOS_BEGIN_DECL 44 45 struct nagios_macros { 46 char *x[MACRO_X_COUNT]; 47 char *argv[MAX_COMMAND_ARGUMENTS]; 48 char *contactaddress[MAX_CONTACT_ADDRESSES]; 49 char *ondemand; 50 host *host_ptr; 51 hostgroup *hostgroup_ptr; 52 service *service_ptr; 53 servicegroup *servicegroup_ptr; 54 contact *contact_ptr; 55 contactgroup *contactgroup_ptr; 56 customvariablesmember *custom_host_vars; 57 customvariablesmember *custom_service_vars; 58 customvariablesmember *custom_contact_vars; 59 }; 60 typedef struct nagios_macros nagios_macros; 61 62 63 64 #define MACRO_HOSTNAME 0 65 #define MACRO_HOSTALIAS 1 66 #define MACRO_HOSTADDRESS 2 67 #define MACRO_SERVICEDESC 3 68 #define MACRO_SERVICESTATE 4 69 #define MACRO_SERVICESTATEID 5 70 #define MACRO_SERVICEATTEMPT 6 71 #define MACRO_LONGDATETIME 7 72 #define MACRO_SHORTDATETIME 8 73 #define MACRO_DATE 9 74 #define MACRO_TIME 10 75 #define MACRO_TIMET 11 76 #define MACRO_LASTHOSTCHECK 12 77 #define MACRO_LASTSERVICECHECK 13 78 #define MACRO_LASTHOSTSTATECHANGE 14 79 #define MACRO_LASTSERVICESTATECHANGE 15 80 #define MACRO_HOSTOUTPUT 16 81 #define MACRO_SERVICEOUTPUT 17 82 #define MACRO_HOSTPERFDATA 18 83 #define MACRO_SERVICEPERFDATA 19 84 #define MACRO_CONTACTNAME 20 85 #define MACRO_CONTACTALIAS 21 86 #define MACRO_CONTACTEMAIL 22 87 #define MACRO_CONTACTPAGER 23 88 #define MACRO_ADMINEMAIL 24 89 #define MACRO_ADMINPAGER 25 90 #define MACRO_HOSTSTATE 26 91 #define MACRO_HOSTSTATEID 27 92 #define MACRO_HOSTATTEMPT 28 93 #define MACRO_NOTIFICATIONTYPE 29 94 #define MACRO_NOTIFICATIONNUMBER 30 /* deprecated - see HOSTNOTIFICATIONNUMBER and SERVICENOTIFICATIONNUMBER macros */ 95 #define MACRO_HOSTEXECUTIONTIME 31 96 #define MACRO_SERVICEEXECUTIONTIME 32 97 #define MACRO_HOSTLATENCY 33 98 #define MACRO_SERVICELATENCY 34 99 #define MACRO_HOSTDURATION 35 100 #define MACRO_SERVICEDURATION 36 101 #define MACRO_HOSTDURATIONSEC 37 102 #define MACRO_SERVICEDURATIONSEC 38 103 #define MACRO_HOSTDOWNTIME 39 104 #define MACRO_SERVICEDOWNTIME 40 105 #define MACRO_HOSTSTATETYPE 41 106 #define MACRO_SERVICESTATETYPE 42 107 #define MACRO_HOSTPERCENTCHANGE 43 108 #define MACRO_SERVICEPERCENTCHANGE 44 109 #define MACRO_HOSTGROUPNAME 45 110 #define MACRO_HOSTGROUPALIAS 46 111 #define MACRO_SERVICEGROUPNAME 47 112 #define MACRO_SERVICEGROUPALIAS 48 113 #define MACRO_HOSTACKAUTHOR 49 114 #define MACRO_HOSTACKCOMMENT 50 115 #define MACRO_SERVICEACKAUTHOR 51 116 #define MACRO_SERVICEACKCOMMENT 52 117 #define MACRO_LASTSERVICEOK 53 118 #define MACRO_LASTSERVICEWARNING 54 119 #define MACRO_LASTSERVICEUNKNOWN 55 120 #define MACRO_LASTSERVICECRITICAL 56 121 #define MACRO_LASTHOSTUP 57 122 #define MACRO_LASTHOSTDOWN 58 123 #define MACRO_LASTHOSTUNREACHABLE 59 124 #define MACRO_SERVICECHECKCOMMAND 60 125 #define MACRO_HOSTCHECKCOMMAND 61 126 #define MACRO_MAINCONFIGFILE 62 127 #define MACRO_STATUSDATAFILE 63 128 #define MACRO_HOSTDISPLAYNAME 64 129 #define MACRO_SERVICEDISPLAYNAME 65 130 #define MACRO_RETENTIONDATAFILE 66 131 #define MACRO_OBJECTCACHEFILE 67 132 #define MACRO_TEMPFILE 68 133 #define MACRO_LOGFILE 69 134 #define MACRO_RESOURCEFILE 70 135 #define MACRO_COMMANDFILE 71 136 #define MACRO_HOSTPERFDATAFILE 72 137 #define MACRO_SERVICEPERFDATAFILE 73 138 #define MACRO_HOSTACTIONURL 74 139 #define MACRO_HOSTNOTESURL 75 140 #define MACRO_HOSTNOTES 76 141 #define MACRO_SERVICEACTIONURL 77 142 #define MACRO_SERVICENOTESURL 78 143 #define MACRO_SERVICENOTES 79 144 #define MACRO_TOTALHOSTSUP 80 145 #define MACRO_TOTALHOSTSDOWN 81 146 #define MACRO_TOTALHOSTSUNREACHABLE 82 147 #define MACRO_TOTALHOSTSDOWNUNHANDLED 83 148 #define MACRO_TOTALHOSTSUNREACHABLEUNHANDLED 84 149 #define MACRO_TOTALHOSTPROBLEMS 85 150 #define MACRO_TOTALHOSTPROBLEMSUNHANDLED 86 151 #define MACRO_TOTALSERVICESOK 87 152 #define MACRO_TOTALSERVICESWARNING 88 153 #define MACRO_TOTALSERVICESCRITICAL 89 154 #define MACRO_TOTALSERVICESUNKNOWN 90 155 #define MACRO_TOTALSERVICESWARNINGUNHANDLED 91 156 #define MACRO_TOTALSERVICESCRITICALUNHANDLED 92 157 #define MACRO_TOTALSERVICESUNKNOWNUNHANDLED 93 158 #define MACRO_TOTALSERVICEPROBLEMS 94 159 #define MACRO_TOTALSERVICEPROBLEMSUNHANDLED 95 160 #define MACRO_PROCESSSTARTTIME 96 161 #define MACRO_HOSTCHECKTYPE 97 162 #define MACRO_SERVICECHECKTYPE 98 163 #define MACRO_LONGHOSTOUTPUT 99 164 #define MACRO_LONGSERVICEOUTPUT 100 165 #define MACRO_TEMPPATH 101 166 #define MACRO_HOSTNOTIFICATIONNUMBER 102 167 #define MACRO_SERVICENOTIFICATIONNUMBER 103 168 #define MACRO_HOSTNOTIFICATIONID 104 169 #define MACRO_SERVICENOTIFICATIONID 105 170 #define MACRO_HOSTEVENTID 106 171 #define MACRO_LASTHOSTEVENTID 107 172 #define MACRO_SERVICEEVENTID 108 173 #define MACRO_LASTSERVICEEVENTID 109 174 #define MACRO_HOSTGROUPNAMES 110 175 #define MACRO_SERVICEGROUPNAMES 111 176 #define MACRO_HOSTACKAUTHORNAME 112 177 #define MACRO_HOSTACKAUTHORALIAS 113 178 #define MACRO_SERVICEACKAUTHORNAME 114 179 #define MACRO_SERVICEACKAUTHORALIAS 115 180 #define MACRO_MAXHOSTATTEMPTS 116 181 #define MACRO_MAXSERVICEATTEMPTS 117 182 #define MACRO_SERVICEISVOLATILE 118 183 #define MACRO_TOTALHOSTSERVICES 119 184 #define MACRO_TOTALHOSTSERVICESOK 120 185 #define MACRO_TOTALHOSTSERVICESWARNING 121 186 #define MACRO_TOTALHOSTSERVICESUNKNOWN 122 187 #define MACRO_TOTALHOSTSERVICESCRITICAL 123 188 #define MACRO_HOSTGROUPNOTES 124 189 #define MACRO_HOSTGROUPNOTESURL 125 190 #define MACRO_HOSTGROUPACTIONURL 126 191 #define MACRO_SERVICEGROUPNOTES 127 192 #define MACRO_SERVICEGROUPNOTESURL 128 193 #define MACRO_SERVICEGROUPACTIONURL 129 194 #define MACRO_HOSTGROUPMEMBERS 130 195 #define MACRO_SERVICEGROUPMEMBERS 131 196 #define MACRO_CONTACTGROUPNAME 132 197 #define MACRO_CONTACTGROUPALIAS 133 198 #define MACRO_CONTACTGROUPMEMBERS 134 199 #define MACRO_CONTACTGROUPNAMES 135 200 #define MACRO_NOTIFICATIONRECIPIENTS 136 201 #define MACRO_NOTIFICATIONISESCALATED 137 202 #define MACRO_NOTIFICATIONAUTHOR 138 203 #define MACRO_NOTIFICATIONAUTHORNAME 139 204 #define MACRO_NOTIFICATIONAUTHORALIAS 140 205 #define MACRO_NOTIFICATIONCOMMENT 141 206 #define MACRO_EVENTSTARTTIME 142 207 #define MACRO_HOSTPROBLEMID 143 208 #define MACRO_LASTHOSTPROBLEMID 144 209 #define MACRO_SERVICEPROBLEMID 145 210 #define MACRO_LASTSERVICEPROBLEMID 146 211 #define MACRO_ISVALIDTIME 147 212 #define MACRO_NEXTVALIDTIME 148 213 #define MACRO_LASTHOSTSTATE 149 214 #define MACRO_LASTHOSTSTATEID 150 215 #define MACRO_LASTSERVICESTATE 151 216 #define MACRO_LASTSERVICESTATEID 152 217 #define MACRO_HOSTIMPORTANCE 153 218 #define MACRO_SERVICEIMPORTANCE 154 219 #define MACRO_HOSTANDSERVICESIMPORTANCE 155 220 #define MACRO_HOSTGROUPMEMBERADDRESSES 156 221 #define MACRO_HOSTINFOURL 157 222 #define MACRO_SERVICEINFOURL 158 223 #define MACRO_HOSTNOTIFICATIONENABLED 159 224 #define MACRO_SERVICENOTIFICATIONENABLED 160 225 #define MACRO_HOSTNOTIFICATIONPERIOD 161 226 #define MACRO_SERVICENOTIFICATIONPERIOD 162 227 228 229 /************* MACRO CLEANING OPTIONS *****************/ 230 231 #define STRIP_ILLEGAL_MACRO_CHARS 1 232 #define ESCAPE_MACRO_CHARS 2 233 #define URL_ENCODE_MACRO_CHARS 4 234 235 236 237 /****************** MACRO FUNCTIONS ******************/ 238 239 nagios_macros *get_global_macros(void); 240 241 /* 242 * Replace macros with their actual values 243 * This function modifies the global_macros struct and is thus 244 * not thread-safe. 245 */ 246 int process_macros(char *, char **, int); 247 248 /* thread-safe version of the above */ 249 int process_macros_r(nagios_macros *mac, char *, char **, int); 250 251 /* cleans macros characters before insertion into output string */ 252 char *clean_macro_chars(char *, int); 253 254 /* 255 * These functions updates **macros with the values from 256 * their respective object type. 257 */ 258 259 int grab_service_macros(service *); 260 int grab_host_macros(host *); 261 int grab_servicegroup_macros(servicegroup *); 262 int grab_hostgroup_macros(hostgroup *); 263 int grab_contact_macros(contact *); 264 265 int grab_macro_value(char *, char **, int *, int *); 266 int grab_macrox_value(int, char *, char *, char **, int *); 267 int grab_custom_macro_value(char *, char *, char *, char **); 268 int grab_datetime_macro(int, char *, char *, char **); 269 int grab_standard_host_macro(int, host *, char **, int *); 270 int grab_standard_hostgroup_macro(int, hostgroup *, char **); 271 int grab_standard_service_macro(int, service *, char **, int *); 272 int grab_standard_servicegroup_macro(int, servicegroup *, char **); 273 int grab_standard_contact_macro(int, contact *, char **); 274 int grab_contact_address_macro(int, contact *, char **); 275 int grab_standard_contactgroup_macro(int, contactgroup *, char **); 276 int grab_custom_object_macro(char *, customvariablesmember *, char **); 277 278 /* thread-safe version of the above */ 279 int grab_service_macros_r(nagios_macros *mac, service *); 280 int grab_host_macros_r(nagios_macros *mac, host *); 281 int grab_servicegroup_macros_r(nagios_macros *mac, servicegroup *); 282 int grab_hostgroup_macros_r(nagios_macros *mac, hostgroup *); 283 int grab_contact_macros_r(nagios_macros *mac, contact *); 284 285 int grab_argv_macros_r(nagios_macros *mac, char *check_command); 286 287 int grab_macro_value_r(nagios_macros *mac, char *, char **, int *, int *); 288 int grab_macrox_value_r(nagios_macros *mac, int, char *, char *, char **, int *); 289 int grab_custom_macro_value_r(nagios_macros *mac, char *, char *, char *, char **); 290 int grab_datetime_macro_r(nagios_macros *mac, int, char *, char *, char **); 291 int grab_standard_host_macro_r(nagios_macros *mac, int, host *, char **, int *); 292 int grab_standard_hostgroup_macro_r(nagios_macros *mac, int, hostgroup *, char **); 293 int grab_standard_service_macro_r(nagios_macros *mac, int, service *, char **, int *); 294 int grab_standard_servicegroup_macro_r(nagios_macros *mac, int, servicegroup *, char **); 295 int grab_standard_contact_macro_r(nagios_macros *mac, int, contact *, char **); 296 int grab_custom_object_macro_r(nagios_macros *mac, char *, customvariablesmember *, char **); 297 298 299 char *get_url_encoded_string(char *); /* URL encode a string */ 300 301 int init_macros(void); 302 int init_macrox_names(void); 303 int free_macrox_names(void); 304 305 extern void copy_constant_macros(char **dest); 306 307 /* clear macros */ 308 int clear_argv_macros(void); 309 int clear_volatile_macros(void); 310 int clear_host_macros(void); 311 int clear_service_macros(void); 312 int clear_hostgroup_macros(void); 313 int clear_servicegroup_macros(void); 314 int clear_contact_macros(void); 315 int clear_contactgroup_macros(void); 316 int clear_summary_macros(void); 317 318 /* thread-safe version of the above */ 319 int clear_argv_macros_r(nagios_macros *mac); 320 int clear_volatile_macros_r(nagios_macros *mac); 321 int clear_host_macros_r(nagios_macros *mac); 322 int clear_service_macros_r(nagios_macros *mac); 323 int clear_hostgroup_macros_r(nagios_macros *mac); 324 int clear_servicegroup_macros_r(nagios_macros *mac); 325 int clear_contact_macros_r(nagios_macros *mac); 326 int clear_contactgroup_macros_r(nagios_macros *mac); 327 int clear_summary_macros_r(nagios_macros *mac); 328 int clear_datetime_macros_r(nagios_macros *mac); 329 330 331 #ifndef NSCGI 332 int set_all_macro_environment_vars(int); 333 int set_macrox_environment_vars(int); 334 int set_argv_macro_environment_vars(int); 335 int set_custom_macro_environment_vars(int); 336 int set_contact_address_environment_vars(int); 337 int set_macro_environment_var(char *, char *, int); 338 339 /* thread-safe version of the above */ 340 int set_all_macro_environment_vars_r(nagios_macros *mac, int); 341 int set_macrox_environment_vars_r(nagios_macros *mac, int); 342 int set_argv_macro_environment_vars_r(nagios_macros *mac, int); 343 int set_custom_macro_environment_vars_r(nagios_macros *mac, int); 344 int set_contact_address_environment_vars_r(nagios_macros *mac, int); 345 346 #endif 347 348 NAGIOS_END_DECL 349 #endif 350