Friday, January 24, 2014

Dual y axis in gnuplot

Yeah there are resources out there n this but I had to do some playing around to get what I wanted out of this so I figured I'd post it here. I needed to graph free ram, free swap, and 5 minutes avg load in one graph. Given that the ram and swap were in MB and the cpu avg was going to be in a 0-100 scale, it called for the use of two axis'. So here's the script for it.

#! /bin/bash
# mark hamsel 1/24/14
# Script for plotting free ram, free swap, and 5 min cpu load

## Gnuplot config
#set term png
#set output '/var/www/freemem.png'
#set terminal png size 1500,400
#set ylabel "Megabytes" #left hand y axis label
#set y2label "CPU 5 min Avg" #righthand y axis label
#set xlabel "  " #using an obvious time so no xlabel
#set xtics rotate by 270
#set key outside
#set grid
#unset colorbox
#set yrange [0:4200] #Only had 4 gigs on the machine and I'm measuring in MB
#set y2range [0:100] #Utilization is a percent so 0-100
#set y2tics autofreq  norangelimit #This makes it so you can see the 0-100 tics
#set terminal png truecolor # Needed this to use transparent filledcurves
#set style fill transparent solid 0.50 noborder #Set the default plot style
## 1 is memory and 2 is swap
#plot '/home/mark/scripts/freemem.dat' using 3:xticlabel(1) w filledcurves x1 lc rgb '#630595' title 'Swap' axes x1y1, \
#'/home/mark/scripts/freemem.dat' using 2:xticlabel(1) w filledcurves x1 lc rgb '#00995E' title 'Ram' axes x1y1, \
#'/home/mark/scripts/freemem.dat' using 4:xticlabel(1) w lines lw 3 lc rgb '#DC6022' title 'CPU' axes x2y2



### This part gathers the data ###
date=$(date +%H:%M)
#This gets the free memory and swap values
values=$(free -mo|grep -v total|awk '{print $4}')
#This gets the 5 min load avg
fivenimloadavg=$(cat /proc/loadavg |awk '{print $2}')
#This gets the free mem/swap, load avg, and time in to a file for source data
echo $values $fivenimloadavg|sed "s/^/$date /" >> /home/mark/scripts/freemem.dat
#File cleanup so I only have the most recent 60 samples
for file in `echo /home/mark/scripts/freemem.dat`
do
DATALINES=$(wc -l $file|awk '{print $1}')
while [[ $DATALINES -gt 60 ]]
do
sed -i '1 d' $file
DATALINES=$(wc -l $file|awk '{print $1}')
done
done
#Plot it
gnuplot /home/mark/scripts/gnuplotfreemem.cfg



No comments:

Post a Comment