diff --git a/.gitignore b/.gitignore index 9f11b75..1ddcf91 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .idea/ +/vendor/ diff --git a/AlertHandler.php b/AlertHandler.php new file mode 100644 index 0000000..da200cf --- /dev/null +++ b/AlertHandler.php @@ -0,0 +1,61 @@ +setUsername(Settings::$SMTP_USER) + ->setPassword(Settings::$SMTP_PASSWORD); + + if (Settings::$SECURITY == 'ssl') { + $transport->setStreamOptions( + array( + 'ssl' => array( + 'allow_self_signed' => true, + 'verify_peer' => false + ) + ) + ); + } + + $mailer = new Swift_Mailer($transport); + + $message = (new Swift_Message('Killbot error !')) + ->setFrom(Settings::$MAIL_SENDER) + ->setTo(Settings::$MAIL_RECIPIENT) + ->setBody(self::generateBody($e)); + + + if ($mailer->send($message)) { + echo "Alert mail sent\n"; + } else { + echo "Failed to send alert mail\n"; + } + } + protected static function generateBody(Exception $e) + { + $loader = new \Twig_Loader_Filesystem('.'); + $twig = new Twig_Environment($loader); + + $body = $twig->render( + 'mail.html.twig', + array( + 'message' => $e->getMessage(), + 'path' => $e->getFile(), + 'line' => $e->getLine(), + ) + ); + + return $body; + } +} diff --git a/Killbot.php b/Killbot.php index fc3f140..265ddb1 100644 --- a/Killbot.php +++ b/Killbot.php @@ -1,5 +1,8 @@ You must at least fill the `$SLACK_HOOK` variable and edit `$WATCHED_ENTITIES`. -You must at least fill the `$SLACK_HOOK` variable and edit `$WATCHED_ENTITIES`. - -Use a cronjob to run the `cron.php` file every 5 minutes (or another value matching your timeout settings). +* Setup a cronjob to run the `cron.php` file every 5 minutes (or another value matching your timeout settings). ## Disclaimer diff --git a/Settings.php b/Settings.php index 76d2b91..bfb0df0 100644 --- a/Settings.php +++ b/Settings.php @@ -1,5 +1,7 @@ run(); \ No newline at end of file +require_once ('vendor/autoload.php'); + +use Killbot\AlertHandler; +use Killbot\Killbot; +use Killbot\Settings; + +try { + + $killbot = new Killbot(); + $killbot->run(); +} +catch (Exception $e) { + + if (Settings::$SEND_MAIL) { + AlertHandler::sendAlertMail($e); + } +} diff --git a/mail.html.twig b/mail.html.twig new file mode 100644 index 0000000..1732d84 --- /dev/null +++ b/mail.html.twig @@ -0,0 +1,12 @@ +Hello there ! + +Unfortunately, our beloved killbot encountered an error :/ +Here is more details about it : + +Error : {{ message }} +File : {{ path }} +Line : {{ line }} + +It may be a lonely fail, but if you keep getting more of there mail, you should double check your configuration and/or report the issue ! + +Fly safe o7 \ No newline at end of file