Skip to content

Commit

Permalink
Merge pull request #69 from 0x46616c6b/reporter-mail
Browse files Browse the repository at this point in the history
added reporter to send mail when check fails
  • Loading branch information
lsmith77 committed Jul 9, 2014
2 parents 85984f9 + 0b3afb3 commit a166250
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 0 deletions.
8 changes: 8 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public function getConfigTreeBuilder()
$rootNode
->children()
->booleanNode('enable_controller')->defaultFalse()->end()
->arrayNode('mailer')
->canBeEnabled()
->children()
->scalarNode('recipient')->isRequired()->cannotBeEmpty()->end()
->scalarNode('sender')->isRequired()->cannotBeEmpty()->end()
->scalarNode('subject')->isRequired()->cannotBeEmpty()->end()
->end()
->end()
->arrayNode('checks')
->canBeUnset()
->children()
Expand Down
8 changes: 8 additions & 0 deletions DependencyInjection/LiipMonitorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('controller.xml');
}

if ($config['mailer']['enabled']) {
$loader->load('helper/swift_mailer.xml');

foreach ($config['mailer'] as $key => $value) {
$container->setParameter(sprintf('%s.mailer.%s', $this->getAlias(), $key), $value);
}
}

if (empty($config['checks'])) {
return;
}
Expand Down
110 changes: 110 additions & 0 deletions Helper/SwiftMailerReporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace Liip\MonitorBundle\Helper;

use ArrayObject;
use Swift_Mailer;
use Swift_Message;
use ZendDiagnostics\Check\CheckInterface;
use ZendDiagnostics\Result\Collection as ResultsCollection;
use ZendDiagnostics\Result\ResultInterface;
use ZendDiagnostics\Runner\Reporter\ReporterInterface;

/**
* @author louis <louis@systemli.org>
*/
class SwiftMailerReporter implements ReporterInterface
{
/**
* @var Swift_Mailer
*/
private $mailer;
/**
* @var string
*/
private $recipient;
/**
* @var string
*/
private $subject;
/**
* @var
*/
private $sender;

/**
* Constructor.
*
* @param Swift_Mailer $mailer
* @param string $recipient
* @param $sender
* @param string $subject
*/
public function __construct(Swift_Mailer $mailer, $recipient, $sender, $subject)
{
$this->mailer = $mailer;
$this->recipient = $recipient;
$this->sender = $sender;
$this->subject = $subject;
}

/**
* {@inheritDoc}
*/
public function onStart(ArrayObject $checks, $runnerConfig)
{
}

/**
* {@inheritDoc}
*/
public function onBeforeRun(CheckInterface $check, $checkAlias = null)
{
}

/**
* {@inheritDoc}
*/
public function onAfterRun(CheckInterface $check, ResultInterface $result, $checkAlias = null)
{
}

/**
* {@inheritDoc}
*/
public function onStop(ResultsCollection $results)
{
}

/**
* {@inheritDoc}
*/
public function onFinish(ResultsCollection $results)
{
if ($results->getFailureCount() > 0
|| $results->getWarningCount() > 0
|| $results->getUnknownCount() > 0
) {
$body = '';

foreach ($results as $check) {
/* @var $check CheckInterface */
/* @var $result ResultInterface */
$result = isset($results[$check]) ? $results[$check] : null;

if ($result instanceof ResultInterface) {
$body .= sprintf("Check: %s\n", $check->getLabel());
$body .= sprintf("Message: %s\n\n", $result->getMessage());
}
}

$message = Swift_Message::newInstance()
->setSubject($this->subject)
->setFrom($this->sender)
->setTo($this->recipient)
->setBody($body);

$this->mailer->send($message);
}
}
}
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ To run this reporter with the REST API, add a `reporters` query parameter:
```yaml
liip_monitor:
enable_controller: false
mailer:
enabled: false
recipient: ~
sender: ~
subject: ~
checks:
php_extensions: []
Expand Down
20 changes: 20 additions & 0 deletions Resources/config/helper/swift_mailer.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="liip_monitor.reporter.swift_mailer.class">Liip\MonitorBundle\Helper\SwiftMailerReporter</parameter>
</parameters>

<services>
<service id="liip_monitor.reporter.swift_mailer" class="%liip_monitor.reporter.swift_mailer.class%">
<tag name="liip_monitor.additional_reporter" alias="swift_mailer" />
<argument type="service" id="mailer" />
<argument>%liip_monitor.mailer.recipient%</argument>
<argument>%liip_monitor.mailer.sender%</argument>
<argument>%liip_monitor.mailer.subject%</argument>
</service>
</services>
</container>
50 changes: 50 additions & 0 deletions Tests/DependencyInjection/LiipMonitorExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,56 @@ public function testEnableController()
$this->assertTrue($this->container->has('liip_monitor.health_controller'));
}

public function testMailer()
{
$this->load();

$this->assertEquals(false, $this->container->has('liip_monitor.reporter.swift_mailer'));

$this->load(
array(
'mailer' => array(
'recipient' => 'foo@example.com',
'sender' => 'bar@example.com',
'subject' => 'Health Check'
)
)
);

$this->assertEquals(true, $this->container->has('liip_monitor.reporter.swift_mailer'));
}

/**
* @dataProvider mailerConfigProvider
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testInvalidMailerConfig($config)
{
$this->load($config);
}

public function mailerConfigProvider()
{
return array(
array(
array(
'mailer' => array(
'recipient' => 'foo@example.com'
)
)
),
array(
array(
'mailer' => array(
'recipient' => 'foo@example.com',
'sender' => 'bar@example.com',
'subject' => null
)
)
)
);
}

public function checkProvider()
{
return array(
Expand Down

0 comments on commit a166250

Please sign in to comment.