2019-04-25 00:03:47 +00:00
# Sampler. Visualization for any shell command.
[![Build Status ](https://travis-ci.com/sqshq/sampler.svg?token=LdyRhxxjDFnAz1bJg8fq&branch=master )](https://travis-ci.com/sqshq/sampler) [![Go Report Card ](https://goreportcard.com/badge/github.com/sqshq/sampler )](https://goreportcard.com/report/github.com/sqshq/sampler)
2019-04-20 04:30:13 +00:00
Sampler is a tool for shell commands execution, visualization and alerting. Configured with a simple YAML file.
2019-04-19 03:50:48 +00:00
![sampler ](https://user-images.githubusercontent.com/6069066/56404396-70b14d00-6234-11e9-93cd-54461bf40c96.gif )
2019-04-20 04:30:13 +00:00
## Installation
### macOS
2019-06-18 04:49:53 +00:00
```bash
sudo curl -Lo /usr/local/bin/sampler https://github.com/sqshq/sampler/releases/download/v0.9.1-beta/sampler-0.9.1-darwin-amd64
sudo chmod +x /usr/local/bin/sampler
```
2019-04-20 04:30:13 +00:00
### Linux
2019-06-18 04:49:53 +00:00
```bash
2019-06-22 04:38:33 +00:00
sudo wget https://github.com/sqshq/sampler/releases/download/v0.9.1-beta/sampler-0.9.1-linux-amd64 -O /usr/local/bin/sampler
2019-06-18 04:49:53 +00:00
sudo chmod +x /usr/local/bin/sampler
```
2019-04-20 04:30:13 +00:00
### Windows
2019-06-18 04:49:53 +00:00
[download .exe ](https://github.com/sqshq/sampler/releases/download/v0.9.1-beta/sampler-0.9.1-windows )
2019-04-20 04:30:13 +00:00
## Usage
2019-04-25 00:03:47 +00:00
You specify shell commands, Sampler executes them with a required rate. The output is used for visualization.
2019-06-22 04:10:05 +00:00
One can sample any dynamic process right from the terminal - observe changes in the database, monitor MQ in-flight messages, trigger deployment process and get notification when it's done.
2019-04-25 00:03:47 +00:00
Using Sampler is basically a 3-step process:
2019-04-20 04:30:13 +00:00
- Define your configuration in a YAML file
- Run `sampler -c config.yml`
2019-04-25 00:03:47 +00:00
- Adjust components size and location on UI
2019-04-20 04:30:13 +00:00
2019-06-11 03:01:37 +00:00
## Contents
2019-04-20 04:30:13 +00:00
2019-04-25 00:03:47 +00:00
- [Components ](#components )
- [Runchart ](#runchart )
- [Sparkline ](#sparkline )
- [Barchart ](#barchart )
- [Gauge ](#gauge )
- [Textbox ](#textbox )
- [Asciibox ](#asciibox )
- [Bells and whistles ](#bells-and-whistles )
- [Triggers (conditional actions) ](#triggers )
2019-06-11 03:11:53 +00:00
- [Interactive shell (database interaction, remote server access, etc) ](#interactive-shell-support )
2019-04-25 00:03:47 +00:00
- [Variables ](#variables )
- [Color theme ](#color-theme )
- [Real-world examples (contributions welcome) ](#real-world-examples )
2019-04-20 04:30:13 +00:00
2019-06-11 03:01:37 +00:00
## Components
2019-06-22 04:10:05 +00:00
The following is a list of configuration examples for each component type, with macOS compatible sampling scripts.
2019-06-11 03:11:53 +00:00
2019-06-11 03:01:37 +00:00
### Runchart
2019-06-10 02:21:26 +00:00
![runchart ](https://user-images.githubusercontent.com/6069066/59168666-aff96d00-8b04-11e9-99b6-34d8bae37bd2.png )
2019-04-25 00:03:47 +00:00
```yml
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
2019-06-10 02:21:26 +00:00
sample: curl -o /dev/null -s -w '%{time_total}' https://www.bing.com
2019-04-25 00:03:47 +00:00
```
2019-06-11 03:01:37 +00:00
### Sparkline
2019-06-10 02:21:26 +00:00
![sparkline ](https://user-images.githubusercontent.com/6069066/59167746-de754900-8b00-11e9-9305-c9a4176634d2.png )
2019-04-25 00:03:47 +00:00
```yml
sparklines:
2019-06-10 02:21:26 +00:00
- title: CPU usage
rate-ms: 200
scale: 0
sample: ps -A -o %cpu | awk '{s+=$1} END {print s}'
- title: Free memory pages
rate-ms: 200
scale: 0
sample: memory_pressure | grep 'Pages free' | awk '{print $3}'
2019-04-25 00:03:47 +00:00
```
2019-06-11 03:01:37 +00:00
### Barchart
2019-06-10 02:21:26 +00:00
![barchart ](https://user-images.githubusercontent.com/6069066/59167751-de754900-8b00-11e9-8d01-efd04ae1eec6.png )
2019-04-25 00:03:47 +00:00
```yml
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}'
```
2019-06-11 03:01:37 +00:00
### Gauge
2019-06-12 02:14:54 +00:00
![gauge ](https://user-images.githubusercontent.com/6069066/59318799-4c06ae00-8c96-11e9-868a-7fef803f3739.png )
2019-04-25 00:03:47 +00:00
```yml
gauges:
- title: Minute progress
rate-ms: 500 # sampling rate, default = 1000
scale: 2 # number of digits after sample decimal point, default = 1
2019-06-22 02:56:23 +00:00
percent-only: false # toggle display of the current value, default = false
2019-06-10 02:21:26 +00:00
color: 178 # 8-bit color number, default one is chosen from a pre-defined palette
2019-04-25 00:03:47 +00:00
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
2019-06-10 02:21:26 +00:00
- title: Year progress
cur:
sample: date +%j
max:
sample: echo 365
min:
sample: echo 0
2019-04-25 00:03:47 +00:00
```
2019-06-11 03:01:37 +00:00
### Textbox
2019-06-10 02:36:35 +00:00
![textbox ](https://user-images.githubusercontent.com/6069066/59168949-192db000-8b06-11e9-900b-0e92ff494f62.png )
2019-04-25 00:03:47 +00:00
```yml
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: 178 # 8-bit color number, default is white
- title: Docker containers stats
rate-ms: 500
sample: docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.PIDs}}"
```
2019-06-11 03:01:37 +00:00
### Asciibox
2019-06-10 02:42:54 +00:00
![asciibox ](https://user-images.githubusercontent.com/6069066/59169283-aa515680-8b07-11e9-8beb-716a387aed1b.png )
2019-04-25 00:03:47 +00:00
```yml
asciiboxes:
- title: UTC time
rate-ms: 500 # sampling rate, default = 1000
font: 3d # font type, default = 2d
2019-06-10 02:42:54 +00:00
border: false # border around the item, default = true
2019-06-10 02:36:35 +00:00
color: 43 # 8-bit color number, default is white
2019-04-25 00:03:47 +00:00
sample: env TZ=UTC date +%r
```
2019-04-20 04:30:13 +00:00
2019-06-11 03:01:37 +00:00
## Bells and whistles
2019-04-20 04:30:13 +00:00
2019-06-11 03:01:37 +00:00
### Triggers
2019-04-25 00:03:47 +00:00
Triggers allow to perform conditional actions, like visual/sound alerts or an arbitrary shell command.
2019-06-22 04:10:05 +00:00
The following examples illustrate the concept.
#### Clock gauge, which shows minute progress and announce current time at the beginning of each minute
```yml
gauges:
- title: MINUTE PROGRESS
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 latency chart, which alerts user when latency exceeds a threshold
```yml
runcharts:
- title: SEARCH ENGINE RESPONSE TIME (sec)
items:
- label: GOOGLE
sample: curl -o /dev/null -s -w '%{time_total}' https://www.google.com
triggers:
- title: Latency threshold exceeded
condition: echo "$prev < 0.8 & & $ cur > 0.8" |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
```
2019-04-20 04:30:13 +00:00
2019-06-11 03:01:37 +00:00
### Interactive shell support
2019-06-22 04:10:05 +00:00
In addition to the `sample` command, one can specify `init` command (executed only once before sampling) and `transform` command (to post-process `sample` command output). That covers interactive shell use case, e.g. to establish connection to a database only once, and then perform polling within interactive shell session.
2019-06-22 04:38:33 +00:00
#### Basic mode
2019-06-22 04:10:05 +00:00
```yml
textboxes:
- title: MongoDB polling
rate-ms: 500
init: 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 intractive shell won't work, because its stdin is not a terminal. We can fool it, using PTY mode:
```yml
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 -n 1
- 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
```
2019-04-20 04:30:13 +00:00
2019-06-11 03:01:37 +00:00
### Variables
2019-04-25 00:03:47 +00:00
If the configuration file contains repeated patterns, they can be extracted into the `variables` section.
Also variables can be specified using `-v` /`--variable` flag on startup, and any system environment variables will also be available in the scripts.
2019-04-20 04:30:13 +00:00
2019-06-22 04:10:05 +00:00
```yml
variables:
mongoconnection: mongo --quiet --host=localhost test
barcharts:
- title: MongoDB documents by status
items:
- label: IN_PROGRESS
init: $mongoconnection
2019-06-22 04:38:33 +00:00
sample: db.getCollection('events').find({status:'IN_PROGRESS'}).count()
2019-06-22 04:10:05 +00:00
- label: SUCCESS
init: $mongoconnection
2019-06-22 04:38:33 +00:00
sample: db.getCollection('events').find({status:'SUCCESS'}).count()
2019-06-22 04:10:05 +00:00
- label: FAIL
init: $mongoconnection
2019-06-22 04:38:33 +00:00
sample: db.getCollection('events').find({status:'FAIL'}).count()
2019-06-22 04:10:05 +00:00
```
2019-06-11 03:01:37 +00:00
### Color theme
2019-06-22 04:28:32 +00:00
![light-theme ](https://user-images.githubusercontent.com/6069066/59959405-994c0200-9484-11e9-856b-c4d18716e1de.png )
2019-06-22 04:10:05 +00:00
```yml
theme: light # default = dark
sparklines:
- title: CPU usage
sample: ps -A -o %cpu | awk '{s+=$1} END {print s}'
```
2019-04-20 04:30:13 +00:00
2019-06-11 03:01:37 +00:00
## Real-world examples
2019-04-25 00:03:47 +00:00
...