1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
|
<?php
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
class CMediatypeHelper {
/**
* Message types.
*/
public const MSG_TYPE_PROBLEM = 0;
public const MSG_TYPE_RECOVERY = 1;
public const MSG_TYPE_UPDATE = 2;
public const MSG_TYPE_SERVICE = 3;
public const MSG_TYPE_SERVICE_RECOVERY = 4;
public const MSG_TYPE_SERVICE_UPDATE = 5;
public const MSG_TYPE_DISCOVERY = 6;
public const MSG_TYPE_AUTOREG = 7;
public const MSG_TYPE_INTERNAL = 8;
public const MSG_TYPE_INTERNAL_RECOVERY = 9;
/**
* Email type providers.
*/
public const EMAIL_PROVIDER_SMTP = 0;
public const EMAIL_PROVIDER_GMAIL = 1;
public const EMAIL_PROVIDER_GMAIL_RELAY = 2;
public const EMAIL_PROVIDER_OFFICE365 = 3;
public const EMAIL_PROVIDER_OFFICE365_RELAY = 4;
/**
* Returns an array of Email providers default settings.
*
* @return array
*/
public static function getEmailProviders($provider = null) {
$providers = [
self::EMAIL_PROVIDER_SMTP => [
'name' => 'Generic SMTP',
'smtp_server' => 'mail.example.com',
'smtp_email' => 'zabbix@example.com',
'smtp_port' => 25,
'smtp_security' => SMTP_SECURITY_NONE,
'smtp_authentication' => SMTP_AUTHENTICATION_NONE,
'smtp_verify_host' => ZBX_HTTP_VERIFY_HOST_OFF,
'smtp_verify_peer' => ZBX_HTTP_VERIFY_PEER_OFF,
'message_format' => ZBX_MEDIA_MESSAGE_FORMAT_HTML
],
self::EMAIL_PROVIDER_GMAIL => [
'name' => 'Gmail',
'smtp_server' => 'smtp.gmail.com',
'smtp_email' => 'zabbix@example.com',
'smtp_port' => 587,
'smtp_security' => SMTP_SECURITY_STARTTLS,
'smtp_authentication' => SMTP_AUTHENTICATION_NORMAL,
'smtp_verify_host' => ZBX_HTTP_VERIFY_HOST_OFF,
'smtp_verify_peer' => ZBX_HTTP_VERIFY_PEER_OFF,
'message_format' => ZBX_MEDIA_MESSAGE_FORMAT_HTML
],
self::EMAIL_PROVIDER_GMAIL_RELAY => [
'name' => 'Gmail relay',
'smtp_server' => 'smtp-relay.gmail.com',
'smtp_email' => 'zabbix@example.com',
'smtp_port' => 587,
'smtp_security' => SMTP_SECURITY_STARTTLS,
'smtp_authentication' => SMTP_AUTHENTICATION_NONE,
'smtp_verify_host' => ZBX_HTTP_VERIFY_HOST_OFF,
'smtp_verify_peer' => ZBX_HTTP_VERIFY_PEER_OFF,
'message_format' => ZBX_MEDIA_MESSAGE_FORMAT_HTML
],
self::EMAIL_PROVIDER_OFFICE365 => [
'name' => 'Office365',
'smtp_server' => 'smtp.office365.com',
'smtp_email' => 'zabbix@example.com',
'smtp_port' => 587,
'smtp_security' => SMTP_SECURITY_STARTTLS,
'smtp_authentication' => SMTP_AUTHENTICATION_NORMAL,
'smtp_verify_host' => ZBX_HTTP_VERIFY_HOST_OFF,
'smtp_verify_peer' => ZBX_HTTP_VERIFY_PEER_OFF,
'message_format' => ZBX_MEDIA_MESSAGE_FORMAT_HTML
],
self::EMAIL_PROVIDER_OFFICE365_RELAY => [
'name' => 'Office365 relay',
'smtp_server' => '.mail.protection.outlook.com',
'smtp_email' => 'zabbix@example.com',
'smtp_port' => 25,
'smtp_security' => SMTP_SECURITY_STARTTLS,
'smtp_authentication' => SMTP_AUTHENTICATION_NONE,
'smtp_verify_host' => ZBX_HTTP_VERIFY_HOST_OFF,
'smtp_verify_peer' => ZBX_HTTP_VERIFY_PEER_OFF,
'message_format' => ZBX_MEDIA_MESSAGE_FORMAT_HTML
]
];
if ($provider === null) {
return $providers;
}
return $providers[$provider];
}
/**
* Returns all providers names.
*
* @return array
*/
public static function getAllEmailProvidersNames() {
return array_column(self::getEmailProviders(), 'name');
}
/**
* Returns media types names.
*
* @return array
*/
public static function getMediaTypes($type = null) {
$types = [
MEDIA_TYPE_EMAIL => _('Email'),
MEDIA_TYPE_EXEC => _('Script'),
MEDIA_TYPE_SMS => _('SMS'),
MEDIA_TYPE_WEBHOOK => _('Webhook')
];
if ($type === null) {
natsort($types);
return $types;
}
return $types[$type];
}
/**
* Returns an array of message templates.
*
* @return array
*/
protected static function messageTemplates() {
return [
self::MSG_TYPE_PROBLEM => [
'eventsource' => EVENT_SOURCE_TRIGGERS,
'recovery' => ACTION_OPERATION,
'name' => _('Problem'),
'template' => [
'subject' => 'Problem: {EVENT.NAME}',
'html' => '<b>Problem started</b> at {{EVENT.TIME}.htmlencode()} on {{EVENT.DATE}.htmlencode()}<br>'.
'<b>Problem name:</b> {{EVENT.NAME}.htmlencode()}<br><b>Host:</b> {{HOST.NAME}.htmlencode()}<br>'.
'<b>Severity:</b> {{EVENT.SEVERITY}.htmlencode()}<br><b>Operational data:</b> {{EVENT.OPDATA}.htmlencode()}<br>'.
'<b>Original problem ID:</b> {{EVENT.ID}.htmlencode()}<br>{{TRIGGER.URL}.htmlencode()}',
'sms' => "{EVENT.SEVERITY}: {EVENT.NAME}\nHost: {HOST.NAME}\n{EVENT.DATE} {EVENT.TIME}",
'text' => "Problem started at {EVENT.TIME} on {EVENT.DATE}\n".
"Problem name: {EVENT.NAME}\nHost: {HOST.NAME}\nSeverity: {EVENT.SEVERITY}\n".
"Operational data: {EVENT.OPDATA}\nOriginal problem ID: {EVENT.ID}\n{TRIGGER.URL}"
]
],
self::MSG_TYPE_RECOVERY => [
'eventsource' => EVENT_SOURCE_TRIGGERS,
'recovery' => ACTION_RECOVERY_OPERATION,
'name' => _('Problem recovery'),
'template' => [
'subject' => 'Resolved in {EVENT.DURATION}: {EVENT.NAME}',
'html' => '<b>Problem has been resolved</b> at {{EVENT.RECOVERY.TIME}.htmlencode()} on {{EVENT.RECOVERY.DATE}.htmlencode()}<br>'.
'<b>Problem name:</b> {{EVENT.NAME}.htmlencode()}<br><b>Problem duration:</b> {{EVENT.DURATION}.htmlencode()}<br><b>Host:</b> {{HOST.NAME}.htmlencode()}<br>'.
'<b>Severity:</b> {{EVENT.SEVERITY}.htmlencode()}<br><b>Original problem ID:</b> {{EVENT.ID}.htmlencode()}<br>{{TRIGGER.URL}.htmlencode()}',
'sms' => "Resolved in {EVENT.DURATION}: {EVENT.NAME}\nHost: {HOST.NAME}\n{EVENT.DATE} {EVENT.TIME}",
'text' => "Problem has been resolved at {EVENT.RECOVERY.TIME} on {EVENT.RECOVERY.DATE}\n".
"Problem name: {EVENT.NAME}\nProblem duration: {EVENT.DURATION}\nHost: {HOST.NAME}\nSeverity: {EVENT.SEVERITY}\n".
"Original problem ID: {EVENT.ID}\n{TRIGGER.URL}"
]
],
self::MSG_TYPE_UPDATE => [
'eventsource' => EVENT_SOURCE_TRIGGERS,
'recovery' => ACTION_UPDATE_OPERATION,
'name' => _('Problem update'),
'template' => [
'subject' => 'Updated problem in {EVENT.AGE}: {EVENT.NAME}',
'html' =>
'<b>{{USER.FULLNAME}.htmlencode()} {{EVENT.UPDATE.ACTION}.htmlencode()} problem</b> at {{EVENT.UPDATE.DATE}.htmlencode()} {{EVENT.UPDATE.TIME}.htmlencode()}.<br>'.
'{{EVENT.UPDATE.MESSAGE}.htmlencode()}<br><br><b>Current problem status:</b> {{EVENT.STATUS}.htmlencode()}<br>'.
'<b>Age:</b> {{EVENT.AGE}.htmlencode()}<br><b>Acknowledged:</b> {{EVENT.ACK.STATUS}.htmlencode()}.',
'sms' => '{USER.FULLNAME} {EVENT.UPDATE.ACTION} problem in {EVENT.AGE} at {EVENT.UPDATE.DATE} {EVENT.UPDATE.TIME}',
'text' =>
"{USER.FULLNAME} {EVENT.UPDATE.ACTION} problem at {EVENT.UPDATE.DATE} {EVENT.UPDATE.TIME}.\n".
"{EVENT.UPDATE.MESSAGE}\n\n".
"Current problem status is {EVENT.STATUS}, age is {EVENT.AGE}, acknowledged: {EVENT.ACK.STATUS}."
]
],
self::MSG_TYPE_SERVICE => [
'eventsource' => EVENT_SOURCE_SERVICE,
'recovery' => ACTION_OPERATION,
'name' => _('Service'),
'template' => [
'subject' => 'Service "{SERVICE.NAME}" problem: {EVENT.NAME}',
'html' =>
'<b>Service problem started</b> at {{EVENT.TIME}.htmlencode()} on {{EVENT.DATE}.htmlencode()}<br>'.
'<b>Service problem name:</b> {{EVENT.NAME}.htmlencode()}<br>'.
'<b>Service:</b> {{SERVICE.NAME}.htmlencode()}<br>'.
'<b>Severity:</b> {{EVENT.SEVERITY}.htmlencode()}<br>'.
'<b>Original problem ID:</b> {{EVENT.ID}.htmlencode()}<br>'.
'<b>Service description:</b> {{SERVICE.DESCRIPTION}.htmlencode()}<br><br>'.
'{{SERVICE.ROOTCAUSE}.htmlencode()}',
'sms' => "{EVENT.NAME}\n{EVENT.DATE} {EVENT.TIME}",
'text' =>
"Service problem started at {EVENT.TIME} on {EVENT.DATE}\n".
"Service problem name: {EVENT.NAME}\n".
"Service: {SERVICE.NAME}\n".
"Severity: {EVENT.SEVERITY}\n".
"Original problem ID: {EVENT.ID}\n".
"Service description: {SERVICE.DESCRIPTION}\n\n".
"{SERVICE.ROOTCAUSE}"
]
],
self::MSG_TYPE_SERVICE_RECOVERY => [
'eventsource' => EVENT_SOURCE_SERVICE,
'recovery' => ACTION_RECOVERY_OPERATION,
'name' => _('Service recovery'),
'template' => [
'subject' => 'Service "{SERVICE.NAME}" resolved in {EVENT.DURATION}: {EVENT.NAME}',
'html' =>
'<b>Service "{{SERVICE.NAME}.htmlencode()}" has been resolved</b> at {{EVENT.RECOVERY.TIME}.htmlencode()} on {{EVENT.RECOVERY.DATE}.htmlencode()}<br>'.
'<b>Problem name:</b> {{EVENT.NAME}.htmlencode()}<br>'.
'<b>Problem duration:</b> {{EVENT.DURATION}.htmlencode()}<br>'.
'<b>Severity:</b> {{EVENT.SEVERITY}.htmlencode()}<br>'.
'<b>Original problem ID:</b> {{EVENT.ID}.htmlencode()}<br>'.
'<b>Service description:</b> {{SERVICE.DESCRIPTION}.htmlencode()}',
'sms' => "{EVENT.NAME}\n{EVENT.DATE} {EVENT.TIME}",
'text' =>
"Service \"{SERVICE.NAME}\" has been resolved at {EVENT.RECOVERY.TIME} on {EVENT.RECOVERY.DATE}\n".
"Problem name: {EVENT.NAME}\n".
"Problem duration: {EVENT.DURATION}\n".
"Severity: {EVENT.SEVERITY}\n".
"Original problem ID: {EVENT.ID}\n".
"Service description: {SERVICE.DESCRIPTION}"
]
],
self::MSG_TYPE_SERVICE_UPDATE => [
'eventsource' => EVENT_SOURCE_SERVICE,
'recovery' => ACTION_UPDATE_OPERATION,
'name' => _('Service update'),
'template' => [
'subject' => 'Changed "{SERVICE.NAME}" service status to {EVENT.UPDATE.SEVERITY} in {EVENT.AGE}',
'html' =>
'<b>Changed "{{SERVICE.NAME}.htmlencode()}" service status</b> to {{EVENT.UPDATE.SEVERITY}.htmlencode()} at {{EVENT.UPDATE.DATE}.htmlencode()} {{EVENT.UPDATE.TIME}.htmlencode()}.<br>'.
'<b>Current problem age</b> is {{EVENT.AGE}.htmlencode()}.<br>'.
'<b>Service description:</b> {{SERVICE.DESCRIPTION}.htmlencode()}<br><br>'.
'{{SERVICE.ROOTCAUSE}.htmlencode()}',
'sms' => "{EVENT.NAME}\n{EVENT.DATE} {EVENT.TIME}",
'text' =>
"Changed \"{SERVICE.NAME}\" service status to {EVENT.UPDATE.SEVERITY} at {EVENT.UPDATE.DATE} {EVENT.UPDATE.TIME}.\n".
"Current problem age is {EVENT.AGE}.\n".
"Service description: {SERVICE.DESCRIPTION}\n\n".
"{SERVICE.ROOTCAUSE}"
]
],
self::MSG_TYPE_DISCOVERY => [
'eventsource' => EVENT_SOURCE_DISCOVERY,
'recovery' => ACTION_OPERATION,
'name' => _('Discovery'),
'template' => [
'subject' => 'Discovery: {DISCOVERY.DEVICE.STATUS} {DISCOVERY.DEVICE.IPADDRESS}',
'html' => '<b>Discovery rule:</b> {{DISCOVERY.RULE.NAME}.htmlencode()}<br><br>'.
'<b>Device IP:</b> {{DISCOVERY.DEVICE.IPADDRESS}.htmlencode()}<br>'.
'<b>Device DNS:</b> {{DISCOVERY.DEVICE.DNS}.htmlencode()}<br>'.
'<b>Device status:</b> {{DISCOVERY.DEVICE.STATUS}.htmlencode()}<br>'.
'<b>Device uptime:</b> {{DISCOVERY.DEVICE.UPTIME}.htmlencode()}<br><br>'.
'<b>Device service name:</b> {{DISCOVERY.SERVICE.NAME}.htmlencode()}<br>'.
'<b>Device service port:</b> {{DISCOVERY.SERVICE.PORT}.htmlencode()}<br>'.
'<b>Device service status:</b> {{DISCOVERY.SERVICE.STATUS}.htmlencode()}<br>'.
'<b>Device service uptime:</b> {{DISCOVERY.SERVICE.UPTIME}.htmlencode()}',
'sms' => 'Discovery: {DISCOVERY.DEVICE.STATUS} {DISCOVERY.DEVICE.IPADDRESS}',
'text' => "Discovery rule: {DISCOVERY.RULE.NAME}\n\n".
"Device IP: {DISCOVERY.DEVICE.IPADDRESS}\nDevice DNS: {DISCOVERY.DEVICE.DNS}\n".
"Device status: {DISCOVERY.DEVICE.STATUS}\n".
"Device uptime: {DISCOVERY.DEVICE.UPTIME}\n\n".
"Device service name: {DISCOVERY.SERVICE.NAME}\n".
"Device service port: {DISCOVERY.SERVICE.PORT}\n".
"Device service status: {DISCOVERY.SERVICE.STATUS}\n".
"Device service uptime: {DISCOVERY.SERVICE.UPTIME}"
]
],
self::MSG_TYPE_AUTOREG => [
'eventsource' => EVENT_SOURCE_AUTOREGISTRATION,
'recovery' => ACTION_OPERATION,
'name' => _('Autoregistration'),
'template' => [
'subject' => 'Autoregistration: {HOST.HOST}',
'html' => '<b>Host name:</b> {{HOST.HOST}.htmlencode()}<br><b>Host IP:</b> {{HOST.IP}.htmlencode()}<br><b>Agent port:</b> {{HOST.PORT}.htmlencode()}',
'sms' => "Autoregistration: {HOST.HOST}\nHost IP: {HOST.IP}\nAgent port: {HOST.PORT}",
'text' => "Host name: {HOST.HOST}\nHost IP: {HOST.IP}\nAgent port: {HOST.PORT}"
]
],
self::MSG_TYPE_INTERNAL => [
'eventsource' => EVENT_SOURCE_INTERNAL,
'recovery' => ACTION_OPERATION,
'name' => _('Internal problem'),
'template' => [
'subject' => '',
'html' => '',
'sms' => '',
'text' => ''
]
],
self::MSG_TYPE_INTERNAL_RECOVERY => [
'eventsource' => EVENT_SOURCE_INTERNAL,
'recovery' => ACTION_RECOVERY_OPERATION,
'name' => _('Internal problem recovery'),
'template' => [
'subject' => '',
'html' => '',
'sms' => '',
'text' => ''
]
]
];
}
/**
* Returns all message templates.
*
* @return array
*/
public static function getAllMessageTemplates() {
return self::messageTemplates();
}
/**
* Returns all message types.
*
* @return array
*/
public static function getAllMessageTypes() {
return array_keys(self::messageTemplates());
}
/**
* Gets an array of event source and operation mode from the specified message type.
*
* @param int $message_type Message type.
*
* @return array|bool
*/
public static function transformFromMessageType($message_type) {
$message_templates = self::messageTemplates();
return array_key_exists($message_type, $message_templates) ? $message_templates[$message_type] : false;
}
/**
* Gets message type form the specified event source and operation mode.
*
* @param int $eventsource Event source.
* @param int $recovery Operation mode.
*
* @return int|bool
*/
public static function transformToMessageType($eventsource, $recovery) {
foreach (self::messageTemplates() as $message_type => $message_template) {
if ($eventsource == $message_template['eventsource'] && $recovery == $message_template['recovery']) {
return $message_type;
}
}
return false;
}
/**
* Returns a message template array with message subject and body.
*
* @param int $media_type Media type.
* @param int $message_type Message type.
* @param int $message_format Message format. Used by Email media type.
*
* @return array
*/
public static function getMessageTemplate($media_type, $message_type, $message_format = null) {
$message_templates = self::messageTemplates();
if ($media_type == MEDIA_TYPE_SMS) {
return [
'subject' => '',
'message' => $message_templates[$message_type]['template']['sms']
];
}
if ($media_type == MEDIA_TYPE_EMAIL && $message_format == ZBX_MEDIA_MESSAGE_FORMAT_HTML) {
return [
'subject' => $message_templates[$message_type]['template']['subject'],
'message' => $message_templates[$message_type]['template']['html']
];
}
return [
'subject' => $message_templates[$message_type]['template']['subject'],
'message' => $message_templates[$message_type]['template']['text']
];
}
}
|