Files
xi-saturationreport/index.php
2026-02-10 18:51:31 +00:00

273 lines
13 KiB
PHP

<?php
//
// Storage Saturation Report Component - Main Display Page
//
require_once(dirname(__FILE__).'/../../../config.inc.php');
require_once(dirname(__FILE__) . '/../../common.inc.php');
// Initialization
pre_init();
init_session();
grab_request_vars();
check_prereqs();
check_authentication(false);
$mode = grab_request_var('mode');
// Return report body only (for AJAX inject and for Chromium PDF export)
if ($mode === 'getreport') {
get_saturationreport_report();
exit;
}
// Submit PDF/JPG command and return JSON { command_id } for reports.js polling.
// Use get_localhost_url() so Chromium fetches the report via localhost (fixes ERR_ACCESS_DENIED when server cannot reach external IP).
if ($mode === 'submitpdf' || $mode === 'submitjpg') {
if (!function_exists('submit_command') || !function_exists('user_generate_auth_token')) {
require_once(dirname(__FILE__) . '/../../utils-reports-export.inc.php');
}
$type = ($mode === 'submitjpg') ? 'jpg' : 'pdf';
$query = array();
foreach ($_GET as $k => $v) {
$query[$k] = $v;
}
$query['token'] = user_generate_auth_token(get_user_id($_SESSION['username']));
$query['locale'] = isset($_SESSION['language']) ? $_SESSION['language'] : 'en_US';
$query['records'] = 100000;
$query['mode'] = 'getreport';
$query['hideoptions'] = 1;
$query['export'] = 1;
$base_url = get_localhost_url();
$path = (substr($base_url, -1) === '/') ? 'includes/components/saturationreport/index.php' : '/includes/components/saturationreport/index.php';
$url = $base_url . $path . '?' . http_build_query($query);
$url = str_replace(["\r", "\n"], '', trim($url));
$filename = 'index-' . uniqid() . '.' . $type;
$args = array(
'filename' => $filename,
'url' => $url,
'type' => $type,
'orientation' => 0,
);
$command_id = submit_command(COMMAND_DOWNLOAD_REPORT, serialize($args));
header('Content-Type: application/json');
echo json_encode(array('command_id' => $command_id));
exit;
}
// Normal display
display_saturationreport();
/**
* Output report HTML only (no full page wrapper).
* Used by: (1) AJAX load into #report, (2) Chromium for PDF/JPG export.
*/
function get_saturationreport_report()
{
define('SATURATIONREPORT_DATA_ONLY', 1);
require_once(dirname(__FILE__) . '/saturationreport.api.php');
$services = get_saturationreport_services_data();
if (isset($services['error'])) {
echo '<div class="alert alert-danger">' . encode_form_val($services['error']) . '</div>';
return;
}
if (!is_array($services)) {
$services = array();
}
$table_class = (function_exists('is_neptune') && is_neptune())
? 'table table-condensed table-striped table-bordered tablesorter'
: 'table table-striped table-bordered';
$hideoptions = grab_request_var('hideoptions', 0);
$export = grab_request_var('export', 0);
$is_export = (int)$hideoptions === 1 || (int)$export === 1;
if ($is_export) {
// Minimal HTML document for Chromium print-to-PDF (and client-side capture)
header('Content-Type: text/html; charset=utf-8');
echo '<!DOCTYPE html><html><head><meta charset="utf-8"><title>' . _('Storage Saturation Report') . '</title>';
echo '<style>';
echo 'body{font-family:inherit;padding:12px;} .text-right{text-align:right;}';
echo '.progress{height:20px;margin:0;background:#e9ecef;border-radius:4px;overflow:hidden;position:relative;}';
echo '.progress-bar{float:left;height:100%;font-size:12px;line-height:20px;text-align:center;font-weight:bold;padding:0 5px;box-sizing:border-box;}';
echo '.progress-bar-success{background:#28a745;color:#fff;}';
echo '.progress-bar-info{background:#fd7e14;color:#212529;}';
echo '.progress-bar-warning{background:#f0ad4e;color:#fff;}';
echo '.progress-bar-danger{background:#dc3545;color:#fff;}';
echo '</style></head><body>';
}
echo '<div id="saturationreport-container" class="saturationreport-report">';
if ($is_export) {
echo '<div class="report-header">';
echo '<h1>' . _('Storage Saturation Report') . '</h1>';
echo '<p class="subtitle">' . _('Summary of Tracked Storage: Volumes') . '</p>';
echo '<p class="report-date">' . _('Report Date:') . ' ' . date('n/j/y, g:i A') . '</p>';
echo '</div>';
}
echo '<table class="' . encode_form_val($table_class) . '" id="saturationreport-table">';
echo '<thead><tr>';
echo '<th>' . _('DISPLAY NAME') . '</th>';
echo '<th>' . _('CAPTION') . '</th>';
echo '<th class="text-right">' . _('DISK SPACE USED') . '</th>';
echo '<th class="text-right">' . _('DISK SPACE AVAILABLE') . '</th>';
echo '<th class="text-right">' . _('PERCENT USED') . '</th>';
echo '</tr></thead><tbody>';
if (count($services) === 0) {
echo '<tr><td colspan="5" class="text-center">' . _('No services with trackvolume custom variable found') . '</td></tr>';
} else {
foreach ($services as $s) {
$host_name = isset($s['host_name']) ? $s['host_name'] : 'N/A';
$caption = isset($s['caption_display']) ? $s['caption_display'] : (isset($s['service_description']) ? $s['service_description'] : 'N/A');
$disk_used = isset($s['disk_used_display']) ? $s['disk_used_display'] : 'N/A';
$disk_available = isset($s['disk_available_display']) ? $s['disk_available_display'] : 'N/A';
$percent = isset($s['disk_usage_percent']) ? $s['disk_usage_percent'] : 0;
$available_bytes = isset($s['disk_available_bytes']) ? (int)$s['disk_available_bytes'] : 0;
$available_extra = '';
if ($available_bytes > 0) {
$available_gb = $available_bytes / (1024 * 1024 * 1024);
if ($available_gb < 5) {
$available_extra = ' text-danger';
} elseif ($available_gb < 50) {
$available_extra = ' text-warning';
}
}
$percent_class = 'progress-bar-success';
if ($percent >= 95) {
$percent_class = 'progress-bar-danger';
} elseif ($percent >= 85) {
$percent_class = 'progress-bar-warning';
} elseif ($percent >= 70) {
$percent_class = 'progress-bar-info';
}
echo '<tr>';
echo '<td>' . encode_form_val($host_name) . '</td>';
echo '<td>' . encode_form_val($caption) . '</td>';
echo '<td class="text-right">' . encode_form_val($disk_used) . '</td>';
echo '<td class="text-right' . $available_extra . '">' . encode_form_val($disk_available) . '</td>';
echo '<td class="text-right">';
if ($percent > 0) {
echo '<div class="progress"><div class="progress-bar ' . $percent_class . '" style="width:' . (float)$percent . '%" role="progressbar" aria-valuenow="' . (float)$percent . '" aria-valuemin="0" aria-valuemax="100">' . number_format($percent, 1) . '%</div></div>';
} else {
echo '<span class="text-muted">N/A</span>';
}
echo '</td>';
echo '</tr>';
}
}
echo '</tbody></table></div>';
if ($is_export) {
echo '</body></html>';
}
}
/**
* Display the storage saturation report (full page with form and report container)
*/
function display_saturationreport()
{
$component_url = get_component_url_base('saturationreport');
$table_class = (function_exists('is_neptune') && is_neptune())
? 'table table-condensed table-striped table-bordered tablesorter'
: 'table table-striped table-bordered';
do_page_start(array('page_title' => _('Storage Saturation Report')), true);
?>
<head>
<link rel="stylesheet" href="<?php echo $component_url; ?>/styles/style.css">
</head>
<body>
<div class="container-fluid saturationreport-page">
<?php
$report_url = get_base_url();
if (substr($report_url, -1) !== '/') {
$report_url .= '/';
}
$report_url .= 'includes/components/saturationreport/index.php';
?>
<form method="get" id="saturationreport-form" data-type="saturationreport">
<div class="well report-options form-inline">
<div class="reportexportlinks">
<?php
if (function_exists('get_add_myreport_html')) {
echo get_add_myreport_html(_('Storage Saturation Report'), $report_url, array());
}
$base = get_base_url();
if (substr($base, -1) !== '/') {
$base .= '/';
}
$schedule_url = $base . 'includes/components/scheduledreporting/schedulereport.php?name=' . urlencode('Storage Saturation Report');
$email_url = $schedule_url . '&sendonce=1';
?>
<a href="<?php echo encode_form_val($schedule_url); ?>" data-url="<?php echo encode_form_val($schedule_url); ?>" alt="<?php echo _('Schedule this Report'); ?>" title="<?php echo _('Schedule this Report'); ?>" class="btn btn-sm btn-default tt-bind btn-report-action icon-in-btn" data-placement="bottom"><i class="material-symbols-outlined md-middle md-400">schedule</i></a>
<a href="<?php echo encode_form_val($email_url); ?>" data-url="<?php echo encode_form_val($email_url); ?>" alt="<?php echo _('Email this Report'); ?>" title="<?php echo _('Email this Report'); ?>" class="btn btn-sm btn-default tt-bind btn-report-action icon-in-btn" data-placement="bottom"><i class="material-symbols-outlined md-middle md-400 md-fill">mail</i></a>
</div>
<button type="button" id="export-pdf-btn" class="btn btn-sm btn-primary btn-export" data-type="submitpdf">
<i class="fa fa-file-pdf-o"></i> <?php echo _('Export PDF'); ?>
</button>
<button type="button" id="export-image-btn" class="btn btn-sm btn-info">
<i class="fa fa-file-image-o"></i> <?php echo _('Export Image'); ?>
</button>
<div class="neptune-drawer-options">
<div class="options-drawer-header">
<h4><?php echo _('Options'); ?></h4>
<i id="close-report-options" class="material-symbols-outlined md-20 md-400 md-button md-action">close</i>
</div>
<div class="reportoptionpicker">
<p class="text-muted"><?php echo _('No additional options for this report.'); ?></p>
</div>
</div>
</div>
</form>
<div class="report-header">
<h1><?php echo _('Storage Saturation Report'); ?></h1>
<p class="subtitle"><?php echo _('Summary of Tracked Storage: Volumes'); ?></p>
<p class="report-date"><?php echo _('Report Date:'); ?> <?php echo date('n/j/y, g:i A'); ?></p>
</div>
<div id="report">
<div id="saturationreport-container">
<table class="<?php echo encode_form_val($table_class); ?>" id="saturationreport-table">
<thead>
<tr>
<th><?php echo _('DISPLAY NAME'); ?></th>
<th><?php echo _('CAPTION'); ?></th>
<th class="text-right"><?php echo _('DISK SPACE USED'); ?></th>
<th class="text-right"><?php echo _('DISK SPACE AVAILABLE'); ?></th>
<th class="text-right"><?php echo _('PERCENT USED'); ?></th>
</tr>
</thead>
<tbody id="saturationreport-services">
<tr>
<td colspan="5" class="text-center">
<i class="fa fa-spinner fa-spin"></i> <?php echo _('Loading...'); ?>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<script>
window.saturationreportApiUrl = '<?php echo $component_url; ?>/saturationreport.api.php';
window.saturationreportBaseUrl = '<?php echo get_base_url(); ?>includes/components/saturationreport/index.php';
</script>
<?php if (function_exists('get_build_id')) { ?>
<script src="<?php echo get_base_url(); ?>includes/js/reports.js?<?php echo get_build_id(); ?>"></script>
<?php } ?>
<script src="<?php echo $component_url; ?>/scripts/main.js"></script>
</body>
<?php
do_page_end(true);
}