一切皆可监控 - v2ray

一切皆可监控

Promethues exporter for V2ray metrics

V2Ray Exporter an exporter that collect V2Ray metrics over its Stats API and export them to Prometheus.

V2ray Dashboard

  • StatsObject

V2Ray提供一些关于其运行状态的统计信息.StatsObject对应配置文件(v2ray的配置文件 config.json)中的stats项.

1
2
3
{
"stats": {} // 目前统计信息没有任何参数,只要StatsObject项存在,内部的统计即会开启,同时你还需要在`Policy`中开启对应的项,才可以统计对应的数据.
}

目前已有的统计信息如下:

  • 用户数据

user ->>> [email] ->>> traffic ->>> uplink
特定用户的上行流量,单位字节bytes

user ->>> [email] ->>> traffic ->>> downlink
特定用户的下行流量,单位字节bytes

如果对应用户没有指定Email, 则不会开启统计

  • 全局数据

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
  • Grafana Dashboard

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的路由规则
  • config.json 模版
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.62KB inbound>>>api>>>traffic>>>downlink
2 170.72KB inbound>>>api>>>traffic>>>uplink
3 76.96MB inbound>>>tcp>>>traffic>>>downlink
4 2.26MB inbound>>>tcp>>>traffic>>>uplink
5 79.36MB outbound>>>direct>>>traffic>>>downlink
6 2.15MB outbound>>>direct>>>traffic>>>uplink
7 79.36MB user>>>iphone>>>traffic>>>downlink

Total: 240.80MB
  • 启动V2Ray-Exporter
  • 二进制方式运行

    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 Service
Documentation=https://www.v2fly.org/
After=network.target nss-lookup.target

[Service]
User=username
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
NoNewPrivileges=true
ExecStart=/usr/local/bin/v2ray-exporter --v2ray-endpoint "127.0.0.1:10085"
Restart=on-failure
RestartPreventExitStatus=23

[Install]
WantedBy=multi-user.target

启动v2ray-exporter服务

1
2
3
$ sudo systemctl daemon-reload
$ sudo systemctl enable v2ray-exporter
$ sudo systemctl 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; 1h 51min ago
Docs: https://www.v2fly.org/
Main PID: 938268 (v2ray-exporter)
Tasks: 5 (limit: 1030)
Memory: 12.1M
CPU: 4.098s
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-62d9e88 (built 2021-12-23T07:52:01Z)
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

v2ray export v0.6.0


一切皆可监控 - v2ray
https://blog.chyidl.com/2024/01/30/anything-goes-exporter/
作者
Yaqing Chyi
发布于
2024年1月30日
许可协议