点击上方“民工哥技术之路”,选择“设为星标”

回复“1024”获取独家整理的学习资料!

Sampler是一个用于shell命令执行,可视化和告警的工具。其配置使用的是一个简单的YAML文件。

1、为什么我需要它?

你可以直接从终端对任意动态进程进行采样 – 观察数据库中的更改,监控MQ动态消息(in-flight messages),触发部署脚本并在完成后获取通知。

如果有一种方法可以使用shell命令获取指标(metric),那么可以使用Sampler立即对其进行可视化。

2、安装

macOS

brew cask install sampler

sudo curl -Lo /usr/local/bin/sampler https://github.com/sqshq/sampler/releases/download/v1.0.3/sampler-1.0.3-darwin-amd64
sudo chmod +x /usr/local/bin/sampler

Linux

sudo wget https://github.com/sqshq/sampler/releases/download/v1.0.3/sampler-1.0.3-linux-amd64 -O /usr/local/bin/sampler
sudo chmod +x /usr/local/bin/sampler

注意:需要为Sampler安装libasound2-dev系统库用以播放触发器声音。通常库已安装在相应位置,但如果没有 – 你可以使用你习惯的包管理器进行安装,例如apt install libasound2-dev

Windows(实验)

建议在高级控制台模拟器下使用,如Cmder

Download .exe

3、使用

指定shell命令,Sampler会相应的速率执行这些命令。输出用于可视化。

使用Sampler基本上的三步过程:

在YAML配置文件中定义shell命令

运行sampler -c config.yml

在UI上调整组件大小和位置

市面早已有许多监控系统

Sampler绝不是监控系统的替代品,而是易于设置的开发工具。

如果spinning up和使用Grafana配置Prometheus是完全多余的任务,那么Sampler可能是正确的解决方案。没有服务器,没有数据库,不需要部署 – 你指定了shell命令,它就可以工作了。

我监控的每台服务器上都需要安装吗?

不,你可以在本地运行Sampler,但仍然可以从多台远程计算机上收集遥测数据。任何可视化都可能具有init命令,你可以在其中ssh到远程服务器。请参阅SSH example

4、组件

以下是每种组件类型的配置示例列表,其中包含与macOS兼容的采样脚本。

Runchart

runcharts:- title: Search engine response timerate-ms: 500        # sampling rate, default = 1000scale: 2            # number of digits after sample decimal point, default = 1legend:enabled: true     # enables item labels, default = truedetails: false    # enables item statistics: cur/min/max/dlt values, default = trueitems:- label: GOOGLEsample: curl -o /dev/null -s -w '%{time_total}'  https://www.google.comcolor: 178      # 8-bit color number, default one is chosen from a pre-defined palette- label: YAHOOsample: curl -o /dev/null -s -w '%{time_total}'  https://search.yahoo.com- label: BINGsample: curl -o /dev/null -s -w '%{time_total}'  https://www.bing.com

Sparkline

sparklines:- title: CPU usagerate-ms: 200scale: 0sample: ps -A -o %cpu | awk '{s+=$1} END {print s}'- title: Free memory pagesrate-ms: 200scale: 0sample: memory_pressure | grep 'Pages free' | awk '{print $3}'

Barchart

barcharts:- title: Local network activityrate-ms: 500        # sampling rate, default = 1000scale: 0            # number of digits after sample decimal point, default = 1items:- label: UDP bytes insample: nettop -J bytes_in -l 1 -m udp | awk '{sum += $4} END {print sum}'- label: UDP bytes outsample: nettop -J bytes_out -l 1 -m udp | awk '{sum += $4} END {print sum}'- label: TCP bytes insample: nettop -J bytes_in -l 1 -m tcp | awk '{sum += $4} END {print sum}'- label: TCP bytes outsample: nettop -J bytes_out -l 1 -m tcp | awk '{sum += $4} END {print sum}'

Gauge

gauges:- title: Minute progressrate-ms: 500        # sampling rate, default = 1000scale: 2            # number of digits after sample decimal point, default = 1percent-only: false # toggle display of the current value, default = falsecolor: 178          # 8-bit color number, default one is chosen from a pre-defined palettecur:sample: date +%S  # sample script for current valuemax:sample: echo 60   # sample script for max valuemin:sample: echo 0    # sample script for min value- title: Year progresscur:sample: date +%jmax:sample: echo 365min:sample: echo 0

Textbox

textboxes:- title: Local weatherrate-ms: 10000      # sampling rate, default = 1000sample: curl wttr.in?0ATQFborder: false       # border around the item, default = truecolor: 178          # 8-bit color number, default is white- title: Docker containers statsrate-ms: 500sample: docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.PIDs}}"

Asciibox

asciiboxes:- title: UTC timerate-ms: 500        # sampling rate, default = 1000font: 3d            # font type, default = 2dborder: false       # border around the item, default = true    color: 43           # 8-bit color number, default is whitesample: env TZ=UTC date +%r

5、额外功能

Triggers

触发器允许执行条件操作,如视觉/声音告警或任意shell命令。以下示例说明了此概念。

Clock gauge,从开始的每分钟显示时间进度和当前时间

gauges:- title: MINUTE PROGRESSposition: [[0, 18], [80, 0]]  cur:sample: date +%Smax:sample: echo 60min:sample: echo 0triggers:- title: CLOCK BELL EVERY MINUTEcondition: '[ $label == "cur" ] && [ $cur -eq 0 ] && echo 1 || echo 0'  # expects "1" as TRUE indicatoractions:terminal-bell: true  # standard terminal bell, default = falsesound: true    # NASA quindar tone, default = falsevisual: false  # notification with current value on top of the component area, default = falsescript: say -v samantha `date +%I:%M%p`  # an arbitrary script, which can use $cur, $prev and $label variables

搜索引擎延迟图表,在延迟超过阈值时向用户发出告警

runcharts:- title: SEARCH ENGINE RESPONSE TIME (sec)rate-ms: 200items:- label: GOOGLEsample: curl -o /dev/null -s -w '%{time_total}'  https://www.google.com- label: YAHOOsample: curl -o /dev/null -s -w '%{time_total}'  https://search.yahoo.com     triggers:- title: Latency threshold exceededcondition: echo "$prev < 0.3 && $cur > 0.3" |bc -l  # expects "1" as TRUE indicatoractions:terminal-bell: true  # standard terminal bell, default = falsesound: true   # NASA quindar tone, default = falsevisual: true  # visual notification on top of the component area, default = falsescript: 'say alert: ${label} latency exceeded ${cur} second' # an arbitrary script, which can use $cur, $prev and $label variables

交互式 shell 支持

除了sample命令之外,还可以指定init命令(在采样前仅执行一次)和transform命令(后处理采样命令输出)。这包括交互式shell用例,例如仅建立与数据库的连接一次,然后在交互式shell会话中执行轮询。

Basic mode

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

PTY mode

在某些情况下,交互式shell将无法工作,因为它的stdin不是终端。这种情况下我们可以使用PTY模式:

textboxes:- title: Neo4j pollingpty: true  # enables pseudo-terminal mode, default = falseinit: cypher-shell -u neo4j -p pwd --format plainsample: RETURN rand();transform: echo "$sample" | tail -n 1- title: Top on a remote serverpty: true  # enables pseudo-terminal mode, default = falseinit: ssh -i ~/user.pem ec2-user@1.2.3.4sample: top

init 命令逐步执行

在开始采样之前,还可以逐个执行多个init命令。

textboxes:- title: Java application uptimemultistep-init:- java -jar jmxterm-1.0.0-uber.jar- open host:port # or local PID- bean java.lang:type=Runtimesample: get Uptime

变量

如果配置文件包含重复的模式,则可以将它们提取到变量部分。此外,还可以在启动时使用-v/–variable标志指定变量,并且任意的系统环境变量也可以在脚本中使用。

variables:mongoconnection: mongo --quiet --host=localhost test
barcharts:- title: MongoDB documents by statusitems:- label: IN_PROGRESSinit: $mongoconnectionsample: db.getCollection('events').find({status:'IN_PROGRESS'}).count()- label: SUCCESSinit: $mongoconnectionsample: db.getCollection('events').find({status:'SUCCESS'}).count()- label: FAILinit: $mongoconnectionsample: db.getCollection('events').find({status:'FAIL'}).count()

颜色主题

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

6、真实场景

数据库

以下是不同的数据库连接示例。建议使用交互式shell(init脚本)仅建立一次连接,然后在采样期间重用即可。

MySQL

# prerequisite: installed mysql shell
variables:mysql_connection: mysql -u root -s --database mysql --skip-column-names
sparklines:  - title: MySQL (random number example)pty: trueinit: $mysql_connectionsample: select rand();

PostgreSQL

# prerequisite: installed psql shell
variables:PGPASSWORD: pwdpostgres_connection: psql -h localhost -U postgres --no-align --tuples-only
sparklines:- title: PostgreSQL (random number example)init: $postgres_connectionsample: select random();

MongoDB

# prerequisite: installed mongo shell
variables:mongo_connection: mongo --quiet --host=localhost test
sparklines:- title: MongoDB (random number example)init: $mongo_connectionsample: Math.random();

Neo4j

# prerequisite: installed cypher shell
variables:neo4j_connection: cypher-shell -u neo4j -p pwd --format plain
sparklines:- title: Neo4j (random number example)pty: trueinit: $neo4j_connectionsample: RETURN rand();transform: echo "$sample" | tail -n 1

Kafka

检查kafka lag值,计算每个队列lag值的和,高于阈值报警,多consumergroup,多topic。

variables:kafka_connection: $KAFKA_HOME/bin/kafka-consumer-groups --bootstrap-server localhost:9092
runcharts:- title: Kafka lag per consumer grouprate-ms: 5000scale: 0items:- label: A->Bsample: $kafka_connection --group group_a --describe | awk 'NR>1 {sum += $5} END {print sum}'- label: B->Csample: $kafka_connection --group group_b --describe | awk 'NR>1 {sum += $5} END {print sum}'- label: C->Dsample: $kafka_connection --group group_c --describe | awk 'NR>1 {sum += $5} END {print sum}'

Docker

Docker容器统计信息(CPU,MEM,O/I)

textboxes:- title: Docker containers statssample: docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemPerc}}\t{{.MemUsage}}\t{{.NetIO}}\t{{.BlockIO}}\t{{.PIDs}}"

SSH

远程服务器上的TOP命令

variables:sshconnection: ssh -i ~/my-key-pair.pem ec2-user@1.2.3.4
textboxes:- title: SSHpty: trueinit: $sshconnectionsample: top

JMX

Java应用程序的正常运行示例

# prerequisite: download [jmxterm jar file](https://docs.cyclopsgroup.org/jmxterm)
textboxes:- title: Java application uptimemultistep-init:- java -jar jmxterm-1.0.0-uber.jar- open host:port # or local PID- bean java.lang:type=Runtimesample: get Uptimetransform: echo $sample | tr -dc '0-9' | awk '{printf "%.1f min", $1/1000/60}'

来自:FreeBuf.COM,作者:secist

链接:https://www.freebuf.com/sectool/212820.html

最新整理的 2TB 干货资源,包括但不限于:Linux运维架构师、大数据、Docker、数据库、redis、MongoDB、电子书、Java、机器学习、BAT面试精讲视频等。在公众号对话框回复「1024 」即可免费获取!!

推荐阅读 点击标题可跳转

常见互联网公司职级和薪资一览!

就是要让你搞懂Nginx,这篇就够了!

噩耗成真!美国正式“拉黑”中芯国际

道友自诉:入职中软一个月(外包华为)就离职了!

2020 年一线城市程序员工资大调查

提升效率!Linux 管理员必用的10个关键技巧

SQL面试通关秘籍:面试知识点+技巧分享!

Elasticsearch 最佳实践!

Shell 命令执行可视化和告警工具相关推荐

  1. runtime无法执行grep_让你的 Shell 命令执行可视化和告警

    Sampler是一个用于shell命令执行,可视化和告警的工具.其配置使用的是一个简单的YAML文件. 1.为什么我需要它? 你可以直接从终端对任意动态进程进行采样 – 观察数据库中的更改,监控MQ动 ...

  2. Linux的系统调用、网络连接状态、磁盘I/O;可疑行为监控/日志收集、SHELL命令执行流程

    http://man7.org/linux/man-pages/man7/capabilities.7.html http://www.cnblogs.com/LittleHann/p/3850653 ...

  3. linux命令执行的通过程,Linux下shell命令执行过程简介

    浅析linux 下shell命令执行和守护进程 执行shell脚本有以下几种方式 1.相对路径方式,需先cd到脚本路径下 [root@banking tmp]# cd /tmp [root@banki ...

  4. linux中安shell怎么传入参数,【linux】linux 下 shell命令 执行结果赋值给变量【两种方式】...

    方法1:[通用方法] 使用Tab键上面的反引号 例子如下: find命令 模糊查询在/apps/swapping目录下 查找 文件名中包含swapping并且以.jar结尾的文件 使用反引号 引住命令 ...

  5. linux shell 命令执行超时终止

    shell下控制命令执行超时自动终止 持续执行的任务 & { sleep 超时时间 ; kill $! & } 例如: ping 命令执行10秒后 终止 ping 192.168.1. ...

  6. C程序中如何获取shell命令执行结果和返回值

    如果在C程序中调用了shell命令,那么往往希望得到输出结果以及命令执行的返回布尔值.在这里分为两步来处理: 1.使用 popen 与 pclose 来执行shell命令: 2.使用'echo $?' ...

  7. linux shell 命令执行结果,如何通过程序执行shell命令并获取命令执行结果?

    1 参考资料 1.[c/c++]如何调用[linux]shell命令行命令并获取命令行的输出内容(https://blog.csdn.net/youngstar70/article/details/7 ...

  8. python shell常用命令_python 之 shell命令执行

    python中有几种常用的执行shell命令的模块 1,os.system() 2, os.popen() 3,pexpect.run() 下面介绍3个模块的差别 1,os.system() 直接执行 ...

  9. Linux C popen函数返回Shell命令执行结果

    1.popen() popen():进程I/O函数,与pclose函数一起使用. 1.1函数定义: FILE * popen ( const char * command , const char * ...

最新文章

  1. PetaPoco - 轻量级高性能的ORM框架(支持.NET Core)
  2. java request 原理_JavaWeb-seession原理
  3. linux fg 参数,Linux的bg和fg命令简单介绍
  4. word2vec相似度计算_干货|文本相似度计算
  5. 雪城大学信息安全讲义 七、格式化字符串漏洞
  6. [JS] 001_JavaScript基础增强
  7. linux 创建目录和删除目录下,Linux下怎么创建目录命令与删除命令
  8. PHP之webservice调用接口
  9. python模型预测结果 取整_一日一技:Python里面的//并不是做了除法以后取整
  10. StringUtils测试
  11. 上海2018计算机中级职称,2018中级职称评定申报要求
  12. 步进电机、伺服电机、舵机的原理和区别?
  13. 智能科学与技术是不是属于计算机专业,智能科学与技术专业属于什么类别
  14. php----拷贝兔源代码分析dl函数和getdata函数
  15. Hello China V1.75版本运行截图
  16. Unity 接入百度AI - 植物识别
  17. 【10-11】PR调色+多机位剪辑
  18. ActiveMQ的简单使用
  19. DM6467T移植图像算法
  20. 【bug解决】TypeError: forward() missing 1 required positional argument: ‘x‘

热门文章

  1. Mac无法安装第三方软件
  2. Spring @InitBinder注解
  3. Unity插件-AVPro Video 使用案例之视频播放
  4. 乱花渐欲迷人眼的C编译器中,谁才是“编译之王”?
  5. 如何用python一键去除图片、PDF水印?
  6. 安全防御之入侵检测篇
  7. NET性能优化-推荐使用Collections.Pooled(补充)
  8. 混沌者 pat basic 练习七十一 小赌怡情
  9. 中科大科学岛计算机复试,2020年中国科学技术大学研究生院科学岛分院复试办法及复试内容...
  10. 要想成为一个优秀的测试工程师,需要具备哪些知识和经验?