1 | <?php |
||
2 | /** |
||
3 | * AlertUtil.php |
||
4 | * |
||
5 | * Extending the built in logging to add an event logger function |
||
6 | * |
||
7 | * This program is free software: you can redistribute it and/or modify |
||
8 | * it under the terms of the GNU General Public License as published by |
||
9 | * the Free Software Foundation, either version 3 of the License, or |
||
10 | * (at your option) any later version. |
||
11 | * |
||
12 | * This program is distributed in the hope that it will be useful, |
||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
||
15 | * GNU General Public License for more details. |
||
16 | * |
||
17 | * You should have received a copy of the GNU General Public License |
||
18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
19 | * |
||
20 | * @link https://www.librenms.org |
||
21 | * |
||
22 | * @copyright 2019 KanREN, Inc. |
||
23 | * @author Heath Barnhart <[email protected]> |
||
24 | */ |
||
25 | |||
26 | namespace LibreNMS\Alert; |
||
27 | |||
28 | use App\Models\Device; |
||
29 | use App\Models\User; |
||
30 | use DeviceCache; |
||
31 | use LibreNMS\Config; |
||
32 | use PHPMailer\PHPMailer\PHPMailer; |
||
33 | |||
34 | class AlertUtil |
||
35 | { |
||
36 | /** |
||
37 | * Get the rule_id for a specific alert |
||
38 | * |
||
39 | * @param int $alert_id |
||
40 | * @return mixed|null |
||
41 | */ |
||
42 | private static function getRuleId($alert_id) |
||
43 | { |
||
44 | $query = 'SELECT `rule_id` FROM `alerts` WHERE `id`=?'; |
||
45 | |||
46 | return dbFetchCell($query, [$alert_id]); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Get the transport for a given alert_id |
||
51 | * |
||
52 | * @param int $alert_id |
||
53 | * @return array |
||
54 | */ |
||
55 | public static function getAlertTransports($alert_id) |
||
56 | { |
||
57 | $query = "SELECT b.transport_id, b.transport_type, b.transport_name FROM alert_transport_map AS a LEFT JOIN alert_transports AS b ON b.transport_id=a.transport_or_group_id WHERE a.target_type='single' AND a.rule_id=? UNION DISTINCT SELECT d.transport_id, d.transport_type, d.transport_name FROM alert_transport_map AS a LEFT JOIN alert_transport_groups AS b ON a.transport_or_group_id=b.transport_group_id LEFT JOIN transport_group_transport AS c ON b.transport_group_id=c.transport_group_id LEFT JOIN alert_transports AS d ON c.transport_id=d.transport_id WHERE a.target_type='group' AND a.rule_id=?"; |
||
58 | $rule_id = self::getRuleId($alert_id); |
||
59 | |||
60 | return dbFetchRows($query, [$rule_id, $rule_id]); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Returns the default transports |
||
65 | * |
||
66 | * @return array |
||
67 | */ |
||
68 | public static function getDefaultAlertTransports() |
||
69 | { |
||
70 | $query = 'SELECT transport_id, transport_type, transport_name FROM alert_transports WHERE is_default=true'; |
||
71 | |||
72 | return dbFetchRows($query); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Find contacts for alert |
||
77 | * |
||
78 | * @param array $results Rule-Result |
||
79 | * @return array |
||
80 | */ |
||
81 | public static function getContacts($results) |
||
82 | { |
||
83 | if (empty($results)) { |
||
84 | return []; |
||
85 | } |
||
86 | if (Config::get('alert.default_only') === true || Config::get('alerts.email.default_only') === true) { |
||
87 | $email = Config::get('alert.default_mail', Config::get('alerts.email.default')); |
||
88 | |||
89 | return $email ? [$email => ''] : []; |
||
90 | } |
||
91 | $users = User::query()->thisAuth()->get(); |
||
92 | $contacts = []; |
||
93 | $uids = []; |
||
94 | foreach ($results as $result) { |
||
95 | $tmp = null; |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
96 | if (is_numeric($result['bill_id'])) { |
||
97 | $tmpa = dbFetchRows('SELECT user_id FROM bill_perms WHERE bill_id = ?', [$result['bill_id']]); |
||
98 | foreach ($tmpa as $tmp) { |
||
99 | $uids[$tmp['user_id']] = $tmp['user_id']; |
||
100 | } |
||
101 | } |
||
102 | if (is_numeric($result['port_id'])) { |
||
103 | $tmpa = dbFetchRows('SELECT user_id FROM ports_perms WHERE port_id = ?', [$result['port_id']]); |
||
104 | foreach ($tmpa as $tmp) { |
||
105 | $uids[$tmp['user_id']] = $tmp['user_id']; |
||
106 | } |
||
107 | } |
||
108 | if (is_numeric($result['device_id'])) { |
||
109 | if (Config::get('alert.syscontact') == true) { |
||
110 | if (dbFetchCell("SELECT attrib_value FROM devices_attribs WHERE attrib_type = 'override_sysContact_bool' AND device_id = ?", [$result['device_id']])) { |
||
111 | $tmpa = dbFetchCell("SELECT attrib_value FROM devices_attribs WHERE attrib_type = 'override_sysContact_string' AND device_id = ?", [$result['device_id']]); |
||
112 | } else { |
||
113 | $tmpa = dbFetchCell('SELECT sysContact FROM devices WHERE device_id = ?', [$result['device_id']]); |
||
114 | } |
||
115 | if (! empty($tmpa)) { |
||
116 | $contacts[$tmpa] = ''; |
||
117 | } |
||
118 | } |
||
119 | $tmpa = dbFetchRows('SELECT user_id FROM devices_perms WHERE device_id = ?', [$result['device_id']]); |
||
120 | foreach ($tmpa as $tmp) { |
||
121 | $uids[$tmp['user_id']] = $tmp['user_id']; |
||
122 | } |
||
123 | } |
||
124 | } |
||
125 | foreach ($users as $user) { |
||
126 | if (empty($user['email'])) { |
||
127 | continue; // no email, skip this user |
||
128 | } |
||
129 | if (empty($user['realname'])) { |
||
130 | $user['realname'] = $user['username']; |
||
131 | } |
||
132 | if (Config::get('alert.globals') && ($user['level'] >= 5 && $user['level'] < 10)) { |
||
133 | $contacts[$user['email']] = $user['realname']; |
||
134 | } elseif (Config::get('alert.admins') && $user['level'] == 10) { |
||
135 | $contacts[$user['email']] = $user['realname']; |
||
136 | } elseif (Config::get('alert.users') == true && in_array($user['user_id'], $uids)) { |
||
137 | $contacts[$user['email']] = $user['realname']; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | $tmp_contacts = []; |
||
142 | foreach ($contacts as $email => $name) { |
||
143 | if (strstr($email, ',')) { |
||
144 | $split_contacts = preg_split('/[,\s]+/', $email); |
||
145 | foreach ($split_contacts as $split_email) { |
||
146 | if (! empty($split_email)) { |
||
147 | $tmp_contacts[$split_email] = $name; |
||
148 | } |
||
149 | } |
||
150 | } else { |
||
151 | $tmp_contacts[$email] = $name; |
||
152 | } |
||
153 | } |
||
154 | |||
155 | if (! empty($tmp_contacts)) { |
||
156 | // Validate contacts so we can fall back to default if configured. |
||
157 | $mail = new PHPMailer(); |
||
158 | foreach ($tmp_contacts as $tmp_email => $tmp_name) { |
||
159 | if ($mail->validateAddress($tmp_email) != true) { |
||
160 | unset($tmp_contacts[$tmp_email]); |
||
161 | } |
||
162 | } |
||
163 | } |
||
164 | |||
165 | // Copy all email alerts to default contact if configured. |
||
166 | $default_mail = Config::get('alert.default_mail'); |
||
167 | if (! isset($tmp_contacts[$default_mail]) && Config::get('alert.default_copy')) { |
||
168 | $tmp_contacts[$default_mail] = ''; |
||
169 | } |
||
170 | // Send email to default contact if no other contact found |
||
171 | if (empty($tmp_contacts) && Config::get('alert.default_if_none') && $default_mail) { |
||
172 | $tmp_contacts[$default_mail] = ''; |
||
173 | } |
||
174 | |||
175 | return $tmp_contacts; |
||
176 | } |
||
177 | |||
178 | public static function getRules($device_id) |
||
179 | { |
||
180 | $query = 'SELECT DISTINCT a.* FROM alert_rules a |
||
181 | LEFT JOIN alert_device_map d ON a.id=d.rule_id AND (a.invert_map = 0 OR a.invert_map = 1 AND d.device_id = ?) |
||
182 | LEFT JOIN alert_group_map g ON a.id=g.rule_id AND (a.invert_map = 0 OR a.invert_map = 1 AND g.group_id IN (SELECT DISTINCT device_group_id FROM device_group_device WHERE device_id = ?)) |
||
183 | LEFT JOIN alert_location_map l ON a.id=l.rule_id AND (a.invert_map = 0 OR a.invert_map = 1 AND l.location_id IN (SELECT DISTINCT location_id FROM devices WHERE device_id = ?)) |
||
184 | LEFT JOIN device_group_device dg ON g.group_id=dg.device_group_id AND dg.device_id = ? |
||
185 | WHERE a.disabled = 0 AND ( |
||
186 | (d.device_id IS NULL AND g.group_id IS NULL) |
||
187 | OR (a.invert_map = 0 AND (d.device_id=? OR dg.device_id=?)) |
||
188 | OR (a.invert_map = 1 AND (d.device_id != ? OR d.device_id IS NULL) AND (dg.device_id != ? OR dg.device_id IS NULL)) |
||
189 | )'; |
||
190 | |||
191 | $params = [$device_id, $device_id, $device_id, $device_id, $device_id, $device_id, $device_id, $device_id]; |
||
192 | |||
193 | return dbFetchRows($query, $params); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Check if device is under maintenance |
||
198 | * |
||
199 | * @param int $device_id Device-ID |
||
200 | * @return bool |
||
201 | */ |
||
202 | public static function isMaintenance($device_id) |
||
203 | { |
||
204 | return DeviceCache::get($device_id)->isUnderMaintenance(); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Check if device is set to ignore alerts |
||
209 | * |
||
210 | * @param int $device_id Device-ID |
||
211 | * @return bool |
||
212 | */ |
||
213 | public static function hasDisableNotify($device_id) |
||
214 | { |
||
215 | $device = Device::find($device_id); |
||
216 | |||
217 | return ! is_null($device) && $device->disable_notify; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Process Macros |
||
222 | * |
||
223 | * @param string $rule Rule to process |
||
224 | * @param int $x Recursion-Anchor |
||
225 | * @return string|bool |
||
226 | */ |
||
227 | public static function runMacros($rule, $x = 1) |
||
228 | { |
||
229 | $macros = Config::get('alert.macros.rule', []); |
||
230 | krsort($macros); |
||
231 | foreach ($macros as $macro => $value) { |
||
232 | if (! strstr($macro, ' ')) { |
||
233 | $rule = str_replace('%macros.' . $macro, '(' . $value . ')', $rule); |
||
234 | } |
||
235 | } |
||
236 | if (strstr($rule, '%macros.')) { |
||
237 | if (++$x < 30) { |
||
238 | $rule = self::runMacros($rule, $x); |
||
239 | } else { |
||
240 | return false; |
||
241 | } |
||
242 | } |
||
243 | |||
244 | return $rule; |
||
245 | } |
||
246 | } |
||
247 |