Advertisement
Guest User

Untitled

a guest
Mar 7th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.53 KB | None | 0 0
  1. <?php
  2.  
  3. $servername = "asd";
  4. $username = "asd";
  5. $password = "asd";
  6. $dbname = "servers";
  7.  
  8.  
  9. // Create connection
  10. $conn = new mysqli($servername, $username, $password, $dbname);
  11. // Check connection
  12. if ($conn->connect_error) {
  13.     die("Connection failed: " . $conn->connect_error);
  14. }
  15.  
  16. //read the json file contents
  17. $files = scandir('ansible/');
  18.  
  19.  
  20. foreach($files as $file) {
  21.     if ($file != '.' && $file != '..') {
  22.  
  23.         $jsondata = file_get_contents('ansible/'.$file);
  24.         $data = json_decode($jsondata, true);
  25.  
  26.         $hostname = $data['ansible_facts']['ansible_hostname'];
  27.  
  28.         echo 'Data For :'.$hostname;
  29.         echo '<br />';
  30.         echo '<br />';
  31.  
  32.         echo $hostname;
  33.         echo '<br />';
  34.         $domainname = $data['ansible_facts']['ansible_domain'];
  35.         echo $domainname;
  36.         echo '<br />';
  37.         $vendorname = $data['ansible_facts']['ansible_system_vendor'];
  38.         echo $vendorname;
  39.         echo '<br />';
  40.         $productname = $data['ansible_facts']['ansible_product_name'];
  41.         echo $productname;
  42.         echo '<br />';
  43.  
  44.         if($vendorname == "VMware, Inc."){
  45.             $machinetype = "VMware Guest";
  46.             echo $machinetype;
  47.             echo '<br />';
  48.         } else {
  49.             $machinetype = "Standalone";
  50.             echo $machinetype;
  51.             echo '<br />';
  52.         }
  53.  
  54.         $operatingsystem = $data['ansible_facts']['ansible_distribution'];
  55.         echo $operatingsystem;
  56.         echo '<br />';
  57.         $osrelease = $data['ansible_facts']['ansible_distribution_version'];
  58.         echo $osrelease;
  59.         echo '<br />';
  60.         $machineserial = $data['ansible_facts']['ansible_product_serial'];
  61.         echo $machineserial;
  62.         echo '<br />';
  63.         echo '<br />';
  64.         echo '<br />';
  65.         echo '<br />';
  66.  
  67.         # First do a query to see if the hostname exists.  If so, grab ID.  If not, then insert and retrieve ID after
  68.         # You shouldn't specify "id" field if it's auto-increment.
  69.         $sql = "SELECT id FROM hostname WHERE systemname = '$hostname'";
  70.         if ($result = $conn->query($sql)) {
  71.             switch ($result->num_rows) {
  72.                 case 0:
  73.                     $sql = "INSERT INTO hostname (id, systemname, domain, vendor, model, machinetype, os, osrelease, machineserial) VALUES ('NULL','$hostname', '$domainname', '$vendorname','$productname','$machinetype','$operatingsystem','$osrelease','$machineserial')";
  74.                     if (!$conn->query($sql)) {
  75.                         die ("Something wrong with insert query");
  76.                     }
  77.                     $id = $conn->insert_id;
  78.                 case 1:
  79.                     $row = $query->fetch_assoc();
  80.                     $id = $row['id'];
  81.                 default:
  82.                     die ("Something went wrong... we got multiple records for hostname... this is bad.  STOP");
  83.             }
  84.         } else {
  85.             die ("Something wrong with select query");
  86.         }
  87.  
  88.         # We should now have $id set or we would have died
  89.  
  90.         $interfacez = $data['ansible_facts']['ansible_interfaces'];
  91.         foreach ($interfacez as $interf) {
  92.  
  93.     echo "$interf <br>\n";
  94. }
  95. /*
  96.             if (interf == "lo") {
  97.                 continue;
  98.             }
  99.             $idevice = $data['ansible_facts']['ansible_' . $interf]['device'];
  100.             $iaddress = $data['ansible_facts']['ansible_' . $interf]['ipv4']['address'];
  101.             $imacaddress = $data['ansible_facts']['ansible_' . $interf]['macaddress'];
  102.             $igateway = $data['ansible_facts']['ansible_default_ipv4']['gateway'];
  103.             $istatus = $data['ansible_facts']['ansible_' . $interf]['active'];
  104.  
  105.             $sql = "INSERT INTO network (id, interface, ipaddress, macaddress, gateway, interfacestatus)
  106.             VALUES ('$id','$idevice', '$iaddress', '$imacaddress','$igateway','$istatus')";
  107.  
  108.             if ($conn->query($sql) === TRUE) {
  109.                 echo "New record created successfully ".$idevice;
  110.                 echo '<br />';
  111.             } else {
  112.                 echo "Error: " . $sql . "<br>" . $conn->error;
  113.             }
  114.         } # end foreach on interfaces
  115. */
  116.     }
  117.  
  118. }
  119.  
  120. $conn->close();
  121. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement