[% USE date %]
[% PROCESS _header.tt %]
[% PROCESS _message.tt %]
[% PROCESS _blocks.tt %]

<div class="card w-full" style="min-width: 800px;">
  <div class="mainTableHeader">
    <div>
      [% PROCESS _table_search_field.tt ids=["agentTable"] %]
    </div>
    <div>
      [% PROCESS _pager.tt %]
    </div>
    <div>
      <div class="w-40">
        [% IF reload_required %]
        [% PROCESS _button btn = {
          form   => { action => 'conf.cgi', },
          data   => { sub => 'objects', 'apply' => 'yes', 'reload' => 'yes', CSRFtoken => get_user_token(c), },
          button => { class => 'w-full orange', title => "Apply all changes and reload core", onclick => "removeParams['activate'] = true; return send_form_in_background_and_reload(this);", html => '<i class="uil uil-sync"></i>Activate Changes' },
        }%]
        [% END %]
      </div>
      <a href="agents.cgi?action=new" class="button green w-40 mx-2"><i class="fa-solid fa-plus"></i> add new host</a>

      [% PROCESS _pager_per_page.tt %]
    </div>
  </div>

  <div class="mainTable [% IF main_table_full %]fullsize[% END %] border-t-0" style="max-height: calc(100vh - 155px);">
    <table class="mainTable js-striped hide_on_page_load" id="agentTable">
      <thead>
        <tr>
          [% IF has_sections %]
            <th class="w-20">Section</th>
          [% END %]
          <th class="">
            <a class="sort-by" href='#' data-sort-type="1" data-sort-option="hostname">Hostname</a>
          </th>
          [% IF has_tags %]
            <th class="w-10">
              <a class="sort-by" href='#' data-sort-type="1" data-sort-option="tags">Tags</a>
            </th>
          [% END %]
          <th class="w-20 text-center">
            <a class="sort-by" href='#' data-sort-type="1" data-sort-option="status">Status</a>
          </th>
          [% IF num_backends > 1 %]
            <th class="w-20">
              <a class="sort-by" href='#' data-sort-type="1" data-sort-option="site">Site</a>
            </th>
          [% END %]
          <th class="w-24">
            <a class="sort-by" href='#' data-sort-type="1" data-sort-option="version">Version</a>
          </th>
          <th class="">
            <a class="sort-by" href='#' data-sort-type="1" data-sort-option="os">OS</a>
          </th>
          <th class="w-10">
            <a class="sort-by" href='#' data-sort-type="1" data-sort-option="arch">Arch</a>
          </th>
          <th class="w-20">
            <a class="sort-by" href='#' data-sort-type="1" data-sort-option="cpu">CPU</a>
          </th>
          <th class="w-20">
            <a class="sort-by" href='#' data-sort-type="1" data-sort-option="mem">Memory</a>
          </th>
          <th class="w-20">
            <a class="sort-by" href='#' data-sort-type="1" data-sort-option="disk">Disk</a>
          </th>
          <th class="w-24">
            <a class="sort-by" href='#' data-sort-type="1" data-sort-option="checks">Checks</a>
          </th>
          <th class="w-20">
            <div class="flexrow flex-nowrap gap-x-1 justify-between">
              <div class="flexrow flex-nowrap gap-x-1 h-5">
                <a href="#" onclick="return agents_run_all(this, '.js-update-btn', {})"><i class="fa-solid fa-arrows-rotate text-sm" title="Update inventory for all visible servers"></i></a>
              </div>
            </div>
          </th>
        </tr>
      </thead>
      <tbody class="rowhover">
        [% FOREACH h = data; agent = build_agent(h); hostname = h.name %]
          <tr id="agent_row_[% name2id(h.peer_key) | html %]_[% name2id(hostname) | html %]">
            [% IF has_sections %]
              <td>[% agent.section %]</td>
            [% END %]
            <td class="truncate w-60 min-w-32 max-w-max js-agent-row">
              <div class="flexrow flex-nowrap gap-x-1 justify-between items-center">
                <div class="flex-1">
                  <a href="agents.cgi?action=edit&amp;hostname=[% h.name | uri %]&backend=[% h.peer_key | uri %]" title="[% h.address | html %] - [% h.alias | html %]">[% _host(h) | html %]</a>
                </div>
                [% comment_count = h.comments.size;
                   PROCESS _status_host_attributes
                      hostprefix=""
                      host=h
                      host_comment_count=comment_count
                      with_status=1
                      skip_graph=1
                      loop_index=loop.index
                %]
              </div>
            </td>
            [% IF has_tags %]
              <td
                [% FOREACH t = agent.tags %]
                  data-tag[% loop.index %]="t:[% t | html %]:"
                [% END %]
              >
                <div class="flexrow flex-nowrap gap-x-0.5">
                  [% FOREACH t = agent.tags %]
                    <div class="badge w-min min-w-min clickable" onclick="setTableSearchFilter('t:[% t | html %]:')" title="show all hosts with tag : [% t | html %]">[% t | html %]</div>
                  [% END %]
                </div>
              </td>
            [% END %]
            <td class="w-20">
              [% state  = 'PENDING' %]
              [% output = '' %]
              [% IF info.exists(hostname) %]
                [% state = servicestatetext(info.$hostname) %]
                [% IF info.$hostname.state >= 1 %]
                  [% output = info.$hostname.plugin_output %]
                [% END %]
              [% END %]
              <div class='text-center badge [% state %]'[% IF output != "" %] title="[% output | html %]"[% END %]>[% state %]</div>
            </td>
            [% IF num_backends > 1 %]
              <td>[% peer_name(h) | html %]</td>
            [% END %]
            [% IF state == "OK" %]
            <td data-version="v:[% info.$hostname.version | html %]:">
              <div class="flexrow flex-nowrap gap-x-1">
                <div class="w-5 h-5 m-0 p-0 min-w-fit"><img src="[% url_prefix %]plugins/[% plugin_name %]/images/[% agent.icon | uri %]" alt="[% agent.type | html %]" width=25 height=25></div>
                [% IF info.exists(hostname) %]<div class="w-full h-full clickable" title="[% info.$hostname.full_version | html %]" onclick="setTableSearchFilter('v:[% info.$hostname.version | html %]:')">[% info.$hostname.version | html %]</div>[% END %]
              </div>
            </td>
            <td title="[% info.$hostname.os_version_full | html %]" class="truncate w-60 max-w-max">
              [% info.$hostname.os_version | html %]
            </td>
            <td>[% info.$hostname.os_arch %]</td>
            <td class='align-top relative overflow-hidden' title="[% IF info.$hostname.cpu_perc != ''; sprintf("%d", info.$hostname.cpu_perc*100); "% used"; END %]">
              [% IF info.$hostname.cpu_perc; %]
                [%IF info.$hostname.cpu_perc > 1; info.$hostname.cpu_perc = 1; END %]
                <div style='width: [% 100 * info.$hostname.cpu_perc %]%; height: 100%;' class='[% IF info.$hostname.cpu_state == 2 %]CRITICAL[% ELSIF info.$hostname.cpu_state == 1 %]WARNING[% ELSE %]OK[% END %] absolute top-0 left-0'></div>
              [% END %]
              <span class='absolute top-0 left-0' style='margin-left: 3px;'>
                [% IF info.$hostname.cpu_cores; info.$hostname.cpu_cores; " Cores"; END %]
              </span>
            </td>
            [% perc = 0; IF info.$hostname.memfree; perc = ( info.$hostname.memtotal - info.$hostname.memfree ) / info.$hostname.memtotal; END %]
            [%IF perc > 1; perc = 1; END %]
            <td class='align-top relative overflow-hidden' title="[% IF info.$hostname.memfree; sprintf("%.1f", perc*100); "% used"; END %]">
              <div style='width: [% 100 * perc %]%; height: 100%;' class='[% IF info.$hostname.mem_state == 2 %]CRITICAL[% ELSIF info.$hostname.mem_state == 1 %]WARNING[% ELSE %]OK[% END %] absolute top-0 left-0'></div>
              <span class='absolute top-0 left-0' style='margin-left: 3px;'>
                [% IF info.$hostname.memtotal; n = reduce_number(info.$hostname.memtotal, 'B', 1024); sprintf('%.1f %s',n.0, n.1); END %]
              </span>
            </td>
            [% perc = 0; IF info.$hostname.disk_total; perc = ( info.$hostname.disk_total - info.$hostname.disk_free ) / info.$hostname.disk_total; END %]
            [%IF perc > 1; perc = 1; END %]
            <td class='align-top relative overflow-hidden' title="[% IF info.$hostname.disk_total %][% sprintf("%.1f", perc*100) %]% used, [% n = reduce_number(info.$hostname.disk_free, 'B', 1024); sprintf('%.1f %s',n.0, n.1) %] free[% END %]">
              <div style='width: [% 100 * perc %]%; height: 100%;' class='[% IF info.$hostname.disk_state == 2 %]CRITICAL[% ELSIF info.$hostname.disk_state == 1 %]WARNING[% ELSE %]OK[% END %] absolute top-0 left-0'></div>
              <span class='absolute top-0 left-0' style='margin-left: 3px;'>
                [% IF info.$hostname.disk_total; n = reduce_number(info.$hostname.disk_total, 'B', 1024); sprintf('%.1f %s',n.0, n.1); END %]
              </span>
            </td>
            <td>
              [% IF info.exists(hostname) && info.$hostname.inv_checks != "" %]
                <div class="badge min-w-full text-center [% IF info.$hostname.inv_new > 0 %]WARNING[% END %]" title="[% IF info.$hostname.inv_new > 0 %][% info.$hostname.inv_out | html %][% ELSE %][% info.$hostname.inv_new | html %] new&#13;[% info.$hostname.inv_obsolete | html %] obsolete&#13;[% info.$hostname.inv_checks | html %] active&#13;[% info.$hostname.inv_disabled | html %] disabled[% END %]">
                  <span class="[% IF info.$hostname.inv_new > 0 %]textHINT[% END %]">[% info.$hostname.inv_new | html %]</span> /
                  <span>[% info.$hostname.inv_obsolete | html %]</span> /
                  <span>[% info.$hostname.inv_checks   | html %]</span> /
                  <span>[% info.$hostname.inv_disabled | html %]</span>
                </div>
              [% END %]
            </td>
            [% ELSE %]
            <td colspan="7">
              <a class="link" href="extinfo.cgi?type=2&host=[% h.name | uri %]&service=agent+version&backend=[% h.peer_key | uri %]" title="view agent version service">
                <div class="flexrow flex-nowrap gap-x-1 justify-between">
                  <div>[% info.$hostname.plugin_output.replace("^" _ state _ "(: | - )", "") %]</div>
                  [% IF info.$hostname.last_state_change %]<div title="[% date_format(c, info.$hostname.last_state_change) %]">(for [% duration(date.now - info.$hostname.last_state_change, 6) %])</div>[% END %]
                </div>
              </a>
            </td>
            [% END %]
            <td>
              <div class="flexrow flex-nowrap gap-x-2">
                <form action="cmd.cgi" method="POST">
                  <input type="hidden" name="cmd_typ" value="7">
                  <input type="hidden" name="cmd_mod" value="2">
                  <input type="hidden" name="host" value="[% h.name | html %]">
                  <input type="hidden" name="service" value="agent version">
                  <input type="hidden" name="service" value="agent inventory">
                  <input type="hidden" name="force_check" value="1">
                  <input type="hidden" name="backend" value="[% h.peer_key | html %]">
                  <input type="hidden" name="start_time" value="[% date.now %]">
                  <input type="hidden" name="json" value="1">
                  <input type="hidden" name="CSRFtoken" value="[% get_user_token(c) %]">
                  <a href="#" class="js-update-btn" onclick="return send_form_in_background_and_reload(this)"><i class="fa-solid fa-arrows-rotate text-sm" title="Update inventory"></i></a>
                </form>
                <a href="agents.cgi?action=edit&amp;hostname=[% h.name | uri %]&backend=[% h.peer_key | uri %]"><i class="fa-solid fa-pencil text-sm" title='Edit this host'></i></a>
                <a href="agents.cgi?action=clone&amp;hostname=[% h.name | uri %]&backend=[% h.peer_key | uri %]"><i class="fa-solid fa-clone text-sm" title='Clone this host'></i></a>
                [% PROCESS _button btn = {
                  form   => { action => 'agents.cgi', },
                  data   => { 'action' => 'remove', 'hostname' => h.name, backend => h.peer_key, CSRFtoken => get_user_token(c), },
                  button => { class => 'iconOnly', title => "Remove this host with all services.", onclick => "return(confirm('really remove host?'))", html => '<i class="uil uil-trash-alt"></i>' },
                }%]
              </div>
            </td>
          </tr>
        [% END %]
      </tbody>
    </table>
  </div>
  <div class="mainTableFooter">
    <div class="flex-1 self-center">
      [% PROCESS _pager_total_items.tt %]
    </div>
  </div>
</div>


<script>
<!--
// used for updating all (visible) servers
function agents_run_all(mainBtn, cls, extraData) {
    setBtnSpinner(mainBtn, true);

    var list = [];
    jQuery(cls).each(function(i, el) {
        if(jQuery(el).is(":visible") && !jQuery(el).hasClass("invisible")) {
            list.push(el);
        }
    });

    var running = 0;
    var startNext = function() {
        if(list.length == 0) {
            if(running == 0) {
                setBtnNoSpinner(mainBtn);
                reloadPage();
            }
            return;
        }
        running++;
        var btn = list.shift();
        var rowid = jQuery(btn).parents("TR").attr("id");
        setBtnSpinner(btn, true);
        var form = jQuery(btn).parents('FORM');
        submitFormInBackground(form, function() {
            running--;
            refresh_table_rows("agents.cgi", {}, "#"+rowid);
            setBtnNoSpinner(btn);
            startNext();
        }, extraData);
    }
    var parallel = 3;
    for(var x = 0; x < parallel; x++) {
        startNext();
    }
}
-->
</script>


[% PROCESS _footer.tt %]
