r4772 2011-08-31 modified by Neil Scholten <neil.scholten@gamigo.com>
authorDenis Ovsienko <infrastation@yandex.ru>
Wed, 31 Aug 2011 15:42:38 +0000 (15:42 +0000)
committerDenis Ovsienko <infrastation@yandex.ru>
Mon, 26 Mar 2012 10:12:25 +0000 (14:12 +0400)
- removed the break for undocumented interfaces bc it'll break the for loop
- added some more ignored interfaces (don't document bridges|bond|vnet(kvm) unless they have a valid IP
- matching for Objects enhanced with match by serial number, so you can adjust your common name if necessary
- Adjusted productname (for Dell and probably others) to match the presets.
- We don't touch already existing complex ports (iif_id = 9) due to BUG: http://www.freelists.org/post/racktables-users/Copy-Objects,2

facter.php

index dff7703..4dc2d6f 100644 (file)
@@ -49,7 +49,17 @@ function Update()
        $facter['fqdn']=str_replace("\n","", $facter['fqdn']);
 
        // Check if it's an existing machine
-       $query = "select id from RackObject where name = \"$facter[fqdn]\" LIMIT 1";
+       // 2011-08-31 <neil.scholten@gamigo.com>
+       // * expanded query to try to match via facter Serialnumber to be able to
+       //   match unnamed HW assets. Serial is more precise and less likely to be changed.
+       if (
+               array_key_exists('serialnumber', $facter) &&
+               strlen($facter['serialnumber']) > 0 &&
+               $facter['serialnumber'] != 'Not Specified' ) {
+               $query = "select id from RackObject where name = \"$facter[fqdn]\" OR asset_no = \"$facter[serialnumber]\" LIMIT 1";
+       } else {
+               $query = "select id from RackObject where name = \"$facter[fqdn]\" LIMIT 1";
+       }
        unset($result);
        $result = usePreparedSelectBlade ($query);
        $resultarray = $result->fetchAll (PDO::FETCH_ASSOC);
@@ -61,7 +71,7 @@ function Update()
        if (! isset($id))
        {
                // Check to see if it's a physical machine and get the correct id for Server
-               if ($facter['is_virtual']=="false\n")
+               if ($facter['is_virtual']=="false")
                {
                        // Find server id
                        $query = "select dict_key from Dictionary where dict_value='Server' LIMIT 1";
@@ -101,6 +111,25 @@ function Update()
 
        // Add lot's of attributes to the machine. Next version use an array (from config file) and do a loop for most of these fields.....
 
+       // 2011-08-31 <neil.scholten@gamigo.com>
+       // * Update (unique) name of object.
+       if (
+               array_key_exists('serialnumber', $facter) &&
+               strlen($facter['serialnumber']) > 0 &&
+               $facter['serialnumber'] != 'Not Specified' ) {
+               
+               unset($result);
+               $query                          = "select * from RackObject where asset_no = \"$facter[serialnumber]\" LIMIT 1";
+               $result                         = usePreparedSelectBlade ($query);
+               $resultarray    = $result->fetchAll (PDO::FETCH_ASSOC);
+               if($resultarray) {
+                       $id                     = $resultarray[0]['id'];
+                       $label  = $resultarray[0]['label'];
+                       // Update FQDN
+                       commitUpdateObject($id, $facter['fqdn'], $label, 'no', $facter['serialnumber'], 'Facter Import::Update Common Name');
+               }
+       }
+
        // Find FQDN id
        $query = "select id from Attribute where name='FQDN' LIMIT 1";
        unset($result);
@@ -114,14 +143,20 @@ function Update()
 
 
        // Find HW type id
-       $query = "select id from Attribute where name='HW type' LIMIT 1";
+       // 2011-08-31 <neil.scholten@gamigo.com>
+       // * adjust format to match default Dictionary Sets
+       $iHWTemp                                = preg_match('([a-zA-Z]{1,})', $facter['manufacturer'], $matches);
+       $sManufacturer  = $matches[0];
+       $sHW                                            = preg_replace('(\ )', '\1%GPASS%', $facter['productname']);
+       $sHWType                                = $sManufacturer.' '.$sHW;
+       $query                                  = "select id from Attribute where name='HW type' LIMIT 1";
        unset($result);
        $result = usePreparedSelectBlade ($query);
        $resultarray = $result->fetchAll (PDO::FETCH_ASSOC);
        if($resultarray) {
                $id=$resultarray[0]['id'];
                // Update HW type
-               $hw_dict_key = getdict($hw=$facter['productname'], $chapter=11 );
+               $hw_dict_key = getdict($sHWType, $chapter=11 );
                commitUpdateAttrValue ($object_id = $newmachine, $attr_id = $id, $value = $hw_dict_key);
        }
 
@@ -203,88 +238,98 @@ function Update()
 
                // We generally don't monitor sit interfaces.
                // We don't do this for lo interfaces, too
-               if ($nics[$i] == "sit0" || $nics[$i] == "lo")
-               break;
-
-               // Get IP
-               if (isset($facter['ipaddress_' . $nics[$i]]))
-               $ip = $facter['ipaddress_' . $nics[$i]];
-
-               // Get MAC
-               if (isset($facter['macaddress_' . $nics[$i]]))
-               $mac = $facter['macaddress_' . $nics[$i]];
-
-               //check if VM or not
-               if ($facter['is_virtual']=="false\n")
-               {
-                       // Find 1000Base-T id
-                       $query = "select dict_key from Dictionary where dict_value='1000Base-T' LIMIT 1";
-               }
-               else
-               {
-                       // Find virtual port id
-                       $query = "select dict_key from Dictionary where dict_value='virtual port' LIMIT 1";
-               }
-               unset($result);
-               $result = usePreparedSelectBlade ($query);
-               $resultarray = $result->fetchAll (PDO::FETCH_ASSOC);
-               if($resultarray) {
-                       $nictypeid=$resultarray[0]['dict_key'];
-               }
-
-               // Remove newline from ip
-               $ip=str_replace("\n","", $ip);
-
-               // Check to se if the interface has an ip assigned
-               $query = "SELECT object_id FROM IPv4Allocation where object_id=$newmachine and name=\"$nics[$i]\"";
-               unset($result);
-               $result = usePreparedSelectBlade ($query);
-               $resultarray = $result->fetchAll (PDO::FETCH_ASSOC);
-
-               if($resultarray) {
-                       unset($id);
-                       $ipcheck=$resultarray;
-               }
-
-
-               // Check if it's been configured a port already
-               $query = "SELECT id FROM Port where object_id=$newmachine and name=\"$nics[$i]\"";
-               unset($result);
-               $result = usePreparedSelectBlade ($query);
-               $resultarray = $result->fetchAll (PDO::FETCH_ASSOC);
-
-               if($resultarray) {
-                       $portid = $resultarray[0]['id'];
-                       unset($id);
-                       $portcheck=$resultarray;
-               }
-
-               // Add/update port
-               if ( count($portcheck) == 1 ) {
-                       commitUpdatePort($newmachine,$portid, $nics[$i], $nictypeid, "Ethernet port", "$mac", NULL);
-               }
-               else
-               {
-                       commitAddPort($object_id = $newmachine, $nics[$i], $nictypeid,'Ethernet port',"$mac");
-               }
-
-               // Add/update ip
-               if (count($ipcheck) == 1 ) {
-                       if( $ip ) {
-                               updateAddress($ip , $newmachine, $nics[$i],'regular');
+               // 2011-08-31 <neil.scholten@gamigo.com>
+               // * Only Document real interfaces, dont do bridges, bonds, vlan-interfaces
+               //   when they have no IP defined.
+               if ( preg_match('(_|^(br|bond|lo|sit|vnet|virbr))',$nics[$i]) != 0 && !isset($facter['ipaddress_' . $nics[$i]]) ) {
+                       // do nothing
+               } else {
+                       // Get IP
+                       if (isset($facter['ipaddress_' . $nics[$i]]))
+                       $ip = $facter['ipaddress_' . $nics[$i]];
+       
+                       // Get MAC
+                       if (isset($facter['macaddress_' . $nics[$i]]))
+                       $mac = $facter['macaddress_' . $nics[$i]];
+       
+                       //check if VM or not
+                       if ($facter['is_virtual']=="false")
+                       {
+                               // Find 1000Base-T id
+                               $query = "select dict_key from Dictionary where dict_value='1000Base-T' LIMIT 1";
                        }
-               }
-               else
-               {
-                       if( $ip ) {
-                               bindIpToObject($ip , $newmachine, $nics[$i],'regular');
+                       else
+                       {
+                               // Find virtual port id
+                               $query = "select dict_key from Dictionary where dict_value='virtual port' LIMIT 1";
+                       }
+                       unset($result);
+                       $result = usePreparedSelectBlade ($query);
+                       $resultarray = $result->fetchAll (PDO::FETCH_ASSOC);
+                       if($resultarray) {
+                               $nictypeid=$resultarray[0]['dict_key'];
+                       }
+       
+                       // Remove newline from ip
+                       $ip=str_replace("\n","", $ip);
+       
+                       // Check to se if the interface has an ip assigned
+                       $query = "SELECT object_id FROM IPv4Allocation where object_id=$newmachine and name=\"$nics[$i]\"";
+                       unset($result);
+                       $result = usePreparedSelectBlade ($query);
+                       $resultarray = $result->fetchAll (PDO::FETCH_ASSOC);
+       
+                       if($resultarray) {
+                               unset($id);
+                               $ipcheck=$resultarray;
                        }
+       
+       
+                       // Check if it's been configured a port already
+                       $query = "SELECT id,iif_id FROM Port where object_id=$newmachine and name=\"$nics[$i]\"";
+                       unset($result);
+                       $result = usePreparedSelectBlade ($query);
+                       $resultarray = $result->fetchAll (PDO::FETCH_ASSOC);
+       
+                       if($resultarray) {
+                               $portid = $resultarray[0]['id'];
+                               unset($id);
+                               $portcheck=$resultarray;
+                       }
+       
+                       // Add/update port
+                       // 2011-08-31 <neil.scholten@gamigo.com>
+                       // * Don't touch already existing complex ports
+                       if ( $resultarray[0]['type'] != 9 ) {
+                               if ( count($portcheck) == 1 ) {
+                                       commitUpdatePort($newmachine,$portid, $nics[$i], $nictypeid, "Ethernet port", "$mac", NULL);
+                               }
+                               else
+                               {
+                                       commitAddPort($object_id = $newmachine, $nics[$i], $nictypeid,'Ethernet port',"$mac");
+                               }
+                       } else {
+                               //We've got a complex port, don't touch it, it raises an error with 'Database error: foreign key violation'
+                       }
+       
+                       // Add/update ip
+                       if (count($ipcheck) == 1 ) {
+                               if( $ip ) {
+                                       updateAddress($ip , $newmachine, $nics[$i],'regular');
+                               }
+                       }
+                       else
+                       {
+                               if( $ip ) {
+                                       bindIpToObject($ip , $newmachine, $nics[$i],'regular');
+                               }
+                       }
+       
+                       unset($portcheck);
+                       unset($ipcheck);
+                       unset($ip);
+                       unset($mac);
                }
-
-               unset($portcheck);
-               unset($ipcheck);
-               unset($ip);
-               unset($mac);
        }
        return buildWideRedirectURL ();
 }