通过日志来查看 squid 的一些基本的运行状态:
1. access.log
配置语句:
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh:%tr
cache_access_log /data1/logs/access.log combined
打下的log格式为:
125.71.196.17 - - [14/May/2008:12:16:13 +0800] "GET http://you.video.sina.com.cn/b/13441121-1212188024.html HTTP/1.1" 200 8820 "http://you.video.sina.com.cn/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)" TCP_MEM_HIT:NONE:21089
可以通过脚本查看一些统计信息,如各种反应状态所占的比例,通常较好的情况下HIT所占的比例(应该就是所谓的命中率)可以在70%~80%
$requests = $hits = $kbytes_out = $diskread = $diskwrite = "";
$command = "/usr/local/squid/bin" . "/squidclient -h {$ip} -p 80 mgr:5min";
$pf = @popen($command, "r");
if ($pf)
{
while (!feof($pf))
{
$s = trim(fgets($pf));
if (strpos($s, "client_http.requests") !== false)
{
//client_http.requests = 78.816658/sec
$requests = str_replace("/sec", "", trim(substr($s, strpos($s, "=")+1)));
}
if (strpos($s, "client_http.hits") !== false)
{
//client_http.hits = 56.603327/sec
$hits = str_replace("/sec", "", trim(substr($s, strpos($s, "=")+1)));
}
if (strpos($s, "client_http.kbytes_out") !== false)
{
//client_http.kbytes_out = 2728.756667/sec
$kbytes_out = str_replace("/sec", "", trim(substr($s, strpos($s, "=")+1)));
}
if (strpos($s, "syscalls.disk.reads") !== false)
{
//syscalls.disk.reads = 341.276664/sec
$diskread = str_replace("/sec", "", trim(substr($s, strpos($s, "=")+1)));
}
if (strpos($s, "syscalls.disk.writes") !== false)
{
//syscalls.disk.writes = 150.846666/sec
$diskwrite = str_replace("/sec", "", trim(substr($s, strpos($s, "=")+1)));
}
}
}
@pclose($pf);
if ($requests == "" && $hits == "")
{
printf("[%02d]IP: %s \t\t 返回为空或者无法连接\n", $i, $ip);
}
else
{
printf("[%02d]IP:%s\t命中率:%u%% \t总请求:%u 次/秒\tSquid命中:%u 次/秒\t流量:%u K/秒\t磁盘读/写:%u/%u 次/秒\n", $i,
$ip, ($hits/$requests)*100, $requests, $hits, $kbytes_out, $diskread, $diskwrite);
//printf("[%02d] IP: %s\t命中率: %u%% \t总请求: %u 次/秒\tSquid命中: %u 次/秒\t流量: %u K/秒\t磁盘读/写: %u/%u 次/秒
\n", $i, $ip, (($diskread-$diskwrite)/$diskread)*100, $requests, $hits, $kbytes_out, $diskread, $diskwrite);
}
PS:官方文档中关于Result Codes的说明
============================================================
TCP_HIT
A valid copy of the requested object was in the cache.
TCP_MISS
The requested object was not in the cache.
TCP_REFRESH_HIT
The requested object was cached but STALE. The IMS query for the object resulted in “304 not modified”.
TCP_REF_FAIL_HIT
The requested object was cached but STALE. The IMS query failed and the stale object was delivered.
TCP_REFRESH_MISS
The requested object was cached but STALE. The IMS query returned the new content.
TCP_CLIENT_REFRESH_MISS
The client issued a “no-cache” pragma, or some analogous cache control command along with the request. Thus, the cachehas to refetch the object.
TCP_IMS_HIT
The client issued an IMS request for an object which was in the cache and fresh.
TCP_SWAPFAIL_MISS
The object was believed to be in the cache, but could not be accessed.
TCP_NEGATIVE_HIT
Request for a negatively cached object, e.g. “404 not found”, for which the cache believes to know that it isinaccessible. Also refer to the explainations for negative_ttl in your squid.conf file.
TCP_MEM_HIT
A valid copy of the requested object was in the cache and it was in memory, thus avoiding disk accesses.
TCP_DENIED
Access was denied for this request.
TCP_OFFLINE_HIT
The requested object was retrieved from the cache during offline mode. The offline mode never validates any object, seeoffline_mode in squid.conf file.
UDP_HIT
A valid copy of the requested object was in the cache.
UDP_MISS
The requested object is not in this cache.
UDP_DENIED
Access was denied for this request.
UDP_INVALID
An invalid request was received.
UDP_MISS_NOFETCH
During “-Y” startup, or during frequent failures, a cache in hit only mode will return either UDP_HIT or this code.Neighbours will thus only fetch hits.
NONE
Seen with errors and cachemgr requests.
The following codes are no longer available in Squid-2:
ERR_*
Errors are now contained in the status code.
TCP_CLIENT_REFRESH
See: TCP_CLIENT_REFRESH_MISS.
TCP_SWAPFAIL
See: TCP_SWAPFAIL_MISS.
TCP_IMS_MISS
Deleted, TCP_IMS_HIT used instead.
UDP_HIT_OBJ
Hit objects are no longer available.
UDP_RELOADING
See: UDP_MISS_NOFETCH.
后面找到了中文版,补充全一些:
access.log 结果编码
相应于HTTP请求,下列标签可能出现在access.log文件的第四个域。
TCP_HIT
Squid发现请求资源的貌似新鲜的拷贝,并将其立即发送到客户端。
TCP_MISS
Squid没有请求资源的cache拷贝。
TCP_REFRESH_HIT
Squid发现请求资源的貌似陈旧的拷贝,并发送确认请求到原始服务器。原始服务器返回304(未修改)响应,指示squid的拷贝仍旧是新鲜的。
TCP_REF_FAIL_HIT
Squid发现请求资源的貌似陈旧的拷贝,并发送确认请求到原始服务器。然而,原始服务器响应失败,或者返回的响应Squid不能理解。在此情形下,squid发送现有cache拷贝(很可能是陈旧的)到客户端。
TCP_REFRESH_MISS
Squid发现请求资源的貌似陈旧的拷贝,并发送确认请求到原始服务器。原始服务器响应新的内容,指示这个cache拷贝确实是陈旧的。
TCP_CLIENT_REFRESH_MISS
Squid发现了请求资源的拷贝,但客户端的请求包含了Cache-Control: no-cache指令。Squid转发客户端的请求到原始服务器,强迫cache确认。
TCP_IMS_HIT
客户端发送确认请求,Squid发现更近来的、貌似新鲜的请求资源的拷贝。Squid发送更新的内容到客户端,而不联系原始服务器。
TCP_SWAPFAIL_MISS
Squid发现请求资源的有效拷贝,但从磁盘装载它失败。这时squid发送请求到原始服务器,就如同这是个cache丢失一样。
TCP_NEGATIVE_HIT
在对原始服务器的请求导致HTTP错误时,Squid也会cache这个响应。在短时间内对这些资源的重复请求,导致了否命中。 negative_ttl指令控制这些错误被cache的时间数量。请注意这些错误只在内存cache,不会写往磁盘。下列HTTP状态码可能导致否定 cache(也遵循于其他约束): 204, 305, 400, 403, 404, 405, 414, 500, 501, 502, 503, 504。
TCP_MEM_HIT
Squid在内存cache里发现请求资源的有效拷贝,并将其立即发送到客户端。注意这点并非精确的呈现了所有从内存服务的响应。例如,某些cache在内存里,但要求确认的响应,会以TCP_REFRESH_HIT, TCP_REFRESH_MISS等形式记录。
TCP_DENIED
因为http_access或http_reply_access规则,客户端的请求被拒绝了。注意被http_access拒绝的请求在第9域的值是NONE/-,然而被http_reply_access拒绝的请求,在相应地方有一个有效值。
TCP_OFFLINE_HIT
当offline_mode激活时,Squid对任何cache响应返回cache命中,而不用考虑它的新鲜程度。
TCP_REDIRECT
重定向程序告诉Squid产生一个HTTP重定向到新的URI(见11.1节)。正常的,Squid不会记录这些重定向。假如要这样做,必须在编译squid前,手工定义LOG_TCP_REDIRECTS预处理指令。
NONE
无分类的结果用于特定错误,例如无效主机名。
相应于ICP查询,下列标签可能出现在access.log文件的第四域。
UDP_HIT
Squid在cache里发现请求资源的貌似新鲜的拷贝。
UDP_MISS
Squid没有在cache里发现请求资源的貌似新鲜的拷贝。假如同一目标通过HTTP请求,就可能是个cache丢失。请对比UDP_MISS_NOFETCH。
UDP_MISS_NOFETCH
跟UDP_MISS类似,不同的是这里也指示了Squid不愿去处理相应的HTTP请求。假如使用了-Y命令行选项,Squid在启动并编译其内存索引时,会返回这个标签而不是UDP_MISS。
UDP_DENIED
因为icp_access规则,ICP查询被拒绝。假如超过95%的到某客户端的ICP响应是UDP_DENIED,并且客户端数据库激活了(见附录A),Squid在1小时内,停止发送任何ICP响应到该客户端。若这点发生,你也可在cache.log里见到一个警告。
UDP_INVALID
Squid接受到无效查询(例如截断的消息、无效协议版本、URI里的空格等)。Squid发送UDP_INVALID响应到客户端。
HTTP响应状态码
下面列出了数字HTTP响应CODE和理由短句。注意Squid和其他HTTP客户端仅仅关注这些数字值。理由短句是纯解释性的,不会影响响应的意义。对每个状态码,也提供了一个到RFC 2616的具体节的索引。注意状态码0和600是squid使用的非标准的值,不会在RFC里提到。
HTTP response status codes:
| Code | Reason phrase | RFC 2616 section |
| 0 | No Response Received (Squid-specific) | N/A |
| 1xx | Informational | 10.1 |
| 100 | Continue | 10.1.1 |
| 101 | Switching Protocols | 10.1.2 |
| 2xx | Successful | 10.2 |
| 200 | OK | 10.2.1 |
| 201 | Created | 10.2.2 |
| 202 | Accepted | 10.2.3 |
| 203 | Non-Authoritative Information | 10.2.4 |
| 204 | No Content | 10.2.5 |
| 205 | Reset Content | 10.2.6 |
| 206 | Partial Content | 10.2.7 |
| 3xx | Redirection | 10.3 |
| 300 | Multiple Choices | 10.3.1 |
| 301 | Moved Permanently | 10.3.2 |
| 302 | Found | 10.3.3 |
| 303 | See Other | 10.3.4 |
| 304 | Not Modified | 10.3.5 |
| 305 | Use Proxy | 10.3.6 |
| 306 | (Unused) | 10.3.7 |
| 307 | Temporary Redirect | 10.3.8 |
| 4xx | Client Error | 10.4 |
| 400 | Bad Request | 10.4.1 |
| 401 | Unauthorized | 10.4.2 |
| 402 | Payment Required | 10.4.3 |
| 403 | Forbidden | 10.4.4 |
| 404 | Not Found | 10.4.5 |
| 405 | Method Not Allowed | 10.4.6 |
| 406 | Not Acceptable | 10.4.7 |
| 407 | Proxy Authentication Required | 10.4.8 |
| 408 | Request Timeout | 10.4.9 |
| 409 | Conflict | 10.4.10 |
| 410 | Gone | 10.4.11 |
| 411 | Length Required | 10.4.12 |
| 412 | Precondition Failed | 10.4.13 |
| 413 | Request Entity Too Large | 10.4.14 |
| 414 | Request-URI Too Long | 10.4.15 |
| 415 | Unsupported Media Type | 10.4.16 |
| 416 | Requested Range Not Satisfiable | 10.4.17 |
| 417 | Expectation Failed | 10.4.18 |
| 5xx | Server Error | 10.5 |
| 500 | Internal Server Error | 10.5.1 |
| 501 | Not Implemented | 10.5.2 |
| 502 | Bad Gateway | 10.5.3 |
| 503 | Service Unavailable | 10.5.4 |
| 504 | Gateway Timeout | 10.5.5 |
| 505 | HTTP Version Not Supported | 10.5.6 |
| 6xx | Proxy Error | N/A |
| 600 | Unparseable Response Headers (Squid-specific) | N/A |
假如Squid从原始服务器没有接受到任何响应,你可以在access.log里看到状态码 0 。假如Squid接受到的响应没有包含HTTP头部,就会出现状态码 600 。在少数情况下,某些原始服务器仅发送响应body,而忽略了任何头部。
access.log对端编码
下列编码可能出现在access.log的第9域。
NONE
这指明Squid对本次请求,不会与任何其他服务器(邻居或原始服务器)通信。它通常与cache命中、拒绝请求、cache管理请求、错误、和所有的ICP查询这些类型联合出现。
DIRECT
Squid直接转发请求到原始服务器。该域的第2半部分显示原始服务器的IP地址,或主机名–假如禁止了log_ip_on_direct。
SIBLING_HIT
在姐妹cache返回ICP或HTCP命中后,Squid发送请求到姐妹cache。
PARENT_HIT
在父cache返回ICP或HTCP命中后,Squid发送请求到父cache。
DEFAULT_PARENT
Squid选择该父cache,因为其在squid.conf的cache_peer行里被标志为default。
FIRST_UP_PARENT
Squid转发请求到该父cache,因为它是位于已知活跃列表里的第一个父cache。
FIRST_PARENT_MISS
Squid转发请求到该父cache,它第一个响应ICP/HTCP丢失消息。换句话说,对这个特殊的ICP/HTCP查询,在这个特殊时刻,被选 中的父cache有最佳的往返时间(RTT)。注意标准RTT可能被人工矫正过,取决于cache_peer指令的weight选项。
CLOSEST_PARENT_MISS
Squid选择该父cache,因为它报告到原始服务器的RTT最低。这点仅在2个cache都激活了netdb,并且原始服务器(或在同一子网内的其他server)返回ICMP ping消息。
CLOSEST_PARENT
这点类似CLOSEST_PARENT_MISS,除了RTT计算不是来自ICP/HTCP响应消息外。代替的,它们来自Squid保留的更老的计算方式,例如netdb交换功能。
CLOSEST_DIRECT
Squid基于netdb算法,转发请求到原始服务器。这点在满足下述任何条件时发生:
- 1)在Squid和原始服务器之间的RTT小于配置的minimum_direct_rtt值。
- 2)在Squid和原始服务器之间的标准路由跳数少于配置的minimum_direct_hops值。
- 3)在ICP/HTCP响应里返回的RTT值,指示Squid离原始服务器近于任何其他邻居。
ROUNDROBIN_PARENT
Squid转发请求到该父cache,因为设置了round-robin选项,并且它有最低的使用计数器。
CD_PARENT_HIT
Squid基于cache摘要算法(见10.7节)转发请求到该父cache。
CD_SIBLING_HIT
Squid基于cache摘要算法转发请求到该姐妹cache。
CARP
Squid选择该父cache,基于cache数组路由协议算法(见10.9节)。
ANY_PARENT
作为最后的手段,Squid选择该父cache,因为没有其他方法能选择可行的下一跳。
注意大部分上述编码可能以TIMEOUT_开头,这表明在等待ICP/HTCP响应时发生超时。
可使用icp_query_timeout指令来调整超时。
例如:
gawk ‘{print $NF}’ access.log |sort|uniq -c|sort -nr
15508 TCP_NEGATIVE_HIT:NONE 在对原始服务器的请求导致HTTP错误时,Squid也会cache这个响应。在短时间内对这些资源的重复请求,导致了否命中。
8212 TCP_IMS_HIT:NONE 客户端发送确认请求,Squid发现更近来的、貌似新鲜的请求资源的拷贝。Squid发送更新的内容到客户端,而不联系原始服务器。(这指明Squid对本次请求,不会与任何其他服务器(邻居或原始服务器)通信。)
3771 TCP_HIT:NONE Squid发现请求资源的貌似新鲜的拷贝,并将其立即发送到客户端。
3468 TCP_MISS:DIRECT Squid没有请求资源的cache拷贝。(Squid直接转发请求到原始服务器)
2379 TCP_MEM_HIT:NONE 从内存的响应
1876 TCP_DENIED:NONE 因为http_access或http_reply_access规则,客户端的请求被拒绝了 全是错误地址链接
1732 TCP_REFRESH_HIT:DIRECT Squid发现请求资源的貌似陈旧的拷贝,并发送确认请求到原始服务器。原始服务器返回304(未修改)响应,指示squid的拷贝仍旧是新鲜的。(Squid直接转发请求到原始服务器)
708 TCP_CLIENT_REFRESH_MISS:DIRECT Squid发现了请求资源的拷贝,但客户端的请求包含了Cache-Control: no-cache指令。Squid转发客户端的请求到原始服务器,强迫cache确认。 (Squid直接转发请求到原始服务器)
7 TCP_MISS:NONE Squid没有请求资源的cache拷贝。(这指明Squid对本次请求,不会与任何其他服务器(邻居或原始服务器)通信)
您还没有登录,请登录后继续操作。