How to visualise statistics in the CLI
I do love CLI. However, there are things that are better shown using graphical overview, such as a linegraph. A terrific example are the memory statistics in /proc/meminfo in linux. It's a long list, and even if you grep a few relevant statistics, you have to query repeatedly to see what is happening, and then still that doesn't make it easy to understand how these move over time.
But you can't create graphs such as linegraphs directly in the CLI...or can that be done? Have a look at: [sampler](https://github.com/sqshq/sampler).
Using a very simple configuration file, you can take one or more statistics, and visualize these in multiple forms, of which one is a linegraph:
This can be accomplished using the following 'sampler' script:
runcharts:
- title: memory in MB
rate-ms: 500 # sampling rate, default = 1000
scale: 0 # number of digits after sample decimal point, default = 1
items:
- label: free memory
sample: awk '/MemFree/ { print $2/1024 }' /proc/meminfo
- label: available memory
sample: awk '/MemAvailable/ { print $2/1024 }' /proc/meminfo
It's really simple make the sampler script use a statistic, as you can see from the above awk scripts. But these can be anything, it just needs to produce a number.
Some things to produce a number are just too time consuming and CPU intensive to do all the time, such as logging on to a database. And even if it's doable, you shouldn't, pretty much for every database.
For these situations there is the option to perform the initialisation of a process that it will reuse, instead of performing the complete logon every time: (this is the PostgreSQL example from the sampler website):
# 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();
Please mind there are some things that I would have loved to see, which do not appear to be implemented:
For line graphs, and for many more graphs, it's quite often very helpful to have the Y axis start with zero, instead of just showing the range of min and max values.
For line graphs, I would love to see stacked line or area graphs.