forked from librenms/librenms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alerts.inc.php
executable file
·112 lines (96 loc) · 2.74 KB
/
alerts.inc.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* Observium Network Management and Monitoring System
* Copyright (C) 2006-2012, Adam Armstrong - http://www.observium.org
*
* @package alerting
* @author Adam Armstrong <[email protected]>
*
*/
/**
* Build a cache of default alert conditions
*
* @return array
*/
function cache_conditions_global() {
$cache = array();
foreach (dbFetchRows("SELECT * FROM `alert_conditions_global`") as $entry)
{
$cache[$entry['type']][$entry['subtype']][$entry['metric']][] = array('operator' => $entry['operator'], 'value' => $entry['value'],
'severity' => $entry['severity'], 'alerter' => $entry['alerter'], 'enable' => $entry['enable']);
}
return $cache;
}
/**
* Build a cache of device-specific alert conditions
*
* @return array
* @param device_id
*/
function cache_conditions_device($device_id) {
$cache = array();
foreach (dbFetchRows("SELECT * FROM `alert_conditions` WHERE `device_id` = ?", array($device_id)) as $entry)
{
$cache[$entry['type']][$entry['subtype']][$entry['entity']][$entry['metric']][] = array('condition' => $entry['operator'], 'value' => $entry['value'],
'severity' => $entry['severity'], 'alerter' => $entry['alerter'],
'enable' => $entry['enable']);
}
return $cache;
}
/**
* Compare two values
*
* @return integer
* @param value_a
* @param condition
* @param value_b
*/
function test_condition($value_a, $condition, $value_b)
{
switch($condition)
{
case ">":
if($value_a > $value_b) { $alert = 1; } else { $alert = 0; }
break;
case "<":
if($value_a < $value_b) { $alert = 1; } else { $alert = 0; }
break;
case "!=":
if($value_a != $value_b) { $alert = 1; } else { $alert = 0; }
break;
case "=":
if($value_a = $value_b) { $alert = 1; } else { $alert = 0; }
break;
default:
$alert = -1;
break;
}
return $alert;
}
/**
* Check entity data against alert conditions
*
* @param value_a
* @param condition
* @param value_b
*/
function check_entity($device, $entity_type, $entity_id, $data)
{
global $glo_conditions;
global $dev_conditions;
if(!empty($entity_id)) { echo(" $entity_id"); }
foreach($data as $name => $value)
{
foreach($dev_conditions[$entity_type][$entity_id][$name] as $condition)
{
$alert = test_condition($value, $condition['condition'], $condition['value']);
if($alert == 1)
{
echo("ALERT ");
} else {
echo("OK ");
}
}
}
}
?>