Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $servername = "asd";
- $username = "asd";
- $password = "asd";
- $dbname = "servers";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- //read the json file contents
- $files = scandir('ansible/');
- foreach($files as $file) {
- if ($file != '.' && $file != '..') {
- $jsondata = file_get_contents('ansible/'.$file);
- $data = json_decode($jsondata, true);
- $hostname = $data['ansible_facts']['ansible_hostname'];
- echo 'Data For :'.$hostname;
- echo '<br />';
- echo '<br />';
- echo $hostname;
- echo '<br />';
- $domainname = $data['ansible_facts']['ansible_domain'];
- echo $domainname;
- echo '<br />';
- $vendorname = $data['ansible_facts']['ansible_system_vendor'];
- echo $vendorname;
- echo '<br />';
- $productname = $data['ansible_facts']['ansible_product_name'];
- echo $productname;
- echo '<br />';
- if($vendorname == "VMware, Inc."){
- $machinetype = "VMware Guest";
- echo $machinetype;
- echo '<br />';
- } else {
- $machinetype = "Standalone";
- echo $machinetype;
- echo '<br />';
- }
- $operatingsystem = $data['ansible_facts']['ansible_distribution'];
- echo $operatingsystem;
- echo '<br />';
- $osrelease = $data['ansible_facts']['ansible_distribution_version'];
- echo $osrelease;
- echo '<br />';
- $machineserial = $data['ansible_facts']['ansible_product_serial'];
- echo $machineserial;
- echo '<br />';
- echo '<br />';
- echo '<br />';
- echo '<br />';
- # First do a query to see if the hostname exists. If so, grab ID. If not, then insert and retrieve ID after
- # You shouldn't specify "id" field if it's auto-increment.
- $sql = "SELECT id FROM hostname WHERE systemname = '$hostname'";
- if ($result = $conn->query($sql)) {
- switch ($result->num_rows) {
- case 0:
- $sql = "INSERT INTO hostname (id, systemname, domain, vendor, model, machinetype, os, osrelease, machineserial) VALUES ('NULL','$hostname', '$domainname', '$vendorname','$productname','$machinetype','$operatingsystem','$osrelease','$machineserial')";
- if (!$conn->query($sql)) {
- die ("Something wrong with insert query");
- }
- $id = $conn->insert_id;
- case 1:
- $row = $query->fetch_assoc();
- $id = $row['id'];
- default:
- die ("Something went wrong... we got multiple records for hostname... this is bad. STOP");
- }
- } else {
- die ("Something wrong with select query");
- }
- # We should now have $id set or we would have died
- $interfacez = $data['ansible_facts']['ansible_interfaces'];
- foreach ($interfacez as $interf) {
- echo "$interf <br>\n";
- }
- /*
- if (interf == "lo") {
- continue;
- }
- $idevice = $data['ansible_facts']['ansible_' . $interf]['device'];
- $iaddress = $data['ansible_facts']['ansible_' . $interf]['ipv4']['address'];
- $imacaddress = $data['ansible_facts']['ansible_' . $interf]['macaddress'];
- $igateway = $data['ansible_facts']['ansible_default_ipv4']['gateway'];
- $istatus = $data['ansible_facts']['ansible_' . $interf]['active'];
- $sql = "INSERT INTO network (id, interface, ipaddress, macaddress, gateway, interfacestatus)
- VALUES ('$id','$idevice', '$iaddress', '$imacaddress','$igateway','$istatus')";
- if ($conn->query($sql) === TRUE) {
- echo "New record created successfully ".$idevice;
- echo '<br />';
- } else {
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- } # end foreach on interfaces
- */
- }
- }
- $conn->close();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement