Alert Summary
HTML;
}
if ($externalURL) {
$externalURLSafe = htmlspecialchars($externalURL, ENT_QUOTES, 'UTF-8');
$alertSummary .= <<
Alertmanager URL
HTML;
}
$alertSummary .= <<
HTML;
// Build common labels section
$labelsContent = '';
if (!empty($groupLabels) || !empty($commonLabels)) {
$labelsContent .= 'Labels
';
if (!empty($groupLabels)) {
$labelsContent .= '
Group Labels
HTML;
}
$labelsContent .= '
';
}
if (!empty($commonLabels)) {
$labelsContent .= 'Common Labels
HTML;
}
$labelsContent .= '';
}
$labelsContent .= '';
}
// Build common annotations section
$annotationsContent = '';
if (!empty($commonAnnotations)) {
$annotationsContent .= 'Common Annotations
HTML;
}
$annotationsContent .= '
';
}
// Build individual alerts section
$alertsContent = '';
if (!empty($alerts)) {
$alertsContent .= 'Alerts
';
foreach ($alerts as $index => $alert) {
$alertStatus = $alert['status'] ?? 'unknown';
$alertLabels = $alert['labels'] ?? [];
$alertAnnotations = $alert['annotations'] ?? [];
$startsAt = $alert['startsAt'] ?? null;
$endsAt = $alert['endsAt'] ?? null;
$generatorURL = $alert['generatorURL'] ?? null;
$fingerprint = $alert['fingerprint'] ?? null;
$alertStatusSafe = htmlspecialchars($alertStatus, ENT_QUOTES, 'UTF-8');
$alertNumber = $index + 1;
// Choose alert icon
$alertIcon = '🔔';
$alertColor = '#666666';
if ($alertStatus === 'firing') {
$alertIcon = '🔥';
$alertColor = '#dc3545';
} elseif ($alertStatus === 'resolved') {
$alertIcon = '✅';
$alertColor = '#28a745';
}
$alertsContent .= <<
$alertIcon Alert $alertNumber - $alertStatusSafe
HTML;
// Add alert labels
if (!empty($alertLabels)) {
$alertsContent .= '
Labels
HTML;
}
$alertsContent .= '
';
}
// Add alert annotations
if (!empty($alertAnnotations)) {
$alertsContent .= '
Annotations
HTML;
}
$alertsContent .= '
HTML;
}
if ($endsAt) {
$endsAtSafe = htmlspecialchars($endsAt, ENT_QUOTES, 'UTF-8');
$alertsContent .= <<
Ended At
$endsAtSafe
HTML;
}
if ($generatorURL) {
$generatorURLSafe = htmlspecialchars($generatorURL, ENT_QUOTES, 'UTF-8');
$alertsContent .= <<
Generator URL
HTML;
}
if ($fingerprint) {
$fingerprintSafe = htmlspecialchars($fingerprint, ENT_QUOTES, 'UTF-8');
$alertsContent .= <<
Fingerprint
$fingerprintSafe
HTML;
}
$alertsContent .= '';
}
$alertsContent .= '';
}
$emailTitle = "$statusIcon Grafana Alert: $statusSafe";
$emailStamp = "$dateSafe";
$body = <<
📋 Request Information
HTML;
$html = emailShell($emailTitle, $emailStamp, $body, "This Grafana/Prometheus alert was automatically forwarded to your email address.");
// Create subject line
$alertCountText = $alertCount === 1 ? '1 alert' : "$alertCount alerts";
$subject = "$statusIcon Grafana Alert: $statusSafe - $alertCountText";
return [
'html' => $html,
'subject' => $subject
];
}