2015-04-03 18:22:29 +00:00
< ? php
2015-07-13 20:10:26 +02:00
/*
* Copyright ( C ) 2015 Daniel Preussker < f0o @ devilcode . org >
2015-04-03 18:22:29 +00:00
* 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 .
2015-07-13 20:10:26 +02:00
*
2015-04-03 18:22:29 +00:00
* 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 .
2015-07-13 20:10:26 +02:00
*
2015-04-03 18:22:29 +00:00
* You should have received a copy of the GNU General Public License
2015-07-13 20:10:26 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
*/
2015-04-03 18:22:29 +00:00
/**
* Device - Grouping
* @ author Daniel Preussker < f0o @ devilcode . org >
* @ copyright 2015 f0o , LibreNMS
* @ license GPL
* @ package LibreNMS
* @ subpackage Devices
*/
2015-07-13 20:10:26 +02:00
2015-04-03 18:22:29 +00:00
/**
* Generate SQL from Group - Pattern
* @ param string $pattern Pattern to generate SQL for
2015-07-13 20:10:26 +02:00
* @ param string $search What to searchid for
2015-04-03 18:22:29 +00:00
* @ return string
*/
2015-07-13 20:10:26 +02:00
function GenGroupSQL ( $pattern , $search = '' ) {
2015-07-15 19:55:57 +00:00
$pattern = RunGroupMacros ( $pattern );
if ( $pattern === false ) {
return false ;
}
2015-07-13 20:10:26 +02:00
$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 );
}
}
2015-11-19 08:40:49 +00:00
$pattern = rtrim ( $pattern , '&&' );
$pattern = rtrim ( $pattern , '||' );
2015-07-13 20:10:26 +02:00
$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 ++ ;
}
if ( ! empty ( $search )) {
$search .= ' &&' ;
}
2015-12-12 13:47:44 +00:00
$sql = 'SELECT DISTINCT(' . str_replace ( '(' , '' , $tables [ 0 ]) . '.device_id),`devices`.* FROM ' . implode ( ',' , $tables ) . ' WHERE ' . $search . ' (' . str_replace ( array ( '%' , '@' , '!~' , '~' ), array ( '' , '.*' , 'NOT REGEXP' , 'REGEXP' ), $pattern ) . ')' ;
2015-07-13 20:10:26 +02:00
return $sql ;
} //end GenGroupSQL()
2015-04-03 18:22:29 +00:00
/**
* Get all devices of Group
2015-07-13 20:10:26 +02:00
* @ param integer $group_id Group - ID
2015-04-03 18:22:29 +00:00
* @ return string
*/
function GetDevicesFromGroup ( $group_id ) {
2015-07-13 20:10:26 +02:00
$pattern = dbFetchCell ( 'SELECT pattern FROM device_groups WHERE id = ?' , array ( $group_id ));
$pattern = rtrim ( $pattern , '&&' );
$pattern = rtrim ( $pattern , '||' );
if ( ! empty ( $pattern )) {
return dbFetchRows ( GenGroupSQL ( $pattern ));
}
return false ;
} //end GetDevicesFromGroup()
2015-04-03 18:22:29 +00:00
/**
* Get all Device - Groups
* @ return array
*/
function GetDeviceGroups () {
2015-08-03 15:18:21 -08:00
return dbFetchRows ( 'SELECT * FROM device_groups ORDER BY name' );
2015-07-13 20:10:26 +02:00
} //end GetDeviceGroups()
2015-04-03 18:22:29 +00:00
/**
* Get all groups of Device
2015-07-13 20:10:26 +02:00
* @ param integer $device Device - ID
2015-04-03 18:22:29 +00:00
* @ return array
*/
function GetGroupsFromDevice ( $device ) {
2015-07-13 20:10:26 +02:00
$ret = array ();
foreach ( GetDeviceGroups () as $group ) {
if ( dbFetchCell ( GenGroupSQL ( $group [ 'pattern' ], 'device_id=?' ) . ' LIMIT 1' , array ( $device )) == $device ) {
$ret [] = $group [ 'id' ];
}
}
return $ret ;
} //end GetGroupsFromDevice()
2015-07-15 19:55:57 +00:00
2015-12-12 12:58:07 +00:00
/**
* Get all groups of Device
* @ param integer $device Device - ID
* @ return array
*/
function GetFullGroupsFromDevice ( $device ) {
$ret = array ();
foreach ( GetDeviceGroups () as $group ) {
if ( dbFetchCell ( GenGroupSQL ( $group [ 'pattern' ], 'device_id=?' ) . ' LIMIT 1' , array ( $device )) == $device ) {
$ret [] = $group ;
}
}
return $ret ;
} //end GetGroupsFromDevice()
2015-07-15 19:55:57 +00:00
/**
* Process Macros
* @ param string $rule Rule to process
* @ param int $x Recursion - Anchor
* @ return string | boolean
*/
function RunGroupMacros ( $rule , $x = 1 ) {
global $config ;
krsort ( $config [ 'alert' ][ 'macros' ][ 'group' ]);
foreach ( $config [ 'alert' ][ 'macros' ][ 'group' ] as $macro => $value ) {
if ( ! strstr ( $macro , " " ) ) {
$rule = str_replace ( '%macros.' . $macro , '(' . $value . ')' , $rule );
}
}
if ( strstr ( $rule , " %macros " ) ) {
if ( ++ $x < 30 ) {
$rule = RunGroupMacros ( $rule , $x );
} else {
return false ;
}
}
return $rule ;
} //end RunGroupMacros()