Skip to content

Instantly share code, notes, and snippets.

@draco2003
Created February 9, 2012 05:02
Show Gist options
  • Save draco2003/1777463 to your computer and use it in GitHub Desktop.
Save draco2003/1777463 to your computer and use it in GitHub Desktop.
Beta/debugging Processor
diff --git a/plugins/email_plugin.php b/plugins/email_plugin.php
index 98b12b5..f51807b 100644
--- a/plugins/email_plugin.php
+++ b/plugins/email_plugin.php
@@ -43,14 +43,14 @@ function email_plugin_send_methods(){
}
//email plugin
-function email_plugin_notify($check,$check_result,$subscription,$alt_email=false) {
+function email_plugin_notify($check,$check_result,$subscription,$alt_email=false,$data='') {
global $status_array;
$user = new User($subscription->getUserId());
$email = new fEmail();
// This sets up fSMTP to connect to the gmail SMTP server
// with a 5 second timeout. Gmail requires a secure connection.
- $smtp = new fSMTP(sys_var('smtp_server'), sys_var('smtp_port'), TRUE, 5);
- $smtp->authenticate(sys_var('smtp_user'), sys_var('smtp_pass'));
+ $smtp = new fSMTP(sys_var('smtp_server'), sys_var('smtp_port'), FALSE, 5);
+ //$smtp->authenticate(sys_var('smtp_user'), sys_var('smtp_pass'));
if ($alt_email) {
$email_address = usr_var('alt_email',$user->getUserId());
} else {
@@ -63,7 +63,7 @@ function email_plugin_notify($check,$check_result,$subscription,$alt_email=false
$email->setSubject(str_replace('{check_name}', $check->prepareName(), sys_var('email_subject')));
// Set the body to include a string containing UTF-8
$state = $status_array[$check_result->getStatus()];
- $email->setHTMLBody("<p>$state Alert for {$check->prepareName()} </p><p>The check returned {$check_result->prepareValue()}</p><p>Warning Threshold is : ". $check->getWarn() . "</p><p>Error Threshold is : ". $check->getError() . '</p><p>View Alert Details : <a href="' . fURL::getDomain() . '/' . CheckResult::makeURL('list',$check_result) . '">'.$check->prepareName()."</a></p>");
+ $email->setHTMLBody("<p>$state Alert for {$check->prepareName()} </p><p>The check returned {$check_result->prepareValue()}</p><p>Warning Threshold is : ". $check->getWarn() . "</p><p>Error Threshold is : ". $check->getError() . '</p><p>View Alert Details : <a href="' . fURL::getDomain() . '/' . CheckResult::makeURL('list',$check_result) . '">'.$check->prepareName()."</a></p><div>Data Debug : <pre>" . var_export($data,true) ."</pre>");
$email->setBody("
$state Alert for {$check->prepareName()}
The check returned {$check_result->prepareValue()}
diff --git a/processor.php b/processor.php
index d480fc6..f9f7334 100644
--- a/processor.php
+++ b/processor.php
@@ -25,29 +25,33 @@ if ($debug) {
$checks = Check::findActive();
foreach ($checks as $check) {
- $data = Check::getData($check);
- if (count($data) > 0) {
- $title = $check->prepareName();
- fCore::debug('Processing :' . $title . ":\n",FALSE);
- $check_value = Check::getResultValue($data,$check);
- fCore::debug("Result :" . $check_value . ":\n",FALSE);
- $result = Check::setResultsLevel($check_value,$check);
- fCore::debug("Check Value:" . $result . ":\n",FALSE);
- if (is_null($check->getLastCheckTime())) {
- $next_check = new fTimestamp();
- fCore::debug("is null?\n",FALSE);
- } else {
- $next_check = $check->getLastCheckTime()->adjust('+' . $check->getRepeatDelay() . ' minutes');
- }
- $end = new fTimestamp();
- if ($next_check->lt($end)) {
- fCore::debug("next check is lt then now\n",FALSE);
- } else {
- fCore::debug("not less then now\n",FALSE);
- }
- // If It's been more then the Repeat Delay or the Status has changed
- if ($next_check->lt($end) || $check->getLastCheckStatus() != $result) {
- fCore::debug("Send Notification \n",FALSE);
+ $title = $check->prepareName();
+ fCore::debug('Processing :' . $title . ":\n",FALSE);
+
+ if (is_null($check->getLastCheckTime())) {
+ $next_check = new fTimestamp();
+ $check_dirty = true;
+ fCore::debug("is null?\n",FALSE);
+ } else {
+ $check_dirty = false;
+ $next_check = $check->getLastCheckTime()->adjust('+' . $check->getRepeatDelay() . ' minutes');
+ }
+ $end = new fTimestamp();
+
+ // If It's been more then the Repeat Delay then grab the data and run the check
+ if ($next_check->lt($end)) {
+ fCore::debug("next check is lt then now " . var_export($next_check, true) . " : " . var_export($end,true) ." \n",FALSE);
+ $data = Check::getData($check);
+ fCore::debug("Data" . print_r($data, TRUE) . ":\n");
+ // Store last check results otherwise it's overwritten when we check for state change
+ $last_check_results = (int)$check->getLastCheckStatus();
+ // If we get an array back from graphite, lets run the date through
+ if (count($data) > 0) {
+ $check_value = Check::getResultValue($data,$check);
+ fCore::debug("Result :" . $check_value . ":\n",FALSE);
+ $result = Check::setResultsLevel($check_value,$check);
+ fCore::debug("Check Value:" . $result . ":\n",FALSE);
+ fCore::debug("Last Check Status : " . print_r($last_check_results,true) . ":\n");
fCore::debug("State :" . $result . ":\n",FALSE);
$check_result = new CheckResult();
$check_result->setCheckId($check->getCheckId());
@@ -59,16 +63,28 @@ foreach ($checks as $check) {
$check->setLastCheckTime($end);
$check_result->store();
$check->store();
- $subscriptions = Subscription::findAll($check->getCheckId());
- foreach ($subscriptions as $subscription) {
- $notify_function = $subscription->getMethod();
- if (function_exists($notify_function)){
- $notify_result = $notify_function($check,$check_result,$subscription);
+ $check_dirty = false;
+ fCore::debug("check status = " . var_export($last_check_results,true) . " : " . var_export($result,true) . ":\n");
+ if ($last_check_results != $result) {
+ fCore::debug("Send Notification \n",FALSE);
+ $subscriptions = Subscription::findAll($check->getCheckId());
+ foreach ($subscriptions as $subscription) {
+ $notify_function = $subscription->getMethod();
+ if (function_exists($notify_function)){
+ $notify_result = $notify_function($check,$check_result,$subscription,false,$data);
+ }
}
+ } else {
+ fCore::debug("Skip Notification \n",FALSE);
}
- } else {
- fCore::debug("Skip Notification \n",FALSE);
}
+ } else {
+ fCore::debug("not less then now : " . var_export($next_check, true) . " : " . var_export($end,true) . "\n",FALSE);
+ }
+ if ($check_dirty) {
+ // do a quick store of the updated timestamp if we ever errored and don't have a timestamp
+ // this can happen if we never get data back from graphite for this metric, it'll make the check every cron run otherwise
+ $check->store();
}
fCore::debug("check done moving to next \n\n",FALSE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment