Skip to content

Commit

Permalink
Move functions to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
gbirke committed Sep 10, 2015
1 parent 45791ec commit e5db650
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 47 deletions.
46 changes: 46 additions & 0 deletions monitoring/functions.php
@@ -0,0 +1,46 @@
<?php

/**
* Send notification mail if notification interval is exceeded.
*
* @param string $email
* @param string $subject
* @param string $body
* @param int $notificationInterval Number of seconds between notifications
*/
function notifyConditionally( $email, $subject, $body, $notificationInterval ) {
$lastNotify = __DIR__ . '/monitor_response.lastnotify.txt';
if ( mustNotify( $lastNotify, $notificationInterval ) ) {
$lines = [$email, $subject, $body];
file_put_contents( $lastNotify, implode( "\n", $lines ) );
mail( $email, $subject, $body );
}
}

/**
* Check if the notification mail should be sent by examining the modification date of the file.
*
* @param string $filename
* @param int $notificationInterval
* @return bool
*/
function mustNotify( $filename, $notificationInterval ) {
if ( !file_exists( $filename ) ) {
return true;
}
$currentInterval = time() - filemtime( $filename );
return $currentInterval > $notificationInterval;
}

/**
* Check if the HTTP code is a redirect and if the response headers contain the expected location
* @param int $httpCode
* @param string $response
* @param string $expectedLocation
* @return bool
*/
function resultHasErrors( $httpCode, $response, $expectedLocation ) {
return $httpCode !== 301 ||
$response === false ||
stripos( $response, "Location: $expectedLocation" ) === false;
}
50 changes: 3 additions & 47 deletions monitoring/monitor_response.php
Expand Up @@ -5,51 +5,7 @@
*/

include __DIR__ . '/config.php';

/**
* Send notification mail if notification interval is exceeded.
*
* @param string $email
* @param string $subject
* @param string $body
* @param int $notificationInterval Number of seconds between notifications
*/
function notifyConditionally( $email, $subject, $body, $notificationInterval ) {
$lastNotify = __DIR__ . '/monitor_response.lastnotify.txt';
if ( mustNotify( $lastNotify, $notificationInterval ) ) {
$lines = [$email, $subject, $body];
file_put_contents( $lastNotify, implode( "\n", $lines ) );
mail( $email, $subject, $body );
}
}

/**
* Check if the notification mail should be sent by examining the modification date of the file.
*
* @param string $filename
* @param int $notificationInterval
* @return bool
*/
function mustNotify( $filename, $notificationInterval ) {
if ( !file_exists( $filename ) ) {
return true;
}
$currentInterval = time() - filemtime( $filename );
return $currentInterval > $notificationInterval;
}

/**
* Check if the HTTP code is a redirect and if the response headers contain the expected location
* @param int $httpCode
* @param string $response
* @param string $expectedLocation
* @return bool
*/
function resultHasErrors( $httpCode, $response, $expectedLocation ) {
return $httpCode !== 301 ||
$response === false ||
stripos( $response, "Location: $expectedLocation" ) === false;
}
require __DIR__ . '/functions.php';

$ch = curl_init( $checkURL );

Expand All @@ -62,9 +18,9 @@ function resultHasErrors( $httpCode, $response, $expectedLocation ) {

$httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
if ( resultHasErrors( $httpCode, $response, $expectedLocation ) ) {
$errorMessage = "Got the following result back:\n\n$response";
$errorMessage = "Got the following result back:\n\n$response";
$errorMessage .= 'CURL error: ' . curl_error( $ch );
notifyConditionally(
notifyConditionally(
$notifyMail,
"$subjectPrefix Forward script check failed",
$errorMessage,
Expand Down

0 comments on commit e5db650

Please sign in to comment.