怀念夏天 发表于 2025-4-14 00:43:55

【监控工具Sampler】还在用命令行检察服务器状态?试试这款监控工具吧,直观又易用!

我们在检察服务器运行状态时,除了使用命令行照旧命令行。那么有什么办法可以不用命令行,直观地检察服务器的CPU、内存、网络、容器运行状态等信息呢?今天给各人分享一款好用的服务器监控工具Sampler,使用它能快速方便地检察这些信息,盼望对各人有所资助!
Sampler简介

Sampler是一款基于shell命令的可视化监控工具,通过简单的YAML文件来进行设置即可实现监控,目前在Github上已有12k+Star。下面是使用Sampler来进行服务器状态监控的结果图,看起来照旧挺炫酷的!
https://i-blog.csdnimg.cn/direct/f30cd18d7796443eb63473986553cda0.png
安装

Sampler的安装非常简单,这里以Linux系统为例,使用如下命令下载Sampler的安装包;
# 下载sampler的安装包保存到/usr/local/bin目录下
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
安装完成后在/usr/local/bin目录下,就有一个可执行的sampler文件了。
https://i-blog.csdnimg.cn/direct/4ef05ad4b2ef483e9751213d8b0f6e43.png
使用

Sampler的使用非常简单,只要在yaml文件中设置好监控脚本,然后使用如下命令执行即可;
# 配置文件需要添加可执行权限
chmod +x config.yml
# 然后通过配置文件启动Sampler
sampler -c config.yml
假如你想检察服务器CPU和可用内存情况,可以在config.yml文件中添加如下脚本,然后使用上面的命令来运行;
sparklines:
- title: CPU usage(%)
    rate-ms: 200
    scale: 0
    sample: ps -A -o %cpu | awk '{s+=$1} END {print s}'
- title: RAM free(MB)
    rate-ms: 200
    scale: 0
    sample: free -m| grep "Mem:" | awk '{print $4}'
执行命令后,显示结果如下:
https://i-blog.csdnimg.cn/direct/5e17d6d6069c4406897a62bb1e87f91f.png
假如你想检察服务器上Docker容器的运行状态,可以添加如下脚本;
textboxes:
- title: Docker containers stats
    rate-ms: 500
    sample: docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.PIDs}}"
执行命令后,显示结果如下:
https://i-blog.csdnimg.cn/direct/b811cbddc3e24a92a2adf7be565eefdd.png
假如你想检察服务器的网络状态,例如UDP和TCP的收发情况,可以添加如下脚本;
barcharts:
- title: Local network activity
    rate-ms: 500
    scale: 0
    items:
      - label: UDP bytes in
      sample: netstat -anu | awk '{sum += $2} END {print sum}'
      - label: UDP bytes out
      sample: netstat -anu | awk '{sum += $3} END {print sum}'
      - label: TCP bytes in
      sample: netstat -ant | awk '{sum += $2} END {print sum}'
      - label: TCP bytes out
      sample: netstat -ant | awk '{sum += $3} END {print sum}'
执行命令后,显示结果如下:
https://i-blog.csdnimg.cn/direct/e6d8630e670e41abb81f49d254717fbf.png
假如你想检察服务器的时间,可以添加如下脚本;
asciiboxes:
- title: CST time
    rate-ms: 500
    font: 2d
    border: false   
    color: 43
    sample: env TZ=Asia/Shanghai date +%r
执行命令后,显示结果如下:
https://i-blog.csdnimg.cn/direct/74d131beb26a4ef6b21cba1236fcc088.png
我们再把上面这些监控信息组合起来,制作一个面板来看下结果,照旧挺炫酷的;
https://i-blog.csdnimg.cn/direct/d7ed8645445b411ea4b3ee517f1d839d.png
这里使用的脚本如下,感爱好的小伙伴可以尝试下!
runcharts:
- title: Search engine response time
    position: [, ]
    rate-ms: 500
    triggers:
      - title: Latency threshold exceeded
      condition: echo "$prev < 0.8 && $cur > 0.8" |bc -l
      actions:
            terminal-bell: true
            sound: true
            visual: true
            script: 'say alert: ${label} latency exceeded ${cur} second'
    legend:
      enabled: true
      details: false
    scale: 2
    items:
      - label: BAIDU
      color: 178
      sample: curl -o /dev/null -s -w '%{time_total}'https://www.baidu.com
      - label: BING
      sample: curl -o /dev/null -s -w '%{time_total}'https://www.bing.com
barcharts:
- title: Local network activity
    position: [, ]
    rate-ms: 500
    scale: 0
    items:
      - label: UDP bytes in
      sample: netstat -anu | awk '{sum += $2} END {print sum}'
      - label: UDP bytes out
      sample: netstat -anu | awk '{sum += $3} END {print sum}'
      - label: TCP bytes in
      sample: netstat -ant | awk '{sum += $2} END {print sum}'
      - label: TCP bytes out
      sample: netstat -ant | awk '{sum += $3} END {print sum}'
gauges:
- title: YEAR PROGRESS
    position: [, ]
    cur:
      sample: date +%j
    max:
      sample: echo 365
    min:
      sample: echo 0
- title: DAY PROGRESS
    position: [, ]
    cur:
      sample: date +%H
    max:
      sample: echo 24
    min:
      sample: echo 0
- title: HOUR PROGRESS
    position: [, ]
    cur:
      sample: date +%M
    max:
      sample: echo 60
    min:
      sample: echo 0
- title: MINUTE PROGRESS
    position: [, ]
    triggers:
      - title: CLOCK BELL EVERY MINUTE
      condition: '[ $label == "cur" ] && [ $cur -eq 0 ] && echo 1 || echo 0'
      actions:
            sound: true
            script: say -v samantha `date +%I:%M%p`
    cur:
      sample: date +%S
    max:
      sample: echo 60
    min:
      sample: echo 0
sparklines:
- title: CPU usage
    position: [, ]
    rate-ms: 200
    scale: 0
    sample: ps -A -o %cpu | awk '{s+=$1} END {print s}'
- title: RAM free(MB)
    position: [, ]
    rate-ms: 200
    scale: 0
    sample: free -m | grep "Mem:" | awk '{print $4}'
textboxes:
- title: Docker containers stats
    position: [, ]
    rate-ms: 500
    sample: docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.PIDs}}"
- title: Local weather
    position: [, ]
    rate-ms: 10000
    sample: curl wttr.in?0ATQF
    border: false
- title: New York weather
    position: [, ]
    rate-ms: 10000
    sample: curl wttr.in/newyork?0ATQF
    transform: echo 1
    border: false
- title: San Francisco weather
    position: [, ]
    rate-ms: 10000
    sample: curl wttr.in/sanfrancisco?0ATQF
    border: false
asciiboxes:
- title: CST time
    position: [, ]
    rate-ms: 500
    sample: env TZ=Asia/Shanghai date +%r
    border: false
    font: 2d
总结

今天给各人介绍了一款直观易用的服务器监控工具Sampler,使用它来检察服务器运行状态真的是一览无余。其实Sampler的功能不止于此,它还能用于监控MySQL、MongoDB、Kafka这类中间件的信息,该爱好的小伙伴可以尝试下!
项目地点

https://github.com/sqshq/sampler

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 【监控工具Sampler】还在用命令行检察服务器状态?试试这款监控工具吧,直观又易用!