forked from Graphite-Tattle/Tattle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.php
76 lines (72 loc) · 2.68 KB
/
processor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
include 'inc/init.php';
$debug = false;
if (isset($_SERVER['argc'])) {
$args = getopt('d::h::',array('debug','help'));
if (isset($args['debug']) || isset($args['d'])) {
$debug = true;
} elseif (isset($args['help']) || isset($args['h'])) {
print "Tattle Check Processor: \n".
"\n" .
"--help, -h : Displays this help \n".
"\n" .
"--debug, -d : Enables debuging (?debug=true can be used via a web request) \n";
}
} elseif ($debug = fRequest::get('debug','boolean')) {
$debug = true;
}
if ($debug) {
print "debug enabled";
fCore::enableDebugging(TRUE);
}
$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 ".$next_check);
fCore::debug("End ".$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) && $result >= $check->getWarn()) {
fCore::debug("Send Notification \n",FALSE);
fCore::debug("State :" . $result . ":\n",FALSE);
$check_result = new CheckResult();
$check_result->setCheckId($check->getCheckId());
$check_result->setStatus($result);
$check_result->setValue($check_value);
$check_result->setState(0);
$check->setLastCheckStatus($result);
$check->setLastCheckValue($check_value);
$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);
}
}
} else {
fCore::debug("Skip Notification \n",FALSE);
}
}
fCore::debug("check done moving to next \n\n",FALSE);
}