From b4644e8d2e2f4765572aaa4afc8e206f0f7319ae Mon Sep 17 00:00:00 2001 From: github138 Date: Mon, 8 Aug 2016 09:58:59 +0200 Subject: [PATCH] add SNMP Profiles create, edit and delete profiles on the configuration page ( SNMP Profiles ). Assign profiles on the objects "SNMP sync" page. Old SNMP setups are still available. * getSNMPProfileList (), getSNMPProfile (), getObjectSNMPProfile (), setObjectSNMPProfile (), renderSNMPProfiles (), renderSNMPProfilesEditor (), renderSNMPProfilesEditProfile (), renderSNMPSetProfileForm (), printSNMPProfileForm (), updateObjectAttributes (), updateSNMPProfile (), deleteSNMPProfile (), setSNMPProfile (): add new functions * doSNMPmining (), querySNMPData (), renderSNMPPortFinder (): add SNMP Profiles * get_pseudo_file (): add new tables --- wwwroot/inc/database.php | 63 ++++++++ wwwroot/inc/install.php | 24 +++ wwwroot/inc/interface-config.php | 51 +++++++ wwwroot/inc/interface-lib.php | 249 +++++++++++++++++++++++++++++++ wwwroot/inc/interface.php | 12 ++ wwwroot/inc/navigation.php | 10 ++ wwwroot/inc/ophandlers.php | 122 +++++++++++++++ wwwroot/inc/snmp.php | 24 +-- 8 files changed, 546 insertions(+), 9 deletions(-) diff --git a/wwwroot/inc/database.php b/wwwroot/inc/database.php index e4d74ef5f..34ae37178 100644 --- a/wwwroot/inc/database.php +++ b/wwwroot/inc/database.php @@ -6060,4 +6060,67 @@ function releaseDBMutex ($name) return $row === '1'; } +function getSNMPProfile ($id = NULL) +{ + $query = 'SELECT id, name, version as ver, community, sec_name, + sec_level, auth_protocol, auth_passphrase, + priv_protocol, priv_passphrase, + contextname, contextengineid, + (SELECT count(SNMPProfileMapping.profile_id) FROM SNMPProfileMapping WHERE SNMPProfileMapping.profile_id = SNMPProfile.id) as refs + FROM SNMPProfile'; + + $params = array(); + if ($id !== NULL) + { + $query .= ' WHERE id = ?'; + $params[] = $id; + } + + $result = usePreparedSelectBlade ($query, $params); + + if ($id !== NULL) + return $result->fetch (PDO::FETCH_ASSOC); + else + return reindexById ($result->fetchAll (PDO::FETCH_ASSOC)); +} + +function getObjectSNMPProfile ($object_id) +{ + $result = usePreparedSelectBlade + ( + 'SELECT profile_id as id, host + FROM SNMPProfileMapping + WHERE object_id = ?', + array ($object_id) + ); + $ret = $result->fetch (PDO::FETCH_ASSOC); + + if ($ret === FALSE) + return FALSE; + + $profile = getSNMPProfile ($ret['id']); + + if (! $profile) + return FALSE; + + return array_merge ($ret, $profile); +} + +function setObjectSNMPProfile ($object_id, $profile_id, $host = NULL) +{ + if ($profile_id == 0) + { + usePreparedDeleteBlade ('SNMPProfileMapping', array ('object_id' => $object_id)); + return -1; + } + else + return usePreparedExecuteBlade + ( + 'INSERT INTO SNMPProfileMapping(object_id, profile_id, host) + VALUES(?, ?, ?) + ON DUPLICATE KEY UPDATE profile_id = ?, host = ?', + array ($object_id, $profile_id, $host, $profile_id, $host) + ); +} + ?> diff --git a/wwwroot/inc/install.php b/wwwroot/inc/install.php index dc102b4e3..b05c9af75 100644 --- a/wwwroot/inc/install.php +++ b/wwwroot/inc/install.php @@ -1262,6 +1262,30 @@ function get_pseudo_file ($name) CONSTRAINT `VSEnabledPorts-FK-vs_id-proto-vport` FOREIGN KEY (`vs_id`, `proto`, `vport`) REFERENCES `VSPorts` (`vs_id`, `proto`, `vport`) ON DELETE CASCADE ) ENGINE=InnoDB"; + $query[] = "CREATE TABLE `SNMPProfile` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `name` char(255) DEFAULT NULL, + `version` tinyint DEFAULT NULL, + `community` char(255) DEFAULT NULL, + `sec_name` char(255) DEFAULT NULL, + `sec_level` char(12) DEFAULT NULL, + `auth_protocol` char(3) DEFAULT NULL, + `auth_passphrase` char(255) DEFAULT NULL, + `priv_protocol` char(3) DEFAULT NULL, + `priv_passphrase` char(255) DEFAULT NULL, + `contextname` char(255) DEFAULT NULL, + `contextengineid` char(255) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB"; + + $query[] = "CREATE TABLE `SNMPProfileMapping` ( + `object_id` int(10) unsigned not NULL, + `profile_id` int(10) unsigned not NULL, + `host` char(255) DEFAULT NULL, + PRIMARY KEY (`object_id`), + CONSTRAINT `SNMPProfileMapping_object_id` FOREIGN KEY (`object_id`) REFERENCES `Object` (`id`) ON DELETE CASCADE +) ENGINE=InnoDB"; + $query[] = " CREATE TRIGGER `EntityLink-before-insert` BEFORE INSERT ON `EntityLink` FOR EACH ROW EntityLinkTrigger:BEGIN diff --git a/wwwroot/inc/interface-config.php b/wwwroot/inc/interface-config.php index d7043f1a3..ad027fea8 100644 --- a/wwwroot/inc/interface-config.php +++ b/wwwroot/inc/interface-config.php @@ -1165,4 +1165,55 @@ function printNewItemTR() echo ''; } +function renderSNMPProfiles () +{ + startPortlet ("SNMP Profiles"); + + $cols = array ( + array ('row_key' => 'id', 'th_text' => 'Id'), + array ('row_key' => 'name', 'th_text' => 'Name'), + array ('row_key' => 'ver', 'th_text' => 'Version'), + array ('row_key' => 'community', 'th_text' => 'Community'), + array ('row_key' => 'sec_name', 'th_text' => 'Security Name'), + array ('row_key' => 'sec_level', 'th_text' => 'Security Level'), + array ('row_key' => 'auth_protocol', 'th_text' => 'Auth Protocol'), + array ('row_key' => 'priv_protocol', 'th_text' => 'Priv Protocol') + ); + + renderTableViewer ($cols, getSNMPProfile ()); + + finishPortlet (); +} + +function renderSNMPProfilesEditor () +{ + startPortlet ("SNMP Profiles"); + + addSNMPProfileFormJS (TRUE); + + echo ''; + echo ' + + + + + + + + + + + '; + + echo ''; + printSNMPProfileFormFields (array ('id' => 'new'), FALSE, FALSE); + foreach (getSNMPProfile () as $profile) + { + printSNMPProfileFormFields ($profile, FALSE, FALSE); + } + echo '
IdNameVersionCommunity/
Security Name
Security LevelAuth
Protocol
Auth
Passphrase
Priv
Protocol
Priv
Passphrase
'; + + finishPortlet (); +} + ?> diff --git a/wwwroot/inc/interface-lib.php b/wwwroot/inc/interface-lib.php index 816f95b0d..a7f7626e4 100644 --- a/wwwroot/inc/interface-lib.php +++ b/wwwroot/inc/interface-lib.php @@ -1207,4 +1207,253 @@ function showMySQLWarnings() $rtdebug_mysql_warnings = array(); } +function renderSNMPSetProfileForm ($object_id) +{ + $object = spotEntity ('object', $object_id); + global $pageno, $tabno; + + echo '
'SNMPProfile', 'tab' => 'edit', 'op' => 'set')).'>'; + echo ""; + echo ""; + echo ""; + echo ''; + echo ''; + echo "
'; + + $profile = getObjectSNMPProfile ($object_id); + + $profiles = array(0 => '-- NOT SET --'); + foreach (getSNMPProfile () as $value) + $profiles[$value['id']] = $value['name']; + + echo "Name: "; + printSelect ($profiles, array('name' => 'profile_id'), $profile['id']); + + echo ''; + + $endpoints = array (); + foreach (findAllEndpoints ($object['id'], $object['name']) as $endpoint) + $endpoints[$endpoint] = $endpoint; + + foreach( getObjectIPv4Allocations ($object['id']) as $ip => $value) + { + $ip = ip_format ($ip); + if (! in_array ($ip, $endpoints)) + $endpoints[$ip] = $ip; + } + + foreach( getObjectIPv6Allocations ($object['id']) as $value) + { + $ip = ip_format (ip_parse ($value['addrinfo']['ip'])); + if (! in_array ($ip, $endpoints)) + $endpoints[$ip] = $ip; + } + + $host = $profile['host']; + $endpoints = array_merge (array ('FQDN' => 'FQDN'), $endpoints); + if (! in_array ($host, $endpoints)) + $endpoints = array_merge (array ($host => $host), $endpoints); + + echo " @Host "; + printSelect ($endpoints, array ('name' => 'host'), $host); + + echo ''.getImageHREF ('save', 'Save changes', TRUE).'
"; +} + +function addSNMPProfileFormJS ($disable = FALSE) +{ + addJS(<< ($newprofile ? 'new' : '0'), + 'name' => '', + 'ver' => 2, + 'community' => getConfigVar ('DEFAULT_SNMP_COMMUNITY') + ); + + if (! $table) + { + echo ""; + printOpFormIntro ('update'); + } + + echo ""; + + if ($table) echo ''; + + if (! $table) + { + if ($newprofile) + echo ''; + else + if ($profile['refs'] == 0) + echo ''; + else + echo ''; + + echo ''; + echo ""; + } + + if ($table) echo ''; + echo ''; + + if ($table) echo ' + + '; + + echo ''; + + if ($table) echo ' + '; + echo ''; + + if ($table) echo ''; + echo ''; + + if ($table) echo ''; + echo ''; + + if ($table) echo ''; + echo ''; + + + if ($table) echo ''; + echo ''; + if ($table) echo ''; + + if (! $table) + { + if ($newprofile) + echo ''; + else + echo ''; + echo ''; + } + else + echo '
'.getImageHREF ('create','add profile', TRUE).''.getOpLink (array ('op' => 'delete', 'id' => $profile['id']), '', 'destroy', 'Delete profile').''.getImageHREF ('nodestroy','profile used ('.$profile['refs'].')', FALSE).''.($newprofile ? '' : $profile['id']).'
'; + echo getSelect (array ('1' => 'v1', '2' => 'v2c', '3' => 'v3'), + array ('name' => 'ver', 'class' => 'snmpversion', 'pid' => $profile['id'], 'onchange' => 'showsnmpversion(this)'), + $profile['ver'], FALSE); + echo '
+ + +
'.getImageHREF ('create','add profile', TRUE).''.getImageHREF ('save','save profile', TRUE).'
'; +} + ?> diff --git a/wwwroot/inc/interface.php b/wwwroot/inc/interface.php index 0cbbb1667..dcbcbc3f4 100644 --- a/wwwroot/inc/interface.php +++ b/wwwroot/inc/interface.php @@ -4166,6 +4166,18 @@ function renderSNMPPortFinder ($object_id) echo "
The PHP SNMP extension is not loaded. Cannot continue.
"; return; } + + startPortlet ('SNMP Profile'); + renderSNMPSetProfileForm ($object_id); + $profile = getObjectSNMPProfile($object_id); + printOpFormIntro ('querySNMPData', array ('ver' => $profile['ver'])); + echo ""; + echo ""; + printSNMPProfileFormFields ($profile); + echo ''; + echo ""; + finishPortlet(); + if ('' == $snmpcomm = getConfigVar ('DEFAULT_SNMP_COMMUNITY')) $snmpcomm = 'public'; diff --git a/wwwroot/inc/navigation.php b/wwwroot/inc/navigation.php index e252a4ecb..a64e9760e 100644 --- a/wwwroot/inc/navigation.php +++ b/wwwroot/inc/navigation.php @@ -919,4 +919,14 @@ $popuphandler['portlist'] = 'renderPopupPortSelector'; $popuphandler['inet4list'] = 'renderPopupIPv4Selector'; +$page['SNMPProfile']['title'] = 'SNMP Profiles'; +$page['SNMPProfile']['parent'] = 'config'; +$tab['SNMPProfile']['default'] = 'SNMP Profiles'; +$tab['SNMPProfile']['edit'] = 'Edit SNMP Profiles'; +$tabhandler['SNMPProfile']['default'] = 'renderSNMPProfiles'; +$tabhandler['SNMPProfile']['edit'] = 'renderSNMPProfilesEditor'; +$ophandler['SNMPProfile']['edit']['update'] = 'updateSNMPProfile'; +$ophandler['SNMPProfile']['edit']['delete'] = 'deleteSNMPProfile'; +$ophandler['SNMPProfile']['edit']['set'] = 'setSNMPProfile'; +$interface_requires['SNMPProfile-*'] = 'interface-config.php'; ?> diff --git a/wwwroot/inc/ophandlers.php b/wwwroot/inc/ophandlers.php index 904c4e5e4..c04c17256 100644 --- a/wwwroot/inc/ophandlers.php +++ b/wwwroot/inc/ophandlers.php @@ -2588,6 +2588,15 @@ function querySNMPData () default: throw new InvalidRequestArgException ('ver', $ver); } + + if (isset ($_REQUEST['host'])) + { + assertStringArg ('host', TRUE); + $snmpsetup['host'] = $_REQUEST['host']; + } + else + $snmpsetup['host'] = 'FQDN'; + $snmpsetup['version'] = $ver; doSNMPmining (getBypassValue(), $snmpsetup); // shows message by itself } @@ -3833,4 +3842,117 @@ function updateVLANDomain() showSuccess ("VLAN domain updated successfully"); } +function updateSNMPProfile () +{ + $profile = array(); + + try + { + + $profile['name'] = genericAssertion ('name', 'string'); + + $version = genericAssertion ('ver', 'uint'); + + $profile['version'] = $version; + + switch ($version) + { + case 1: + case 2: + $profile['community'] = genericAssertion ('community', 'string'); + break; + case 3: + $profile['sec_name'] = genericAssertion ('sec_name', 'string'); + $sec_level = genericAssertion ('sec_level', 'string'); + $profile['sec_level'] = $sec_level; + switch ($sec_level) + { + case 'authPriv': + case 'authNoPriv': + $profile['auth_protocol'] = genericAssertion ('auth_protocol', 'string'); + $profile['auth_passphrase'] = genericAssertion ('auth_passphrase', 'string'); + + if ($sec_level == 'authNoPriv') + break; + + $profile['priv_protocol'] = genericAssertion ('priv_protocol', 'string'); + $profile['priv_passphrase'] = genericAssertion ('priv_passphrase', 'string'); + + if (isset ($_POST['contextname'])) + { + $profile['contextname'] = genericAssertion ('contextname', 'string'); + if (isset ($_POST['contextengineid'])) + $profile['contextengineid'] = genericAssertion ('contextengineid', 'string'); + } + + case 'noAuthNoPriv': + break; + } + break; + } + + if ($_POST['id'] == 'new') + { + usePreparedInsertBlade ('SNMPProfile', $profile); + //$id = lastInsertID (); + showSuccess ("Profile created successfully."); + } + else + { + $id = genericAssertion ('id', 'uint'); + //TODO reset unneeded values + usePreparedUpdateBlade ('SNMPProfile', $profile, array ('id' => $id), 'AND'); + showSuccess ("Profile update successfully"); + } + + } + catch (InvalidArgException $iae) + { + showError ($iae->getMessage()); + // TODO return to edit form without losing entered values + } + return buildRedirectURL (NULL, 'edit'); +} + +function deleteSNMPProfile () +{ + $id = genericAssertion ('id', 'uint'); + usePreparedDeleteBlade ('SNMPProfile', array ( 'id' => $id)); + showSuccess ("Deleted profile successfully."); + return buildRedirectURL (NULL, 'edit'); +} + +function setSNMPProfile () +{ + global $pageno, $tabno; + + $object_id = genericAssertion ('object_id', 'uint'); + $profile_id = genericAssertion ('profile_id', 'uint0'); + $host = genericAssertion ('host', 'string0'); + $pageno = genericAssertion ('returnpage', 'string0'); + $tabno = genericAssertion ('returntab', 'string0'); + + $rowcount = setObjectSNMPProfile ($object_id, $profile_id, $host); + + switch ($rowcount) + { + case -1: + showSuccess ("unset SNMP profile successfull"); + break; + case 0: + showWarning ("set SNMP profile no values changed"); + break; + case 1: + showSuccess ("set SNMP profile successfull"); + break; + case 2: + showSuccess ("update SNMP profile successfull"); + break; + default: + showError ("set SNMP profile error!!"); + } + + return buildRedirectURL ($pageno, $tabno, array ('object_id' => $object_id)); +} + ?> diff --git a/wwwroot/inc/snmp.php b/wwwroot/inc/snmp.php index 71a80b891..a6a7b6a78 100644 --- a/wwwroot/inc/snmp.php +++ b/wwwroot/inc/snmp.php @@ -4172,17 +4172,23 @@ function doSNMPmining ($object_id, $snmpsetup) { $objectInfo = spotEntity ('object', $object_id); $objectInfo['attrs'] = getAttrValues ($object_id); - $endpoints = findAllEndpoints ($object_id, $objectInfo['name']); - if (count ($endpoints) == 0) - { - showError ('Endpoint not found. Please either set FQDN attribute or assign an IP address to the object.'); - return; - } - if (count ($endpoints) > 1) + + if ($snmpsetup['host'] == 'FQDN' || $snmpsetup['host'] == '') { - showError ('More than one IP address is assigned to this object, please configure FQDN attribute.'); - return; + $endpoints = findAllEndpoints ($object_id, $objectInfo['name']); + if (count ($endpoints) == 0) + { + showError ('Endpoint not found. Please either set FQDN attribute or assign an IP address to the object.'); + return; + } + if (count ($endpoints) > 1) + { + showError ('More than one IP address is assigned to this object, please configure FQDN attribute.'); + return; + } } + else + $endpoints[0] = $snmpsetup['host']; switch ($objectInfo['objtype_id']) {