From ba99eb25a2ecc2926e9b4dad3d94734dc48cef1d Mon Sep 17 00:00:00 2001 From: f0o Date: Fri, 3 Apr 2015 18:22:29 +0000 Subject: [PATCH] Device-Groups Draft --- html/ajax_search.php | 17 +- html/forms/create-alert-item.inc.php | 9 + html/forms/create-device-group.inc.php | 52 +++++ html/forms/create-map-item.inc.php | 59 ++++++ html/forms/delete-alert-map.inc.php | 31 +++ html/forms/delete-device-group.inc.php | 31 +++ html/forms/parse-alert-map.inc.php | 30 +++ html/forms/parse-device-group.inc.php | 28 +++ html/includes/modal/delete_alert_map.inc.php | 70 +++++++ .../modal/delete_device_group.inc.php | 70 +++++++ html/includes/modal/new_alert_map.inc.php | 127 ++++++++++++ html/includes/modal/new_alert_rule.inc.php | 50 +++++ html/includes/modal/new_device_group.inc.php | 189 ++++++++++++++++++ html/includes/print-menubar.php | 9 + html/pages/alert-map.inc.php | 30 +++ html/pages/device-groups.inc.php | 25 +++ html/pages/devices.inc.php | 10 + includes/alerts.inc.php | 19 +- includes/device-groups.inc.php | 95 +++++++++ sql-schema/045.sql | 3 + 20 files changed, 952 insertions(+), 2 deletions(-) create mode 100644 html/forms/create-device-group.inc.php create mode 100644 html/forms/create-map-item.inc.php create mode 100644 html/forms/delete-alert-map.inc.php create mode 100644 html/forms/delete-device-group.inc.php create mode 100644 html/forms/parse-alert-map.inc.php create mode 100644 html/forms/parse-device-group.inc.php create mode 100644 html/includes/modal/delete_alert_map.inc.php create mode 100644 html/includes/modal/delete_device_group.inc.php create mode 100644 html/includes/modal/new_alert_map.inc.php create mode 100644 html/includes/modal/new_device_group.inc.php create mode 100644 html/pages/alert-map.inc.php create mode 100644 html/pages/device-groups.inc.php create mode 100644 includes/device-groups.inc.php create mode 100644 sql-schema/045.sql diff --git a/html/ajax_search.php b/html/ajax_search.php index d9d3925507..d6aa5fe2c6 100755 --- a/html/ajax_search.php +++ b/html/ajax_search.php @@ -29,7 +29,22 @@ if (isset($_REQUEST['search'])) { $found = 0; - if($_REQUEST['type'] == 'device') { + if( $_REQUEST['type'] == 'group' ) { + include_once('../includes/device-groups.inc.php'); + foreach( dbFetchRows("SELECT name FROM device_groups WHERE name LIKE '%".$search."%'") as $group ) { + if( $_REQUEST['map'] ) { + $results[] = array('name'=>'g:'.$group['name']); + } else { + $results[] = array('name'=>$group['name']); + } + } + die(json_encode($results)); + } elseif( $_REQUEST['type'] == 'alert-rules' ) { + foreach( dbFetchRows("SELECT name FROM alert_rules WHERE name LIKE '%".$search."%'") as $rules ) { + $results[] = array('name'=>$rules['name']); + } + die(json_encode($results)); + } elseif($_REQUEST['type'] == 'device') { // Device search $results = dbFetchRows("SELECT * FROM `devices` WHERE `hostname` LIKE '%" . $search . "%' OR `location` LIKE '%" . $search . "%' ORDER BY hostname LIMIT 8"); diff --git a/html/forms/create-alert-item.inc.php b/html/forms/create-alert-item.inc.php index 9efdef119d..7940fc4c28 100644 --- a/html/forms/create-alert-item.inc.php +++ b/html/forms/create-alert-item.inc.php @@ -55,6 +55,15 @@ if(empty($rule)) { } else { if( dbInsert(array('device_id'=>$device_id,'rule'=>$rule,'severity'=>mres($_POST['severity']),'extra'=>$extra_json,'name'=>$name),'alert_rules') ) { $update_message = "Added Rule: $name: $rule"; + if( is_array($_POST['maps']) ) { + foreach( $_POST['maps'] as $target ) { + $_POST['rule'] = $name; + $_POST['target'] = $target; + $_POST['map_id'] = ''; + include('forms/create-map-item.inc.php'); + unset($ret,$target,$raw,$rule,$msg,$map_id); + } + } } else { $update_message = "ERROR: Failed to add Rule: ".$rule.""; } diff --git a/html/forms/create-device-group.inc.php b/html/forms/create-device-group.inc.php new file mode 100644 index 0000000000..94c410c9e9 --- /dev/null +++ b/html/forms/create-device-group.inc.php @@ -0,0 +1,52 @@ + + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. Please see LICENSE.txt at the top level of + * the source code distribution for details. + */ + +if(is_admin() === false) { + die('ERROR: You need to be admin'); +} + +$pattern = $_POST['patterns']; +$group_id = $_POST['group_id']; +$name = mres($_POST['name']); +$desc = mres($_POST['desc']); + +if( is_array($pattern) ) { + $pattern = implode(" ", $pattern); + $pattern = rtrim($pattern,'&&'); + $pattern = rtrim($pattern,'||'); +} elseif( !empty($_POST['pattern']) && !empty($_POST['condition']) && !empty($_POST['value']) ) { + $pattern = '%'.$_POST['pattern'].' '.$_POST['condition'].' '; + if( is_numeric($_POST['value']) ) { + $pattern .= $_POST['value']; + } else { + $pattern .= '"'.$_POST['value'].'"'; + } +} + +if(empty($pattern)) { + $update_message = "ERROR: No group was generated"; +} elseif(is_numeric($group_id) && $group_id > 0) { + if(dbUpdate(array('pattern' => $pattern,'name'=>$name,'desc'=>$desc), 'device_groups', 'id=?',array($group_id)) >= 0) { + $update_message = "Edited Group: $name: $pattern"; + } else { + $update_message = "ERROR: Failed to edit Group: ".$pattern.""; + } +} else { + if( dbInsert(array('pattern'=>$pattern,'name'=>$name,'desc'=>$desc),'device_groups') ) { + $update_message = "Added Group: $name: $pattern"; + } else { + $update_message = "ERROR: Failed to add Group: ".$pattern.""; + } +} +echo $update_message; diff --git a/html/forms/create-map-item.inc.php b/html/forms/create-map-item.inc.php new file mode 100644 index 0000000000..d05f0f0dd7 --- /dev/null +++ b/html/forms/create-map-item.inc.php @@ -0,0 +1,59 @@ + + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. Please see LICENSE.txt at the top level of + * the source code distribution for details. + */ + +if(is_admin() === false) { + die('ERROR: You need to be admin'); +} + +$rule = mres($_POST['rule']); +$target = mres($_POST['target']); +$map_id = mres($_POST['map_id']); +$ret = array(); + +if( empty($rule) || empty($target) ) { + $ret[] = "ERROR: No map was generated"; +} else { + $raw = $rule; + $rule = dbFetchCell('SELECT id FROM alert_rules WHERE name = ?',array($rule)); + if( !is_numeric($target) && $target[0] != "g" ) { + array_unshift($ret, "ERROR: Could not find rule for '".$raw."'"); + } else { + $raw = $target; + if( $target[0].$target[1] == "g:" ) { + $target = "g".dbFetchCell('SELECT id FROM device_groups WHERE name = ?',array(substr($target,2))); + } else { + $target = dbFetchCell('SELECT device_id FROM devices WHERE hostname = ?',array($target)); + } + if( !is_numeric($target) && $target[0] != "g" ) { + array_unshift($ret, "ERROR: Could not find entry for '".$raw."'"); + } else { + if(is_numeric($map_id) && $map_id > 0) { + if(dbUpdate(array('rule' => $rule,'target'=>$target), 'alert_map', 'id=?',array($map_id)) >= 0) { + $ret[] = "Edited Map: ".$map_id.": ".$rule." = ".$target.""; + } else { + array_unshift($ret,"ERROR: Failed to edit Map: ".$map_id.": ".$rule." = ".$target.""); + } + } else { + if( dbInsert(array('rule'=>$rule,'target'=>$target),'alert_map') ) { + $ret[] = "Added Map: ".$rule." = ".$target.""; + } else { + array_unshift($ret,"ERROR: Failed to add Map: ".$rule." = ".$target.""); + } + } + } + } +} +foreach( $ret as $msg ) { + echo $msg."
"; +} diff --git a/html/forms/delete-alert-map.inc.php b/html/forms/delete-alert-map.inc.php new file mode 100644 index 0000000000..4136fe983b --- /dev/null +++ b/html/forms/delete-alert-map.inc.php @@ -0,0 +1,31 @@ + + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. Please see LICENSE.txt at the top level of + * the source code distribution for details. + */ + +if(is_admin() === false) { + die('ERROR: You need to be admin'); +} + +if(!is_numeric($_POST['map_id'])) { + echo('ERROR: No map selected'); + exit; +} else { + if(dbDelete('alert_map', "`id` = ?", array($_POST['map_id']))) { + echo('Map has been deleted.'); + exit; + } else { + echo('ERROR: Map has not been deleted.'); + exit; + } +} + diff --git a/html/forms/delete-device-group.inc.php b/html/forms/delete-device-group.inc.php new file mode 100644 index 0000000000..a81f10d2f6 --- /dev/null +++ b/html/forms/delete-device-group.inc.php @@ -0,0 +1,31 @@ + + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. Please see LICENSE.txt at the top level of + * the source code distribution for details. + */ + +if(is_admin() === false) { + die('ERROR: You need to be admin'); +} + +if(!is_numeric($_POST['group_id'])) { + echo('ERROR: No group selected'); + exit; +} else { + if(dbDelete('device_groups', "`id` = ?", array($_POST['group_id']))) { + echo('group has been deleted.'); + exit; + } else { + echo('ERROR: group has not been deleted.'); + exit; + } +} + diff --git a/html/forms/parse-alert-map.inc.php b/html/forms/parse-alert-map.inc.php new file mode 100644 index 0000000000..393b1dc68b --- /dev/null +++ b/html/forms/parse-alert-map.inc.php @@ -0,0 +1,30 @@ + + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. Please see LICENSE.txt at the top level of + * the source code distribution for details. + */ + +if(is_admin() === false) { + die('ERROR: You need to be admin'); +} + +$map_id = $_POST['map_id']; + +if(is_numeric($map_id) && $map_id > 0) { + $map = dbFetchRow("SELECT alert_rules.name,alert_map.target FROM alert_map,alert_rules WHERE alert_map.rule=alert_rules.id && alert_map.id = ?",array($map_id)); + if( $map['target'][0] == "g" ) { + $map['target'] = 'g:'.dbFetchCell("SELECT name FROM device_groups WHERE id = ?",array(substr($map['target'],1))); + } else { + $map['target'] = dbFetchCell("SELECT hostname FROM devices WHERE device_id = ?",array($map['target'])); + } + $output = array('rule'=>$map['name'],'target'=>$map['target']); + echo _json_encode($output); +} diff --git a/html/forms/parse-device-group.inc.php b/html/forms/parse-device-group.inc.php new file mode 100644 index 0000000000..7009459f26 --- /dev/null +++ b/html/forms/parse-device-group.inc.php @@ -0,0 +1,28 @@ + + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. Please see LICENSE.txt at the top level of + * the source code distribution for details. + */ + +if(is_admin() === false) { + die('ERROR: You need to be admin'); +} + +$group_id = $_POST['group_id']; + +if(is_numeric($group_id) && $group_id > 0) { + $group = dbFetchRow("SELECT * FROM `device_groups` WHERE `id` = ? LIMIT 1",array($group_id)); + $group_split = preg_split('/([a-zA-Z0-9_\-\.\=\%\<\>\ \"\'\!\~\(\)\*\/\@]+[&&\|\|]+)/',$group['pattern'], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); + $count = count($group_split) - 1; + $group_split[$count] = $group_split[$count].' &&'; + $output = array('name'=>$group['name'],'desc'=>$group['desc'],'pattern'=>$group_split); + echo _json_encode($output); +} diff --git a/html/includes/modal/delete_alert_map.inc.php b/html/includes/modal/delete_alert_map.inc.php new file mode 100644 index 0000000000..2656fd4cbe --- /dev/null +++ b/html/includes/modal/delete_alert_map.inc.php @@ -0,0 +1,70 @@ + + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. Please see LICENSE.txt at the top level of + * the source code distribution for details. + */ + +if(is_admin() === false) { + die('ERROR: You need to be admin'); +} + +?> + + + + diff --git a/html/includes/modal/delete_device_group.inc.php b/html/includes/modal/delete_device_group.inc.php new file mode 100644 index 0000000000..105442935d --- /dev/null +++ b/html/includes/modal/delete_device_group.inc.php @@ -0,0 +1,70 @@ + + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. Please see LICENSE.txt at the top level of + * the source code distribution for details. + */ + +if(is_admin() === false) { + die('ERROR: You need to be admin'); +} + +?> + + + + diff --git a/html/includes/modal/new_alert_map.inc.php b/html/includes/modal/new_alert_map.inc.php new file mode 100644 index 0000000000..09b0795ca5 --- /dev/null +++ b/html/includes/modal/new_alert_map.inc.php @@ -0,0 +1,127 @@ + + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. Please see LICENSE.txt at the top level of + * the source code distribution for details. + */ + +if(is_admin() !== false) { + +?> + + + + + + diff --git a/html/includes/modal/new_alert_rule.inc.php b/html/includes/modal/new_alert_rule.inc.php index 861ba72453..d106f4ec3e 100644 --- a/html/includes/modal/new_alert_rule.inc.php +++ b/html/includes/modal/new_alert_rule.inc.php @@ -108,6 +108,22 @@ if(is_admin() !== false) { +
+
+ +
+ +
+
+ +
+
+
+
+ +
+
+
@@ -126,6 +142,12 @@ $("[name='invert']").bootstrapSwitch('offColor','danger'); $('#create-alert').on('hide.bs.modal', function (event) { $('#response').data('tagmanager').empty(); + $('#map-tags').data('tagmanager').empty(); +}); + +$('#add-map').click('',function (event) { + $('#map-tags').data('tagmanager').populate([ $('#map-stub').val() ]); + $('#map-stub').val(''); }); $('#create-alert').on('show.bs.modal', function (event) { @@ -140,6 +162,16 @@ $('#create-alert').on('show.bs.modal', function (event) { strategy: 'array', tagFieldName: 'rules[]' }); + $('#map-tags').tagmanager({ + strategy: 'array', + tagFieldName: 'maps[]', + initialCap: false + }); + if( $('#alert_id').val() == '' ) { + $('#preseed-maps').show(); + } else { + $('#preseed-maps').hide(); + } $.ajax({ type: "POST", url: "/ajax_form.php", @@ -183,6 +215,24 @@ $('#suggest').typeahead([ engine: Hogan } ]); +$('#map-stub').typeahead([ + { + name: 'map_devices', + remote : '/ajax_search.php?search=%QUERY&type=device&map=1', + header : '
 Devices
', + template: '{{name}}', + valueKey:"name", + engine: Hogan + }, + { + name: 'map_groups', + remote : '/ajax_search.php?search=%QUERY&type=group&map=1', + header : '
 Groups
', + template: '{{name}}', + valueKey:"name", + engine: Hogan + } +]); $('#and, #or').click('', function(e) { e.preventDefault(); diff --git a/html/includes/modal/new_device_group.inc.php b/html/includes/modal/new_device_group.inc.php new file mode 100644 index 0000000000..8e8814541f --- /dev/null +++ b/html/includes/modal/new_device_group.inc.php @@ -0,0 +1,189 @@ + + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. Please see LICENSE.txt at the top level of + * the source code distribution for details. + */ + +if(is_admin() !== false) { + +?> + + + + + + diff --git a/html/includes/print-menubar.php b/html/includes/print-menubar.php index b43c4e1b98..37e0850416 100644 --- a/html/includes/print-menubar.php +++ b/html/includes/print-menubar.php @@ -60,6 +60,7 @@ if (isset($config['site_style']) && ($config['site_style'] == 'dark' || $config[ if ($_SESSION['userlevel'] >= '10') { ?>
  • Alert Rules
  • +
  • Alert Map
  • Alert Templates
  • ' . ucfirst($devtype['type']) . ''); } + +require_once('../includes/device-groups.inc.php'); +foreach( GetDeviceGroups() as $group ) { + echo '
  • '.ucfirst($group['name']).'
  • '; +} +unset($group); + echo (' '); @@ -125,6 +133,7 @@ if ($config['show_locations']) } echo(' +
  • Manage Groups
  • Add Device
  • Delete Device
  • '); } diff --git a/html/pages/alert-map.inc.php b/html/pages/alert-map.inc.php new file mode 100644 index 0000000000..87b4bc58fe --- /dev/null +++ b/html/pages/alert-map.inc.php @@ -0,0 +1,30 @@ +
    '; +echo '
    '; +echo ''; +echo ''; +echo ''; +foreach( dbFetchRows('SELECT alert_map.target,alert_map.id,alert_rules.name FROM alert_map,alert_rules WHERE alert_map.rule=alert_rules.id ORDER BY alert_map.rule ASC') as $link ) { + if( $link['target'][0] == "g" ) { + $link['target'] = substr($link['target'],1); + $link['target'] = ''.ucfirst(dbFetchCell('SELECT name FROM device_groups WHERE id = ?',array($link['target']))).''; + } elseif( is_numeric($link['target']) ) { + $link['target'] = ''.dbFetchCell('SELECT hostname FROM devices WHERE device_id = ?',array($link['target'])).''; + } + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; +} +echo '
    RuleTargetActions
    '.$link['name'].''.$link['target'].''; + echo " "; + echo ""; + echo '
    '; +echo " "; +?> diff --git a/html/pages/device-groups.inc.php b/html/pages/device-groups.inc.php new file mode 100644 index 0000000000..344bf48406 --- /dev/null +++ b/html/pages/device-groups.inc.php @@ -0,0 +1,25 @@ +
    '; +echo '
    '; +echo ''; +echo ''; +echo ''; +foreach( GetDeviceGroups() as $group ) { + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; +} +echo '
    NameDescriptionPatternActions
    '.$group['name'].''.$group['desc'].''.$group['pattern'].''; + echo " "; + echo ""; + echo '
    '; +echo " "; +?> diff --git a/html/pages/devices.inc.php b/html/pages/devices.inc.php index 3e35848d0f..38528cc2f1 100644 --- a/html/pages/devices.inc.php +++ b/html/pages/devices.inc.php @@ -37,6 +37,16 @@ if (!empty($vars['disabled'])) { $where .= " AND disabled= ?"; $sql_param[] if (!empty($vars['ignore'])) { $where .= " AND `ignore`= ?"; $sql_param[] = $vars['ignore']; } if (!empty($vars['location']) && $vars['location'] == "Unset") { $location_filter = ''; } if (!empty($vars['location'])) { $location_filter = $vars['location']; } +if( !empty($vars['group']) ) { + require_once('../includes/device-groups.inc.php'); + $where .= " AND ( "; + foreach( GetDevicesFromGroup($vars['group']) as $dev ) { + $where .= "device_id = ? OR "; + $sql_param[] = $dev['device_id']; + } + $where = substr($where, 0, strlen($where)-3); + $where .= " )"; +} $pagetitle[] = "Devices"; diff --git a/includes/alerts.inc.php b/includes/alerts.inc.php index b98f4e112b..accdd09a96 100644 --- a/includes/alerts.inc.php +++ b/includes/alerts.inc.php @@ -22,6 +22,8 @@ * @subpackage Alerts */ +include_once('includes/device-groups.inc.php'); + /** * Generate SQL from Rule * @param string $rule Rule to generate SQL for @@ -52,6 +54,21 @@ function GenSQL($rule) { return $sql; } +/** + * Get Alert-Rules for Devices + * @param int $device Device-ID + * @return array + */ +function GetRules($device) { + $groups = GetGroupsFromDevice($device); + $params = array($device,$device); + $where = ""; + foreach( $groups as $group ) { + $where .= " || alert_map.target = ?"; + $params[] = 'g'.$group; + } + return dbFetchRows('SELECT alert_rules.* FROM alert_rules,alert_map WHERE (alert_rules.device_id = -1 || alert_rules.device_id = ? ) || ( alert_rules.id=alert_map.rule && ( alert_map.target = ? '.$where.' ) )',$params); +} /** * Run all rules for a device @@ -64,7 +81,7 @@ function RunRules($device) { if( $chk['id'] > 0 ) { return false; } - foreach( dbFetchRows("SELECT * FROM alert_rules WHERE alert_rules.disabled = 0 && ( alert_rules.device_id = -1 || alert_rules.device_id = ? ) ORDER BY device_id,id",array($device)) as $rule ) { + foreach( GetRules($device) as $rule ) { echo " #".$rule['id'].":"; $inv = json_decode($rule['extra'],true); if( isset($inv['invert']) ) { diff --git a/includes/device-groups.inc.php b/includes/device-groups.inc.php new file mode 100644 index 0000000000..748efd2ca2 --- /dev/null +++ b/includes/device-groups.inc.php @@ -0,0 +1,95 @@ + + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + +/** + * Device-Grouping + * @author Daniel Preussker + * @copyright 2015 f0o, LibreNMS + * @license GPL + * @package LibreNMS + * @subpackage Devices + */ + +/** + * Generate SQL from Group-Pattern + * @param string $pattern Pattern to generate SQL for + * @return string + */ +function GenGroupSQL($pattern) { + $tmp = explode(" ",$pattern); + $tables = array(); + foreach( $tmp as $opt ) { + if( strstr($opt,'%') && strstr($opt,'.') ) { + $tmpp = explode(".",$opt,2); + $tmpp[0] = str_replace("%","",$tmpp[0]); + $tables[] = mres(str_replace("(","",$tmpp[0])); + $pattern = str_replace($opt,$tmpp[0].'.'.$tmpp[1],$pattern); + } + } + $tables = array_keys(array_flip($tables)); + $x = sizeof($tables); + $i = 0; + $join = ""; + while( $i < $x ) { + if( isset($tables[$i+1]) ) { + $join .= $tables[$i].".device_id = ".$tables[$i+1].".device_id && "; + } + $i++; + } + $sql = "SELECT ".str_replace("(","",$tables[0]).".device_id FROM ".implode(",",$tables)." WHERE (".str_replace(array("%","@","!~","~"),array("","%","NOT LIKE","LIKE"),$pattern).")"; + return $sql; +} + +/** + * Get all devices of Group + * @param int $group_id Group-ID + * @return string + */ +function GetDevicesFromGroup($group_id) { + $pattern = dbFetchCell("SELECT pattern FROM device_groups WHERE id = ?",array($group_id)); + if( !empty($pattern) ) { + return dbFetchRows(GenGroupSQL($pattern)); + } + return false; +} + +/** + * Get all Device-Groups + * @return array + */ +function GetDeviceGroups() { + return dbFetchRows("SELECT * FROM device_groups"); +} + +/** + * Get all groups of Device + * @param int $device Device-ID + * @return array + */ +function GetGroupsFromDevice($device) { + $ret = array(); + foreach( GetDeviceGroups() as $group ) { + foreach( GetDevicesFromGroup($group['id']) as $dev ) { + if( $dev['device_id'] == $device ) { + if( !in_array($group['id'],$ret) ) { + $ret[] = $group['id']; + } + continue 2; + } + } + } + return $ret; +} +?> diff --git a/sql-schema/045.sql b/sql-schema/045.sql new file mode 100644 index 0000000000..890747e43c --- /dev/null +++ b/sql-schema/045.sql @@ -0,0 +1,3 @@ +CREATE TABLE IF NOT EXISTS `device_groups` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL DEFAULT '', `desc` varchar(255) NOT NULL DEFAULT '', `pattern` varchar(255) NOT NULL DEFAULT '', PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`)) ENGINE=InnoDB DEFAULT; +CREATE TABLE IF NOT EXISTS `alert_map` ( `id` int(11) NOT NULL AUTO_INCREMENT, `rule` int(11) NOT NULL DEFAULT '0', `target` varchar(255) CHARACTER SET utf8 NOT NULL DEFAULT '', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT; +ALTER TABLE `alert_rules` ADD UNIQUE (`name`);