Skip to content

Commit

Permalink
[NF] CLI tool to email a report on ports with error / discard counts
Browse files Browse the repository at this point in the history
This commit introduces two new CLI actions:

    ixptool.php -a statistics-cli.email-ports-with-errors
    ixptool.php -a statistics-cli.email-ports-with-discards

Which emails a report (including graphs) of any member ports have error
or discard counts from 'yesterday'.

A new configuration block is required:

    ; note that the %s in the subject below is replaced
    ; with Errors / Discards as appropriate by the script
    ;
    cli.ports_with_counts.from_email = "ops@example.com"
    cli.ports_with_counts.from_name  = "IXP Operations"
    cli.ports_with_counts.subject = "IXP - Ports with %s"
    cli.ports_with_counts.recipients[] = "someone@example.com"
    cli.ports_with_counts.recipients[] = "someone-else@example.com"

The documentation has also been updated:

https://github.com/inex/IXP-Manager/wiki/Email-Notifications
  • Loading branch information
barryo committed Jul 24, 2013
1 parent 748452c commit 686689b
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 2 deletions.
11 changes: 9 additions & 2 deletions application/configs/application.ini.dist
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,9 @@ mailinglist.cmd.changepw = "/usr/local/mailman/bin/withlist -q -l -r chang

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; IXP Tool / CLI Settings
;; IXP Tool / Statistics CLI Settings
;;
;; We have a few useful tools which also need better documentation.
;; See: https://github.com/inex/IXP-Manager/wiki/Email-Notifications
;;

cli.traffic_differentials.stddev_calc_length = 60
Expand All @@ -426,6 +426,13 @@ cli.port_utilisation.subject = "XXX Port Utilisation Report"
cli.port_utilisation.recipients[] = "someone@example.com"
cli.port_utilisation.recipients[] = "someone-else@example.com"

; note that the %s in the subject below is replaced with Errors / Discards as appropriate by the script
cli.ports_with_counts.from_email = "ops@example.com"
cli.ports_with_counts.from_name = "IXP Operations"
cli.ports_with_counts.subject = "IXP - Ports with %s"
cli.ports_with_counts.recipients[] = "someone@example.com"
cli.ports_with_counts.recipients[] = "someone-else@example.com"



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down
71 changes: 71 additions & 0 deletions application/controllers/StatisticsCliController.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,77 @@ public function uploadTrafficStatsToDbAction()
}


public function emailPortsWithErrorsAction()
{
$this->emailPortsWithCounts( 'Errors', IXP_Mrtg::CATEGORY_ERRORS, 'day_tot_in', 'day_tot_out' );
}

public function emailPortsWithDiscardsAction()
{
$this->emailPortsWithCounts( 'Discards', IXP_Mrtg::CATEGORY_DISCARDS, 'day_tot_in', 'day_tot_out' );
}

private function emailPortsWithCounts( $type, $category, $inField, $outField )
{
$day = date( 'Y-m-d', strtotime( '-1 days' ) );

$data = $this->getD2R( '\\Entities\\TrafficDaily' )->load( $day, $category );

$mail = $this->getMailer();
$mail->setFrom( $this->_options['cli']['ports_with_counts']['from_email'], $this->_options['cli']['ports_with_counts']['from_name'] )
->setSubject( sprintf( $this->_options['cli']['ports_with_counts']['subject'], $type ) )
->setType( Zend_Mime::MULTIPART_RELATED );

foreach( $this->_options['cli']['ports_with_counts']['recipients'] as $r )
$mail->addTo( $r );

$this->view->type = $type;
$mailHtml = $this->view->render( 'statistics-cli/email/counts-header.phtml' );

$numWithCounts = 0;

foreach( $data as $d )
{
if( $d[ $inField ] == 0 && $d[ $outField ] == 0 )
continue;

$numWithCounts++;

if( $this->isVerbose() || $this->isDebug() )
echo "{$d['Customer']['name']}\t\tIN / OUT: {$d[ $inField ]} / {$d[ $outField ]}\n";

$mrtg = $mail->createAttachment(
file_get_contents(
IXP_Mrtg::getMrtgFilePath(
$this->_options['mrtg']['path'] . '/members',
'PNG',
'aggregate',
$category,
$d['Customer']['shortname'],
IXP_Mrtg::PERIOD_DAY
)
),
"image/png",
Zend_Mime::DISPOSITION_INLINE,
Zend_Mime::ENCODING_BASE64,
"{$d['Customer']['shortname']}-aggregate.png"
);

$this->view->mrtg_id = $mrtg->id = "{$d['Customer']['shortname']}-aggregate";
$this->view->ecust = $d['Customer'];
$this->view->in = $d[ $inField ];
$this->view->out = $d[ $outField ];
$mailHtml .= $this->view->render( 'statistics-cli/email/counts-member.phtml' );
}

if( $numWithCounts )
{
$this->view->numWithCounts = $numWithCounts;
$mailHtml .= $this->view->render( 'statistics-cli/email/counts-footer.phtml' );
$mail->setBodyHtml( $mailHtml );
$mail->send();
}
}

}

9 changes: 9 additions & 0 deletions application/views/statistics-cli/email/counts-footer.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

{if $numWithCounts eq 0}
<p>
No ports were found with {$type|lower}.
</p>
{/if}

</body>
</html>
16 changes: 16 additions & 0 deletions application/views/statistics-cli/email/counts-header.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<html>
<head>
<title>Ports with {$type} Report</title>
</head>

<body>

<h1>Ports with {$type} Report</h1>

<p>
The following ports had {$type|lower} yesterday.
</p>




14 changes: 14 additions & 0 deletions application/views/statistics-cli/email/counts-member.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@


<h2>{$ecust.name}</h2>

<p>
<strong>{$type} IN / OUT:</strong> {$in} / {$out}
</p>

<p>
<a href="{$options.identity.url}statistics/member/shortname/{$ecust.shortname}">
<img src="cid:{$mrtg_id}" alt="[{$ecust['shortname']}]" />
</a>
</p>

0 comments on commit 686689b

Please sign in to comment.