跳转至

Squid

配置

通过 squid-exporter采集 Squid 的指标数据,DataKit 使用 prom 采集器接收 squid-exporter 发送的指标数据。

Squid 暴露指标

  1. 下载 squid-exporter

  2. 运行 squid-exporter

squid-exporter 暴露了 9301 端口,暴露 Prometheus 格式指标数据

nohup  ./squid-exporter -squid-hostname "localhost" -squid-port 3128 -listen ":9301" &

采集器配置

复制 prom.conf.samplesquid.conf

cd /usr/local/datakit/conf.d/samples/
cp prom.conf.sample squid.conf

调整配置文件 squid.conf

  ## Exporter URLs.
  urls = ["http://127.0.0.1:9301/metrics"]
  ## Stream Size. 
  ## The source stream segmentation size, (defaults to 1).
  ## 0 source stream undivided. 
  # stream_size = 1
  ## Unix Domain Socket URL. Using socket to request data when not empty.
  uds_path = ""
  ## Ignore URL request errors.
  ignore_req_err = false
  ## Collector alias.
  source = "squid-prom"
  ...

指标

指标 描述 单位
client_http_errors_total 客户端 HTTP 请求中返回错误(4xx/5xx)的总次数
client_http_hit_kbytes_out_bytes_total 客户端 HTTP 命中(HIT)时向外发送的流量 KB
client_http_hits_total 客户端 HTTP 请求缓存命中的总次数
client_http_kbytes_in_kbytes_total 客户端 HTTP 请求时从客户端接收的流量 KB
client_http_kbytes_out_kbytes_total 客户端 HTTP 请求时向客户端发送的流量 KB
client_http_requests_total 客户端 HTTP 请求的总次数
exporter_build_info Squid exporter 构建信息(版本、修订号等)
info_Available_number_of_file_descriptors 当前可用文件描述符数量
info_Average_HTTP_requests_per_minute_since_start 自启动以来平均每分钟 HTTP 请求数 次/分钟
info_Average_ICP_messages_per_minute_since_start 自启动以来平均每分钟 ICP 消息数 次/分钟
info_CPU_Time Squid 进程累计消耗的 CPU 时间
info_CPU_Usage 当前瞬时 CPU 使用百分比 %
info_CPU_Usage_5_minute_avg 最近 5 分钟平均 CPU 使用百分比 %
info_CPU_Usage_60_minute_avg 最近 60 分钟平均 CPU 使用百分比 %
info_Disk_hits_as_pct_of_hit_requests_5min 最近 5 分钟缓存命中里磁盘命中的占比 %
info_Disk_hits_as_pct_of_hit_requests_60min 最近 60 分钟缓存命中里磁盘命中的占比 %
info_Files_queued_for_open 等待打开文件的队列长度
info_Hits_as_pct_of_all_requests_5min 最近 5 分钟命中率(命中/总请求) %
info_Hits_as_pct_of_all_requests_60min 最近 60 分钟命中率 %
info_Hits_as_pct_of_bytes_sent_5min 最近 5 分钟命中流量占发送总流量的百分比 %
info_Hits_as_pct_of_bytes_sent_60min 最近 60 分钟命中流量占发送总流量的百分比 %
info_Hot_Object_Cache_Items 热对象缓存条目数量
info_Largest_file_desc_currently_in_use 当前使用中的最大文件描述符号
info_Maximum_Resident_Size Squid 进程最大常驻内存 KB
info_Maximum_number_of_file_descriptors 系统允许的最大文件描述符数量
info_Mean_Object_Size 缓存对象的平均大小 KB
info_Memory_hits_as_pct_of_hit_requests_5min 最近 5 分钟缓存命中里内存命中的占比 %
info_Memory_hits_as_pct_of_hit_requests_60min 最近 60 分钟缓存命中里内存命中的占比 %
info_Number_of_HTCP_messages_received 已接收的 HTCP 消息总数
info_Number_of_HTCP_messages_sent 已发送的 HTCP 消息总数
info_Number_of_HTTP_requests_received 已接收的 HTTP 请求总数
info_Number_of_ICP_messages_received 已接收的 ICP 消息总数
info_Number_of_ICP_messages_sent 已发送的 ICP 消息总数
info_Number_of_clients_accessing_cache 当前访问缓存的客户端数量
info_Number_of_file_desc_currently_in_use 当前已使用的文件描述符数量
info_Number_of_queued_ICP_replies 等待回复的 ICP 队列长度
info_Request_failure_ratio 请求失败率(失败/总请求) %
info_Requests_given_to_unlinkd 交给 unlinkd 删除的请求次数
info_Reserved_number_of_file_descriptors 预留的文件描述符数量
info_Select_loop_called select/poll 循环调用次数
info_Storage_Mem_capacity 内存缓存最大容量 KB
info_Storage_Mem_size 内存缓存当前已用容量 KB
info_Storage_Swap_capacity 磁盘缓存最大容量 KB
info_Storage_Swap_size 磁盘缓存当前已用容量 KB
info_StoreEntries 当前 Store 条目总数
info_StoreEntries_with_MemObjects 带内存对象的 Store 条目数
info_Store_Disk_files_open 当前打开的磁盘文件数量
info_Total_accounted Squid 进程已分配内存总量 KB
info_UP_Time Squid 进程自启动以来的运行时间
info_memPoolAlloc_calls memPool 分配调用次数
info_memPoolFree_calls memPool 释放调用次数
info_on_disk_objects 当前落盘对象数量
info_service Squid 服务状态标识(1=运行中,0=停止)
server_all_errors_total 所有后端(HTTP/FTP/其他)错误总次数
server_all_kbytes_in_kbytes_total 所有后端接收流量 KB
server_all_kbytes_out_kbytes_total 所有后端发送流量 KB
server_all_requests_total 所有后端请求总次数
server_ftp_errors_total FTP 后端错误次数
server_ftp_kbytes_in_kbytes_total FTP 后端接收流量 KB
server_ftp_kbytes_out_kbytes_total FTP 后端发送流量 KB
server_ftp_requests_total FTP 后端请求次数
server_http_errors_total HTTP 后端错误次数
server_http_kbytes_in_kbytes_total HTTP 后端接收流量 KB
server_http_kbytes_out_kbytes_total HTTP 后端发送流量 KB
server_http_requests_total HTTP 后端请求次数
server_other_errors_total 其他协议后端错误次数
server_other_kbytes_in_kbytes_total 其他协议后端接收流量 KB
server_other_kbytes_out_kbytes_total 其他协议后端发送流量 KB
server_other_requests_total 其他协议后端请求次数
swap_files_cleaned_total 清理的 swap 文件总数
swap_ins_total swap in(磁盘 → 内存)次数
swap_outs_total swap out(内存 → 磁盘)次数
up exporter 成功抓取 Squid 指标时为 1,失败为 0

日志

日志采集

squid 默认日志输出到 /var/log/squid 目录下: - access.log: 访问日志 - cache.log: 缓存日志

进入 /usr/local/datakit/conf.d/log 目录,执行以下命令:

cp logging.conf.sample squid.conf

调整配置文件:

[[inputs.logging]]
  # List of log files, supports batch specification using glob patterns.
  # It is recommended to use absolute paths and specify file extensions.
  # Narrow the scope as much as possible to avoid collecting unexpected files.
  logfiles = [
    "/var/log/squid/access.log",
    "/var/log/squid/cache.log"
  ]
  ## Socket currently supports two protocols: tcp/udp. It is recommended to use internal
  ## network ports for security.
  sockets = [
   #"tcp://0.0.0.0:9540",
   #"udp://0.0.0.0:9541",
  ]
  # File path filtering using glob patterns, any file matching these patterns will not be collected
  ignore = [""]
  # Logging source, defaults to 'default' if empty
  source = "squid"
  ...

日志解析

grok(_, "%{NUMBER:timestamp:float}%{SPACE}%{INT:response_time:int}%{SPACE}%{IP:client_ip}%{SPACE}%{WORD:squid_result}/%{INT:http_status:int}%{SPACE}%{INT:bytes:int}%{SPACE}%{WORD:method}%{SPACE}%{NOTSPACE:uri}%{SPACE}-%{SPACE}(?:%{WORD:hier_status}/%{NOTSPACE:hier_ip}|%{NOTSPACE:hier_full})%{SPACE}%{GREEDYDATA:content_type}")

client_ip = get_key("client_ip")
if client_ip {
  add_key("log_type", "squid_access")
  geoip("client_ip")
}

grok(_, "%{DATE:d} %{TIME:t} %{WORD:process}(?:\\|)%{GREEDYDATA:raw_msg}")

if (raw_msg){
  if ( "ERROR" in raw_msg ){
    add_key("status","error")
  }
  drop_key("raw_msg")
  add_key("log_type","squid_cache")
}

if d {
 default_time("d + t", "2006/01/02 15:04:05") 
  drop_key("d")
  drop_key("t")
}

access.log

  • 原始日志
    1756280173.452    362 116.62.210.211 TCP_TUNNEL/200 56389 CONNECT www.guance.com:443 - HIER_DIRECT/61.170.77.75 -
    1756280241.702    303 120.253.192.179 TCP_TUNNEL/200 56249 CONNECT www.guance.com:443 - HIER_DIRECT/61.170.77.74 -
    

字段说明:

字段名 例子 含义
timestamp 1755830457.018 Unix 时间戳(秒.毫秒)
response_time 0 响应时间(毫秒)
client_ip 113.87.27.138 客户端 IP
result TCP_DENIED Squid 结果码(如 TCP_DENIED)
http_status 403 HTTP 状态码
bytes 3809 响应字节数
method CONNECT 请求方法
uri www.guance.com:443 请求 URI
hier_status HIER_NONE 层级状态(HIER_NONE 表示无上游)
hier_ip - 上游服务器地址(无时为 -
content_type text/html 响应 Content-Type

cache.log

  • 原始日志

2025/08/28 09:37:35 kid1| Logfile: opening log stdio:/var/spool/squid/netdb.state
2025/08/28 09:37:35 kid1| Logfile: closing log stdio:/var/spool/squid/netdb.state
2025/08/28 09:37:35 kid1| NETDB state saved; 9 entries, 0 msec
2025/08/28 10:14:17 pinger| SendEcho ERROR: sending to ICMPv6 packet to [2606:4700::6812:1dd6]: (101) Network is unreachable
2025/08/28 10:21:29 kid1| Logfile: opening log stdio:/var/spool/squid/netdb.state
2025/08/28 10:21:29 kid1| Logfile: closing log stdio:/var/spool/squid/netdb.state
2025/08/28 10:21:29 kid1| NETDB state saved; 9 entries, 0 msec
- 日志字段

字段 例子 含义
@timestamp 2025-08-28 10:14:17 日志时间
process pinger 进程标识
status ERROR 日志级别
message SendEcho ERROR: sending to ICMPv6 packet to [2606:4700::6812:1dd6]: (101) Network is unreachable 原始日志正文

文档评价

文档内容是否对您有帮助? ×