一切皆可监控
Promethues exporter for V2ray metrics
V2Ray Exporter an exporter that collect V2Ray metrics over its Stats API and export them to Prometheus.
V2Ray提供一些关于其运行状态的统计信息.StatsObject
对应配置文件(v2ray的配置文件 config.json)中的stats项.
1 2 3 { "stats" : {} // 目前统计信息没有任何参数,只要StatsObject项存在,内部的统计即会开启,同时你还需要在`Policy`中开启对应的项,才可以统计对应的数据. }
目前已有的统计信息如下:
user ->>> [email] ->>> traffic ->>> uplink 特定用户的上行流量,单位字节bytes
user ->>> [email] ->>> traffic ->>> downlink 特定用户的下行流量,单位字节bytes
inbound ->>> [tag] ->>> traffic ->>> uplink 特定入站代理的上行流量, 单位字节bytes
inbound ->>> [tag] ->>> traffic ->>> downlink 特定入站代理的下行流量, 单位字节bytes
快速入门
1 2 3 $ wget -O /tmp/v2ray-exporter https://github.com/wi1dcard/v2ray-exporter/releases/latest/download/v2ray-exporter_linux_amd64 $ mv /tmp/v2ray-exporter /usr/local/bin/v2ray-exporter$ chmod +x /usr/local/bin/v2ray-exporter
V2Ray Dashboard
教程 需要确认v2ray config.json文件中API和statistics统计配置已经开启
V2Ray(> 4.26.0+)具有流量统计功能,默认情况下不开启,流量统计分为三类: inbound-入站流量
, user-用户流量
, outbound-出站流量
inbound
: 入站inbound
是配置config.json
中入站统计,需要根据标签tag
记录入站流量
user
user是VMESS协议中的用户统计,用户的电子邮件是统计和区分的依据,socks,shadowsocks,http和其他协议中的用户不支持统计
outbound
出站指的是配置中每个出站的统计数据,自4.26.0新增的出站流量需要根据标签进行记录
Configuration of Stats - 配置Stats
要启用traffic
流量统计功能,配置中必须包含以下配置
1 2 3 4 1. `"stats": {}` 必须出现在配置中2. `"policy"` 统计开关必须为true , 全局统计开关在"system" 中,用户统计开关在"levels" 中3. 相应的`inbound` 和`outbound` 配置中必须有`tag` 标记4. 用户统计必须在"clients" 中包含电子邮件
要使用api
查询流量,要确保配置中存在以下配置
1 2 3 1 . `api`配置对象中有`StatsService`2 . 使用`dokodemo`协议的标签为`api`3 . 必须有inboundTag:api -> outboundTag:api的路由规则
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 { "stats" : { } , "api" : { "tag" : "api" , "services" : [ "StatsService" ] } , "policy" : { "levels" : { "0" : { "statsUserUplink" : true , "statsUserDownlink" : true } } , "system" : { "statsInboundUplink" : true , "statsInboundDownlink" : true , "statsOutboundUplink" : true , "statsOutboundDownlink" : true } } , "inbounds" : [ { "tag" : "tcp" , "port" : 3307 , "protocol" : "vmess" , "settings" : { "clients" : [ { "email" : "auser" , "id" : "e731f153-4f31-49d3-9e8f-ff8f396135ef" , "level" : 0 , "alterId" : 64 } , { "email" : "buser" , "id" : "e731f153-4f31-49d3-9e8f-ff8f396135ee" , "level" : 0 , "alterId" : 64 } ] } } , { "listen" : "127.0.0.1" , "port" : 10085 , "protocol" : "dokodemo-door" , "settings" : { "address" : "127.0.0.1" } , "tag" : "api" } ] , "outbounds" : [ { "tag" : "direct" , "protocol" : "freedom" , "settings" : { } } ] , "routing" : { "rules" : [ { "inboundTag" : [ "api" ] , "outboundTag" : "api" , "type" : "field" } ] , "domainStrategy" : "AsIs" } }
查看流量信息是v2ray
的功能之一,使用v2ray api -h
查看查询示例,上面配置中设置的api dokodemo-door
端口就是--server
参数的端口
v2ray v5版本已经移除v2ctl
命令,所有有关资料上使用v2ctl
命令都可以使用v2ray
命令替换
1 2 3 4 5 6 7 8 9 10 11 $ v2ray api stats --server=127.0 .0 .1 :10085 Value Name 1 559.62 KB inbound>>>api>>>traffic>>>downlink2 170.72 KB inbound>>>api>>>traffic>>>uplink3 76.96 MB inbound>>>tcp>>>traffic>>>downlink4 2.26 MB inbound>>>tcp>>>traffic>>>uplink5 79.36 MB outbound>>>direct >>>traffic>>>downlink6 2.15 MB outbound>>>direct >>>traffic>>>uplink7 79.36 MB user>>>iphone>>>traffic>>>downlink Total: 240.80 MB
二进制方式运行
1 v2ray-exporter --v2ray-endpoint "127.0.0.1:10085"
systemd服务模式运行
新建/etc/systemd/system/v2ray-exporter.service
服务文件,内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [Unit] Description =V2Ray Exporter ServiceDocumentation =https://www.v2fly.org/After =network.target nss-lookup.target[Service] User =usernameCapabilityBoundingSet =CAP_NET_ADMIN CAP_NET_BIND_SERVICEAmbientCapabilities =CAP_NET_ADMIN CAP_NET_BIND_SERVICENoNewPrivileges =true ExecStart =/usr/local/bin/v2ray-exporter --v2ray-endpoint "127.0.0.1:10085" Restart =on -failureRestartPreventExitStatus =23 [Install] WantedBy =multi-user.target
启动v2ray-exporter
服务
1 2 3 $ sudo system ctl daemon-reload $ sudo system ctl enable v2ray-exporter $ sudo system ctl start v2ray-exporter
查看服务运行情况
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ➜ sudo systemctl status v2ray-exporter.service ● v2ray-exporter.service - V2Ray Exporter Service Loaded: loaded (/etc/ systemd/system/ v2ray-exporter.service ; enabled ; vendor preset: enabled) Active: active (running) since Tue 2024 -01 -30 13 :40 :02 +08 ; 1 h 51 min ago Docs: https: Main PID: 938268 (v2ray-exporter) Tasks: 5 (limit: 1030 ) Memory: 12.1 M CPU: 4.098 s CGroup: /system.slice/v2ray-exporter.service └─938268 /usr/ local/bin/ v2ray-exporter --v2ray-endpoint 0.0 .0 .0 :10085 Jan 30 13 :40 :02 unreal-pod-1. localdomain systemd[1 ]: Started V2Ray Exporter Service. Jan 30 13 :40 :02 unreal-pod-1. localdomain v2ray-exporter[938268 ]: V2Ray Exporter v0.6 .0 -62 d9e88 (built 2021 -12 -23 T07:52 :01 Z) Jan 30 13 :40 :02 unreal-pod-1. localdomain v2ray-exporter[938268 ]: time = "2024-01-30T13:40:02+08:00" level = info msg = "Server is ready to handle incoming scrape reques>
v2ray-exporter
命令启动的时候可以使用--listen
命令修改默认监听端口9550
. 现在你可以打开http://localhost:9550