Ir al contenido principal

Playing with Gnuplot and load average

Ok, I was curious how is my load average going at night.

Metrics

First of all, I wrote a short script to dump the load average every 5 minutes:

#!/bin/bash
while true ; do
  val=$(cat /proc/loadavg | awk '{print $1,$3}')
  time=$(date "+%R")
  echo "$time $val" >> kakafile
  sleep 5m
done

The content of /proc/loadavg is similar to the output of the uptime command, but it is limited to the load data on 1, 5 and 15 minutes, without any extra records to be trimmed.
I am only interested in the most recent load and the overall average on 15min (1st and 3rd field).
The time format "%R" corresponds to "%H:%M" → that is hour:minute.

The script generates output like:

09:03 57.60 24.05
09:08 36.83 31.91
09:13 19.48 28.07
09:18 12.64 23.80
09:23 15.61 21.59
09:28 16.31 19.84
09:33 16.06 19.10

Gnuplot

Well then, with this kind of data, if we want to get a nice and easy graph in the terminal, we have several options. I just use 'gnuplot' program. Feel free to use and document your favourites, though!

You can run Gnuplot in an interactive mode, invoking it from the command line, so you can easily modify the options → perfect for testing purposes:

# gnuplot
> 

First of all, we instruct the program to generate ASCII output directly in the terminal:

> set terminal dumb

As far as I understand, gnuplot takes the first column as a measure of horizontal (x) axis. In our case, we have to inform the program that in our input we use time measures and that it is in HH:MM format. Then, we also want to have the x axis on the graph labeled with the time format:

> set xdata time
> set timefmt "%H:%M"
> set format x "%H:%M"

Now we can check the graph:

> plot 'kakafile' using 1:2 notitle with lines, 'kakafile' using 1:3 notitle with lines


  60 +---------------------------------------------------------------------+   
     | *     +       +      +       +       +       +      +       +       |   
  55 |-*                                                                 +-|   
     |  *                                                                  |   
  50 |-+*                                                                +-|   
     |  *                                                                  |   
  45 |-+*                                                                +-|   
     |   *                                                                 |   
  40 |-+ *                                                               +-|   
     |   *                                                                 |   
  35 |-+ *                                                               +-|   
     |   ###                                                               |   
  30 |-+# *                             *                                +-|   
     |  #  *##                          **                                 |   
  25 |-#   *  ###                      * *                               +-|   
     |      *    ###                   *###                                |   
  20 |-+    *       #####             *#  *#####   **                    +-|   
     |       *     ***   #####    *****#   *    ##########     **######    |   
  15 |-+     *  ***   *** ****#########     *  **        *#######    **# +-|   
     |       +**     +   *  + *     +       +**     +   *  +       +       |   
  10 +---------------------------------------------------------------------+   
   09:00   09:15   09:30  09:45   10:00   10:15   10:30  10:45   11:00   11:15 

And that's it!

Now, we can get the same output directly from the command line prompt:

# gnuplot -e "set terminal dumb; set xdata time; set timefmt '%H:%M'; set format x '%H:%M'; p 'kakafile' using 1:2 notitle with lines, 'kakafile' using 1:3 notitle with lines"

Playing further

To play further, you can change the sleep time to 3 seconds in the initial data collection script (put sleep 3) and have a real time graph with :

# watch -n 1 "gnuplot -e \"set terminal dumb; set xdata time; set timefmt '%H:%M'; set format x '%H:%M'; p 'kakafile' using 1:2 notitle with lines, 'kakafile' using 1:3 notitle with lines\""

Have fun and light loads!