<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Page Title -->
  <title>Web Infra Reports</title>
  <link rel="shortcut icon" type="image/png" href="/include/favicon-32x32.png">

  <!-- JQuery -->
  <script src="/js/jquery-3.6.1.min.js"></script>
  <!-- Bootstrap -->
  <link rel="stylesheet" href="/css/bootstrap.min.css">
  <link rel="stylesheet" href="/css/bootstrap-icons/bootstrap-icons.css">
  <link rel="stylesheet" href="/css/preloader.css">
  <script src="/js/bootstrap.bundle.min.js"></script>
  <!-- Bootstrap-tables -->
  <link rel="stylesheet" href="/css/bootstrap-table.min.css">
  <script src="/js/bootstrap-table.min.js"></script>
  <script src="/js/bootstrap-table-fr-FR.min.js"></script>
  <script src="/js/tableExport.min.js"></script>
  <script src="/js/bootstrap-table-export.min.js"></script>
  <script src="/js/libs/js-xlsx/xlsx.core.min.js"></script>

</head>

<body class="bg-light text-dark">
  <?php include $_SERVER['DOCUMENT_ROOT'] . "/include/global.php"; ?>
  <?php  // DATA
    $servers = Invoke_Infra("select * from x_inventory where type is not null");
    $UTDS1 = (Invoke_Infra("select max(Ver) as utd from X_cmdb_Product where Name = 's1'"))[0]['utd'];
    $UTDNessus = (Invoke_Infra("select max(Ver) as utd from X_cmdb_Product where Name = 'nessus'"))[0]['utd'];
    $UTDZabbix = (Invoke_Infra("select max(Ver) as utd from X_cmdb_Product where Name = 'zabbix'"))[0]['utd'];
  ?>

  <!-- HTML -->
  <div class="container-fluid" id="content">
    <div class="row flex-nowrap">
      <!-- Left NAVBAR -->
      <div class="col-auto col-md-2 col-xl-2 px-sm-2 px-0 bg-dark vh-100 position-sticky top-0" style="-ms-flex: 0 0 230px;flex: 0 0 230px;">
        <?php include $_SERVER['DOCUMENT_ROOT'] . "/navbar.html"; ?>
      </div>
      <!-- Display -->
      <div class="col py-3">
        <!-- Page Title -->
        <h1><span class="badge text-bg-secondary font-weight-bold" style="width:100%;">AIX / Linux inventory</span></h1>

        <!-- Main content -->
        <div class="container-fluid">
          <!-- TABLE -->
          <div>
          <table class='table table-bordered table-hover table-sm' id='t1' data-height="620" data-toggle="table" data-search="true" data-show-columns="true" data-export-types="['xlsx','csv','json']" data-show-export="true" data-sortable="true" data-sort-name="VM">
              <thead> <!-- Header -->
                <tr>
                <th data-field="Name" data-sortable="true">Name</th>
                <th data-field="Host" data-sortable="true">Host</th>
                <th data-field="OS" data-sortable="true">OS Type</th>
                <th data-field="distrib" data-sortable="true">Distrib</th>
                <th data-field="Last Boot" data-sortable="true">Last reboot</th>
                <th data-field="Last Backup" data-sortable="true">Last backup</th>
                <th data-field="Auth" data-sortable="true">Auth</th>
                <th data-field="SentinelOne" data-sortable="true">SentinelOne</th>
                <th data-field="Nessus" data-sortable="true">Nessus</th>
                <th data-field="Zabbix" data-sortable="true">Zabbix</th>
                </tr>
              </thead>

              <tbody> <!-- Body -->
                <?php
                $total = 0;
                foreach ($servers as $s) {
                  $total++;
                  echo "<tr>";
                    //name
                    $name = $s['hostname'];
                    echo "<td><a href='/inventory/server-detail.php?s=".$name."' target='_blank'>$name</a></td>";

                    //Host
                    echo "<td>".$s['Owner']."</td>";
                    //OS Type
                    $typ = match ($s['type'] ?? null) {
                      'AIX'    => 'AIX',
                      null, '' => '',
                      default  => 'LINUX',
                    };
                    echo "<td>".$s['type']."</td>";

                    //Distrib
                    $Distrib = "";
                    if($typ == "AIX"){
                      $Distrib = $s['osversion'];
                    }
                    if($typ == "LINUX"){
                      $Distrib = $s['osversion']." <small>(".str_replace(".x86_64","",$s['kernel']).")</small>";
                    }
                    echo "<td>$Distrib</td>";

                    //LastReboot
                    $lr = $s['lastboot'] ?? ""; $lrDate="";
                    try {
                      $lrDate = new DateTime($lr);
                     } catch (DateMalformedStringException $e) {}
                    $interval = (new DateTime())->diff($lrDate);
                    if($interval->days > 60){
                      echo "<td class='table-danger'>$lr</td>";
                    }else{
                      if($interval->days > 1){
                        echo "<td>$lr</td>";
                      }else{
                        echo "<td class='table-success'>$lr</td>";
                      }
                    }

                    //LastBackup
                    $lr = explode(".",$s['backuplast'] ?? "")[0];
                    $size = 0;
                    if($s['backuplog'] != "") {
                      if (str_contains($s['backuplog'],"Length")) {
                        $size = explode(" ", $s['backuplog'])[1];
                      } else {
                        $size = explode(" ", $s['backuplog'])[3];
                      }
                      $size = floor($size / 1024 / 1024 / 1024);
                    }
                    if($size >= 15){
                      $size = "<span class='text-danger'><b>$size GB</b></span>";
                    }else{
                      $size = "$size GB";
                    }
                    try {
                      $lrDate = new DateTime($lr);
                     } catch (DateMalformedStringException $e) {}
                    $interval = (new DateTime())->diff($lrDate);
                    if($interval->days > 7 || $s['backuplast'] == ""){
                      echo "<td class='table-danger'>$lr"." ".$size."</td>";
                    }else{
                      if($interval->days > 1){
                        echo "<td>".date_format($lrDate,"Y-m-d H:i:s")." - ".$size."</td>";
                      }else{
                        echo "<td class='table-success'>$lr"." - ".$size."</td>";
                      }
                    }

                    //Auth
                    echo "<td>".$s['auth']."</td>";

                    //SentinelOne
                    $s1s = $s['S1State']; $s1v = $s['S1Version'];
                    if($typ == "LINUX"){
                      if($s1s == "YY"){
                        if($s1v == $UTDS1) {
                          echo "<td class='table-success'>$s1v $s1s</td>";
                        }else{
                          echo "<td class='table-warning'><b>$s1v</b> $s1s</td>";
                        }
                      }else{
                        if($s1v == "") {
                          echo "<td class='table-danger text-center'>MISSING</td>";
                        }else{
                          echo "<td class='table-danger'>$s1v <b>$s1s</b></td>";
                        }
                      }
                    }else{
                      echo "<td></td>";
                    }

                  //Nessus
                  $s1s = $s['NessusState']; $s1v = $s['NessusVersion'];
                  if($typ == "LINUX"){
                    if($s1s == "YYY_YYY"){
                      if($s1v == $UTDNessus) {
                        echo "<td class='table-success'>$s1v $s1s</td>";
                      }else{
                        echo "<td class='table-warning'><b>$s1v</b> $s1s</td>";
                      }
                    }else {
                      if ($s1v == "") {
                        echo "<td class='table-danger text-center'>MISSING</td>";
                      } else {
                        echo "<td class='table-danger'>$s1v <b>$s1s</b></td>";
                      }
                    }
                  }else{
                    echo "<td></td>";
                  }

                  //Zabbix
                  $s1s = $s['ZabbixState']; $s1v = $s['ZabbixVersion'];
                    if($s1s == "Y"){
                      if($s1v == $UTDZabbix) {
                        echo "<td class='table-success'>$s1v $s1s</td>";
                      }else{
                        echo "<td class='table-warning'><b>$s1v</b> $s1s</td>";
                      }
                    }else{
                      if($s1v == "") {
                        echo "<td class='table-danger text-center'>MISSING</td>";
                      }else{
                        echo "<td class='table-danger'>$s1v <b>$s1s</b></td>";
                      }
                    }

                  echo "</tr>";
                }

                ?>
              </tbody>
            </table><br>
            <h5 class="text-center"><?php echo $total; ?> servers</h5>
          </div>
        </div>
        <!-- End of main content -->
      </div>
    </div>
  </div>
</body>
<script src="/js/switch.js"></script>
</HTML>
<script src="/js/tableResize.js"></script>