feature: Added new alert rule builder UI and rule mapping UI (#8293)

* feature: Added new alert rule builder UI

* Updated to export sql queries

* More updates

* more changes

* removed debug

* fix scrut

* Updated to include import options + various other fixes

* fix rule

* Populate name from collection rules.

* Fix default rule import
Allow new and old style rules in the collection.
Don't add new yet as I'm not sure GenSQL() is working.

* Fix GenSQL call

* Extract filter building to class so it is nicely contained in one place

* moved schema

* some fixes and tweaks

* travis fixes

* Some more features / updates

* Fix up my mistakes when adding default rules

* Use a modal for new alert (Incomplete)
Larger dialog!!
Remove page loading stuff.

Working:
Loading rules, resetting dialog, importing from collection.

Not working yet:
select width
device limited rule access? don't know what this is...

Lots of unused stuff to delete...

* reload "table" after save

* fixed editing rule

* Auto select2 width

* Reload window on save

* Restore per-device alert. Remove debug.

* Small cleanups. Rule Name first.

* Restore button to button type. Rename schema.

* Fixes: wrong command to reload window, remove extra attributes, rule is never passed

* Fixed old rule editing

* some small updates for old imports

* travis update to use trusty

* maybe travis fix

* Ability to set alert rule mappings on the rule edit screen

* pip installs one line, no quiet for deploy

* update schema def

* Fix style and some copyright headers

* fix docs missing file

* Allow new versions of snmpsim and libraries

* Parser WIP

* Fix default rules insert

* reorganize

* Legacy import first draft done

* Implement saving
Skip translation to sql for now

* Working on glues

* small rule collection fix

* Working on glues

* Working on glues

* Docs updates + small UI changes

* Parser WIP

* reorganize

* Legacy import first draft done

* Implement saving
Skip translation to sql for now

* Working on glues

* Working on glues

* Working on glues

* Add table mapping, should move to it's own class

* WIP

* Glue working!!

* Extract Schema class

* Some final touches.
revert alerts_rules.json for now.

* Finish up initial implementation
Needs more tests

* Fix a few places

* small doc updates

* Fix finding tables in grouped rules.

* remove unused code

* code format fixes

* Some quick tests for Schema
Simplified output for findRelationshipPath. Always includes start and target in the result.
This simplifies a lot of code in QueryBuilderParser.php
This also always loads the target table data now (which we want)

* Make bill_id the PRIMARY index for the bills table

* Load macros from a json file in misc instead of the database.

* Fix whitespace and wrong key for collection.

* Handle IN properly when generating SQL

* Fix glue (devices.device_id = ports.port_id) is incorrect :D
Show ALL tables we can resolve relationships for in the query builder filter.

* Remove all macros from the database
Remove insert statements, leave updates to update user's existing rules.
This commit is contained in:
Neil Lathwood
2018-03-14 20:25:19 +00:00
committed by GitHub
parent ba4c86f4c0
commit 03076c4025
67 changed files with 2263 additions and 1107 deletions

View File

@@ -12,6 +12,8 @@
* the source code distribution for details.
*/
use LibreNMS\Alerting\QueryBuilderParser;
if (is_admin() === false) {
header('Content-type: text/plain');
die('ERROR: You need to be admin');
@@ -21,22 +23,40 @@ $alert_id = $_POST['alert_id'];
$template_id = $_POST['template_id'];
if (is_numeric($alert_id) && $alert_id > 0) {
$rule = dbFetchRow('SELECT * FROM `alert_rules` WHERE `id` = ? LIMIT 1', array($alert_id));
$rule = dbFetchRow('SELECT * FROM `alert_rules` WHERE `id` = ? LIMIT 1', [$alert_id]);
$maps = [];
$devices = dbFetchRows('SELECT `device_id`, `hostname`, `sysName` FROM `alert_device_map` LEFT JOIN `devices` USING (`device_id`) WHERE `rule_id`=?', [$alert_id]);
foreach ($devices as $device) {
$maps[] = ['id' => $device['device_id'], 'text' => format_hostname($device)];
}
$groups = dbFetchRows('SELECT `group_id`, `name` FROM `alert_group_map` LEFT JOIN `device_groups` ON `device_groups`.`id`=`alert_group_map`.`group_id` WHERE `rule_id`=?', [$alert_id]);
foreach ($groups as $group) {
$maps[] = ['id' => 'g' . $group['group_id'], 'text' => $group['name']];
}
} elseif (is_numeric($template_id) && $template_id >= 0) {
$tmp_rules = get_rules_from_json();
$rule = $tmp_rules[$template_id];
$maps = [];
}
if (is_array($rule)) {
$rule_split = preg_split('/([a-zA-Z0-9_\-\.\=\%\<\>\ \"\'\!\~\(\)\*\/\@\|]+[&&|\|\|]{2})/', $rule['rule'], -1, (PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));
$count = (count($rule_split) - 1);
$rule_split[$count] = $rule_split[$count].' &&';
$output = array(
'severity' => $rule['severity'],
'extra' => $rule['extra'],
if (empty($rule['builder'])) {
// convert old rules when editing
$builder = QueryBuilderParser::fromOld($rule['rule'])->toArray();
} else {
$builder = json_decode($rule['builder']);
}
header('Content-type: application/json');
echo json_encode([
'extra' => isset($rule['extra']) ? json_decode($rule['extra']) : null,
'maps' => $maps,
'name' => $rule['name'],
'proc' => $rule['proc'],
'rules' => $rule_split,
);
header('Content-type: application/json');
echo _json_encode($output);
'builder' => $builder,
'severity' => $rule['severity'],
]);
}