Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of Sampler in Shell

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces what is the use of Sampler in Shell. It is very detailed and has a certain reference value. Interested friends must finish reading it!

Sampler is a tool for shell command execution, visualization and alarm. Its configuration uses a simple YAML file.

Why do I need it?

You can sample any dynamic process directly from the terminal-observe changes in the database, monitor MQ dynamic messages (in-flight messages), trigger deployment scripts and get notifications after completion.

If there is a way to get metrics (metric) using the shell command, you can use Sampler to visualize them immediately.

Installation

MacOS

Brew cask install sampler

Or

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

Note: the libasound2-dev system library needs to be installed for Sampler to play trigger sounds. Usually the library is installed in the appropriate location, but if not-you can install it using the package manager you are used to, such as apt install libasound2-dev

Windows (experiment)

It is recommended to use it under advanced console simulators, such as Cmder

Download .exe

Use

Specify the shell commands, and Sampler executes them at the appropriate rate. The output is for visualization.

The basic three-step process of using Sampler:

Define the shell command in the YAML configuration file

Run sampler-c config.yml

Resize and reposition components on UI

There are already many monitoring systems on the market.

Sampler is by no means a substitute for monitoring systems, but a development tool that is easy to set up.

If spinning up and configuring Prometheus with Grafana are completely redundant tasks, then Sampler may be the right solution. No server, no database, no deployment-you specify the shell command, and it works.

Do I need to install it on every server I monitor?

No, you can run Sampler locally, but you can still collect telemetry data from multiple remote computers. Any visualization may have an init command where you can ssh to a remote server. See SSH example

module

The following is a list of configuration examples for each component type, which contains macOS-compatible sampling scripts.

Runchart

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

Sparkline

Sparklines:-title: CPU usage rate-ms: 200scale: 0 sample: ps-A-o% cpu | awk'{print s} 1} END {print s}'- title: Free memory pages rate-ms: 200 scale: 0 sample: memory_pressure | grep 'Pages free' | awk' {print $3}'

Barchart

Barcharts:-title: Local network activity rate-ms: 500 # sampling rate, default = 1000 scale: 0 # number of digits after sample decimal point Default = 1 items:-label: UDP bytes in sample: nettop-J bytes_in-l 1-m udp | awk'{sum + = $4} END {print sum}'- label: UDP bytes out sample: nettop-J bytes_out-l 1-m udp | awk'{sum + = $4} END {print sum}'- label: TCP bytes in 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}'

Gauge

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

Textbox

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: 10000 # 8-bit color number Default is white-title: Docker containers stats rate-ms: 500sample: docker stats-- no-stream-- format "table {{.Name}}\ t {{.CPUPerc}}\ t {{.MemUsage}}\ t {{.PIDs}}"

Asciibox

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

Extra function

Triggers

Triggers allow conditional actions, such as visual / audio alarms or arbitrary shell commands, to be performed. The following example illustrates this concept.

Clock gauge, showing time progress and current time every minute from the beginning

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

Search engine delay chart, which alerts users when the delay exceeds the threshold

Runcharts:-title: SEARCH ENGINE RESPONSE TIME (sec) rate-ms: 200items:-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: Latency threshold exceeded 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

Interactive shell support

In addition to the sample command, you can also specify the init command (which is executed only once before sampling) and the transform command (which processes the output of the sampling command after processing). This includes interactive shell use cases, such as establishing a connection to the database only once, and then performing polling in an interactive shell session.

Basic mode

Textboxes:-title: MongoDB polling rate-ms: 500init: 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

PTY mode

In some cases, the interactive shell will not work because its stdin is not a terminal. In this case, we can use the PTY mode:

Textboxes:-title: Neo4j polling pty: true # enables pseudo-terminal mode, default = false init: cypher-shell-u neo4j-p pwd-- format plain sample: RETURN rand (); transform: echo "$sample" | tail-N1-title: Top on a remote server pty: true # enables pseudo-terminal mode, default = false init: ssh-I / user.pem ec2-user@1.2.3.4 sample: top

Init command is executed step by step

Before you start sampling, you can also execute multiple init commands one by one.

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

Variable

If the configuration file contains duplicate patterns, you can extract them into the variables section. In addition, variables can be specified at startup using the-v/-variable flag, and any system environment variable can be used in the script.

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 ()

Color theme

Theme: light # default = dark sparklines:-title: CPU usage sample: ps-A-o% cpu | awk'{print s} 1} END {print s}'

Real scene

Database

The following are examples of different database connections. It is recommended that you use an interactive shell (init script) to establish a connection only once, and then reuse it during sampling.

MySQL

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

PostgreSQL

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

MongoDB

# prerequisite: installed mongo shell variables: mongo_connection: mongo-- quiet-- host=localhost test sparklines:-title: MongoDB (random number example) init: $mongo_connection sample: 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: true init: $neo4j_connection sample: RETURN rand (); transform: echo "$sample" | tail-n 1

Kafka

Check kafka lags, calculate the sum of lags in each queue, above threshold alarm, multi-consumergroup, multi-topic.

Variables: kafka_connection: $KAFKA_HOME/bin/kafka-consumer-groups-- bootstrap-server localhost:9092 runcharts:-title: Kafka lag per consumer group rate-ms: 5000 scale: 0 items:-label: a-> B sample: $kafka_connection-- group group_a-- describe | awk'NR > 1 {sum + = $5} END {print sum}'- label: B-> C sample: $kafka_connection-group Group_b-- describe | awk'NR > 1 {sum + = $5} END {print sum}'- label: C-> D sample: $kafka_connection-- group group_c-- describe | awk'NR > 1 {sum + = $5} END {print sum}'

Docker

Docker Container Statistics (CPU,MEM,O/I)

Textboxes:-title: Docker containers stats sample: docker stats-- no-stream-- format "table {{name}}\ t {{.CPUPerc}\ t {{.MemPerc}}\ t {{.NetIO}}\ t {.BlockIO}}\ t {.PIDs}}"

SSH

TOP commands on remote servers

Variables: sshconnection: ssh-I ~ / my-key-pair.pem ec2-user@1.2.3.4 textboxes:-title: SSH pty: true init: $sshconnection sample: top

JMX

A working example of a Java application

# prerequisite: download [jmxterm jar file] (https://docs.cyclopsgroup.org/jmxterm) 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 transform: echo $sample | tr-dc'0-9' | awk'{printf "% .1f min" The above are all the contents of the article "what's the use of Sampler in Shell"? Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report