Skip to content

Commit

Permalink
Add mailing on errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Slivo-fr committed Nov 9, 2017
1 parent f9c7b11 commit 02d356d
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea/
/vendor/
61 changes: 61 additions & 0 deletions AlertHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Killbot;

use Exception;
use Swift_Mailer;
use Swift_Message;
use Swift_SmtpTransport;
use Twig_Environment;

class AlertHandler
{

public static function sendAlertMail(Exception $e) {

$transport = (new Swift_SmtpTransport(Settings::$SMTP_SERVER, Settings::$SMTP_PORT, Settings::$SECURITY))
->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;
}
}
3 changes: 3 additions & 0 deletions Killbot.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

namespace Killbot;

use stdClass;

class Killbot {

Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ This bot use RedisQ (https://github.com/zKillboard/RedisQ) in order to provide q

## Requirements
* Slack
* PHP (with ext-curl)
* PHP 7 (with ext-curl)
* Composer

## Setup

Checkout this project and edit the first part of `Setting.php` to match your configuration.
* Checkout the project
* Run `composer install`
* Edit the first part of `Setting.php` to match your configuration.
> 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

Expand Down
25 changes: 24 additions & 1 deletion Settings.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Killbot;

class Settings {

/*******************************************************************************************************************
Expand Down Expand Up @@ -28,6 +30,27 @@ class Settings {
]
];

/*******************************************************************************************************************
* Mail settings
* Allow script to send mail alert when something goes wrong
******************************************************************************************************************/

// Enable sending mails
public static $SEND_MAIL = true;

// Server configuration
public static $SMTP_SERVER = 'smtp.example.com';
public static $SMTP_PORT = '465';
public static $SMTP_USER = '[email protected]';
public static $SMTP_PASSWORD = 'my_password';

// Use null or ssl if required
public static $SECURITY = 'ssl';

// Mail addresses
public static $MAIL_RECIPIENT = '[email protected]';
public static $MAIL_SENDER = '[email protected]';

/*******************************************************************************************************************
* Advanced configuration
* Do not edit unless you know what you are doing
Expand All @@ -47,4 +70,4 @@ class Settings {

// ESI base URL
public static $ESI_URL = 'https://esi.tech.ccp.is/latest';
}
}
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "slivo/killbot",
"type": "project",
"authors": [
{
"name": "slivo-fr",
"email": "[email protected]"
}
],
"require": {
"php": "^7.0",
"swiftmailer/swiftmailer": "^6.0",
"twig/twig": "^2.0"
},
"autoload": {
"psr-4": {
"Killbot\\":""
}
}
}
25 changes: 21 additions & 4 deletions cron.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
<?php

include_once('Settings.php');
include_once('Killbot.php');
set_error_handler(function($code, $message, $file, $line) {
echo $message . "\n";
throw new ErrorException($message, 0, $code, $file, $line);
});

$killbot = new Killbot();
$killbot->run();
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);
}
}
12 changes: 12 additions & 0 deletions mail.html.twig
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 02d356d

Please sign in to comment.