<!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>
  <script src="/js/apexcharts.min.js"></script>
  <!-- Gauge -->
  <link rel="stylesheet" href="/css/gauge.css">
  <script src="/js/jquery.AshAlom.gaugeMeter-2.0.0.min.js"></script>

</head>

<body class="bg-light text-dark">
  <?php include $_SERVER['DOCUMENT_ROOT'] . "/include/all.php"; ?> <!-- Database connexion -->
  <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" 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 -->
        <h1><span class="badge text-bg-secondary font-weight-bold" style="width:100%;">Hyper-V Clusters</span></h1>

        <!-- Main content -->
        <?php
        $clusters = Invoke_Infra("select distinct cluster from cmdb_HyperV_Clusters where cluster <> '' and cluster not like '%-WM%' and cluster not like '%-C1MAS%' order by cluster");
        ?>
        <div class="container-fluid" style="zoom:90%">
          <div class="row">

            <?php
            $count = 0;
            $totalleft = 0;
            foreach ($clusters as $cluster) {
              if ($count / 4 == (int) ($count / 4)) {
                echo "</div><div class='row'>";
              }
              $data = Invoke_Infra("select * from cmdb_HyperV_Clusters where cluster = '" . $cluster['cluster'] . "'");
              $nodes = count($data);
              $vms = 0;
              $vm_mem = 0;
              $disk = 0;
              $io = 0;
              $capacity = 0;
              foreach ($data as $d) {
                $vms += $d['vm_count'];
                $vm_mem += $d['vm_memory'];
                $node_mem = 0 + (int) $d['node_ram'];
                foreach (explode("|", $d['csvs']) as $csv) {
                  if (instr($csv, ';')) {
                    $free = explode(";", $csv)[1] - explode(";", $csv)[2];
                    $disk = max($disk, $free);
                    $capacity += (int) explode(";", $csv)[1];
                  }
                }
                $io += $d['io_disk'];
              }
              $node_mem = $node_mem * count($data) / 2;
              $io = number_format($io / count($data), 2);
              $vmleft = floor(($node_mem - 24 - $vm_mem) / 16);
              $storageleft = floor($disk / 110);
              $vmleft = min($vmleft, $storageleft);
              $totalleft += $vmleft;
              if ($vmleft > 2) {
                $vleft = "<span class='text-success'><b>(" . $vmleft . "</b> VMs left)";
              } elseif ($vmleft == 2) {
                $vleft = "<span class='text-warning'><b>(" . $vmleft . "</b> VMs left)";
              } else {
                $vleft = "<span class='text-danger'><b>(" . $vmleft . "</b> VM left)";
              }

              ?>
              <div class='col-3'>
                <div class='card border-secondary mb-3'>
                  <div class='card-header text-white bg-dark border-secondary'>
                    <h4>
                      <center><?php echo $cluster['cluster']; ?></center>
                    </h4>
                    <h6>
                      <center><b><?php echo $nodes; ?></b> Nodes / <b><?php echo $vms; ?></b> VMs <?php echo $vleft; ?>
                      </center>
                    </h6>
                  </div>
                  <div class='card-body text-white bg-dark'>
                    <div class='row'>

                      <div class='col'><!-- STORAGE -->
                        <?php if($storageleft>2){$color='green';}elseif($storageleft==2){$color='orange';}else{$color='red';}?>
                        <h5><span class="badge text-bg-secondary font-weight-bold" style="width:100%;">Max Storage</span></h5>
                        <center>
                          <div class='GaugeMeter' 
                            data-total='<?php echo (int) $capacity; ?>'
                            data-used='<?php echo (int) ($capacity - $free); ?>' 
                            data-text='<?php echo $disk; ?>'
                            data-size=100 data-width=5 data-back=RGBa(200,200,200,0.05)
                            data-color='<?php echo $color; ?>' data-style='full' data-animationstep=5
                          >
                        </center>
                      </div>

                      <div class='col'><!-- Memory -->
                        <?php if(floor(($node_mem-24-$vm_mem)/16)>2){$color='green';}elseif(floor(($node_mem-24-$vm_mem)/16)==2){$color='orange';}else{$color='red';}?>
                        <h5><span class="badge text-bg-secondary font-weight-bold" style="width:100%;">Max Memory</span></h5>
                        <center>
                          <div class='GaugeMeter' 
                            data-total='<?php echo (int) ($node_mem - 24); ?>' 
                            data-used='<?php echo (int) ($vm_mem); ?>'
                            data-text='<?php echo ($node_mem - 24 - $vm_mem); ?>' 
                            data-size=100 data-width=5 data-back=RGBa(200,200,200,0.05) 
                            data-color='<?php echo $color; ?>' data-style='full' data-animationstep=5
                          >
                        </center>
                      </div>

                      <div class='col'><!-- I/O Disk -->
                        <?php if($io<15){$color='green';}elseif($io<20){$color='orange';}else{$color='red';}?>
                        <h5><span class="badge text-bg-secondary font-weight-bold" style="width:100%;">I/O Disk</span></h5>
                        <center>
                          <div class='GaugeMeter'  
                            data-total=25
                            data-used='<?php echo min((int) $io, 25); ?>' 
                            data-text='<?php echo $io; ?>' 
                            data-size=100 data-width=5 data-back=RGBa(200,200,200,0.05) 
                            data-color='<?php echo $color; ?>' data-style='full' data-animationstep=5
                          >
                        </center>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              <?php
                $count++; }
              ?>
              <br><br><h3><center>Total VM Left : <?php echo $totalleft;?> </center></h3>



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

<script>
  $(".GaugeMeter").gaugeMeter();
</script>