Varnish 是高性能 HTTP 反向代理緩存服務器,監控其命中率、連接數、流量等指標對性能調優至關重要。本文提供一個 PHP 腳本,通過解析 varnishstat -x 的 XML 輸出,實時展示關鍵指標與命中率。
varnishstat 不同參數)shell_exec() 或 exec()varnishstat 權限(可配置 sudoers 免密)<?php
// 執行 varnishstat -x 獲取 XML 統計
$xmlString = shell_exec('/usr/bin/varnishstat -x');
if (!$xmlString) die('無法獲取 varnishstat 輸出,檢查權限與路徑');
$xml = simplexml_load_string($xmlString);
if (!$xml) die('XML 解析失敗');
$stats = [];
foreach ($xml->children() as $child) {
$name = '';
$value = '';
foreach ($child->children() as $sub) {
if ($sub->getName() === 'name') $name = (string)$sub;
elseif ($sub->getName() === 'value') $value = (string)$sub;
}
if ($name) $stats[$name] = $value;
}
// 字節單位轉換
function byteReduce($bytes) {
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
$bytes = (float)$bytes;
$i = 0;
while ($bytes >= 1024 && $i < count($units)-1) {
$bytes /= 1024;
$i++;
}
return round($bytes, 2) . ' ' . $units[$i];
}
// 關鍵指標
$clientConn = isset($stats['client_conn']) ? $stats['client_conn'] : 0;
$clientReq = isset($stats['client_req']) ? $stats['client_req'] : 0;
$cacheHit = isset($stats['cache_hit']) ? $stats['cache_hit'] : 0;
$cacheMiss = isset($stats['cache_miss']) ? $stats['cache_miss'] : 0;
$nLruNuked = isset($stats['n_lru_nuked']) ? $stats['n_lru_nuked'] : 0;
$sBodyBytes = isset($stats['s_bodybytes']) ? $stats['s_bodybytes'] : 0;
$sHdrBytes = isset($stats['s_hdrbytes']) ? $stats['s_hdrbytes'] : 0;
$hitRate = $clientReq > 0 ? round(($cacheHit / $clientReq) * 100, 2) : 0;
$totalBytes = $sBodyBytes + $sHdrBytes;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Varnish 監控面板</title>
<meta http-equiv="refresh" content="10"> <!-- 10秒自動刷新 -->
<style>body{font-family:monospace;margin:20px} .metric{margin:5px 0} .label{display:inline-block;width:250px;font-weight:bold} .val{color:#0066cc} .warn{color:#cc0000} </style>
</head>
<body>
<h1>Varnish 緩存服務器狀態</h1>
<p>更新時間:<?= date('Y-m-d H:i:s') ?></p>
<div class="metric"><span class="label">客戶端連接總數 (client_conn):</span><span class="val"><?= number_format($clientConn) ?></span></div>
<div class="metric"><span class="label">客戶端請求總數 (client_req):</span><span class="val"><?= number_format($clientReq) ?></span></div>
<div class="metric"><span class="label">緩存命中 (cache_hit):</span><span class="val"><?= number_format($cacheHit) ?></span></div>
<div class="metric"><span class="label">緩存未命中 (cache_miss):</span><span class="val"><?= number_format($cacheMiss) ?></span></div>
<div class="metric"><span class="label">緩存命中率:</span><span class="val <?= $hitRate < 50 ? 'warn' : '' ?>"><?= $hitRate ?>%</span></div>
<div class="metric"><span class="label">LRU 淘汰對象 (n_lru_nuked):</span><span class="val <?= $nLruNuked > 0 ? 'warn' : '' ?>"><?= number_format($nLruNuked) ?></span></div>
<div class="metric"><span class="label">總傳輸內容:</span><span class="val"><?= byteReduce($totalBytes) ?> (Header: <?= byteReduce($sHdrBytes) ?>, Body: <?= byteReduce($sBodyBytes) ?>)</span></div>
<h3>所有統計指標</h3>
<table border="1" cellpadding="3">
<tr><th>名稱</th><th>值</th></tr>
<?php foreach ($stats as $k => $v): ?>
<tr><td><?= htmlspecialchars($k) ?></td><td><?= htmlspecialchars($v) ?></td></tr>
<?php endforeach; ?>
</table>
</body>
</html>
| 指標 | 含義 | 關注重點 |
|---|---|---|
| client_conn | 接受的客戶端連接總數 | 流量趨勢 |
| client_req | 處理的客戶端請求總數 | 請求量基準 |
| cache_hit | 緩存命中次數 | 核心指標,越高越好 |
| cache_miss | 緩存未命中,需回源 | 回源壓力 |
| cache_hitrate | 命中率 = hit/req | 目標 > 80%,<50% 需調優 |
| n_lru_nuked | LRU 淘汰對象數 | >0 表示內存不足,需增大緩存 |
| s_bodybytes/s_hdrbytes | 響應體/頭部傳輸字節 | 帶寬使用 |
/etc/sudoers 添加 www-data ALL=(ALL) NOPASSWD: /usr/bin/varnishstat -x,腳本用 sudo /usr/bin/varnishstat -x。host 參數輪詢聚合。此監控頁面輕量、無依賴、秒級部署,能快速讓運維與開發掌握 Varnish 運行狀態,輔助緩存策略調優。