Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.13 KB | None | 0 0
  1. <?php
  2.  
  3. $servername = "localhost";
  4. $username = "xxxx";
  5. $password = "xxxx";
  6. $dbname = "xxxxx";
  7.  
  8.  
  9. // Create connection
  10. $conn = new mysqli($servername, $username, $password, $dbname);
  11. // Check connection
  12. if ($conn->connect_error)
  13. {
  14. die("Connection failed: " . $conn->connect_error);
  15. }
  16. //read the json file contents
  17. $files = scandir('ansible/');
  18.  
  19. foreach($files as $file)
  20.   {
  21.   if ($file != '.' && $file != '..')
  22.   {
  23.     $jsondata = file_get_contents('ansible/'.$file);
  24.     $data = json_decode($jsondata, true);
  25.     $hostname = $data['ansible_facts']['ansible_hostname'];
  26.       echo 'Data For :'.$hostname;
  27.       echo '<br />';
  28.       echo '<br />';
  29.       echo $hostname;
  30.       echo '<br />';
  31.     $domainname = $data['ansible_facts']['ansible_domain'];
  32.       echo $domainname;
  33.       echo '<br />';
  34.     $vendorname = $data['ansible_facts']['ansible_system_vendor'];
  35.       echo $vendorname;
  36.       echo '<br />';
  37.     $productname = $data['ansible_facts']['ansible_product_name'];
  38.       echo $productname;
  39.       echo '<br />';
  40.   if($vendorname == "VMware, Inc.")
  41.     {
  42.     $machinetype = "VMware Guest";
  43.       echo $machinetype;
  44.       echo '<br />';
  45.     }
  46.   else
  47.     {
  48.     $machinetype = "Standalone";
  49.       echo $machinetype;
  50.       echo '<br />';
  51.     }
  52.     $operatingsystem = $data['ansible_facts']['ansible_distribution'];
  53.       echo $operatingsystem;
  54.       echo '<br />';
  55.     $osrelease = $data['ansible_facts']['ansible_distribution_version'];
  56.       echo $osrelease;
  57.       echo '<br />';
  58.     $machineserial = $data['ansible_facts']['ansible_product_serial'];
  59.       echo $machineserial;
  60.       echo '<br />';
  61.       echo '<br />';
  62.       echo '<br />';
  63.       echo '<br />';
  64.  
  65.  
  66. $sql = "SELECT id FROM hostname WHERE systemname = '$hostname'";
  67.   if ($result = $conn->query($sql))
  68.   {
  69.     switch ($result->num_rows)
  70.       {
  71.         case 0:
  72.                 $sql = "INSERT INTO hostname (systemname, domain, vendor, model, machinetype, os, osrelease, machineserial) VALUES ('$hostname', '$domainname', '$vendorname','$productname','$machinetype','$operatingsystem','$osrelease','$machineserial')";
  73.                 if (!$conn->query($sql))
  74.                   {
  75.                     die ("Something wrong with insert query");
  76.                   }
  77.                 $id = $conn->insert_id;
  78.                 $sql1 = "INSERT INTO details (detid, inservice,globalstatus,managedby,machineconsole,monitoring,application,datacenter,rackspace,mlog)
  79.                        VALUES ('$id','1','1','TechOps','console-mgmt.domain','Monitoring','Application','Datacenter','rackspace','')";
  80.                 if ($conn->query($sql1) === TRUE)
  81.                   {
  82.                     echo "New record created successfully ";
  83.                     echo '<br />';
  84.                   }
  85.                 else
  86.                   {
  87.                     echo "Error: " . $sql1 . "<br>" . $conn->error;
  88.                   }
  89.         break;
  90.         case 1:
  91.                 $row = $result->fetch_assoc();
  92.                 $id = $row['id'];
  93.                 $sql = "UPDATE hostname SET systemname = '$hostname', domain = '$domainname', vendor = '$vendorname', model = '$productname', machinetype = '$machinetype', os = '$operatingsystem', osrelease = '$osrelease', machineserial = '$machineserial' WHERE id = '$id'";
  94.                 if ($conn->query($sql) === TRUE)
  95.                   {
  96.                     echo "Record updated successfully ";
  97.                     echo '<br />';
  98.                   }
  99.                 else
  100.                   {
  101.                     echo "Error: " . $sql . "<br>" . $conn->error;
  102.                   }
  103.         break;
  104.         default:
  105.                 die ("Something went wrong... we got multiple records for hostname... this is bad.  STOP");
  106.         break;
  107.     } #end of switch
  108.  } #end of if ($result = $conn->query($sql))
  109.  else
  110.   {
  111.     die ("Something wrong with select query");
  112.   }
  113.  
  114. #Network Part
  115.  
  116. $interfacez = $data['ansible_facts']['ansible_interfaces'];
  117.   foreach ($interfacez as $interf)
  118.     {
  119.       if (preg_match("/p1/i", $interf)) {
  120.       continue;
  121.     }
  122.   elseif (preg_match("/p2/i", $interf))
  123.     {
  124.       continue;
  125.     }
  126.   elseif (preg_match("/p3/i", $interf))
  127.     {
  128.       continue;
  129.     }
  130.   elseif (preg_match("/p4/i", $interf))
  131.     {
  132.       continue;
  133.     }
  134.   elseif (preg_match("/p5/i", $interf))
  135.     {
  136.       continue;
  137.     }
  138.   elseif (preg_match("/p6/i", $interf))
  139.     {
  140.       continue;
  141.     }
  142.   elseif (preg_match("/lo/i", $interf))
  143.     {
  144.       continue;
  145.     }
  146.  
  147.  
  148. $idevice = $data['ansible_facts']['ansible_' . $interf]['device'];
  149.   if(!isset($data['ansible_facts']['ansible_' . $interf]['ipv4']['address']))
  150.     {
  151.       $iaddress = "N/A";
  152.       $igateway = "N/A";
  153.     }
  154.   else
  155.     {
  156.       $iaddress = $data['ansible_facts']['ansible_' . $interf]['ipv4']['address'];
  157.       $igateway = $data['ansible_facts']['ansible_default_ipv4']['gateway'];
  158.     }
  159. $imacaddress = $data['ansible_facts']['ansible_' . $interf]['macaddress'];
  160.   if(!$data['ansible_facts']['ansible_' . $interf]['active'])
  161.     {
  162.       $istatus = "0";
  163.     }
  164.   else
  165.     {
  166.       $istatus = $data['ansible_facts']['ansible_' . $interf]['active'];
  167.     }
  168.  
  169. $sql = "SELECT * FROM network where netid = '$id'";
  170.     #here i can get the $id for each loop
  171.  if ($result = $conn->query($sql))
  172.     #here i can get the $id for each loop
  173.    {
  174.       switch($result->num_rows)
  175.         {
  176.           case 0:
  177.                   $sql = "INSERT INTO network (netid, interface, ipaddress, macaddress, gateway, interfacestatus)
  178.                          VALUES ('$id','$idevice', '$iaddress', '$imacaddress','$igateway','$istatus')";
  179.                   if ($conn->query($sql) === TRUE)
  180.                     {
  181.                       echo "New record created successfully ".$idevice;
  182.                       echo '<br />';
  183.                     }
  184.                   else
  185.                     {
  186.                       echo "Error: " . $sql . "<br>" . $conn->error;
  187.                     }
  188.                 $id = $conn->insert_id;
  189.                 #here works only first time second time i get an empty variable ? or null ? or what ever
  190.          break;
  191.           case 1:
  192.                   $row = $result->fetch_assoc();
  193.                   $id = $row['netid'];
  194.                   echo $id;
  195.                   echo "<br />";
  196.                     $sql = "UPDATE network SET interface = '$idevice', ipaddress = '$iaddress', macaddress = '$imacaddress', gateway = '$igatewat', interfacestatus = '$istatus',  WHERE netid = '$id'";
  197.                       if ($conn->query($sql) === TRUE)
  198.                         {
  199.                           echo "Record updated successfully ";
  200.                           echo '<br />';
  201.                         }
  202.                       else
  203.                         {
  204.                           echo "Error: " . $sql . "<br>" . $conn->error;
  205.                         }
  206.                 #here works only first time second time i get an empty variable ? or null ? or what ever
  207.          break;
  208.           default:
  209.                   die ("Something went wrong"); #on second loop i end up here and my script dies
  210.          break;
  211. }
  212. }
  213. }  # end foreach on interfaces
  214.  
  215. }
  216.  
  217. }
  218.  
  219. $conn->close();
  220. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement