<!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">
   <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>
   <!-- Chart -->
   <script src="/js/chart.min.js"></script>
   <!-- Blink -->
   <style>
    @keyframes blink {
      0% { opacity: 1; }
      50% { opacity: 0.25; }
      100% { opacity: 1; }
    }
    .blinking {
      animation: blink 1.2s infinite;
    }
   </style>
</head>

<body class="bg-light text-dark">
  <?php include $_SERVER['DOCUMENT_ROOT']."/include/all.php" ; ?>
  <?php
    //GET-DATAS
    $server = $_GET['s'];
    $Qw = Invoke_Infra("select * from cmdb_srvall where hostname = '$server'");
    $Qx = Invoke_Infra("select * from X_srvall where hostname = '$server'");
    $Qvm = Invoke_Infra("select * from cmdb_vms where Name = '$server'");
    //if(!(is_array($Qw) || is_array($Qx) || is_array($Qvm) )){$notfound = 1;}else{$notfound = 0;}
    $notfound = ! (is_array($Qw) || is_array($Qx) || is_array($Qvm));
 
    // OS
    if(is_array($Qw)){
      $OSType = "Windows"; $OSName = $Qw[0]['os']; $OSKernel = "";
    }else{
      if($Qx[0]['Type'] == "AIX"){
        $OSType = "AIX"; $OSName = "AIX ".$Qx[0]['OSVersion'] ; $OSKernel = "";
      }else{
        $OSType = "Linux"; $OSName = $Qx[0]['Type']." ".$Qx[0]['OSVersion'] ; $OSKernel = $Qx[0]['Kernel'];
      }
    }
    echo "<div id='type' data-value='".strtolower($OSType)."' hidden></div>";
    echo "<div id='computer' data-value='$server' hidden></div>";

    // Memory & CPU
    if($OSType == "Windows"){
      $memory = $Qw[0]['memory']." GB";$cpu = $Qw[0]['cpu'];
    }else{
      $cpu = $Qx[0]['CPU'] ; 
      if($OSType == "AIX"){
        $memory = round(str_replace(" Mo","",$Qx[0]['Memory'])/1024,2)." GB";
      }else{
        $memory = $Qvm[0]['Memory'] ?? $Qx[0]['Memory'] ;
      }
    }
    echo "<div id='type' data-value='".strtolower($OSType)."' hidden></div>";
    echo "<div id='computer' data-value='$server' hidden></div>";
  ?>


  <div class="container-fluid">
    <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" ; ?>  <!-- Left Navbar -->
      </div>
      <!-- Display -->
      <div class="col py-3">
        <!-- Page Title -->
      <?php
        //DECOM ???
        if($notfound == 1){
          echo '<h1><span class="badge text-bg-danger font-weight-bold" style="width:100%;">'. strtoupper($server) .' : NOT FOUND !</span></h1>';
          echo '<script src="/js/switch.js"></script>';
          echo "</div></div></body></HTML>";
          exit;
        }

        $isDecomInQw = is_array($Qw) && !empty($Qw[0]['DECOM']) && str_contains($Qw[0]['DECOM'], '-');
        $isDecomInQx = is_array($Qx) && !empty($Qx[0]['Decom']) && str_contains($Qx[0]['Decom'], '-');
        if($isDecomInQw || $isDecomInQx){
          echo '<h1><span class="badge text-bg-danger font-weight-bold blinking" style="width:100%;">'. strtoupper($server) .' : DECOMMISSIONED !</span></h1>';
          echo '<script src="/js/switch.js"></script>';
          echo "</div></div></body></HTML>";
          exit;
        }else{
          echo '<h1><span class="badge text-bg-secondary font-weight-bold" style="width:100%;">' . strtoupper($server) .'</span></h1>';
        }
      ?>
        <!-- Main content -->
        
        <!-- OS, Host, Site, Maintenance --> 
        <div id="OS" class="fs-4"> 
          <span class="badge rounded-pill bg-secondary text-light"><?= $OSName ?></span>
          <?php if($OSKernel){echo "&nbsp;<span class='badge rounded-pill bg-secondary text-light'>$OSKernel</span>";} ?>
          <?php 
            if(is_array($Qvm)){
              $site = Invoke_Infra("SELECT site from cmdb_physical_site where hostname ='".$Qvm[0]['Owner']."'")[0]['site'];
              if($Qvm[0]['Owner'] == $Qvm[0]['PreferredOwner']){
                echo "&nbsp;<span class='badge rounded-pill bg-success text-light'>Host : ".$Qvm[0]['Owner']."</span>";
              }else{
                echo "&nbsp;<span class='badge rounded-pill bg-warning text-dark'>Host : ".$Qvm[0]['Owner']."</span>";
              }
            }else{
              if(is_array($Qx)){
                echo "&nbsp;<span class='badge rounded-pill bg-success text-light'>Host : ".$Qx[0]['Owner']."</span>";
                $site = substr($Qx[0]['Owner'],0,3);
              }else{
                echo "&nbsp;<span class='badge rounded-pill bg-success text-light'>Physical</span>";
                echo "&nbsp;<span class='badge rounded-pill bg-success text-light'>".Invoke_GLPI("select a.name from glpi_computermodels a left join glpi_computers b on a.id = b.computermodels_id where b.name =  '$server'")[0]['name']."</span>";
              }
            }
          ?>
          <?php if(isset($site)){echo "<span class='badge rounded-pill bg-secondary text-light'>Site : $site</span>";} ?>
          <?php if(is_array($Qvm) && $Qvm[0]['IsClustered'] != 'True'){echo "&nbsp;<span class='badge rounded-pill bg-warning text-dark'>Unclustered VM</span>";} ?>
          <?php 
            $m = Invoke_Infra("select * from Maintenance_Status where server ='$server'");
            if($m[0]['scom'] == "Y" || $m[0]['zabbix'] == "Y" || $m[0]['aixcmdb'] == "Y"){
              echo "&nbsp;<span class='badge rounded-pill bg-primary text-dark blinking'>Maintenance ON</span>";
            }
          ?>
        </div>
        <hr>

        <!-- CPU & Memory -->
        <div class="row" id="perf"> 
          <div class="chart-container col">
            <div class="card mb-3" style="border: 3px solid black">
              <div class="card-header text-dark text-center fs-5 bg-info">
                <i class="fs-7 bi-cpu text-black"></i><b> CPU : <?= $cpu ?></b>
                <?php if(is_array($Qvm)){if($Qvm[0]['CPUcomp'] == "True"){echo '<span class="badge rounded-pill bg-warning text-dark">VM CPU compatibility checked</span> ';}} ?>
              </div>
              <div class="card-body bg-dark fs-4">
                <canvas id="cpuChart"></canvas>
              </div>
            </div>
          </div>
          <div class="chart-container col">
            <div class="card mb-3" style="border: 3px solid black">
              <div class="card-header text-dark text-center fs-5 bg-info">
                <i class="fs-7 bi-memory text-black"></i><b> Memory : <?= $memory ?></b>
              </div>
              <div class="card-body bg-dark fs-4">
                <canvas id="ramChart"></canvas>
              </div>
            </div>
          </div>
          <script src="fillchart.js"></script>
        </div>

        <!-- Drives -->
        <?php
          // LUNs ?
          $luns_result = Invoke_WebInfraTools("select count(*) as lun from storage_lun where hostname = '$server'");
          $lun_count = $luns_result[0]['lun'] ?? 0;
          $wwpn_string = $Qvm[0]['WWPNs'] ?? '';

          if ($lun_count > 0 || $wwpn_string !== '') {
            if ($lun_count > 0) {
              $luns_display = $lun_count . " LUNs";
            } else {
              $luns_display = "Some LUNs are owned by cluster";
            }
            $wwpns_display = str_replace("|", ", ", $wwpn_string);
            echo "<div style=\"text-align: center;\">";
            echo "<span class='badge rounded-pill bg-primary text-light fs-5'>$luns_display</span>";
            if ($wwpns_display !== '') {
              echo "&nbsp;<span class='badge rounded-pill bg-secondary text-light fs-5'>WWPNs: $wwpns_display</span>";
            }
            echo "</div><br>";
          }
        ?>
        <div class="row" id="drives">
          <?php
            // Drives
            if($OSType == "Windows"){
              $drives = explode("|",$Qw[0]['drives'] ?? '');
            }else{
              $drives = explode("|",$Qx[0]['Drives'] ?? '');
            }
            if($drives[0] != ""){
              foreach($drives as $drive){
                echo "<div class='col-6'><div class='row'>";
                  $data = explode(",",$drive);
                  echo "<div class='col-3'><b>".$data[0]."</b> : ".round(intval($data[2]))."/<b>".$data[1]." GB</b></div>";
                  $pcent = (intval($data[1]) - intval($data[2]))/intval($data[1])*100;
                  $pcent_restant = 100 - $pcent;
                  if($pcent <= 5){
                    $color = "bg-danger"; $border = "red";
                  }else{
                    if($pcent <= 10){
                      $color = "bg-warning"; $border = "yellow";
                    }else{
                      $color = "bg-success"; $border = "green";
                    }
                  }
                  echo "<div class='col-9'>
                          <div class='progress flex-grow-1' style='border: 2px solid $border; height: 20px; min-width: 150px;'>
                            <div class='progress-bar bg-dark progress-bar-striped' role='progressbar' style='width: $pcent_restant%;' aria-valuenow='$pcent_restant' aria-valuemin='0' aria-valuemax='100'></div>
                            <div class='progress-bar $color' role='progressbar' style='width: $pcent%;' aria-valuenow='$pcent' aria-valuemin='0' aria-valuemax='100'></div>
                          </div>
                        </div>";
                echo "</div></div>";
              }
            }
          ?>
        </div>
        <hr>

        <!-- Last boot, last rollup, next rollup -->
        <?php
          if(is_array($Qw) && $Qw[0]['lastBoot'] != ""){
            echo "<span class='badge rounded-pill bg-secondary text-light fs-5'>Last Boot : ".$Qw[0]['lastBoot']."</span>";
          }elseif(is_array($Qx) && $Qx[0]['LastBoot'] != ""){
            echo "<span class='badge rounded-pill bg-secondary text-light fs-5'>Last Boot : ".$Qx[0]['LastBoot']."</span>";
          }
          if(is_array($Qw) && $Qw[0]['LastKBUpdate'] != ""){
            echo "&nbsp;<span class='badge rounded-pill bg-secondary text-light fs-5'>Last Update : ".$Qw[0]['LastKBUpdate']."</span>";
          }elseif(is_array($Qx) && $Qx[0]['LastUpdate'] != ""){
            echo "<span class='badge rounded-pill bg-secondary text-light fs-5'>Last Update : ".$Qx[0]['LastUpdate']."</span>";
          }
          if($OSType == "Windows"){
            $date = Invoke_Entry01("select next from rollupdate where Server='$server'");
            if(is_array($date)){
              if($date != ""){
                echo "&nbsp;<span class='badge rounded-pill bg-secondary text-light fs-5'>Next Rollup : ".$date[0]['next']."</span>";
              }
            }
          }
          if($OSType == "Linux"){
            $date = Invoke_Infra("select Next from rollup_linux_calendar where Server='$server'")[0]['Next'];
            if($date != ""){
              echo "&nbsp;<span class='badge rounded-pill bg-secondary text-light fs-5'>Next Rollup : $date </span>";
            }
          }
        ?>
        <hr>
        
        <!-- Zabbix, Sentinel1, Nessus -->
        <?php
          if(is_array($Qw)){
            if(strpos($Qw[0]['zabbix'], '.')){
              echo "<span class='badge rounded-pill bg-secondary text-light fs-5'>Zabbix : ".$Qw[0]['zabbix']."</span>";
            }else{
              echo "<span class='badge rounded-pill bg-warning text-dark fs-5'>Zabbix : Not Installed</span>";
            }
            if(strpos($Qw[0]['sentinel'], '.')){
              echo "&nbsp;<span class='badge rounded-pill bg-secondary text-light fs-5'>SentinelOne : ".$Qw[0]['sentinel']."</span>";
            }else{
              echo "&nbsp;<span class='badge rounded-pill bg-warning text-dark fs-5'>SentinelOne : Not Installed</span>";
            }
            if(strpos($Qw[0]['zabbix'], '.')){
              echo "&nbsp;<span class='badge rounded-pill bg-secondary text-light fs-5'>Nessus : ".$Qw[0]['nessus']."</span>";
            }else{
              echo "&nbsp;<span class='badge rounded-pill bg-warning text-dark fs-5'>Nessus : Not Installed</span>";
            }
          }
          if(is_array($Qx)){
            $a = Invoke_Infra("select * from X_cmdb_Product where HOSTNAME='$server' and Name ='zabbix'");
            if(is_array($a) && strpos($a[0]['Ver'], '.')){
              echo "<span class='badge rounded-pill bg-secondary text-light fs-5'>Zabbix : ".$a[0]['Ver']." - ".$a[0]['State']."</span>";
            }else{
              echo "<span class='badge rounded-pill bg-warning text-dark fs-5'>Zabbix : Not Installed</span>";
            }
            $a = Invoke_Infra("select * from X_cmdb_Product where HOSTNAME='$server' and Name ='s1'");
            if(is_array($a) && strpos($a[0]['Ver'], '.')){
              echo "&nbsp;<span class='badge rounded-pill bg-secondary text-light fs-5'>SentinelOne : ".$a[0]['Ver']." - ".$a[0]['State']."</span>";
            }else{
              echo "&nbsp;<span class='badge rounded-pill bg-warning text-dark fs-5'>SentinelOne : Not Installed</span>";
            }
            $a = Invoke_Infra("select * from X_cmdb_Product where HOSTNAME='$server' and Name ='nessus'");
            if(is_array($a) && strpos($a[0]['Ver'], '.')){
              echo "&nbsp;<span class='badge rounded-pill bg-secondary text-light fs-5'>Nessus : ".$a[0]['Ver']." - ".$a[0]['State']."</span>";
            }else{
              echo "&nbsp;<span class='badge rounded-pill bg-warning text-dark fs-5'>Nessus : Not Installed</span>";
            }
          }
          ?>

          <hr>

          <!-- Backup for AIX, Linux -->
          <?php
            if(is_array($Qx)){
              $backup = Invoke_Infra("select * from X_cmdb_Backupsys where HOSTNAME='$server'");
              echo "<span class='badge rounded-pill bg-secondary text-light fs-5'>LastBackup : ".explode(".",$backup[0]['LastBackup'])[0]."</span>&nbsp;";
              echo "<span class='badge rounded-pill bg-secondary text-light fs-5'>BackupType : ".$backup[0]['BackupType']."</span>&nbsp;";
              echo "<span class='badge rounded-pill bg-secondary text-light fs-5'>Media : ".$backup[0]['Location']."</span>";
            }

          ?>
        <!-- End of main content -->
      </div>
    </div>
  </div>
  <script src="/js/switch.js"></script>
</body>

