
之前为了jenkins加速npm/pip/mvn/composer构建包和docker镜像而在服务器上部署clash,当时没注意到作者居然还有个clash-tracing仓库,最近看到微信有相关的推文,遂倒腾一下介入到grafana
clash premium部署
clash 和 clash premium作者是同一个,后者拥有更多功能,之前部署时用clash,现在需要改为clash premium
快速安装
可参考仓库:https://github.com/Kr328/clash-premium-installer
[root@localhost ~]# git clone https://github.com/Kr328/clash-premium-installer
[root@localhost ~]# cd clash-premium-installer/
[root@localhost clash-premium-installer]# yum install jq -y
[root@localhost clash-premium-installer]# ./installer.sh install
[root@localhost clash-premium-installer]# systemctl daemon-reload
[root@localhost clash-premium-installer]# systemctl cat clash
# /etc/systemd/system/clash.service
[Unit]
Description=A rule based proxy tunnel
After=network-online.target nftables.service iptabels.service
[Service]
Type=simple
LimitNOFILE=65535
ExecStartPre=/usr/lib/clash/setup-cgroup.sh
ExecStart=/usr/bin/bypass-proxy /usr/bin/clash -d /srv/clash
[Install]
WantedBy=multi-user.target
[root@localhost clash-premium-installer]# systemctl restart clash
# 自动安装会安装在/srv/clash目录内,读者自行将订阅配置文件放到此处
# 此时再次打开clash的ui界面,左下角提示版本已是clash-premium

clash开启tracing
用上面自动安装脚本安装在配置文件中加入以下配置
profile:
tracing: true
clash-tracing部署
clash-tracing:https://github.com/Dreamacro/clash-tracing
[root@blog ~]# cd /data
[root@blog data]# git clone https://github.com/Dreamacro/clash-tracing
[root@blog data]# cd clash-tracing
[root@blog clash-tracing]# ll
total 8
-rw-r--r-- 1 root root 1811 Jul 29 17:59 docker-compose.yml
drwxr-xr-x 4 root root 40 Jul 29 12:48 grafana
drwxr-xr-x 2 root root 25 Jul 29 12:48 loki
-rw-r--r-- 1 root root 234 Jul 29 12:48 README.md
drwxr-xr-x 2 root root 28 Jul 29 12:48 screenshot
drwxr-xr-x 2 root root 25 Jul 29 20:02 vector
vector数据处理配置
vector这个工具博主也是才知道,这个东西看起来功能很强大,用来做etl似乎是个不错的选择,卖个坑后续找个时间调研下
vector数据处理并发送给loki
[root@blog clash-tracing]# cat vector/vector.toml
[sources.mixed]
type = "socket"
address = "0.0.0.0:9000"
mode = "tcp"
decoding.codec = "json"
host_key = "vector-server"
[transforms.traffic-input]
type = "filter"
inputs = [ "mixed" ]
condition = "exists(.up)"
[transforms.tracing-input]
type = "filter"
inputs = [ "mixed" ]
condition = "!exists(.up)"
[transforms.traffic]
type = "remap"
inputs = [ "traffic-input" ]
source = """
.type = "traffic"
del(.source_type)"""
[transforms.tracing]
type = "remap"
inputs = [ "tracing-input" ]
source = """
.type = downcase!(.type)
del(.source_type)
if exists(.@metadata) {
.metadata_dstip = .metadata.destinationIP
.metadata_dstport = .metadata.destinationPort
.metadata_host = .metadata.host
.metadata_network = .metadata.network
.metadata_srcip = .metadata.sourceIP
.metadata_srcport = .metadata.sourcePort
.metadata_type = .metadata.type
.metadata_dnsmode = .metadata.dnsMode
del(.@metadata)
}
"""
[sinks.loki]
type = "loki"
inputs = [ "traffic", "tracing" ]
# 我的loki服务器
endpoint = "http://192.168.44.170:3100"
encoding.codec = "json"
labels.type = "{{ type }}"
上面注意有坑:作者仓库的vector.toml中的sources里的host_key配置为空,这会导致loki接受到的数据
{"":"192.168.0.3","address":"alive.github.com:443","chain":["美国5|cn-us","代理"],"duration":38265,"host":"alive.github.com","id":"fe79c013-c224-4468-b523-a2c66a9d9191","port":49690,"proxy":"美国5|cn-us","type":"proxydial"}
注意第一个key居然为空,这个就是vector中配置host_key,而此时我们grafana再做一次json解析就会报错

err while creating labelset for {="192.168.0.3", address="alive.github.com:443", duration="38265", host="alive.github.com", id="fe79c013-c224-4468-b523-a2c66a9d9191", port="49690", proxy="美国5|cn-us", type="proxydial", type_extracted="proxydial"}: 1:2: parse error: unexpected "=" in label set, expected identifier or "}"
所以的话我们需要配置下vector中的sources块内的host_key
由于这个仓库里的docker-compose文件里已有grafana和loki,读者可以自行根据需要调整。而博主这里已经有loki和grafana,所以就不必在此处部署,此时博主的docker-compose.yaml
[root@blog clash-tracing]# cat docker-compose.yml
version: '3'
services:
# loki:
# image: grafana/loki:2.6.1
# container_name: loki
# restart: always
# user: root
# volumes:
# - ./loki/data:/loki
# - ./loki/config.yaml:/etc/loki/local-config.yaml
# grafana:
# image: grafana/grafana-oss:latest
# container_name: grafana
# restart: always
# user: root
# volumes:
# - ./grafana/data:/var/lib/grafana
# - ./grafana/panels:/etc/dashboards
# - ./grafana/provisioning/dashboards:/etc/grafana/provisioning/dashboards
# - ./grafana/provisioning/datasources:/etc/grafana/provisioning/datasources
# ports:
# - "3000:3000"
vector:
image: timberio/vector:0.X-alpine
container_name: vector
restart: always
volumes:
- ./vector/vector.toml:/etc/vector/vector.toml
traffic_scraper:
image: vi0oss/websocat:0.10.0
container_name: traffic_scraper
restart: always
command: -v --autoreconnect-delay-millis 15000 autoreconnect:ws://${CLASH_HOST}/traffic?token=${CLASH_TOKEN} autoreconnect:tcp:vector:9000
depends_on:
- vector
tracing_scraper:
image: vi0oss/websocat:0.10.0
container_name: tracing_scraper
restart: always
command: -v --autoreconnect-delay-millis 15000 autoreconnect:ws://${CLASH_HOST}/profile/tracing?token=${CLASH_TOKEN} autoreconnect:tcp:vector:9000
depends_on:
- vector
# 配置.env文件,和docker-compse.yml同级目录
# CLASH_TOKEN配置看读者自己的clash配置文件有没有配置secret参数,没有配置则留空
[root@blog clash-tracing]# cat .env
CLASH_HOST="192.168.44.150:9090"
CLASH_TOKEN="abcdefg"
启动采集和中转服务
# 启动服务
[root@blog clash-tracing]# docker-compose up -d
[+] Running 4/4
⠿ Network clash-tracing_default Created 0.1s
⠿ Container vector Started 0.5s
⠿ Container tracing_scraper Started 1.2s
⠿ Container traffic_scraper Started 1.2s
# 这两个采集服务没有错误日志即可
[root@blog clash-tracing]# docker logs tracing_scraper
websocat: It is recommended to either set --binary or --text explicitly
[INFO websocat::lints] Auto-inserting the line mode
[INFO websocat::net_peer] Resolving hostname to IP addresses
[INFO websocat::net_peer] Got IP: 172.29.0.2:9000
[INFO websocat::ws_client_peer] get_ws_client_peer
[INFO websocat::net_peer] Connected to TCP 172.29.0.2:9000
[INFO websocat::ws_client_peer] Connected to ws
[root@blog clash-tracing]#
[root@blog clash-tracing]# docker logs traffic_scraper
websocat: It is recommended to either set --binary or --text explicitly
[INFO websocat::lints] Auto-inserting the line mode
[INFO websocat::net_peer] Resolving hostname to IP addresses
[INFO websocat::net_peer] Got IP: 172.29.0.2:9000
[INFO websocat::ws_client_peer] get_ws_client_peer
[INFO websocat::net_peer] Connected to TCP 172.29.0.2:9000
[INFO websocat::ws_client_peer] Connected to ws
grafana配置
调整dashboard文件,因为作者提供的dashboard文件中使用的datasource是default,而博主这里的default是prometheus,在调整这个面版datasource时会导致query语句丢失。。。裂开,最后调整了下
{
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": {
"type": "loki",
"uid": "eZIPA-pVk"
},
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"target": {
"limit": 100,
"matchAny": false,
"tags": [],
"type": "dashboard"
},
"type": "dashboard"
}
]
},
"editable": true,
"fiscalYearStartMonth": 0,
"graphTooltip": 0,
"id": 16,
"links": [],
"liveNow": false,
"panels": [
{
"datasource": {
"type": "loki",
"uid": "${datasource}"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "thresholds"
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 10,
"w": 6,
"x": 0,
"y": 0
},
"id": 6,
"options": {
"displayMode": "gradient",
"minVizHeight": 10,
"minVizWidth": 0,
"orientation": "horizontal",
"reduceOptions": {
"calcs": [],
"fields": "",
"values": true
},
"showUnfilled": true,
"text": {}
},
"pluginVersion": "9.2.5",
"targets": [
{
"datasource": {
"type": "loki",
"uid": "${datasource}"
},
"expr": "{type=\"proxydial\"}",
"legendFormat": "{{host}}",
"refId": "A",
"resolution": 1
}
],
"title": "域名访问次数",
"transformations": [
{
"id": "extractFields",
"options": {
"replace": true,
"source": "Line"
}
},
{
"id": "groupBy",
"options": {
"fields": {
"host": {
"aggregations": [],
"operation": "groupby"
},
"id": {
"aggregations": [
"count"
],
"operation": "aggregate"
}
}
}
},
{
"id": "sortBy",
"options": {
"fields": {},
"sort": [
{
"desc": true,
"field": "id (count)"
}
]
}
}
],
"type": "bargauge"
},
{
"datasource": {
"type": "loki",
"uid": "${datasource}"
},
"fieldConfig": {
"defaults": {
"custom": {
"align": "auto",
"displayMode": "color-text",
"filterable": true,
"inspect": false
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
}
},
"overrides": [
{
"matcher": {
"id": "byName",
"options": "duration"
},
"properties": [
{
"id": "unit",
"value": "µs"
},
{
"id": "custom.displayMode",
"value": "gradient-gauge"
}
]
},
{
"matcher": {
"id": "byName",
"options": "proxy"
},
"properties": [
{
"id": "custom.width",
"value": 120
}
]
},
{
"matcher": {
"id": "byName",
"options": "address"
},
"properties": [
{
"id": "custom.width",
"value": 340
}
]
}
]
},
"gridPos": {
"h": 10,
"w": 11,
"x": 6,
"y": 0
},
"id": 8,
"options": {
"footer": {
"fields": "",
"reducer": [
"sum"
],
"show": false
},
"frameIndex": 2,
"showHeader": true,
"sortBy": [
{
"desc": true,
"displayName": "duration"
}
]
},
"pluginVersion": "9.2.5",
"targets": [
{
"datasource": {
"type": "loki",
"uid": "${datasource}"
},
"editorMode": "code",
"expr": "{type=\"proxydial\"} | json",
"legendFormat": "",
"maxLines": 20,
"queryType": "range",
"refId": "A"
}
],
"title": "代理时延",
"transformations": [
{
"id": "extractFields",
"options": {
"format": "auto",
"replace": false,
"source": "Line"
}
},
{
"id": "organize",
"options": {
"excludeByName": {
"Field": true,
"First": true,
"Line": true,
"Time": true,
"Value": true,
"address": false,
"chain": true,
"duration": false,
"error": true,
"host": true,
"id": true,
"labels": true,
"localhost": true,
"port": true,
"proxy": false,
"tsNs": true,
"type": true,
"type_extracted": true,
"vector-server": true
},
"indexByName": {
"Value": 7,
"address": 0,
"chain": 1,
"duration": 3,
"host": 4,
"id": 5,
"port": 8,
"proxy": 2,
"type": 6
},
"renameByName": {
"Value": "",
"chain": "",
"proxy": ""
}
}
}
],
"type": "table"
},
{
"datasource": {
"type": "loki",
"uid": "${datasource}"
},
"description": "",
"fieldConfig": {
"defaults": {
"color": {
"mode": "continuous-GrYlRd"
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 6,
"w": 7,
"x": 17,
"y": 0
},
"id": 10,
"options": {
"displayMode": "gradient",
"minVizHeight": 10,
"minVizWidth": 0,
"orientation": "horizontal",
"reduceOptions": {
"calcs": [],
"fields": "",
"values": true
},
"showUnfilled": true,
"text": {}
},
"pluginVersion": "9.2.5",
"targets": [
{
"datasource": {
"type": "loki",
"uid": "${datasource}"
},
"expr": "{type=\"rulematch\"}",
"refId": "A"
}
],
"title": "局域网访问",
"transformations": [
{
"id": "filterFieldsByName",
"options": {
"include": {}
}
},
{
"id": "extractFields",
"options": {
"format": "kvp",
"replace": true,
"source": "Line"
}
},
{
"id": "groupBy",
"options": {
"fields": {
"id": {
"aggregations": [
"count"
],
"operation": "aggregate"
},
"metadata_srcip": {
"aggregations": [
"count"
],
"operation": "groupby"
},
"sourceIP": {
"aggregations": [],
"operation": "groupby"
}
}
}
},
{
"id": "sortBy",
"options": {
"fields": {},
"sort": [
{
"desc": true,
"field": "id (count)"
}
]
}
}
],
"type": "bargauge"
},
{
"datasource": {
"type": "loki",
"uid": "${datasource}"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisCenteredZero": false,
"axisColorMode": "text",
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"lineInterpolation": "smooth",
"lineWidth": 1,
"pointSize": 6,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "auto",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
},
"unit": "µs"
},
"overrides": []
},
"gridPos": {
"h": 6,
"w": 7,
"x": 17,
"y": 6
},
"id": 12,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom",
"showLegend": false
},
"tooltip": {
"mode": "single",
"sort": "none"
}
},
"targets": [
{
"datasource": {
"type": "loki",
"uid": "${datasource}"
},
"expr": "avg_over_time({type=\"rulematch\"} | json | unwrap duration [$__range]) by (type)",
"refId": "A"
}
],
"title": "规则匹配速度",
"transformations": [],
"type": "timeseries"
},
{
"datasource": {
"type": "loki",
"uid": "${datasource}"
},
"description": "",
"fieldConfig": {
"defaults": {
"color": {
"mode": "thresholds"
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
},
"unit": "bytes"
},
"overrides": []
},
"gridPos": {
"h": 4,
"w": 4,
"x": 0,
"y": 10
},
"id": 4,
"options": {
"colorMode": "value",
"graphMode": "area",
"justifyMode": "auto",
"orientation": "auto",
"reduceOptions": {
"calcs": [
"sum"
],
"fields": "",
"values": false
},
"textMode": "auto"
},
"pluginVersion": "9.2.5",
"targets": [
{
"datasource": {
"type": "loki",
"uid": "${datasource}"
},
"expr": "sum by (type) (sum_over_time({type=\"traffic\"} | json | unwrap down [$__interval]))",
"legendFormat": "",
"refId": "A"
},
{
"datasource": {
"type": "loki",
"uid": "${datasource}"
},
"expr": "sum by (type) (sum_over_time({type=\"traffic\"} | json | unwrap up [$__interval]))",
"hide": false,
"refId": "B"
}
],
"title": "流量统计",
"transformations": [
{
"id": "calculateField",
"options": {
"mode": "reduceRow",
"reduce": {
"include": [
"{type=\"traffic\"}"
],
"reducer": "sum"
},
"replaceFields": true
}
}
],
"type": "stat"
},
{
"datasource": {
"type": "loki",
"uid": "${datasource}"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisCenteredZero": false,
"axisColorMode": "text",
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"lineInterpolation": "linear",
"lineWidth": 1,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "auto",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
},
"unit": "µs"
},
"overrides": []
},
"gridPos": {
"h": 8,
"w": 6,
"x": 4,
"y": 10
},
"id": 16,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom",
"showLegend": true
},
"tooltip": {
"mode": "single",
"sort": "none"
}
},
"targets": [
{
"datasource": {
"type": "loki",
"uid": "${datasource}"
},
"expr": "avg_over_time({type=\"dnsrequest\"} | json | unwrap duration [$__range]) by (qType)",
"legendFormat": "{{qType}}",
"refId": "A"
}
],
"title": "DNS 平均查询时间",
"type": "timeseries"
},
{
"datasource": {
"type": "loki",
"uid": "${datasource}"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisCenteredZero": false,
"axisColorMode": "text",
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"lineInterpolation": "smooth",
"lineWidth": 1,
"pointSize": 6,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "auto",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
},
"unit": "µs"
},
"overrides": []
},
"gridPos": {
"h": 8,
"w": 7,
"x": 10,
"y": 10
},
"id": 14,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom",
"showLegend": true
},
"tooltip": {
"mode": "single",
"sort": "none"
}
},
"targets": [
{
"datasource": {
"type": "loki",
"uid": "${datasource}"
},
"expr": "avg_over_time({type=\"proxydial\"} | json | unwrap duration [$__range]) by (proxy)",
"legendFormat": "{{proxy}}",
"refId": "A"
}
],
"title": "代理 Dial 时间",
"transformations": [],
"type": "timeseries"
},
{
"datasource": {
"type": "loki",
"uid": "${datasource}"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisCenteredZero": false,
"axisColorMode": "text",
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"lineInterpolation": "smooth",
"lineWidth": 1,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "auto",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
},
"unit": "binBps"
},
"overrides": []
},
"gridPos": {
"h": 6,
"w": 7,
"x": 17,
"y": 12
},
"id": 2,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom",
"showLegend": true
},
"tooltip": {
"mode": "single",
"sort": "none"
}
},
"targets": [
{
"datasource": {
"type": "loki",
"uid": "${datasource}"
},
"expr": "avg by (type) (sum_over_time({type=\"traffic\"} | json | unwrap up [$__interval]))",
"legendFormat": "Upload",
"refId": "A"
},
{
"datasource": {
"type": "loki",
"uid": "${datasource}"
},
"expr": "avg by (type) (sum_over_time({type=\"traffic\"} | json | unwrap down [$__interval]))",
"hide": false,
"legendFormat": "Download",
"refId": "B"
}
],
"title": "实时流量",
"type": "timeseries"
},
{
"datasource": {
"type": "loki",
"uid": "${datasource}"
},
"description": "",
"fieldConfig": {
"defaults": {
"color": {
"mode": "continuous-blues"
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
},
"unit": "none"
},
"overrides": []
},
"gridPos": {
"h": 4,
"w": 4,
"x": 0,
"y": 14
},
"id": 17,
"options": {
"colorMode": "value",
"graphMode": "area",
"justifyMode": "auto",
"orientation": "auto",
"reduceOptions": {
"calcs": [
"count"
],
"fields": "",
"values": false
},
"textMode": "auto"
},
"pluginVersion": "9.2.5",
"targets": [
{
"datasource": {
"type": "loki",
"uid": "${datasource}"
},
"expr": "sum by (type) (rate({type=\"dnsrequest\"}[$__interval]))",
"legendFormat": "",
"refId": "A"
}
],
"title": "DNS 查询",
"transformations": [],
"type": "stat"
}
],
"refresh": false,
"schemaVersion": 37,
"style": "dark",
"tags": [],
"templating": {
"list": [
{
"current": {
"selected": false,
"text": "Loki",
"value": "Loki"
},
"description": "datasource",
"hide": 0,
"includeAll": false,
"label": "datasource",
"multi": false,
"name": "datasource",
"options": [],
"query": "loki",
"queryValue": "",
"refresh": 1,
"regex": "/.*/",
"skipUrlSync": false,
"type": "datasource"
}
]
},
"time": {
"from": "now-1h",
"to": "now"
},
"timepicker": {},
"timezone": "",
"title": "Clash",
"uid": "pOXPjK-7z",
"version": 8,
"weekStart": ""
}
图例
