记录使用项目 clash-exporter 搭建ShellCrash流量监控平台的流程,部署时博主的shellcrash使用的clash meta核v1.18.5

安装clash-exporter

项目release中下载对应平台版本的可执行文件,解压至/opt/clash_exporter

1
2
3
cd /opt/clash_exporter
chmod 777 clash-exporter
./clash-exporter #试运行

报错

failed to dial: failed to WebSocket dial: expected handshake response status code 101 but got 404

原因是监听默认127.0.0.1:9090地址,但是ShellCrash运行端口为9999

1
2
3
4
vim /etc/profile
export CLASH_HOST="127.0.0.1:9999" # /etc/profile结尾追加

source /etc/profile

再试运行就没报错了

守护进程

设置守护进程需要的环境变量

1
2
3
vim /opt/clash_exporter/env

CLASH_HOST=127.0.0.1:9999

添加守护进程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
vim /etc/systemd/system/clash_exporter.service

[Unit]
Description=clash_exporter
After=network.target

[Service]
Type=simpl
EnvironmentFile=/opt/clash_exporter/env
ExecStart=/opt/clash_exporter/clash-exporter

[Install]
WantedBy=multi-user.target

systemctl daemon-reload
systemctl start clash_exporter
systemctl enable clash_exporter
systemctl status clash_exporter

浏览器打开 http://IP:2112/metrics 确保有输出

安装prometheus

官网 下载对应平台版本,解压至/opt/prometheus

1
2
cd /opt/prometheus
vim prometheus.yml
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
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
- "first_rules.yml"
# - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"

# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.

static_configs:
- targets: ["localhost:9090"]
- job_name: "clash"
metrics_path: /metrics
scrape_interval: 1s
static_configs:
- targets: ["127.0.0.1:2112"]
1
vim first_rules.yml
1
2
3
4
5
groups:
- name: discard_destination
rules:
- record: source_policy_type:clash_network_traffic_bytes_total:sum
expr: sum without (destination, job) (clash_network_traffic_bytes_total)

试运行

1
./prometheus

守护进程

1
vim /etc/systemd/system/prometheus.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Unit]
Description=prometheus
After=rc-local.service

[Service]
Type=simple
User=root
Group=root
WorkingDirectory=/opt/prometheus
ExecStart=/opt/prometheus/prometheus
Restart=always

[Install]
WantedBy=multi-user.target
1
2
3
4
systemctl daemon-reload
systemctl start prometheus
systemctl enable prometheus
systemctl status prometheus

浏览器访问 http://IP:9090 打开 status->Targets 确保clash状态为up

安装Grafana

1
2
apt install grafana
systemctl start grafana-server

修改Grafana默认端口号

因为Grafana默认端口号为3000,已经被我本机的AdGuardHome占用掉了,所以需要修改一下Grafana端口号为4000

1
2
3
4
5
6
vim /etc/grafana/grafana.ini

[http_server]
http_port = 4000

systemctl restart grafana-server

浏览器打开 http://IP:4000

添加数据源

连接->数据源

添加数据源->prometheus

其他地方不用改,Save &test

添加仪表盘

首页->仪表盘->新建->导入

id 18530 加载

Name随便起

数据源选择刚才添加的prometheus

最终效果

存在的问题是DNS和tracing数据为空,这个没办法,非premium核不支持