4

Basically, I want to graph the memory usage with each username as title, as time passes by. I would set that script as a cron job, to create a graph that shows memory usage of certain usernames.

How can I graph percentage of memory consumption based on user?

Y-axis should be %MEM, X-axis should be time/date

I tried doing it with gnuplot, but failed.

The data I want to graph is:

for USER in $(ps haux | awk '{print $1}' | sort -u); do ps haux | awk -v user=$USER '$1 ~ user { sum += $4} END { print user, sum; }'; done

Example output:

102 0
avahi 0
colord 0
daemon 0
savvas 16.6
miredo 0
nobody 0
postfix 0
root 1.3
rtkit 0
syslog 0
whoopsie 0

The last gnuplot script I used:

set term png small size 800,600
set output "mem-graph.png"

set ylabel "%MEM"
set xlabel "Time"

set xtics nomirror
set ytics nomirror

set xrange [0:*]

set key autotitle columnheader

plot "mem.log" using 2 title columnhead(1) with lines

The script I use to graph:

#!/bin/bash
rm mem.log
while true
do
    for USER in $(ps haux | awk '{print $1}' | sort -u); do ps haux | awk -v user=$USER '$1 ~ user { sum += $4} END { print user, sum; }'; done >> mem.log
    gnuplot gnuplot.script
    sleep 10
done
Savvas Radevic
  • 1,124
  • 7
  • 14
  • How did your gnuplot script fail? – darthbith Sep 29 '13 at 14:10
  • @darthbith I want a separate line/bar for each username. Also, I can't get the title to show the username. I've experimented a lot with plotting but I couldn't manage to achieve this. – Savvas Radevic Sep 29 '13 at 15:22
  • 1
    Rather than using `with lines` try `with boxes`. See [here](http://stackoverflow.com/questions/327576/how-do-you-plot-bar-charts-in-gnuplot). – darthbith Sep 29 '13 at 16:53
  • Thank you, but that doesn't change the fact that I can't see the memory usage with each username as title, as time passes by. I would set that script as a cron job, to create a graph that shows memory usage of certain usernames. It would definitely ease up some site administrations I have to take care of. – Savvas Radevic Sep 29 '13 at 20:39
  • Oh now I see what you want. Unfortunately, I don't know how to do what you that ;-) Hopefully someone else can help you – darthbith Sep 29 '13 at 21:45
  • 1
    You need to log the memory usage data to a file (include a timestamp!), then plot using that file. If you generate a graph using only current information, you won't have a historic line chart, just a snapshot of the current memory usage. – John Lyon Sep 30 '13 at 00:22

1 Answers1

3

I experimented a bit more and I finally created something that more or less fits my needs!

graphmem.sh

#!/bin/bash
cwd="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$cwd"
for iuser in $(ps haxo user | sort -u); do
    [[ $iuser == "root" ]] && continue
    ps haxo user:64,pmem | awk -v tnow="`date -u +'%H:%M:%S'`" -v user="$iuser" -v uid="$((id -u $iuser || echo $iuser) 2>/dev/null)" '$1 ~ user { sum += $2} END { if (sum) print tnow,sum,user,uid; else print tnow,0,user,uid; }'
done | sort -nk +2 >> mem.log
gnuplot "gnuplot.script"
cp mem-graph.png /path/to/www-destination/
echo "Done"

Note: [[ $iuser == "root" ]] && continue removes the "root" user info.

gnuplot.script

reset
set key left top
set xtics nomirror
set ytics nomirror

set term png size 5000,1000

set output "mem-graph.png"

set ylabel "% Memory usage"
set xlabel " "
set auto xy
set xtics out offset 1,0 scale 25,0 rotate by 90
set yrange [0:100]
set xrange [0:*]
set style fill solid 1.0 border -1
set boxwidth 1
set multiplot
set title "Memory usage per user"

plot 'mem.log' using ($0):(($2 > 0.5) ? $2 : 1/0):4:xticlabels(stringcolumn(1)." ".stringcolumn(3)." ".stringcolumn(2)."%") with boxes lc variable notitle, \
    '' using ($0):(($2 > 0.5) ? $2 : 1/0):(sprintf("%.1f%%",$2)) with labels offset 0,1 notitle
unset border
unset multiplot

Add in /etc/crontab

@hourly root /path/to/graphmem.sh

Note: You have to remove the mem.log manually every day, but at least it gives a nice graph for 24 hours.

mem-graph.png

Savvas Radevic
  • 1,124
  • 7
  • 14