sampler-配置

Github 仓库地址

Sampler 用于动态展示程序的输出, 官方给的效果图如:

安装

具体可以看官方仓库, Linux 下的安装为:

1
2
sudo wget https://github.com/sqshq/sampler/releases/download/v1.1.0/sampler-1.1.0-linux-amd64 -O /usr/local/bin/sampler
sudo chmod +x /usr/local/bin/sampler

配置

-c (--config) 参数指定配置文件, 如:

1
sampler -c config.yaml

配置文件为 yaml 格式.

有可选的几种展示形式:

  • Runchart
  • Sparkline
  • Barchart
  • Gauge
  • Textbox
  • Asciibox

Runchart 示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
runcharts:
- title: Search engine response time
rate-ms: 500 # sampling rate, default = 1000
scale: 2 # number of digits after sample decimal point, default = 1
legend:
enabled: true # enables item labels, default = true
details: false # enables item statistics: cur/min/max/dlt values, default = true
items:
- label: GOOGLE
sample: curl -o /dev/null -s -w '%{time_total}' https://www.google.com
color: 178 # 8-bit color number, default one is chosen from a pre-defined palette
- label: YAHOO
sample: curl -o /dev/null -s -w '%{time_total}' https://search.yahoo.com
- label: BING
sample: curl -o /dev/null -s -w '%{time_total}' https://www.bing.com

设置命令运行的间隔

rate-ms.

设置取样值小数点后的位数

scale.

设置 legend

配置 legend 用 legend, 其下有两个开关, enabled 表明是否启用 legend, details 表明是否显示详细输出.

设置为:

1
2
3
legend:
enabled: true
details: false

设置为:

1
2
3
legend:
enabled: true
details: true

设置并行执行的任务

item, 如:

1
2
3
4
5
6
7
8
9
items:
- label: GOOGLE
sample: curl -o /dev/null -s -w '%{time_total}' https://www.google.com
color: 178 # 8-bit color number, default one is chosen from a pre-defined palette
- label: YAHOO
sample: curl -o /dev/null -s -w '%{time_total}' https://search.yahoo.com
- label: BING
sample: curl -o /dev/null -s -w '%{time_total}' https://www.bing.com

label 值为 legend 会显示的值.

sample 用于获取取样值.

sparklines 示例

1
2
3
4
5
6
7
8
9
sparklines:
- title: CPU usage
rate-ms: 200
scale: 0
sample: ps -A -o %cpu | awk '{s+=$1} END {print s}'
- title: Free memory pages
rate-ms: 200
scale: 0
sample: memory_pressure | grep 'Pages free' | awk '{print $3}'

gauges 示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
gauges:
- title: Minute progress
rate-ms: 500 # sampling rate, default = 1000
scale: 2 # number of digits after sample decimal point, default = 1
percent-only: false # toggle display of the current value, default = false
color: 178 # 8-bit color number, default one is chosen from a pre-defined palette
cur:
sample: date +%S # sample script for current value
max:
sample: echo 60 # sample script for max value
min:
sample: echo 0 # sample script for min value
- title: Year progress
cur:
sample: date +%j
max:
sample: echo 365
min:
sample: echo 0

设置多个分屏

添加多个 title 即可.

设置只显示百分数

1
2
- title: test
percent-only: true # toggle display of the current value, default = false

设置颜色

1
2
- title: test
color: 178 # 8-bit color number, default one is chosen from a pre-defined palette

设置当前值, 最大最小值

1
2
3
4
5
6
7
- title: test
cur:
sample: date +%j
max:
sample: echo 365
min:
sample: echo 0

textboxes 示例

1
2
3
4
5
6
7
8
9
textboxes:
- title: Local weather
rate-ms: 10000 # sampling rate, default = 1000
sample: curl wttr.in?0ATQF
border: false # border around the item, default = true
color: 178 # 8-bit color number, default is white
- title: Docker containers stats
rate-ms: 500
sample: docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.PIDs}}"

是否添加边框

1
2
- title: Local weather
border: false # border around the item, default = true

asciiboxesa 示例

1
2
3
4
5
6
7
asciiboxes:
- title: UTC time
rate-ms: 500 # sampling rate, default = 1000
font: 3d # font type, default = 2d
border: false # border around the item, default = true
color: 43 # 8-bit color number, default is white
sample: env TZ=UTC date +%r

设置字体

可选的为 2d3d.

1
2
3
asciiboxes:
- title: UTC time
font: 3d # font type, default = 2d

设置主题

1
2
3
4
theme: light # default = dark
sparklines:
- title: CPU usage
sample: ps -A -o %cpu | awk '{s+=$1} END {print s}'

设置变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
variables:
mongoconnection: mongo --quiet --host=localhost test
barcharts:
- title: MongoDB documents by status
items:
- label: IN_PROGRESS
init: $mongoconnection
sample: db.getCollection('events').find({status:'IN_PROGRESS'}).count()
- label: SUCCESS
init: $mongoconnection
sample: db.getCollection('events').find({status:'SUCCESS'}).count()
- label: FAIL
init: $mongoconnection
sample: db.getCollection('events').find({status:'FAIL'}).count()

设置 triggers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
gauges:
- title: MINUTE PROGRESS
position: [[0, 18], [80, 0]]
cur:
sample: date +%S
max:
sample: echo 60
min:
sample: echo 0
triggers:
- title: CLOCK BELL EVERY MINUTE
condition: '[ $label == "cur" ] && [ $cur -eq 0 ] && echo 1 || echo 0' # expects "1" as TRUE indicator
actions:
terminal-bell: true # standard terminal bell, default = false
sound: true # NASA quindar tone, default = false
visual: false # notification with current value on top of the component area, default = false
script: say -v samantha `date +%I:%M%p` # an arbitrary script, which can use $cur, $prev and $label variables

在 sample 之前和之后的处理

在 sample 之前运行 (有且运行一次), 用 init.

对每一次 sample 之后处理, 用 transform.

1
2
3
4
5
6
textboxes:
- title: MongoDB polling
rate-ms: 500
init: mongo --quiet --host=localhost test # executes only once to start the interactive session
sample: Date.now(); # executes with a required rate, in scope of the interactive session
transform: echo result = $sample # executes in scope of local session, $sample variable is available for transformation

需运行多条命令的 init 用 `multistep-init:

1
2
3
4
5
6
7
textboxes:
- title: Java application uptime
multistep-init:
- java -jar jmxterm-1.0.0-uber.jar
- open host:port # or local PID
- bean java.lang:type=Runtime
sample: get Uptime

sampler-配置
http://example.com/2023/11/05/sampler-配置/
作者
Jie
发布于
2023年11月5日
许可协议