Advertisement
Guest User

Untitled

a guest
May 2nd, 2024
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 38.11 KB | Fixit | 0 0
  1. <?php
  2.  
  3. # Copyright (c) 2014, Erik Ruiter, SURFsara BV, Amsterdam, The Netherlands
  4. # All rights reserved.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; version 2 of the License.
  9.  
  10. /*
  11. -----------------------------------------
  12.  
  13. csvimport/plugin.php
  14.  
  15. Description:
  16.  
  17. This is a Racktables plugin which enables loading batches of data into racktables.
  18. It adds an CSV import page on the configuration menu screen.
  19. From here you can choose to either import a CSV file, or paste some manual CSV lines in a textbox.
  20. CSV files can contain multiple types of import data.
  21. The script currently supports importing of Objects, Racks, VLANs and IP space.
  22. It also supports linking ports, and assigning rackspace to objects.
  23.  
  24. The newest version of this plugin can be found at: https://github.com/RackTables/racktables-contribs
  25. -----------------------------------------
  26. # Plugin updated June 2020 by matt32106@github for compatibility with Racktables version 0.21 new plugin format
  27. # Functions added
  28. # plugin_csvimport_info
  29. # plugin_csvimport_init
  30. # plugin_csvimport_install
  31. # plugin_csvimport_uninstall
  32. # plugin_csvimport_upgrade
  33. # Uninstall previous version first (the code does not handle it) before using this one !!!
  34. # TODO: use the install/upgrade function to remove previous version
  35. # TODO: check if the add functions are still consistent with 0.21 functions
  36.  
  37. Usage:
  38.  
  39. * Importing Objects:
  40.  
  41. Syntax: OBJECT; Objecttype ; Common name ; Visible label ; Asset tag; portname,portname,etc ; porttype,porttype,etc
  42.  
  43. Value 1, OBJECT
  44. Value 2, Objectype: Can be one of the predefined types (SERVER, PATCHPANEL, SWITCH, VM), or a numeric value indicating the object type from Racktables
  45. Value 3, Common name: Common name string
  46. Value 4, Visible label: Visible label string
  47. Value 5, Asset tag: Asset tag string
  48. Value 6, Port array: This is an optional field where you can create ports for the objects, separated by a comma. When you use this , you also need to add the Port type array
  49. An individual port field can be a range of ports. Eg. 'eth[0-9]' creates ten ports ranging from eth0 to eth9.
  50. Value 7, Port type array: This is an array which maps to the previous port array. This allows you to specify the interface type of the ports. It takes the form 'a-b'. Where a is the inner interface type, and b the outer interface type. Both a and b are numeric values.
  51. New inner / outer interface pair types can be linked using the configuration -> Enabled port types page. When the 'a-' value is omitted, the inner port type defaults to 'hardwire'.
  52. Examples:
  53.  
  54. OBJECT;SERVER;myServer;www.server.com;SRV001;IPMI,eth[0-2];1-24,3-24
  55. Creates a Server object named myServer having 4x 1000-Base-T interfaces, named IPMI (hardwired inner iface, 1gbps), eth0, eth1 and eth2 (gbic inner iface, 1gbps)
  56.  
  57. OBJECT;SWITCH;myAccessSwitch1;testswitch;SW0001;ge-0/0/[0-11],fe-0/1/[0-11];24,19
  58. Creates a Switch object named myAccessSwitch1 having 12x 1000-Base-T interfaces named ge-0/0/0 to ge-0/0/11. And also 12x 100-Base-TX interfaces named fe-0/1/0 to fe-0/1/11.
  59.  
  60.  
  61. * Importing Racks
  62.  
  63. Syntax: RACK; Location ; Location child ; Row ; Rack; Height
  64. Value 1, RACK
  65. Value 2, Location ; Specifies the location where the rack is to be placed. This can be the location name string of an existing location. If the location does not exist, it will be created.
  66. Value 3, Location child ; Specifies the child location (eg room) where the rack is to be placed. This can be the name of an existing location. If the location does not exist, it will be created.
  67. Value 4, Row: Specifies the row where the rack is to be placed. This can be the name of an existing row. If the row does not exist, it will be created
  68. Value 5, Rack: Name of the rack that is to be created.
  69. Value 6, Height: Sets the Height of the rack in rackunits. When omitted, the default value is 46 units.
  70.  
  71. Examples:
  72.  
  73. RACK;Datacenter AMS01; Room 0.08; R01; AA-1
  74. Creates a rack named AA-1 in Room 0.08 of location Datacenter AMS01, with a height of 46 units.
  75.  
  76.  
  77. * Assigning Rackspace
  78.  
  79. Syntax: RACKASSIGNMENT; Object name ;Rack; units ; fib
  80. Value 1, RACKASSIGNMENT
  81. Value 2, Object name: Name of the Racktables object
  82. Value 3, Rack; NAme of the rack where the object is to be placed.
  83. Value 4, units: List of units to be assigned to the object. The unit numbers are separated by a comma. 0 for Zero-U.
  84. Value 5, fib: List of Front / Interior / Back indication. This list maps directly to the previous unit list.
  85.  
  86. Examples:
  87.  
  88. RACKASSIGNMENT;myServer;AA-1;32,33,34,35;fi,fi,fi,fi
  89. Mounts the myServer object in Rack AA-1 on rackunits 32-35, using front and interior part of the rack units.
  90.  
  91.  
  92. * Linking ports
  93.  
  94. Syntax:CABLELINK; Objectname A; Portname A; Objectname B; Portname B; Cable ID
  95. Value 1, CABLELINK
  96. Value 2, Objectname A: Specifies the name of the object on the A-side of the link
  97. Value 3, Portname A: Specifies the name of the port on the A-side of the link
  98. Value 4, Objectname B: Specifies the name of the object on the B-side of the link
  99. Value 5, Portname B: Specifies the name of the port on the B-side of the link
  100. Value 6, Cable ID: Specifies the Cable ID. It can be numeric or string.
  101.  
  102. Examples:
  103.  
  104. CABLELINK;myServer;eth1;myAccessSwitch1;ge-0/0/1;0080123
  105. Connects the eth1 port of myServer to the ge-0/0/1 port of myAccessSwitch1, using cable ID 0080123
  106.  
  107.  
  108. * Importing VLANs
  109.  
  110. Syntax: VLAN; VLAN domain; VLAN name; VLAN ID ; Propagation; Attached IP
  111. Value 1, VLAN
  112. Value 2, VLAN domain: Specifies the name of the VLAN domain where the VLAN is to be added. If the domain does not exist, it will be created.
  113. Value 3, VLAN name: Specifies the name of the to be created VLAN.
  114. Value 4, Propagation: Sets the Racktables propagation feature for the VLAN, options are ondemand or compulsory. When omitted the value defaults to compulsory.
  115. Value 5, Attached IP: This is an optional list of existing IPv4/IPv6 networks which can be assigned to the VLAN. The ranges should not have netmasks, and each range is separated by a comma.
  116.  
  117. Examples:
  118.  
  119. VLAN;Private;Netops;1020;compulsory;10.1.3.0,2001:610:1020::0
  120. Creates VLAN 1020, named Netops having the IPv4 range 10.1.3.0 and the IPv6 range 2001:610:1020::0 attached.
  121.  
  122.  
  123. * Importing IP space
  124.  
  125. Syntax: IP; Prefix; Name; is_connected; VLAN domain; VLAN ID
  126. Value 1, IP
  127. Value 2, Prefix: Specifies the IPv4 / IPv6 prefix of the network, including netmask.
  128. Value 3, Name: Specifies the name of the network which is to be added.
  129. Value 4, is_connected: Specifies if broadcast and network address in the subnet need to be reserved. Can be TRUE or FALSE. When omitted, the default is FALSE
  130. Value 5, VLAN domain: This is an optional value which can be used to set the VLAN domain of the network. You have to specifiy the name of the VLAN domain.
  131. Value 6, VLAN ID: This is an optional numeric value setting the VLAN ID of the network. It is to be used in conjunction with the previous VLAN domain value.
  132.  
  133. Examples:
  134.  
  135. IP;10.1.3.0/24;Netops network;TRUE;SURFsara;1020
  136. Creates the IP network 10.1.3.0/24 called 'Netops network' and attaches it to VLAN 1020 in the SURFsara VLAN domain.
  137.  
  138.  
  139. * Importing Object IP interfaces
  140.  
  141. Syntax: OBJECTIP; Objectname; OS Interface name; IP address; Type
  142. Value 1, OBJECTIP
  143. Value 2, Objectname: Specifies the name of the object
  144. Value 3, OS Interface name: Specifies the name of the interface to be added
  145. Value 4, IP address: Specifies the ip address of the interface to b e added (IPv4 or Ipv6) no subnet mask required
  146. Value 5, Type: Chooses the type of interface to be added. Can be: regular, virtual, shared, router, point2point. The default type is: router
  147.  
  148. Examples:
  149.  
  150. OBJECTIP;myRouter;eth0;10.1.3.1;regular
  151. Creates an IP interface name eth0, with address 10.1.3.1 and type 'regular', which is added to the myRouter object.
  152.  
  153. * Setting Object Attributes:
  154.  
  155. Syntax: OBJECTATTRIBUTE
  156. Value 1, OBJECTATTRIBUTE
  157. Value 2, Objectname: Specifies the name of the object
  158. Value 3, attribute id: Specifies the numeric ID of the attribute (can be looked up in Attribute table), also some general attributes are supported, in this case use: NAME / LABEL / ASSETTAG / HASPROBLEMS (yes|no) / COMMENT
  159. Value 4, attribute value; Specificies the value to be set for the attribute
  160.  
  161. Examples:
  162. OBJECTATTRIBUTE;myRouter;3;mgmt.myrouter.com
  163. Sets the FQDN (3) for the myRouter object.
  164.  
  165. OBJECTATTRIBUTE;myRouter;COMMENT;This is a comment
  166. Sets the comment field for the myRouter object.
  167.  
  168. * Creating Container Link:
  169.  
  170. Syntax: CONTAINERLINK
  171. Value 1, CONTAINERLINK
  172. Value 2, Parent Object Name : Specify the name of the Parent Object (eg. Hypervisor Server)
  173. Value 3, Child Object Name : Specify the name of the Child Object (eg. VM)
  174.  
  175. Examples:
  176. CONTAINERLINK;ESX_Host1;VM_1
  177. Adds VM_1 as a member of the Container ESX_Host1
  178.  
  179. * Object Tags:
  180.  
  181. Syntax: OBJECTTAG
  182. Value 1, OBJECTTAG
  183. Value 2, Object Name : Specify the name of the Object to add the tag to(eg. Server)
  184. Value 3, Tag Name : Specify the name of the Tag (eg. VM)
  185.  
  186. Examples:
  187. OBJECTTAG;Server1;Tag1
  188. Adds the tag called Tag1 to server object called Server1
  189.  
  190. * UPDATEIP
  191. Syntax: UPDATEIP
  192. Value 1: UPDATEIP
  193. Value 2: IP Address
  194. Value 3: Name
  195. Value 4: Reseverd: yes or no
  196. Value 5: Comment
  197.  
  198. Examples:
  199. UPDATEIP;192.168.1.2;Test Address;no;Testing only
  200. Updates IP 192.168.1.2 with Name Test, Reserved no and Comment "Testing only"
  201.  
  202.  
  203. */
  204.  
  205. function plugin_csvimport_info()
  206. {
  207. return array
  208. (
  209. 'name' => 'csvimport',
  210. 'longname' => 'CSV Import tool',
  211. 'version' => '2.0',
  212. 'home_url' => 'https://github.com/RackTables/racktables-contribs'
  213. );
  214. }
  215.  
  216. function plugin_csvimport_init()
  217. {
  218. global $page, $tab;
  219.  
  220. // Build Navigation
  221. $page['import']['title'] = 'Import CSV data';
  222. $page['import']['parent'] = 'config';
  223. $tab['import']['default'] = 'Import';
  224. registerTabHandler ('import', 'default', 'import_csv_data');
  225. registerOpHandler ('import', 'default', 'importData', 'importData');
  226.  
  227. // Work in progress
  228. //$tab['import']['delete'] = 'Delete';
  229. //registerTabHandler ('import', 'delete', 'delete_csv_data');
  230. //registerOpHandler ('import', 'delete', 'importData', 'deleteData');
  231. }
  232.  
  233. function plugin_csvimport_install()
  234. {
  235. return TRUE;
  236. }
  237.  
  238. function plugin_csvimport_uninstall()
  239. {
  240. return TRUE;
  241. }
  242.  
  243. function plugin_csvimport_upgrade ()
  244. {
  245. return TRUE;
  246. }
  247.  
  248. // tabhandler
  249. function import_csv_data ()
  250. {
  251. // Used for uploading a csv file, or manually pasting csv data
  252. startPortlet ('Import Racktables data');
  253. printOpFormIntro ('importData', array (), TRUE);
  254. echo "<table border=0 cellspacing=0 cellpadding='5' align='center'>";
  255. echo "<tr><td class=tdleft><label>File: <input type='file' size='10' name='file' tabindex=100></label></td><td class=tdcenter>";
  256. printImageHREF ('CREATE', 'Import file', TRUE, 102);
  257. echo '</td></tr>';
  258. echo '<tr><td>Manual input field</td></tr>';
  259. echo '<tr><td valign=top colspan=2><textarea tabindex=101 name=csv_text rows=10 cols=80></textarea></td>';
  260. echo '<td rowspan=2>';
  261. echo '</td></tr>';
  262. echo "</table></form><br>";
  263. finishPortlet();
  264. }
  265.  
  266. // tabhandler
  267. function delete_csv_data ()
  268. {
  269. // Used for uploading a csv file, or manually pasting csv data
  270. startPortlet ('Delete Racktables data');
  271. printOpFormIntro ('importData', array (), TRUE);
  272. echo "<table border=0 cellspacing=0 cellpadding='5' align='center'>";
  273. echo "<tr><td class=tdleft><label>File: <input type='file' size='10' name='file' tabindex=100></label></td><td class=tdcenter>";
  274. printImageHREF ('CREATE', 'Import file', TRUE, 102);
  275. echo '</td></tr>';
  276. echo '<tr><td>Manual input field</td></tr>';
  277. echo '<tr><td valign=top colspan=2><textarea tabindex=101 name=csv_text rows=10 cols=80></textarea></td>';
  278. echo '<td rowspan=2>';
  279. echo '</td></tr>';
  280. echo "</table></form><br>";
  281. finishPortlet();
  282. }
  283.  
  284. function deleteData()
  285. {
  286. setFuncMessages (__FUNCTION__, array ('OK' => 0, 'ERR1' => 207));
  287. assertStringArg ('csv_text', TRUE);
  288. $row = 1;
  289.  
  290. // if the manual input is empty, load the selected file
  291. if (strlen(($_REQUEST['csv_text'])) == 0)
  292. {
  293. if ($_FILES['file']['error'])
  294. return showFuncMessage (__FUNCTION__, 'ERR1', array ($_FILES['file']['error']));
  295.  
  296. // manage files from different OSes
  297. ini_set("auto_detect_line_endings", TRUE);
  298.  
  299. if (($handle = fopen($_FILES['file']['tmp_name'], "r")) !== FALSE)
  300. {
  301. showNotice ("Deleting from ".$_FILES['file']['name']);
  302. while (($csvdata = fgetcsv($handle, 1000, ";")) !== FALSE)
  303. {
  304. $result = usePreparedSelectBlade ('SELECT id FROM Object WHERE name = ?', array ($csvdata[0]));
  305. $object = $result->fetch (PDO::FETCH_ASSOC);
  306. if (!$object)
  307. showError ("Line ".$row.": Object ".$csvdata[0]. " not found");
  308. else
  309. {
  310. commitDeleteObject ($object['id']);
  311. showSuccess ("Line $row: Object ".$csvdata[0]. " deleted");
  312. }
  313. $row++;
  314. }
  315. fclose($handle);
  316. }
  317. }
  318. else
  319. {
  320. $data = explode("\n",$_REQUEST['csv_text']);
  321. showNotice ("Deleting from manual input field");
  322. foreach ($data as $dataitem)
  323. {
  324. $csvdata = str_getcsv($dataitem,";");
  325. addServerObject($csvdata,$row);
  326. $row++;
  327. }
  328. }
  329.  
  330. return showFuncMessage (__FUNCTION__, 'OK', array (htmlspecialchars ("Deleting finished.")));
  331. }
  332.  
  333. function importData()
  334. {
  335. setFuncMessages (__FUNCTION__, array ('OK' => 0, 'ERR1' => 207));
  336. assertStringArg ('csv_text', TRUE);
  337. $row_number = 1;
  338. // if the manual input is empty, load the selected file
  339. if (strlen(($_REQUEST['csv_text'])) == 0)
  340. {
  341. ini_set("auto_detect_line_endings", TRUE);
  342.  
  343. if (($handle = fopen($_FILES['file']['tmp_name'], "r")) !== FALSE)
  344. {
  345. showNotice ("Importing ".$_FILES['file']['name']);
  346. while (($csvdata = fgetcsv($handle, 1000, ";")) !== FALSE)
  347. {
  348. $csvdata[0] = trim($csvdata[0]);
  349. if ($csvdata[0] == "OBJECT") addObject($csvdata,$row_number);
  350. if ($csvdata[0] == "RACK") addRackImport($csvdata,$row_number);
  351. if ($csvdata[0] == "RACKASSIGNMENT") addRackAssignment($csvdata,$row_number);
  352. if ($csvdata[0] == "VLAN") addVLAN($csvdata,$row_number);
  353. if ($csvdata[0] == "CABLELINK") addCableLink($csvdata,$row_number);
  354. if ($csvdata[0] == "IP") addIP($csvdata,$row_number);
  355. if ($csvdata[0] == "OBJECTIP") addObjectIP($csvdata,$row_number);
  356. if ($csvdata[0] == "OBJECTATTRIBUTE") setObjectAttributes($csvdata,$row_number);
  357. if ($csvdata[0] == "CONTAINERLINK") addContainerLink($csvdata,$row_number);
  358. if ($csvdata[0] == "OBJECTTAG") addObjectTag($csvdata,$row_number);
  359. if ($csvdata[0] == "UPDATEIP") updateIP($csvdata,$row_number);
  360. $row_number++;
  361. }
  362. fclose($handle);
  363. }
  364. else
  365. {
  366. return showFuncMessage (__FUNCTION__, 'ERR1', array ($_FILES['file']['error']));
  367. }
  368. }
  369. else
  370. {
  371. $data = explode("\n",$_REQUEST['csv_text']);
  372. showNotice ("Importing from manual input field");
  373. foreach ($data as $dataitem)
  374. {
  375. $csvdata = str_getcsv($dataitem,";");
  376. $csvdata[0] = trim($csvdata[0]);
  377. if ($csvdata[0] == "OBJECT") addObject($csvdata,$row_number);
  378. if ($csvdata[0] == "RACK") addRackImport($csvdata,$row_number);
  379. if ($csvdata[0] == "RACKASSIGNMENT") addRackAssignment($csvdata,$row_number);
  380. if ($csvdata[0] == "VLAN") addVLAN($csvdata,$row_number);
  381. if ($csvdata[0] == "CABLELINK") addCableLink($csvdata,$row_number);
  382. if ($csvdata[0] == "IP") addIP($csvdata,$row_number);
  383. if ($csvdata[0] == "OBJECTIP") addObjectIP($csvdata,$row_number);
  384. if ($csvdata[0] == "OBJECTATTRIBUTE") setObjectAttributes($csvdata,$row_number);
  385. if ($csvdata[0] == "CONTAINERLINK") addContainerLink($csvdata,$row_number);
  386. if ($csvdata[0] == "OBJECTTAG") addObjectTag($csvdata,$row_number);
  387. if ($csvdata[0] == "UPDATEIP") updateIP($csvdata,$row_number);
  388. $row_number++;
  389. }
  390. }
  391. return showFuncMessage (__FUNCTION__, 'OK', array (htmlspecialchars ("Import finished.")));
  392. }
  393.  
  394. // This function adds a object to racktables and report appropriate results in the GUI
  395. function addObject($csvdata,$row_number)
  396. {
  397. $object_type = trim ($csvdata[1]);
  398. $object_name = trim ($csvdata[2]);
  399. $object_label = trim ($csvdata[3]);
  400. $object_assettag = trim ($csvdata[4]);
  401. $ifName = explode(',',$csvdata[5]);
  402. $ifType = explode(',',$csvdata[6]);
  403.  
  404. // Check Objecttype
  405. if ($object_type == "SERVER") $object_type = 4;
  406. if ($object_type == "PATCHPANEL") $object_type = 9;
  407. if ($object_type == "SWITCH") $object_type = 8;
  408. if ($object_type == "VM") $object_type = 1504;
  409. if (is_numeric($object_type))
  410. {
  411. $result = usePreparedSelectBlade ('SELECT dict_value FROM Dictionary WHERE dict_key = ?', array ($object_type));
  412. $db_object_type = $result->fetch (PDO::FETCH_ASSOC);
  413. if ($db_object_type)
  414. $object_type_name = $db_object_type['dict_value'];
  415. else
  416. {
  417. showError("line $row_number: Object type ".$object_type. " does not exist. Import FAILED.");
  418. return FALSE;
  419. }
  420. }
  421. else
  422. {
  423. showError("line $row_number: Object type ".$object_type. " does not exist. Import FAILED.");
  424. return FALSE;
  425. }
  426.  
  427. if (strlen($object_name) > 0)
  428. {
  429. try
  430. {
  431. $object_id = commitAddObject ( $object_name, $object_label, $object_type, $object_assettag, array());
  432. }
  433. catch (Exception $e)
  434. {
  435. showError("line $row_number: Import ". $object_type_name. " Object ".$object_name. " FAILED; object already exists");
  436. return FALSE;
  437. }
  438. }
  439.  
  440. // When available, import the port information
  441.  
  442. if ((count($ifName) > 0) & (count($ifType > 0)) & (count($ifName) == count($ifType)) )
  443. {
  444. // temporary disable autocreation of ports
  445. $tempAUTOPORTS_CONFIG = getConfigVar ('AUTOPORTS_CONFIG');
  446. setConfigVar ('AUTOPORTS_CONFIG',"");
  447.  
  448. for ($i=0 ; $i < count($ifName); $i++ )
  449. {
  450. if (strlen($ifName[$i]) > 0)
  451. {
  452. /*to do
  453. Add Check for port compatibility, specified itType should be linked it iif_type 1 ('hardwired')
  454. Else an foreign key error is thrown
  455. */
  456.  
  457. $prefix = "";
  458. $suffix = "";
  459. $pattern = "!(?<=[[])[^]]+(?=[]])!";
  460. preg_match($pattern,$ifName[$i],$match);
  461.  
  462. if ((count($match) > 0) & (strpos($match[0],'-') !== FALSE))
  463. {
  464. $prefix = substr($ifName[$i],0, strpos($ifName[$i],'['));
  465. $suffix = substr($ifName[$i],strpos($ifName[$i],']')+1, strlen($ifName[$i])-1);
  466. $portlist = explode('-',$match[0]);
  467. if ((is_numeric($portlist[0])) & (is_numeric($portlist[1])) & ($portlist[0] < $portlist[1]))
  468. {
  469. for ($p = $portlist[0]; $p <= $portlist[1]; $p++)
  470. $new_port_id = commitAddPort ( $object_id, $prefix.$p.$suffix, trim ($ifType[$i]), "", "" );
  471. }
  472. }
  473. else
  474. $new_port_id = commitAddPort ( $object_id, trim ($ifName[$i]), trim ($ifType[$i]), "", "" );
  475. }
  476. }
  477.  
  478. setConfigVar ('AUTOPORTS_CONFIG',$tempAUTOPORTS_CONFIG);
  479. }
  480. else
  481. {
  482. showNotice("No valid Port information found, skipping port import.");
  483. }
  484. showSuccess("line $row_number: Import ". $object_type_name. " Object ".$object_name. " successful; object_id=".$object_id);
  485. }
  486.  
  487. function addRackImport($csvdata,$row_number)
  488. {
  489. $location = trim($csvdata[1]);
  490. $location_child = trim($csvdata[2]);
  491. $rackrow = trim($csvdata[3]);
  492. $rack = trim($csvdata[4]);
  493. if (!isset($csvdata[5]))
  494. $rack_height = 46;
  495. else
  496. $rack_height = $csvdata[5];
  497.  
  498. // Handle Location entry
  499. if (strlen($location ) > 0)
  500. {
  501. $result = usePreparedSelectBlade ('SELECT id, objtype_id FROM Object WHERE name = ?', array ($location));
  502. $db_location = $result->fetch (PDO::FETCH_ASSOC);
  503. // Object already exists
  504. if ($db_location)
  505. {
  506. $location_id = $db_location['id'];
  507. // Object already exists but is not a Location (objecttype 1562) cannot continue
  508. if ($db_location['objtype_id'] != 1562)
  509. {
  510. showError("Line $row_number: Location " . $location . " already exists as another Objecttype, Import FAILED.");
  511. return FALSE;
  512. }
  513. }
  514. // Object does not exist, create new location
  515. else
  516. {
  517. $location_id = commitAddObject ($location, "", 1562, "", array());
  518. showSuccess ("Line $row_number: Location ".$location. " imported; object_id=".$location_id);
  519. }
  520. }
  521.  
  522. //Handle Child location entry
  523. if (strlen($location_child) > 0)
  524. {
  525. $location_child_id = 0;
  526. $result = usePreparedSelectBlade
  527. (
  528. 'SELECT o.id, o.objtype_id, o.name, e.parent_entity_id ' .
  529. 'FROM Object o LEFT JOIN EntityLink e ON e.child_entity_id = o.id WHERE name = ?',
  530. array ($location_child)
  531. );
  532. $db_location_child = $result->fetch (PDO::FETCH_ASSOC);
  533.  
  534. if ($db_location_child) { // Object already exists
  535. $location_child_id = $db_location_child['id'];
  536.  
  537. if ($db_location_child['objtype_id'] != 1562) { // Object already exists but is not a Location (objecttype 1562) cannot continue
  538. showError("Line $row_number: Location Child " . $location_child . " already exists as another Objecttype, Import FAILED.");
  539. return FALSE;
  540. }
  541. if ($db_location_child['parent_entity_id'] != $location_id) { // The child Location id does not match with the parent location ID
  542. showError("Line $row_number: Location Child " . $location_child . " mismatch with parent location_id, Import FAILED.");
  543. return FALSE;
  544. }
  545. }
  546. else { // Location child does not exist, create new object and link to parent location
  547. $location_child_id = commitAddObject ($location_child, "", 1562, "", array());
  548. commitLinkEntities ('location', $location_id , 'location', $location_child_id );
  549. showSuccess ("Line $row_number: Child Location ".$location_child. " imported; object_id=".$location_child_id);
  550. }
  551. }
  552.  
  553. //Handle Row entry
  554. if (strlen($rackrow) > 0)
  555. {
  556. $result = usePreparedSelectBlade
  557. (
  558. 'SELECT o.id, o.objtype_id, o.name, e.parent_entity_id ' .
  559. 'FROM Object o LEFT JOIN EntityLink e ON e.child_entity_id = o.id WHERE name = ?',
  560. array ($rackrow)
  561. );
  562. $db_rackrow = $result->fetch (PDO::FETCH_ASSOC);
  563. // Object already exists
  564. if ($db_rackrow)
  565. {
  566. $rackrow_id = $db_rackrow['id'];
  567. // Object already exists but is not a Row (objecttype 1561) cannot continue
  568. if ($db_rackrow['objtype_id'] != 1561)
  569. {
  570. showError("Line $row_number: Row " . $rackrow. $db_rackrow['objtype_id'] . " already exists as another Objecttype, Import FAILED.");
  571. return FALSE;
  572. }
  573. // The Row does not match with the parent or child location ID
  574. if (($db_rackrow['parent_entity_id'] != $location_id) & ($db_rackrow['parent_entity_id'] != $location_child_id))
  575. {
  576. showError("Line $row_number: Row " . $rackrow . " mismatch with parent location_id, Import FAILED.". $db_rackrow['parent_entity_id']. " , " . $location_id . " , " . $location_child_id);
  577. return FALSE;
  578. }
  579. }
  580. // Row does not exist, create new object and link to parent location
  581. else
  582. {
  583. $rackrow_id = commitAddObject ($rackrow, "", 1561, "", array());
  584. if ( $location_child_id == 0)
  585. commitLinkEntities ('location', $location_id , 'row', $rackrow_id );
  586. else
  587. commitLinkEntities ('location', $location_child_id , 'row', $rackrow_id );
  588. showSuccess ("Line $row_number: Row ".$rackrow. " imported; object_id=".$rackrow_id);
  589. }
  590. }
  591.  
  592. //Handle Rack entry
  593. if (strlen($rack) > 0)
  594. {
  595. $result = usePreparedSelectBlade ('SELECT id, objtype_id FROM Object WHERE name = ?', array ($rack));
  596. $db_rack = $result->fetch (PDO::FETCH_ASSOC);
  597.  
  598. // Rack Object already exists
  599. if ($db_rack)
  600. {
  601. $rack_id = $db_rack['id'];
  602. // Object already exists but is not a Location (objecttype 1562) cannot continue
  603. if ($db_rack['objtype_id'] != 1560)
  604. {
  605. showError("Line $row_number: Rack " . $rack . " already exists as another Objecttype, Import FAILED.");
  606. return FALSE;
  607. }
  608. }
  609. // Rack Object does not exist, create new rack
  610. else
  611. {
  612. $rack_id = commitAddObject ($rack, "", 1560, "", array()); // Object type 1560 = rack
  613. commitLinkEntities ('row', $rackrow_id , 'rack', $rack_id );
  614. commitUpdateAttrValue ($rack_id, 27, $rack_height); // attribute type 27 = height
  615.  
  616. // The new rack(s) should be placed on the bottom of the list, sort-wise
  617. $rowInfo = getRowInfo($rackrow_id);
  618. $sort_order = $rowInfo['count']+1;
  619. commitUpdateAttrValue ($rack_id, 29, $sort_order);
  620.  
  621. showSuccess ("Line $row_number: Rack ".$rack. " imported; object_id=".$rack_id);
  622. }
  623. }
  624. }
  625.  
  626. // This function adds Rack assignment info for an object
  627. function addRackAssignment($csvdata,$row_number)
  628. {
  629.  
  630. $object = trim ($csvdata[1]);
  631. $rack = trim ($csvdata[2]);
  632. $rackUnits = explode(',',$csvdata[3]);
  633. $fib = explode(',',$csvdata[4]);
  634.  
  635. if (strlen($object ) > 0)
  636. {
  637. $query = 'SELECT id, objtype_id FROM Object WHERE name = ?';
  638. $result = usePreparedSelectBlade ($query, array ($object));
  639. $db_object = $result->fetch (PDO::FETCH_ASSOC);
  640.  
  641. $result = usePreparedSelectBlade ($query, array ($rack));
  642. $db_rack = $result->fetch (PDO::FETCH_ASSOC);
  643. // Go ahead when Rack and object exists
  644. if (($db_object) & ($db_rack))
  645. {
  646. for ($i=0 ; $i < count($rackUnits); $i++ )
  647. {
  648. try
  649. {
  650. if($rackUnits[$i] == 0)
  651. {
  652. // Zero-U
  653. commitLinkEntities ('rack', $db_rack['id'], 'object', $db_object['id']);
  654. }
  655. else
  656. {
  657. if (strpos($fib[$i],'f') !== FALSE)
  658. usePreparedInsertBlade ('RackSpace', array ('rack_id' => $db_rack['id'], 'unit_no' => $rackUnits[$i], 'atom' => 'front', 'state' => 'T', 'object_id' => $db_object['id']));
  659. if (strpos($fib[$i],'i') !== FALSE)
  660. usePreparedInsertBlade ('RackSpace', array ('rack_id' => $db_rack['id'], 'unit_no' => $rackUnits[$i], 'atom' => 'interior', 'state' => 'T', 'object_id' => $db_object['id']));
  661. if (strpos($fib[$i],'b') !== FALSE)
  662. usePreparedInsertBlade ('RackSpace', array ('rack_id' => $db_rack['id'], 'unit_no' => $rackUnits[$i], 'atom' => 'rear', 'state' => 'T', 'object_id' => $db_object['id']));
  663. }
  664.  
  665. usePreparedDeleteBlade ('RackThumbnail', array ('rack_id' => $db_rack['id'])); //Updates the thumbnail of the rack
  666. }
  667. catch(Exception $e)
  668. {
  669. showWarning("Line $row_number: \"$object\" \"$rack\" ".$fib[$i]." failure. $e");
  670. continue;
  671. }
  672. }
  673. showSuccess("line $row_number: Rack Assignment for ".$object. " successful");
  674. }
  675. else
  676. {
  677. showError("Line $row_number: Object " . $object . " or Rack " . $rack. " does not exist. Import FAILED.");
  678. return FALSE;
  679. }
  680. }
  681. }
  682.  
  683. function addCableLink($csvdata,$row_number)
  684. {
  685. $object_a = trim ($csvdata[1]);
  686. $port_a = trim ($csvdata[2]);
  687. $object_b = trim ($csvdata[3]);
  688. $port_b = trim ($csvdata[4]);
  689. $cable_id = trim ($csvdata[5]);
  690. $query = 'SELECT Port.id, Object.name FROM Port, Object WHERE Port.object_id = Object.id AND Port.name = ? AND Object.name = ?';
  691.  
  692. // Check if object_a and port_a exist, if not; stop and return false
  693. $result = usePreparedSelectBlade ($query, array ($port_a, $object_a));
  694. $db_result_a = $result->fetch (PDO::FETCH_ASSOC);
  695. if (!$db_result_a)
  696. {
  697. showError("line $row_number: Import CableLink ". $cable_id. " FAILED; The object-port combination ".$object_a." ".$port_a." does not exist.");
  698. return FALSE;
  699. }
  700.  
  701. // Check if object_a and port_a exist, if not; stop and return false
  702. $result = usePreparedSelectBlade ($query, array ($port_b, $object_b));
  703. $db_result_b = $result->fetch (PDO::FETCH_ASSOC);
  704. if (!$db_result_b)
  705. {
  706. showError("line $row_number: Import CableLink ". $cable_id. " FAILED; The object-port combination ".$object_b." ".$port_b." does not exist.");
  707. return FALSE;
  708. }
  709.  
  710. // Check if port types are compatible
  711. // Prevent SQL LOCK TABLES errors
  712. $port1 = getPortInfo($db_result_a['id']);
  713. $port2 = getPortInfo($db_result_b['id']);
  714.  
  715. if (!arePortTypesCompatible($port1['oif_id'], $port2['oif_id']))
  716. {
  717. showError("line $row_number: Import CableLink $cable_id FAILED; The porttypes mismatch $object_a $port_a -> $object_b $port_b. ".$port1['oif_name']." != ".$port2['oif_name']);
  718. return FALSE;
  719. }
  720.  
  721. // Create Link
  722. try
  723. {
  724. $linkresult = linkPorts ($db_result_a['id'], $db_result_b['id'], $cable_id);
  725.  
  726. // port already linked
  727. if(!is_numeric($linkresult))
  728. {
  729. showError("line $row_number: Import CableLink ". $cable_id." FAILED. $object_a $port_a -> $object_b $port_b \"".$linkresult."\" Link exists?!");
  730. return FALSE;
  731. }
  732. }
  733. catch (Exception $e)
  734. {
  735. showError("line $row_number: Import CableLink ". $cable_id." FAILED. Possible porttype mismatch. Complete Exception data: ".$e);
  736. return FALSE;
  737. }
  738. showSuccess ("Line $row_number: Import CableLink ".$cable_id. " imported.");
  739. }
  740.  
  741. function addVLAN($csvdata,$row_number)
  742. {
  743. $vlan_domain = trim ($csvdata[1]);
  744. $vlan_name = trim ($csvdata[2]);
  745. $vlan_id = trim ($csvdata[3]);
  746. $vlan_propagation = trim ($csvdata[4]);
  747. if ($vlan_propagation != 'ondemand') $vlan_propagation = "compulsory";
  748. $ip_ranges = explode(',',$csvdata[5]);
  749. $query = 'SELECT id FROM VLANDomain WHERE description = ?';
  750.  
  751. // Check if VLAN domain exists
  752. $result = usePreparedSelectBlade ($query, array ($vlan_domain));
  753. $db_result = $result->fetch (PDO::FETCH_ASSOC);
  754.  
  755. // If VLAN domain does not exists, create domain
  756. if (!$db_result)
  757. {
  758. usePreparedInsertBlade ('VLANDomain', array ('description' => $vlan_domain));
  759. $result = usePreparedSelectBlade ($query, array ($vlan_domain));
  760. $db_result = $result->fetch (PDO::FETCH_ASSOC);
  761. showSuccess ("Line $row_number: VLAN Domain ".$vlan_domain. " imported; object_id=".$db_result['id']);
  762. }
  763. $domain_id = $db_result['id'];
  764.  
  765. $catched = FALSE;
  766. // Create VLAN
  767. try
  768. {
  769. usePreparedInsertBlade ("VLANDescription", array('domain_id' => $domain_id , 'vlan_id' => $vlan_id, 'vlan_type' => $vlan_propagation, 'vlan_descr' => $vlan_name));
  770. }
  771. catch (Exception $e)
  772. {
  773. showError("line $row_number: Import ". $vlan_name. " vlan_id ".$vlan_id. " FAILED; VLAN already exists");
  774. $catched = TRUE;
  775. }
  776.  
  777. if(!$catched)
  778. showSuccess ("Line $row_number: VLAN ".$vlan_name. " imported; vlan_id=".$vlan_id);
  779.  
  780. // Try to attach VLANs to IP ranges
  781. foreach ($ip_ranges as $ip_range)
  782. {
  783. try
  784. {
  785. $net = spotNetworkByIP (ip_parse($ip_range));
  786. }
  787. catch (Exception $e)
  788. {
  789. showError("line $row_number: Unable to find/parse IP network address ". $ip_range);
  790. }
  791. if (isset($net['id']))
  792. {
  793. try
  794. {
  795. if (strpos($ip_range,".")) commitSupplementVLANIPv4 ($domain_id."-".$vlan_id, $net['id']);
  796. if (strpos($ip_range,":")) commitSupplementVLANIPv6 ($domain_id."-".$vlan_id, $net['id']);
  797. showSuccess ("Line $row_number: VLAN ".$vlan_name. " attached to IP range ".$ip_range);
  798. }
  799. catch (Exception $e)
  800. {
  801. showWarning ("Line $row_number: VLAN ".$vlan_name. " unable to attach to range $ip_range. $e");
  802. }
  803. }
  804. else
  805. {
  806. showError ("Line $row_number: VLAN ".$vlan_name. " unable to attach to range ".$ip_range);
  807. }
  808. }
  809. }
  810.  
  811.  
  812. function addIP($csvdata,$row_number)
  813. {
  814. $prefix = trim ($csvdata[1]);
  815. $ip_name= trim ($csvdata[2]);
  816. $is_connected = trim ($csvdata[3]);
  817. $vlan_domain = trim ($csvdata[4]);
  818. $vlan_id = trim ($csvdata[5]);
  819. $vlan_ck = NULL;
  820.  
  821. // Check if vlan domain - vlan combination exists
  822. if ((strlen($vlan_domain) > 0) & (strlen($vlan_id) > 0))
  823. {
  824. $result = usePreparedSelectBlade
  825. (
  826. 'SELECT VLANDescription.domain_id, VLANDomain.description FROM VLANDescription, VLANDomain ' .
  827. 'WHERE VLANDomain.id = VLANDescription.domain_id AND VLANDescription.vlan_id = ? AND VLANDomain.description = ?',
  828. array ($vlan_id, $vlan_domain)
  829. );
  830. $vlan_result = $result->fetch (PDO::FETCH_ASSOC);
  831. if (!$vlan_result)
  832. {
  833. showError("line $row_number: Import IP ". $cable_id. " FAILED; The VLAN domain - VLAN combination ".$vlan_domain." - ".$vlan_id." does not exist.");
  834. return FALSE;
  835. }
  836. else
  837. {
  838. $vlan_ck = $vlan_result['domain_id']."-".$vlan_id;
  839. }
  840. }
  841.  
  842. // Create IP range
  843. try
  844. {
  845. if (strpos($prefix,".")) createIPv4Prefix($prefix, $ip_name, $is_connected, array(), $vlan_ck);
  846. if (strpos($prefix,":")) createIPv6Prefix($prefix, $ip_name, $is_connected, array(), $vlan_ck);
  847. }
  848. catch (Exception $e)
  849. {
  850. showError("line $row_number: Import IP ". $prefix." FAILED. Complete Exception data: ".$e);
  851. return FALSE;
  852. }
  853. showSuccess ("Line $row_number: Import IP ".$prefix. " imported. ".$vlan_ck);
  854. }
  855.  
  856. function addObjectIP($csvdata,$row_number)
  857. {
  858. $objectName = trim ($csvdata[1]);
  859. $ifName = trim ($csvdata[2]);
  860. $ipAddress = trim ($csvdata[3]);
  861. if (!isset($csvdata[4]))
  862. $type = "router";
  863. else
  864. $type = trim (strtolower($csvdata[4]));
  865.  
  866. //Check if object exists, and return object_id
  867. $result = usePreparedSelectBlade ('SELECT id FROM Object WHERE name = ?', array ($objectName));
  868. $db_object = $result->fetch (PDO::FETCH_ASSOC);
  869.  
  870. //if object exists, create IP interface
  871. if ($db_object)
  872. {
  873. try
  874. {
  875. bindIPToObject (ip_parse($ipAddress), $db_object['id'], $ifName, $type);
  876. }
  877. catch (Exception $e)
  878. {
  879. showError("line $row_number: IP interface ". $ifName. " import FAILED" . "Reason: ". $e);
  880. return FALSE;
  881. }
  882. showSuccess ("Line $row_number: IP interface ".$ifName. " imported.");
  883. }
  884. else
  885. {
  886. showError("Line $row_number: IP interface, Object " .$objectName. " does not exist. Import FAILED.");
  887. }
  888. }
  889.  
  890. // This function sets attributes for an object
  891. function setObjectAttributes($csvdata,$row_number)
  892. {
  893. $object = trim ($csvdata[1]);
  894. $attr_id = trim ($csvdata[2]);
  895. $attr_value = trim ($csvdata[3]);
  896.  
  897. if (strlen($object ) > 0)
  898. {
  899. $result = usePreparedSelectBlade ('SELECT id, name, label, asset_no, has_problems, comment FROM Object WHERE name = ?', array ($object));
  900. $db_object = $result->fetch (PDO::FETCH_ASSOC);
  901.  
  902. // Go ahead when object exists
  903. if ($db_object)
  904. {
  905. if ($attr_id == "NAME") $db_object['name'] = $attr_value;
  906. if ($attr_id == "LABEL") $db_object['label'] = $attr_value;
  907. if ($attr_id == "HASPROBLEMS") $db_object['has_problems'] = $attr_value;
  908. if ($attr_id == "ASSETTAG") $db_object['asset_no'] = $attr_value;
  909. if ($attr_id == "COMMENT") $db_object['comment'] = $attr_value;
  910.  
  911. if (preg_match('/NAME|LABEL|HASPROBLEMS|ASSETTAG|COMMENT/',$attr_id))
  912. {
  913. commitUpdateObject ($db_object['id'],$db_object['name'],$db_object['label'],$db_object['has_problems'],$db_object['asset_no'],$db_object['comment']);
  914. }
  915. else
  916. {
  917. commitUpdateAttrValue ($db_object['id'], $attr_id, $attr_value);
  918. }
  919. showSuccess("line $row_number: attribute for ".$object. ": ".$attr_id." ".$attr_value." updated");
  920. }
  921. else
  922. {
  923. showError("line $row_number: attribute for ".$object. ": ".$attr_id." ".$attr_value." not updated. Import FAILED.");
  924. }
  925. }
  926. }
  927.  
  928. function addContainerLink($csvdata,$row_number)
  929. {
  930. $parentObjectName = trim ($csvdata[1]);
  931. $childObjectName = trim ($csvdata[2]);
  932.  
  933. if ((strlen($parentObjectName) > 0) & (strlen($childObjectName) > 0))
  934. {
  935. $query = 'SELECT id FROM Object WHERE name = ?';
  936. // Check if parent object exists and return object_id
  937. $parentResult = usePreparedSelectBlade ($query, array ($parentObjectName));
  938. $parentDB_object = $parentResult->fetch (PDO::FETCH_ASSOC);
  939.  
  940. // Check if child object exists and return object_id
  941. $childResult = usePreparedSelectBlade ($query, array ($childObjectName));
  942. $childDB_object = $childResult->fetch (PDO::FETCH_ASSOC);
  943.  
  944. // if both objects exist, create an EntityLink between them
  945. if (($parentDB_object) & ($childDB_object))
  946. {
  947. $object_parent_id = $parentDB_object['id'];
  948. $object_child_id = $childDB_object['id'];
  949. commitLinkEntities ('object', $object_parent_id , 'object', $object_child_id );
  950. showSuccess ("Line $row_number: Added ".$childObjectName. " to parent container ".$parentObjectName.".");
  951. }
  952. else
  953. {
  954. showError("Line $row_number: Unable to add ".$childObjectName. " to parent container ".$parentObjectName.". One of the objects does not exist.");
  955. }
  956. }
  957. }
  958.  
  959. function addObjectTag($csvdata,$row_number)
  960. {
  961. $objectName = trim ($csvdata[1]);
  962. $tagName = trim ($csvdata[2]);
  963.  
  964. if ((strlen($objectName) > 0) & (strlen($tagName) > 0))
  965. {
  966. // Check if object exists and return object_id
  967. $objectResult = usePreparedSelectBlade ('SELECT id FROM Object WHERE name = ?', array ($objectName));
  968. $db_Object = $objectResult->fetch (PDO::FETCH_ASSOC);
  969.  
  970. // Check if tag exists and return tag_id
  971. $tagResult = usePreparedSelectBlade ('SELECT id FROM TagTree WHERE tag = ?', array ($tagName));
  972. $db_Tag = $tagResult->fetch (PDO::FETCH_ASSOC);
  973.  
  974. // if both the object and the tag exist, create an entry in the TagStorage table
  975. if (($db_Object) & ($db_Tag))
  976. {
  977. $object_id = $db_Object['id'];
  978. $tag_id = $db_Tag['id'];
  979. addTagForEntity ('object', $object_id, $tag_id );
  980. showSuccess ("Line $row_number: Added tag ".$tagName. " to object ".$objectName.".");
  981. }
  982. else
  983. {
  984. showError("Line $row_number: Unable to add tag ".$tagName. " to object ".$objectName.". Either the object of the tag does not exist.");
  985. }
  986. }
  987. }
  988.  
  989. function updateIP($csvdata,$row_number)
  990. {
  991. $ipaddress = trim ($csvdata[1]);
  992. $name = trim ($csvdata[2]);
  993. $reserved = trim ($csvdata[3]);
  994. $comment = trim ($csvdata[4]);
  995.  
  996. if(isset($csvdata[5]))
  997. $user = trim ($csvdata[5]);
  998. else
  999. $user = FALSE;
  1000.  
  1001. $ip_bin = ip_parse($ipaddress);
  1002.  
  1003. $netaddress = getIPAddressNetworkID($ip_bin);
  1004. if(empty($netaddress))
  1005. {
  1006. showError("line $row_number: FAILED. update IP $ipaddress does not exist!");
  1007. return FALSE;
  1008. }
  1009.  
  1010. $address = getIPAddress($ip_bin);
  1011. if($address['reserved'] == 'yes')
  1012. {
  1013. showError("line $row_number: FAILED. update IP $ipaddress already reserved!");
  1014. return FALSE;
  1015. }
  1016.  
  1017. try
  1018. {
  1019. if($user)
  1020. addIPLogEntry_User($ip_bin, "Import Source Username", $user);
  1021.  
  1022. updateAddress ($ip_bin, $name, $reserved, $comment);
  1023. }
  1024. catch (Exception $e)
  1025. {
  1026. showError("line $row_number: update IP $ipaddress FAILED" . "Reason: ". $e);
  1027. return FALSE;
  1028. }
  1029.  
  1030. showSuccess ("Line $row_number: IP $ipaddress updated.");
  1031. }
  1032.  
  1033. function addIPLogEntry_User($ip_bin, $message, $username)
  1034. {
  1035.  
  1036. switch (strlen ($ip_bin))
  1037. {
  1038. case 4:
  1039. usePreparedExecuteBlade
  1040. (
  1041. "INSERT INTO IPv4Log (ip, date, user, message) VALUES (?, NOW(), ?, ?)",
  1042. array (ip4_bin2db ($ip_bin), $username, $message)
  1043. );
  1044. break;
  1045. case 16:
  1046. usePreparedExecuteBlade
  1047. (
  1048. "INSERT INTO IPv6Log (ip, date, user, message) VALUES (?, NOW(), ?, ?)",
  1049. array ($ip_bin, $username, $message)
  1050. );
  1051. break;
  1052. default: throw new InvalidArgException ('ip_bin', $ip_bin, "Invalid binary IP");
  1053. }
  1054. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement