linux脚本可视化,Shell命令执行可视化和告警工具

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

1、为什么我需要它?

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

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

2、安装

macOS

brewcaskinstallsampler

sudocurl-Lo/usr/local/bin/samplerhttps://github.com/sqshq/sampler/releases/download/v1.0.3/sampler-1.0.3-darwin-amd64 sudochmod+x/usr/local/bin/sampler

sudowgethttps://github.com/sqshq/sampler/releases/download/v1.0.3/sampler-1.0.3-linux-amd64-O/usr/local/bin/sampler sudochmod+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

8b7a15e4502e0586033c23fb3338da6d.png

runcharts: -title:Searchengineresponsetime rate-ms:500#samplingrate,default=1000 scale:2#numberofdigitsaftersampledecimalpoint,default=1 legend: enabled:true#enablesitemlabels,default=true details:false#enablesitemstatistics:cur/min/max/dltvalues,default=true items: -label:GOOGLE sample:curl-o/dev/null-s-w'%{time_total}'https://www.google.com color:178#8-bitcolornumber,defaultoneischosenfromapre-definedpalette -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

Sparkline

e54d6433941e70d2b1f09c695271e9e2.png

sparklines: -title:CPUusage rate-ms:200 scale:0 sample:ps-A-o%cpu|awk'{s+=$1}END{prints}' -title:Freememorypages rate-ms:200 scale:0 sample:memory_pressure|grep'Pagesfree'|awk'{print$3}'

Barchart

bb87a64adf4e69cefdc31ec736287069.png

barcharts: -title:Localnetworkactivity rate-ms:500#samplingrate,default=1000 scale:0#numberofdigitsaftersampledecimalpoint,default=1 items: -label:UDPbytesin sample:nettop-Jbytes_in-l1-mudp|awk'{sum+=$4}END{printsum}' -label:UDPbytesout sample:nettop-Jbytes_out-l1-mudp|awk'{sum+=$4}END{printsum}' -label:TCPbytesin sample:nettop-Jbytes_in-l1-mtcp|awk'{sum+=$4}END{printsum}' -label:TCPbytesout sample:nettop-Jbytes_out-l1-mtcp|awk'{sum+=$4}END{printsum}'

Gauge

0a850c4bafdf0d309df4eec18477c4db.png

gauges: -title:Minuteprogress rate-ms:500#samplingrate,default=1000 scale:2#numberofdigitsaftersampledecimalpoint,default=1 percent-only:false#toggledisplayofthecurrentvalue,default=false color:178#8-bitcolornumber,defaultoneischosenfromapre-definedpalette cur: sample:date+%S#samplescriptforcurrentvalue max: sample:echo60#samplescriptformaxvalue min: sample:echo0#samplescriptforminvalue -title:Yearprogress cur: sample:date+%j max: sample:echo365 min: sample:echo0

Textbox

23dca44b31737d927661cd2a1daa9867.png

textboxes: -title:Localweather rate-ms:10000#samplingrate,default=1000 sample:curlwttr.in?0ATQF border:false#borderaroundtheitem,default=true color:178#8-bitcolornumber,defaultiswhite -title:Dockercontainersstats rate-ms:500 sample:dockerstats--no-stream--format"table{{.Name}} {{.CPUPerc}} {{.MemUsage}} {{.PIDs}}"

Asciibox

f040aa251ae1c42317b9253216fa7072.png

asciiboxes: -title:UTCtime rate-ms:500#samplingrate,default=1000 font:3d#fonttype,default=2d border:false#borderaroundtheitem,default=true color:43#8-bitcolornumber,defaultiswhite sample:envTZ=UTCdate+%r

5、额外功能

Triggers

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

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

gauges: -title:MINUTEPROGRESS position:[[0,18],[80,0]] cur: sample:date+%S max: sample:echo60 min: sample:echo0 triggers: -title:CLOCKBELLEVERYMINUTE condition:'[$label=="cur"]&&[$cur-eq0]&&echo1||echo0'#expects"1"asTRUEindicator actions: terminal-bell:true#standardterminalbell,default=false sound:true#NASAquindartone,default=false visual:false#notificationwithcurrentvalueontopofthecomponentarea,default=false script:say-vsamantha`date+%I:%M%p`#anarbitraryscript,whichcanuse$cur,$prevand$labelvariables

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

runcharts: -title:SEARCHENGINERESPONSETIME(sec) rate-ms:200 items: -label:GOOGLE sample:curl-o/dev/null-s-w'%{time_total}'https://www.google.com -label:YAHOO sample:curl-o/dev/null-s-w'%{time_total}'https://search.yahoo.com triggers: -title:Latencythresholdexceeded condition:echo"$prev0.3"|bc-l#expects"1"asTRUEindicator actions: terminal-bell:true#standardterminalbell,default=false sound:true#NASAquindartone,default=false visual:true#visualnotificationontopofthecomponentarea,default=false script:'sayalert:${label}latencyexceeded${cur}second'#anarbitraryscript,whichcanuse$cur,$prevand$labelvariables

交互式 shell 支持

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

Basic mode

textboxes: -title:MongoDBpolling rate-ms:500 init:mongo--quiet--host=localhosttest#executesonlyoncetostarttheinteractivesession sample:Date.now();#executeswitharequiredrate,inscopeoftheinteractivesession transform:echoresult=$sample#executesinscopeoflocalsession,$samplevariableisavailablefortransformation

PTY mode

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

textboxes: -title:Neo4jpolling pty:true#enablespseudo-terminalmode,default=false init:cypher-shell-uneo4j-ppwd--formatplain sample:RETURNrand(); transform:echo"$sample"|tail-n1 -title:Toponaremoteserver pty:true#enablespseudo-terminalmode,default=false init:ssh-i~/user.pemec2-user@1.2.3.4 sample:top

init 命令逐步执行

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

textboxes: -title:Javaapplicationuptime multistep-init: -java-jarjmxterm-1.0.0-uber.jar -openhost:port#orlocalPID -beanjava.lang:type=Runtime sample:getUptime

变量

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

variables: mongoconnection:mongo--quiet--host=localhosttest barcharts: -title:MongoDBdocumentsbystatus 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()

颜色主题

3c78deba9b4cd38ea7f74e80e5711159.png

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

6、真实场景

数据库

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

MySQL

#prerequisite:installedmysqlshell variables: mysql_connection:mysql-uroot-s--databasemysql--skip-column-names sparklines: -title:MySQL(randomnumberexample) pty:true init:$mysql_connection sample:selectrand();

PostgreSQL

#prerequisite:installedpsqlshell variables: PGPASSWORD:pwd postgres_connection:psql-hlocalhost-Upostgres--no-align--tuples-only sparklines: -title:PostgreSQL(randomnumberexample) init:$postgres_connection sample:selectrandom();

MongoDB

#prerequisite:installedmongoshell variables: mongo_connection:mongo--quiet--host=localhosttest sparklines: -title:MongoDB(randomnumberexample) init:$mongo_connection sample:Math.random();

Neo4j

#prerequisite:installedcyphershell variables: neo4j_connection:cypher-shell-uneo4j-ppwd--formatplain sparklines: -title:Neo4j(randomnumberexample) pty:true init:$neo4j_connection sample:RETURNrand(); transform:echo"$sample"|tail-n1

Kafka

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

variables: kafka_connection:$KAFKA_HOME/bin/kafka-consumer-groups--bootstrap-serverlocalhost:9092 runcharts: -title:Kafkalagperconsumergroup rate-ms:5000 scale:0 items: -label:A->B sample:$kafka_connection--groupgroup_a--describe|awk'NR>1{sum+=$5}END{printsum}' -label:B->C sample:$kafka_connection--groupgroup_b--describe|awk'NR>1{sum+=$5}END{printsum}' -label:C->D sample:$kafka_connection--groupgroup_c--describe|awk'NR>1{sum+=$5}END{printsum}'

Docker

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

textboxes: -title:Dockercontainersstats sample:dockerstats--no-stream--format"table{{.Name}} {{.CPUPerc}} {{.MemPerc}} {{.MemUsage}} {{.NetIO}} {{.BlockIO}} {{.PIDs}}"

SSH

远程服务器上的TOP命令

variables: sshconnection:ssh-i~/my-key-pair.pemec2-user@1.2.3.4 textboxes: -title:SSH pty:true init:$sshconnection sample:top

JMX

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

#prerequisite:download[jmxtermjarfile](https://docs.cyclopsgroup.org/jmxterm) textboxes: -title:Javaapplicationuptime multistep-init: -java-jarjmxterm-1.0.0-uber.jar -openhost:port#orlocalPID -beanjava.lang:type=Runtime sample:getUptime transform:echo$sample|tr-dc'0-9'|awk'{printf"%.1fmin",$1/1000/60}'

责任编辑:lq

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值