|
Sampler是一个用于shell命令执行,可视化和告警的工具。其配置使用的是一个简单的YAML文件。 1、为什么我需要它?你可以直接从终端对任意动态进程进行采样 – 观察数据库中的更改,监控MQ动态消息(in-flight messages),触发部署脚本并在完成后获取通知。 如果有一种方法可以使用shell命令获取指标(metric),那么可以使用Sampler立即对其进行可视化。 2、安装macOSbrew cask install sampler! W+ u) Q, y4 [! ~ 或' {4 ^3 g) k8 Q7 M3 i sudo curl -Lo /usr/local/bin/sampler https://github.com/sqshq/sampler ... -1.0.3-darwin-amd64sudo chmod +x /usr/local/bin/sampler4 f2 i% B: h8 e7 U 2 Z$ M D# ?5 L ? % V% R$ ? \' U9 [2 I Linux8 X* v& }6 c0 n, w# H9 ` l2 Z& |* p) E sudo wget https://github.com/sqshq/sampler ... r-1.0.3-linux-amd64 -O /usr/local/bin/sampler sudo chmod +x /usr/local/bin/sampler Windows(实验) 建议在高级控制台模拟器下使用,如Cmder 3、使用 市面早已有许多监控系统 Sampler绝不是监控系统的替代品,而是易于设置的开发工具。 如果spinning up和使用Grafana配置Prometheus是完全多余的任务,那么Sampler可能是正确的解决方案。没有服务器,没有数据库,不需要部署 – 你指定了shell命令,它就可以工作了。 我监控的每台服务器上都需要安装吗?不,你可以在本地运行Sampler,但仍然可以从多台远程计算机上收集遥测数据。任何可视化都可能具有init命令,你可以在其中ssh到远程服务器。请参阅SSH example 4、组件以下是每种组件类型的配置示例列表,其中包含与macOS兼容的采样脚本。 Runchart
- title: Search engine response time rate-ms: 500 # sampling rate, default = 1000( j, |& R2 b. {* A9 g scale: 2 # number of digits after sample decimal point, default = 1, y. _# D4 B O2 ? Z$ u legend: enabled: true # enables item labels, default = true details: false # enables item statistics: cur/min/max/dlt values, default = true items: - label: GOOGLE3 x* ^) w p9 I6 e sample: curl -o /dev/null -s -w '%{time_total}' https://www.google.com1 g' A3 b2 w: U+ w# X color: 178 # 8-bit color number, default one is chosen from a pre-defined palette - label: YAHOO8 R3 Y) B: p* |; h( {" a sample: curl -o /dev/null -s -w '%{time_total}' https://search.yahoo.com ^! o9 q2 F% m% R* `# \ - label: BING sample: curl -o /dev/null -s -w '%{time_total}' https://www.bing.com Sparkline
- title: CPU usage rate-ms: 200 scale: 05 I" p1 P2 V1 ? sample: ps -A -o %cpu | awk '{s+=$1} END {print s}'4 y& K& w3 p" P9 X - title: Free memory pages* q6 i2 S" e% X; r* n c2 s5 p rate-ms: 200. i0 A" U3 U: k* y& K scale: 0) B: x5 H% `9 z7 P6 M sample: memory_pressure | grep 'Pages free' | awk '{print $3}' + J0 F) q2 u, X0 H2 |: h! i' ?+ i Barchart
- title: Local network activity rate-ms: 500 # sampling rate, default = 1000/ L5 I1 w1 ?3 z- K8 O5 ~3 ]" p2 D8 m scale: 0 # number of digits after sample decimal point, default = 1( x5 y$ m1 p* p$ f1 |' ~; K) J5 P# G items:$ u! ]8 ?2 ^$ Q+ g) w - label: UDP bytes in sample: nettop -J bytes_in -l 1 -m udp | awk '{sum += $4} END {print sum}'3 K6 o2 \* T/ [& ~" e( L1 ` - label: UDP bytes out sample: nettop -J bytes_out -l 1 -m udp | awk '{sum += $4} END {print sum}'6 v, @& T% _( ^ - label: TCP bytes in4 R: p; ~2 u- C2 C sample: nettop -J bytes_in -l 1 -m tcp | awk '{sum += $4} END {print sum}' - label: TCP bytes out sample: nettop -J bytes_out -l 1 -m tcp | awk '{sum += $4} END {print sum}' 7 [8 e" L& t) i4 ^ Gauge
- title: Minute progress6 z! }$ O4 v1 Y4 a2 c1 M& L rate-ms: 500 # sampling rate, default = 1000 scale: 2 # number of digits after sample decimal point, default = 10 C* C& I* |6 b, U3 m. S5 n3 g percent-only: false # toggle display of the current value, default = false8 g1 Q: I% r# `- T* }3 d! W" x color: 178 # 8-bit color number, default one is chosen from a pre-defined palette# n, p+ P6 H: H" J( @ cur:1 R" }1 K3 c; V2 Y sample: date +%S # sample script for current value0 h q' a! j5 _4 e4 M max: sample: echo 60 # sample script for max value) v4 h+ [# T2 M" q4 N6 _! _, _6 o min: sample: echo 0 # sample script for min value# n% w& j& \& \5 J; `. A - title: Year progress) ~ D4 F- x8 I' V. b% V' d$ o, o cur:3 I) O2 z) i# o Z2 ^ sample: date +%j, c, x/ `9 i- x/ `3 @ max: sample: echo 365# Z3 k6 a/ j+ e7 _2 o' v) ~. N min: sample: echo 0Textbox
- title: Local weather rate-ms: 10000 # sampling rate, default = 10008 ^0 `5 y, _/ {2 }& _- n) A sample: curl wttr.in?0ATQF# A# D$ G7 [' l3 J border: false # border around the item, default = true color: 178 # 8-bit color number, default is white0 t1 s) t+ H6 T - title: Docker containers stats rate-ms: 500 sample: docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.PIDs}}"4 o* C9 R! w5 m* O |2 a8 ] 6 m* F) }$ T7 u6 t6 ^ Asciibox
- 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% ^# Q1 ~ `. P( u5 T sample: env TZ=UTC date +%r 5、额外功能 Triggers 触发器允许执行条件操作,如视觉/声音告警或任意shell命令。以下示例说明了此概念。 Clock gauge,从开始的每分钟显示时间进度和当前时间 gauges:, e. Z4 Y0 N0 N& U6 Z- title: MINUTE PROGRESS$ ]5 e& A0 j7 g( B6 Q position: [[0, 18], [80, 0]] cur: sample: date +%S max:9 c6 [' E1 D4 E. P' I9 F" v sample: echo 60* c5 ^+ Z3 N/ P- s5 z min: sample: echo 0% Z8 |3 X" x' B+ B+ }' k triggers: - title: CLOCK BELL EVERY MINUTE condition: '[ $label == "cur" ] && [ $cur -eq 0 ] && echo 1 || echo 0' # expects "1" as TRUE indicator! a- l3 s8 u1 w( m actions: terminal-bell: true # standard terminal bell, default = false. X/ L$ J& ]7 g2 D: Q 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 + K' C1 g3 l6 v# v1 f) } 搜索引擎延迟图表,在延迟超过阈值时向用户发出告警 runcharts:) ~2 b7 P) J" Q, m- title: SEARCH ENGINE RESPONSE TIME (sec) rate-ms: 200" v8 f- [. ?; w- n) B" {0 S* e items:/ L& e: f4 {. \. V) G8 m$ M - label: GOOGLE sample: curl -o /dev/null -s -w '%{time_total}' https://www.google.com - label: YAHOO- G' G9 v5 [. P ] sample: curl -o /dev/null -s -w '%{time_total}' https://search.yahoo.com 5 {2 R* D6 a R4 O9 p" ]( _ triggers:* U+ Q. M2 Y% t0 d) l' D. `) _ - title: Latency threshold exceeded& K$ S1 c: }7 e2 H$ v( o, p4 T condition: echo "$prev < 0.3 && $cur > 0.3" |bc -l # expects "1" as TRUE indicator actions: terminal-bell: true # standard terminal bell, default = false sound: true # NASA quindar tone, default = false visual: true # visual notification on top of the component area, default = false script: 'say alert: ${label} latency exceeded ${cur} second' # an arbitrary script, which can use $cur, $prev and $label variables* y+ B2 ~6 p0 a( L: t0 \- [) Q! [ 交互式 shell 支持 除了sample命令之外,还可以指定init命令(在采样前仅执行一次)和transform命令(后处理采样命令输出)。这包括交互式shell用例,例如仅建立与数据库的连接一次,然后在交互式shell会话中执行轮询。 Basic mode/ H( w2 e% b$ t4 d2 qtextboxes:* z" {3 {5 Y% P* {- P0 K* Z - title: MongoDB polling# y$ I7 I' \- o" M6 L' V rate-ms: 5008 L$ B& Z) F1 [- ]' ?) q9 W init: mongo --quiet --host=localhost test # executes only once to start the interactive session# N( J$ C& D @ sample: Date.now(); # executes with a required rate, in scope of the interactive session/ O- L1 D$ d1 D transform: echo result = $sample # executes in scope of local session, $sample variable is available for transformation PTY mode 在某些情况下,交互式shell将无法工作,因为它的stdin不是终端。这种情况下我们可以使用PTY模式: textboxes: |3 O# |- f, |# l' d! S: e7 x- title: Neo4j polling pty: true # enables pseudo-terminal mode, default = false( X* Q$ x. x( _2 f init: cypher-shell -u neo4j -p pwd --format plain sample: RETURN rand(); transform: echo "$sample" | tail -n 1* h1 H6 x) q' k - title: Top on a remote server pty: true # enables pseudo-terminal mode, default = false+ y4 R) [$ n. ~: p5 l# A1 C init: ssh -i ~/user.pem ec2-user@1.2.3.4 sample: top! b ]. K5 n5 u. j6 V 8 F; `8 K' _- g: |7 {5 E. o! w ! L- X0 V& k' b( K! R( k init 命令逐步执行 在开始采样之前,还可以逐个执行多个init命令。 textboxes:& A# @: f" ]" }9 o7 E; V- title: Java application uptime multistep-init:& v% \; i/ ?$ f. _1 ~' x - java -jar jmxterm-1.0.0-uber.jar - open host:port # or local PID - bean java.lang:type=Runtime1 D$ h* S- N3 C sample: get Uptime 7 @% ]; E# z8 b' A% H 变量 如果配置文件包含重复的模式,则可以将它们提取到变量部分。此外,还可以在启动时使用-v/–variable标志指定变量,并且任意的系统环境变量也可以在脚本中使用。 variables:" R. S2 ~: f: I8 G2 Z- ~mongoconnection: mongo --quiet --host=localhost test barcharts:& g }1 p9 o% v' ?4 ? - title: MongoDB documents by status items: - label: IN_PROGRESS; D- ~9 d3 K( M/ d init: $mongoconnection* p2 x) j$ x. E1 r3 j! b; e sample: db.getCollection('events').find({status:'IN_PROGRESS'}).count()0 p M9 c- G% c' I3 ^ - label: SUCCESS. l+ t4 K) x, o) X0 `5 u init: $mongoconnection8 x3 @6 s5 _) J& F d" Z* }$ `9 X sample: db.getCollection('events').find({status:'SUCCESS'}).count()8 I* b) W- J4 W6 T - label: FAIL6 Q4 e; c5 \/ }' j X init: $mongoconnection sample: db.getCollection('events').find({status:'FAIL'}).count() 颜色主题
sparklines: - title: CPU usage sample: ps -A -o %cpu | awk '{s+=$1} END {print s}'% F* A3 ]3 Q. v( O- \6 R 6、真实场景数据库 以下是不同的数据库连接示例。建议使用交互式shell(init脚本)仅建立一次连接,然后在采样期间重用即可。 MySQL0 V$ b+ k, {5 g% @5 L# prerequisite: installed mysql shell variables: mysql_connection: mysql -u root -s --database mysql --skip-column-names$ T1 Y. }! J. i* @0 h' o sparklines: & \0 y8 @1 \3 t/ S* B) x4 ~" _ - title: MySQL (random number example)* G9 f7 Q d0 w% X( v pty: true init: $mysql_connection sample: select rand();( S0 R% |& n" Y% f PostgreSQL # prerequisite: installed psql shell variables: PGPASSWORD: pwd9 J! j. j/ M- a( L% o% h7 x# V4 s4 Y postgres_connection: psql -h localhost -U postgres --no-align --tuples-only sparklines: - title: PostgreSQL (random number example) init: $postgres_connection sample: select random();3 A# z, n }& O2 M MongoDB # prerequisite: installed mongo shell0 U9 Z2 ~' V' c1 m* O variables: mongo_connection: mongo --quiet --host=localhost test sparklines:3 m, V5 O, s1 f: T9 T% h* c - title: MongoDB (random number example)2 C. T" f5 }6 A/ k3 Y init: $mongo_connection sample: Math.random(); Neo4j \/ [0 A3 y* b5 a+ V # prerequisite: installed cypher shell variables: neo4j_connection: cypher-shell -u neo4j -p pwd --format plain sparklines:9 ?! ?* X% p) N$ S - title: Neo4j (random number example) pty: true init: $neo4j_connection& k; h, H7 Y# r. Q9 d1 p/ l( Q/ o sample: RETURN rand();# `, G9 u- ]" E0 N7 U) N4 f transform: echo "$sample" | tail -n 1. o. a/ n7 D1 a8 e) T! a Kafka 检查kafka lag值,计算每个队列lag值的和,高于阈值报警,多consumergroup,多topic。 variables:0 e, p8 x+ ~7 C Wkafka_connection: $KAFKA_HOME/bin/kafka-consumer-groups --bootstrap-server localhost:9092 runcharts:" N# m1 b3 T" e8 h/ g8 ?+ u - title: Kafka lag per consumer group rate-ms: 50008 [) e( T/ a8 U scale: 0 items:) r, K% k# c U2 x. P0 n - label: A->B sample: $kafka_connection --group group_a --describe | awk 'NR>1 {sum += $5} END {print sum}' - label: B->C; k2 r1 N& Z. h/ S sample: $kafka_connection --group group_b --describe | awk 'NR>1 {sum += $5} END {print sum}'9 |0 f3 K) J: M. {0 `% t - label: C->D$ `6 J3 m4 E$ t" U3 j sample: $kafka_connection --group group_c --describe | awk 'NR>1 {sum += $5} END {print sum}' 3 ~& J# M$ O! E# N- @ Docker Docker容器统计信息(CPU,MEM,O/I) textboxes:- title: Docker containers stats sample: docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemPerc}}\t{{.MemUsage}}\t{{.NetIO}}\t{{.BlockIO}}\t{{.PIDs}}" + H2 i; L0 V, i2 r 7 L+ {+ L$ ^, X0 ] R8 H" K SSH 远程服务器上的TOP命令 variables:sshconnection: ssh -i ~/my-key-pair.pem ec2-user@1.2.3.4 textboxes: - title: SSH pty: true init: $sshconnection6 |3 m0 s3 \8 z: \+ }7 a sample: top& ~( f- d# |8 v ^) L" i 6 ?2 y& ]4 {4 @% L' f8 w. ~ 8 `. @1 [& D. ?! K JMX Java应用程序的正常运行示例 # prerequisite: download [jmxterm jar file](https://docs.cyclopsgroup.org/jmxterm)# I/ `* g7 v" W9 X5 D) ltextboxes:# ^* W' R. C# r5 b8 P - title: Java application uptime multistep-init: - java -jar jmxterm-1.0.0-uber.jar, V; O9 D- P! L+ B" c2 f - open host:port # or local PID; r5 t8 p7 L' H - bean java.lang:type=Runtime sample: get Uptime* j2 n s/ [' E# f% z2 P9 g transform: echo $sample | tr -dc '0-9' | awk '{printf "%.1f min", $1/1000/60}' |
微信公众号
手机版