diff --git a/files/update/7077.sql b/files/update/7077.sql new file mode 100644 index 000000000..b0e6b424c --- /dev/null +++ b/files/update/7077.sql @@ -0,0 +1,3 @@ +-- Add IPD reconciliation field to snmp_configs table +UNLOCK TABLES; +ALTER TABLE `snmp_configs` ADD COLUMN `IPD_RECONCILIATION` VARCHAR(255) DEFAULT NULL; \ No newline at end of file diff --git a/plugins/language/en_GB/en_GB.txt b/plugins/language/en_GB/en_GB.txt index df125b94e..a0525f240 100755 --- a/plugins/language/en_GB/en_GB.txt +++ b/plugins/language/en_GB/en_GB.txt @@ -1729,6 +1729,9 @@ 9038 Number of equipment per type 9039 Quick view 9040 Export all SNMP types +9041 Use for IpDiscover reconciliation (must be an IP address OID) +9042 SNMP devices +9043 An IpDiscover reconciliation field is already set for this type 9100 SSL key path 9101 SSL certificat path @@ -1847,9 +1850,9 @@ 9701 CAS server uri, e.g : /cas 9702 CAS server host, e.g. : authentication.org 9703 CAS default role, applied on first connection initiated using CAS authentication +9704 CAS Configuration 9705 CAS Base URI, e.g. : ocs.example.com 9706 CAS Server CA Certificate Path, e.g. : /path/to/cachain.pem -9704 CAS Configuration 9800 Exclude archived computers from statistics, CVE inventory, all softwares count, computers per TAG count 9801 Show bios diagram in dashboard diff --git a/plugins/language/fr_FR/fr_FR.txt b/plugins/language/fr_FR/fr_FR.txt index 07c90f70a..eb8467554 100644 --- a/plugins/language/fr_FR/fr_FR.txt +++ b/plugins/language/fr_FR/fr_FR.txt @@ -1724,6 +1724,9 @@ 9038 Nombre d'équipements par type 9039 Aperçu rapide 9040 Exporter tous les types SNMP +9041 Utiliser comme réconciliation pour l'IpDiscover (doit être une adresse IP) +9042 machines SNMP +9043 Un champ de réconciliation IpDiscover est déjà configuré pour ce type 9100 Chemin de la clé SSL 9101 Chemin du certificat SSL diff --git a/plugins/main_sections/ms_config/ms_snmp_config.php b/plugins/main_sections/ms_config/ms_snmp_config.php index 176188a45..6fb1d701a 100644 --- a/plugins/main_sections/ms_config/ms_snmp_config.php +++ b/plugins/main_sections/ms_config/ms_snmp_config.php @@ -266,7 +266,7 @@ } if(isset($protectedPost['update_snmp'])) { - $result = $snmp->snmp_config($protectedPost['type_id'], $protectedPost['label_id'], $protectedPost['oid'], $protectedPost['reconciliation'] ?? ''); + $result = $snmp->snmp_config($protectedPost['type_id'], $protectedPost['label_id'], $protectedPost['oid'], $protectedPost['reconciliation'] ?? '', $protectedPost['ipd_reconciliation'] ?? ''); if($result == 0){ msg_success($l->g(572)); }else{ @@ -291,6 +291,7 @@ formGroup('select', 'label_id', 'Label :', '', '', '', '', $label, $label); formGroup('text', 'oid', 'OID :', '', '', '', '', '', '', ""); formGroup('checkbox', 'reconciliation', $l->g(9015).' :', '', '', 'YES', '', '', ''); + formGroup('checkbox', 'ipd_reconciliation', $l->g(9041).' :', '', '', 'YES', '', '', ''); echo ""; echo "



"; @@ -314,7 +315,8 @@ "Type" => 't.TYPE_NAME', "Label" => 'l.LABEL_NAME', "OID" => "c.OID", - "RECONCILIATION" => "c.RECONCILIATION" + "RECONCILIATION" => "c.RECONCILIATION", + "IPD_RECONCILIATION" => "c.IPD_RECONCILIATION", ); $list_fields['SUP'] = 'c.ID'; @@ -323,7 +325,7 @@ $default_fields = $list_fields; $list_col_cant_del = $list_fields; - $queryDetails = "SELECT DISTINCT c.ID, t.TYPE_NAME, l.LABEL_NAME, c.OID, c.RECONCILIATION FROM snmp_configs c + $queryDetails = "SELECT DISTINCT c.ID, t.TYPE_NAME, l.LABEL_NAME, c.OID, c.RECONCILIATION, c.IPD_RECONCILIATION FROM snmp_configs c LEFT JOIN snmp_types t ON c.TYPE_ID = t.ID LEFT JOIN snmp_labels l ON c.LABEL_ID = l.ID". $filter; @@ -433,6 +435,7 @@ '.$l->g(9018).' '.$l->g(9017).' '.$l->g(9015).' + '.$l->g(9041).' @@ -453,6 +456,7 @@ echo ' '; echo ' '.$oid.''; echo ' '; + echo ' '; echo ' '; } diff --git a/plugins/main_sections/ms_ipdiscover/ms_custom_info.php b/plugins/main_sections/ms_ipdiscover/ms_custom_info.php index 8e45fbe11..53d9f6628 100644 --- a/plugins/main_sections/ms_ipdiscover/ms_custom_info.php +++ b/plugins/main_sections/ms_ipdiscover/ms_custom_info.php @@ -28,6 +28,7 @@ } require_once('require/function_ipdiscover.php'); +require_once('require/snmp/Snmp.php'); if (!isset($protectedPost['MODIF']) || (isset($protectedPost['MODIF']) && $protectedPost['MODIF'] == "")) { echo "".$l->g(188)."

"; @@ -184,6 +185,11 @@ if (!(isset($protectedPost["pcparpage"]))) { $protectedPost["pcparpage"] = 5; } + + // SNMP filtering + $snmp = new OCSSnmp(); + $snmpTables = $snmp->getIPDReconciliationColumns(); + if (isset($protectedGet['value'])) { $explode = explode(";", $protectedGet['value']); $value_preg = preg_replace("/[^A-zA-Z0-9\._]/", "", $explode[0]); @@ -192,14 +198,30 @@ if ($protectedGet['prov'] == "no_inv") { $title = $l->g(947); $sql = "SELECT - ip, mac, mask, date, name, n.TAG + ip, mac, mask, date, n.name, n.TAG FROM netmap n - LEFT JOIN networks ns ON ns.macaddr=n.mac - WHERE n.mac NOT IN ( - SELECT DISTINCT(macaddr) FROM network_devices - ) - AND (ns.macaddr IS NULL) - AND n.netid IN ('%s')"; + LEFT JOIN networks ns ON ns.macaddr=n.mac"; + // left join for SNMP tables (excludes SNMP devices) + if ($snmpTables) { + foreach ($snmpTables as $snmpTable) { + $sql .= " LEFT JOIN $snmpTable[TABLE_TYPE_NAME] ON $snmpTable[TABLE_TYPE_NAME].$snmpTable[LABEL_NAME] = n.IP "; + } + } + $sql .= " WHERE "; + // adding WHERE clause for SNMP tables (excludes SNMP devices) + if ($snmpTables) { + foreach($snmpTables as $snmpTable) { + // we want devices which are not in SNMP tables + $sql .= " $snmpTable[TABLE_TYPE_NAME].$snmpTable[LABEL_NAME] IS NULL AND "; + } + } + + $sql .= " n.mac NOT IN ( + SELECT DISTINCT(macaddr) FROM network_devices + ) + AND (ns.macaddr IS NULL) + AND n.netid IN ('%s')"; + if($tag != "") { $sql .= " AND n.TAG = '%s'"; } @@ -300,7 +322,53 @@ $tab_options['FILTRE']['h.userid'] = $l->g(24); $tab_options['FILTRE']['h.osname'] = $l->g(25); $tab_options['FILTRE']['h.ipaddr'] = $l->g(34); + } else if ($protectedGet['prov'] == "snmp") { + // display IPD discovered devices who are also in SNMP tables + $title = $l->g(9042); + + $list_fields = array( + $l->g(34) => 'IP', + $l->g(95) => 'MAC', + $l->g(208) => 'MASK', + $l->g(316) => 'NETID', + $l->g(318) => 'n.NAME', + $l->g(232) => 'date', + 'TAG' => 'TAG' + ); + + $sql = prepare_sql_tab($list_fields); + $tab_options['ARG_SQL'] = $sql['ARG']; + + $snmp = new OCSSnmp(); + $snmpTables = $snmp->getIPDReconciliationColumns(); + + $sql = prepare_sql_tab($list_fields); + $list_fields = array_merge($list_fields, array('TYPE' => "TYPE")); + $default_fields = $list_fields; + $sql = $sql['SQL'] . ",CASE "; + + $case = ""; + $leftjoin = ""; + foreach ($snmpTables as $snmpTable) { + $case .= "WHEN $snmpTable[TABLE_TYPE_NAME].$snmpTable[LABEL_NAME] IS NOT NULL THEN '$snmpTable[TABLE_TYPE_NAME]' "; + $leftjoin .= " LEFT JOIN $snmpTable[TABLE_TYPE_NAME] ON $snmpTable[TABLE_TYPE_NAME].$snmpTable[LABEL_NAME] = n.IP "; + } + $sql .= $case." ELSE 'Unknown' + END AS `TYPE` FROM netmap n + ".$leftjoin." + WHERE ("; + foreach($snmpTables as $snmpTable) { + $sql .= " $snmpTable[TABLE_TYPE_NAME].$snmpTable[LABEL_NAME] IS NOT NULL OR "; + } + $sql = substr($sql, 0, -3); + $sql .= ") AND n.netid = '%s'"; + if($tag != "") { + $sql .= " AND n.TAG = '%s'"; + } + array_push($tab_options['ARG_SQL'], $value_preg, $tag); + $tab_options['ARG_SQL_COUNT'] = array($value_preg, $tag); } + printEnTete($title); echo "

"; diff --git a/plugins/main_sections/ms_ipdiscover/ms_ipdiscover.php b/plugins/main_sections/ms_ipdiscover/ms_ipdiscover.php index 6c1707060..7986f680b 100644 --- a/plugins/main_sections/ms_ipdiscover/ms_ipdiscover.php +++ b/plugins/main_sections/ms_ipdiscover/ms_ipdiscover.php @@ -27,6 +27,12 @@ } require_once('require/ipdiscover/Ipdiscover.php'); +require_once('require/snmp/Snmp.php'); + +// checking if IPD_RECONCILIATION is set to "Yes" in any snmp config +// if not there will be no SNMP data in IPD table +$snmp = new OCSSnmp(); +$snmpTables = $snmp->getIPDReconciliationColumns(); $ipdiscover = new Ipdiscover(); @@ -37,8 +43,8 @@ $identifiant = "PASS"; $on = "PASS"; } else { - $groupby1 = "d.tvalue"; - $groupby2 = "n.ipsubnet"; + $groupby1 = "devices.tvalue"; + $groupby2 = "networks.ipsubnet"; $groupby3 = "netid"; $identifiant = "ID"; $on = "RSX"; @@ -86,109 +92,181 @@ $tab_options['VALUE']['LBL_RSX'] = $_SESSION['OCS']["ipdiscover"][$dpt[$protectedPost['onglet']]]; $arg_sql = array(); + + # base query for ipd table $sql = "SELECT * FROM ( SELECT - n.RSX AS ID, - inv.c AS 'INVENTORIE', - non_ident.c AS 'NON_INVENTORIE', - ipdiscover.c AS 'IPDISCOVER', - ident.c AS 'IDENTIFIE', - n.TAG, - n.PASS - FROM - ( - SELECT netid AS RSX, - CONCAT(netid,';',ifnull(tag,'')) AS PASS, - TAG FROM netmap - WHERE netid IN "; + base_query.RSX AS ID, + inventoried.count AS 'INVENTORIE', + non_identified.count AS 'NON_INVENTORIE', + ipdiscover.count AS 'IPDISCOVER', + identified.count AS 'IDENTIFIE', "; + + if ($snmpTables) { + $sql .= "SNMP_total.count AS 'SNMP',"; + } - $arg = mysql2_prepare($sql, $arg_sql, $array_rsx); + $sql.= "base_query.TAG, + base_query.PASS + FROM ( + SELECT + netid AS RSX, + CONCAT(netid, ';', IFNULL(tag, '')) AS PASS, + TAG + FROM + netmap + WHERE + netid IN "; + + $arg = mysql2_prepare($sql, $arg_sql, $array_rsx); + + # IPDISCOVER devices $arg['SQL'] .= " GROUP BY netid - ) - n LEFT JOIN - ( + ) base_query + LEFT JOIN ( SELECT - COUNT(DISTINCT d.hardware_id) AS c, - 'IPDISCOVER' AS TYPE, - d.tvalue AS RSX, - a.tag, - CONCAT(d.tvalue,';',ifnull(a.tag,'')) as PASS - FROM devices d - LEFT JOIN accountinfo a ON a.HARDWARE_ID = d.HARDWARE_ID - WHERE d.name='IPDISCOVER' AND d.tvalue IN "; + COUNT(DISTINCT devices.hardware_id) AS count, + 'IPDISCOVER' AS TYPE, + devices.tvalue AS RSX, + accountinfo.tag, + CONCAT(devices.tvalue, ';', IFNULL(accountinfo.tag, '')) AS PASS + FROM + devices + LEFT JOIN + accountinfo ON accountinfo.HARDWARE_ID = devices.HARDWARE_ID + WHERE + devices.name='IPDISCOVER' AND devices.tvalue IN "; $arg = mysql2_prepare($arg['SQL'], $arg['ARG'], $array_rsx); - $arg['SQL'] .= " GROUP BY $groupby1 - ) - ipdiscover ON n.$on=ipdiscover.$on LEFT JOIN - ( - SELECT count(DISTINCT h.ID) AS c, - 'INVENTORIE' AS TYPE, - n.ipsubnet AS RSX, - s.TAG as TAG, - CONCAT(n.ipsubnet,';',ifnull(s.tag,'')) as PASS - FROM networks n - LEFT JOIN hardware h ON h.ID = n.HARDWARE_ID - LEFT JOIN accountinfo a ON a.HARDWARE_ID = h.ID - LEFT JOIN subnet s ON a.TAG = s.TAG AND s.NETID = n.IPSUBNET - WHERE n.ipsubnet IN "; + # INVENTORIED devices + $arg['SQL'] .= " GROUP BY + $groupby1 + ) ipdiscover ON base_query.$on=ipdiscover.$on + LEFT JOIN ( + SELECT + COUNT(DISTINCT hardware.ID) AS count, + 'INVENTORIE' AS TYPE, + networks.ipsubnet AS RSX, + subnet.TAG, + CONCAT(networks.ipsubnet, ';', IFNULL(subnet.tag, '')) AS PASS + FROM + networks + LEFT JOIN + hardware ON hardware.ID = networks.HARDWARE_ID + LEFT JOIN + accountinfo ON accountinfo.HARDWARE_ID = hardware.ID + LEFT JOIN + subnet ON accountinfo.TAG = subnet.TAG AND subnet.NETID = networks.IPSUBNET + WHERE + networks.ipsubnet IN "; $arg = mysql2_prepare($arg['SQL'], $arg['ARG'], $array_rsx); - - $arg['SQL'] .= " AND n.status='Up' - GROUP BY $groupby2 - ) - inv ON n.$on=inv.$on LEFT JOIN - ( - SELECT - COUNT(DISTINCT mac) AS c, - 'IDENTIFIE' AS TYPE, + + # IDENTIFIED devices + $arg['SQL'] .= " AND networks.status='Up' + GROUP BY + $groupby2 + ) inventoried ON base_query.$on=inventoried.$on + LEFT JOIN ( + SELECT + COUNT(DISTINCT mac) AS count, + 'IDENTIFIE' AS TYPE, netid AS RSX, - TAG, - CONCAT(netid,';',ifnull(tag,'')) as PASS - FROM netmap - WHERE mac IN - ( - SELECT - DISTINCT(macaddr) - FROM network_devices - ) + TAG, + CONCAT(netid, ';', IFNULL(tag, '')) AS PASS + FROM + netmap + WHERE + mac IN (SELECT DISTINCT(macaddr) FROM network_devices) AND netid IN "; $arg = mysql2_prepare($arg['SQL'], $arg['ARG'], $array_rsx); - - $arg['SQL'] .= " GROUP BY $groupby3 - ) - ident ON n.$on=ident.$on LEFT JOIN - ( + + # NON IDENTIFIED (NON-INVENTORIED) devices + $arg['SQL'] .= " GROUP BY + $groupby3 + ) identified ON base_query.$on=identified.$on + LEFT JOIN ( SELECT - COUNT(DISTINCT n.mac) AS c, - 'NON IDENTIFIE' AS TYPE, - n.netid AS RSX, - n.TAG, - CONCAT(n.netid,';',ifnull(n.tag,'')) as PASS - FROM netmap n - LEFT JOIN networks ns ON ns.macaddr=n.mac - LEFT JOIN accountinfo a ON a.TAG = n.TAG - WHERE n.mac NOT IN ( - SELECT DISTINCT(macaddr) FROM network_devices - ) - AND (ns.macaddr IS NULL) - AND n.netid IN "; + COUNT(DISTINCT netmap.mac) AS count, + 'NON IDENTIFIE' AS TYPE, + netmap.netid AS RSX, + netmap.TAG, + CONCAT(netmap.netid, ';', IFNULL(netmap.tag, '')) AS PASS + FROM + netmap + LEFT JOIN + networks ON networks.macaddr = netmap.mac + LEFT JOIN + accountinfo ON accountinfo.TAG = netmap.TAG "; + // adding LEFT JOINS for SNMP tables + if ($snmpTables) { + foreach($snmpTables as $snmpTable) { + $arg['SQL'] .= " LEFT JOIN $snmpTable[TABLE_TYPE_NAME] ON $snmpTable[TABLE_TYPE_NAME].$snmpTable[LABEL_NAME] = netmap.IP "; + } + } + + $arg['SQL'] .= " WHERE "; + // adding WHERE clause for SNMP tables + if ($snmpTables) { + foreach($snmpTables as $snmpTable) { + $arg['SQL'] .= " $snmpTable[TABLE_TYPE_NAME].$snmpTable[LABEL_NAME] IS NULL AND "; + } + } + + $arg['SQL'] .= " netmap.mac NOT IN (SELECT DISTINCT(macaddr) FROM network_devices) + AND (networks.macaddr IS NULL) + AND netmap.netid IN "; $arg = mysql2_prepare($arg['SQL'], $arg['ARG'], $array_rsx); - $arg['SQL'] .= " GROUP BY $groupby3 - ) - non_ident on n.$on=non_ident.$on - ) - ipd"; + if ($snmpTables) { + $arg['SQL'] .= " GROUP BY + $groupby3 + ) non_identified on base_query.$on=non_identified.$on + LEFT JOIN ( + SELECT SUM(count) AS count, RSX + FROM ( "; + foreach($snmpTables as $snmpTable) { + $arg['SQL'] .= " SELECT + COUNT(DISTINCT $snmpTable[TABLE_TYPE_NAME].$snmpTable[LABEL_NAME]) AS count, + 'SNMP' AS TYPE, + CONCAT(SUBSTRING_INDEX($snmpTable[LABEL_NAME], '.', 3), '.0') AS RSX, + CONCAT(CONCAT(SUBSTRING_INDEX($snmpTable[LABEL_NAME], '.', 3), '.0'), ';', '') AS PASS + FROM + $snmpTable[TABLE_TYPE_NAME] + WHERE CONCAT(SUBSTRING_INDEX($snmpTable[LABEL_NAME], '.', 3), '.0') IN "; + $arg = mysql2_prepare($arg['SQL'], $arg['ARG'], $array_rsx); + + $arg['SQL'] .= " GROUP BY + RSX" ; + + if (next($snmpTables)) { + $arg['SQL'] .= " UNION ALL "; + } + } + $arg['SQL'] .= " ) AS snmp_union + GROUP BY RSX + ) SNMP_total ON base_query.RSX=SNMP_total.RSX + "; + + } else { + $arg['SQL'] .= " GROUP BY + $groupby3 + ) non_identified on base_query.$on=non_identified.$on + "; + + $tab_options['ARG_SQL'] = $arg['ARG']; + } + + // pls do not remove the space after ipd + $arg['SQL'] .= " ) ipd "; $tab_options['ARG_SQL'] = $arg['ARG']; - + $list_fields = array( 'LBL_RSX' => 'LBL_RSX', 'RSX' => 'ID', @@ -198,6 +276,10 @@ 'IDENTIFIE' => 'IDENTIFIE', ); + if ($snmpTables) { + $list_fields['SNMP'] = 'SNMP'; + } + if($ipdiscover->IPDISCOVER_TAG == "1") { $list_fields['TAG'] = "TAG"; } @@ -222,10 +304,16 @@ $tab_options['LIEN_LBL']['IDENTIFIE'] = 'index.php?' . PAG_INDEX . '=' . $pages_refs['ms_custom_info'] . '&prov=ident&head=1&value='; $tab_options['LIEN_CHAMP']['IDENTIFIE'] = $identifiant; + # adding SNMP link + $tab_options['LIEN_LBL']['SNMP'] = 'index.php?' . PAG_INDEX . '=' . $pages_refs['ms_custom_info'] . '&prov=snmp&head=1&value='; + $tab_options['LIEN_CHAMP']['SNMP'] = $identifiant; + $tab_options['REPLACE_WITH_CONDITION']['INVENTORIE'][' '] = '0'; $tab_options['REPLACE_WITH_CONDITION']['IPDISCOVER'][' '] = '0'; $tab_options['REPLACE_WITH_CONDITION']['NON_INVENTORIE'][' '] = '0'; $tab_options['REPLACE_WITH_CONDITION']['IDENTIFIE'][' '] = '0'; + # snmp + $tab_options['REPLACE_WITH_CONDITION']['SNMP'][' '] = '0'; $tab_options['LBL']['LBL_RSX'] = $l->g(863); $tab_options['LBL']['RSX'] = $l->g(869); @@ -233,6 +321,7 @@ $tab_options['LBL']['NON_INVENTORIE'] = $l->g(365); $tab_options['LBL']['IPDISCOVER'] = $l->g(312); $tab_options['LBL']['IDENTIFIE'] = $l->g(366); + $tab_options['LBL']['SNMP'] = $l->g(1136); //you can modify your subnet if ipdiscover is local define if ($_SESSION['OCS']["ipdiscover_methode"] == "OCS" && $_SESSION['OCS']['profile']->getConfigValue('IPDISCOVER') == "YES") { diff --git a/require/function_table_html.php b/require/function_table_html.php index e3d5a9f62..e326eb303 100644 --- a/require/function_table_html.php +++ b/require/function_table_html.php @@ -1378,8 +1378,8 @@ function ajaxfiltre($queryDetails,$tab_options){ // Special process for ipdiscover query if(isset($tab_options['SPECIAL_SEARCH']) && $tab_options['SPECIAL_SEARCH'] == "IPD" && !is_null($filtertxt)) { - $explodeIPDQuery = explode("non_ident on n.RSX=non_ident.RSX", $queryDetails); - $queryDetails = $explodeIPDQuery[0]."non_ident on n.RSX=non_ident.RSX".$filtertxt.")".$explodeIPDQuery[1]; + $explodeIPDQuery = explode(") ipd ", $queryDetails); + $queryDetails = $explodeIPDQuery[0].$filtertxt.")) ipd ".$explodeIPDQuery[1]; } return $queryDetails; diff --git a/require/snmp/Snmp.php b/require/snmp/Snmp.php index ee79a721c..39be9c631 100644 --- a/require/snmp/Snmp.php +++ b/require/snmp/Snmp.php @@ -169,37 +169,60 @@ public function get_label(){ return ($list_label); } - /** - * Insert snmp config into DB - * - * @param int $typeID - * @param int $labelID - * @param string $oid - * @return boolean - */ - public function snmp_config($typeID, $labelID, $oid, $reconciliation = null) { - global $l; - $result_alter_table = $this->add_label_column($typeID, $labelID, $reconciliation); +/** + * Insert snmp config into DB + * + * @param int $typeID + * @param int $labelID + * @param string $oid + * @return boolean + */ +public function snmp_config($typeID, $labelID, $oid, $reconciliation = null, $ipd_reconciliation = null) { + global $l; + $result_alter_table = $this->add_label_column($typeID, $labelID, $reconciliation, $ipd_reconciliation); + + // checking if ipd_reconciliation is already set for this type + $sql = "SELECT `IPD_RECONCILIATION` FROM `snmp_configs` WHERE `TYPE_ID` = %s"; + $sql_arg = array($typeID); + $result = mysql2_query_secure($sql, $_SESSION['OCS']["readServer"], $sql_arg); + $ipd_reconciliation_already_set = false; + while ($item = mysqli_fetch_array($result)) { + if($item['IPD_RECONCILIATION'] == 'Yes') { + $ipd_reconciliation_already_set = true; + } + } - if($result_alter_table) { - if($reconciliation != null) { - $sql = "INSERT INTO `snmp_configs` (`TYPE_ID`,`LABEL_ID`,`OID`,`RECONCILIATION`) VALUES (%s,%s,'%s','%s')"; - $sql_arg = array($typeID, $labelID, addslashes($oid), 'Yes'); - } else { - $sql = "INSERT INTO `snmp_configs` (`TYPE_ID`,`LABEL_ID`,`OID`) VALUES (%s,%s,'%s')"; - $sql_arg = array($typeID, $labelID, addslashes($oid)); - } - $result = mysql2_query_secure($sql, $_SESSION['OCS']["writeServer"], $sql_arg); + if($result_alter_table) { + if ($ipd_reconciliation_already_set && $ipd_reconciliation != null) { + // Do not insert if IPD_RECONCILIATION is already set and the new label also has IPD_RECONCILIATION + return 9043; + } + if($reconciliation != null && $ipd_reconciliation == null) { + $sql = "INSERT INTO `snmp_configs` (`TYPE_ID`,`LABEL_ID`,`OID`,`RECONCILIATION`) VALUES (%s,%s,'%s','%s')"; + $sql_arg = array($typeID, $labelID, addslashes($oid), 'Yes'); - if($result) { - return 0; - } else { - return 9027; - } + } elseif ($reconciliation == null && $ipd_reconciliation != null) { + $sql = "INSERT INTO `snmp_configs` (`TYPE_ID`,`LABEL_ID`,`OID`, `IPD_RECONCILIATION`) VALUES (%s,%s,'%s', '%s')"; + $sql_arg = array($typeID, $labelID, addslashes($oid), 'Yes'); + + } elseif ($reconciliation != null && $ipd_reconciliation != null) { + $sql = "INSERT INTO `snmp_configs` (`TYPE_ID`,`LABEL_ID`,`OID`,`RECONCILIATION`, `IPD_RECONCILIATION`) VALUES (%s,%s,'%s','%s', '%s')"; + $sql_arg = array($typeID, $labelID, addslashes($oid), 'Yes', 'Yes'); + } else { + $sql = "INSERT INTO `snmp_configs` (`TYPE_ID`,`LABEL_ID`,`OID`) VALUES (%s,%s,'%s')"; + $sql_arg = array($typeID, $labelID, addslashes($oid)); + } + $result = mysql2_query_secure($sql, $_SESSION['OCS']["writeServer"], $sql_arg); + + if($result) { + return 0; } else { return 9027; } + } else { + return 9027; } +} /** * Alter table type and add column label @@ -208,11 +231,11 @@ public function snmp_config($typeID, $labelID, $oid, $reconciliation = null) { * @param int $labelID * @return boolean */ - private function add_label_column($typeID, $labelID, $reconciliation) { + private function add_label_column($typeID, $labelID, $reconciliation, $ipd_reconciliation) { $tableName = $this->get_table_type_drop($typeID); $labelName = $this->get_label_drop($labelID); - if($reconciliation != null) { + if($reconciliation != null || $ipd_reconciliation != null) { $sql_alter = "ALTER TABLE `%s` ADD `%s` VARCHAR(255) NOT NULL"; } else { $sql_alter = "ALTER TABLE `%s` ADD `%s` TEXT NOT NULL"; @@ -221,7 +244,7 @@ private function add_label_column($typeID, $labelID, $reconciliation) { $arg_alter = array($tableName, $labelName); $result_alter = mysql2_query_secure($sql_alter, $_SESSION['OCS']["writeServer"], $arg_alter); - if($reconciliation != null) { + if($reconciliation != null || $ipd_reconciliation != null) { $sql_unique = "ALTER TABLE `%s` ADD UNIQUE (`%s`)"; $arg_unique = array($tableName, $labelName); $result_unique = mysql2_query_secure($sql_unique, $_SESSION['OCS']["writeServer"], $arg_unique); @@ -495,7 +518,7 @@ public function sort_mib($post) { if(!empty($config)) { foreach($config as $key => $value) { if($config[$key]['label'] != null && $config[$key]['oid'] != null) { - $result = $this->snmp_config($post['type_id'], $config[$key]['label'], $config[$key]['oid'], $config[$key]['reconciliation'] ?? null); + $result = $this->snmp_config($post['type_id'], $config[$key]['label'], $config[$key]['oid'], $config[$key]['reconciliation'] ?? null, $config[$key]['ipd_reconciliation'] ?? null); if($result != 0) { return false; @@ -688,4 +711,33 @@ public function getReconciliationColumn($type) { return $reconciliation; } + /** + * getIPDReconciliationColumns : get IPD reconciliation column if any + */ + public function getIPDReconciliationColumns() { + $sql = " + SELECT + c.TYPE_ID, c.LABEL_ID, c.OID, + t.TABLE_TYPE_NAME, + l.LABEL_NAME + FROM snmp_configs c + LEFT JOIN snmp_types t ON c.TYPE_ID = t.ID + LEFT JOIN snmp_labels l ON c.LABEL_ID = l.ID + WHERE c.IPD_RECONCILIATION = 'Yes'"; + + $res = mysql2_query_secure($sql, $_SESSION['OCS']["readServer"]); + $snmpTables = array(); + if ($res) { + while ($row = mysqli_fetch_object($res)) { + $snmpTables[] = ['TYPE_ID' => $row->TYPE_ID, + 'LABEL_ID' => $row->LABEL_ID, + 'OID' => $row->OID, + 'TABLE_TYPE_NAME' => $row->TABLE_TYPE_NAME, + 'LABEL_NAME' => $row->LABEL_NAME]; + } + + return $snmpTables; + } + return false; + } } \ No newline at end of file