You are not logged in.

#1 2013-03-22 04:19:41

themadxcow
Member
Registered: 2013-03-22
Posts: 3

First xfce4-panel-genmon bash script

This is a script that displays the current usage of nvidia gpu memory, cpu task time, and ram memory.  I got a little carried away and tried to make the text scale, change colour, and add bold based on their values. It also stores previous cpu data in a text file at /usr/local/etc/.mon. Hopefully it works well.

#!/bin/bash
Data(){
	TMP=(`head -n 3 "/proc/stat" | awk 'NR==i=2 {while(i<13) {a+=$(i++)} {print a,($5+$6)}}'`)
	CUSE=(`tail -n 1 "/usr/local/etc/.mon" | awk -v "t=${TMP[0]}" -v "i=${TMP[1]}" '{printf("%.2f", (( (t-$1)-(i-$2) )/(t-$1))*100)}'`)
	CFREQ=(`head -n 8 "/proc/cpuinfo" | awk 'NR==8 {printf("%.0f", $4)}'`)
   GFREQ=(`nvidia-settings -tq [gpu:0]/GPUCurrentClockFreqs -q [gpu:0]/GPUCurrentProcessorClockFreqs | awk -F "," 'NR==1 {x=$1+$2}; NR==2 {print(x+$1)}'`)
	GPU=(`nvidia-smi -q -d MEMORY,TEMPERATURE | awk 'NR==10 {a=$3}; NR==11 {printf("%.2f",(($3/a)*100))}; NR==14 {print " "$3}'`)
	MEM=(`ps -eo rss | awk '{x+=$0} END {printf("%d", (x/6080436)*100)}'`)
	CPU=(`sensors -A | awk 'match($3,/([0-9]*[0-9]\.[0-9])/) {print substr($3,RSTART,RLENGTH)}'`)
	Colour ${GPU[0]} 	0; gcol=$rgb; gwt=$weight
	Colour $CUSE 		0; ccol=$rgb; cwt=$weight
	Colour $MEM 		0; mcol=$rgb; mwt=$weight
	Colour ${GPU[1]} 	1; gtemp=$rgb; gtwt=$weight
	Colour ${CPU[0]} 	1; ctemp=$rgb; ctwt=$weight
	echo "<u><span color='#ffffff'>|  	   GPU ${GFREQ}hz 	     |" \
	"  	CPU ${CFREQ}hz 	      |"\
	" MEM |</span></u>"
	echo -n "<span color='#ffffff'>|</span>" \
	" <span weight='$gwt' color='$gcol'>	${GPU[0]}%</span>" \
	" <span weight='$gtwt' color='$gtemp'>+${GPU[1]}°C</span>" \
	" <span color='#ffffff'>    |</span>" \
	" <span weight='$cwt' color='$ccol'>    $CUSE%</span>" \
	" <span weight='$ctwt' color='$ctemp'>${CPU[@]}</span>" \
	" <span color='#ffffff'>    |</span>" \
	" <span weight='$mwt' color='$mcol'> $MEM%</span>" \
	" <span color='#ffffff'>|</span>"
	echo -n "${TMP[@]}" > "/usr/local/etc/.mon"
}

Colour(){
depth=(`awk -v x=$1 -v y=$2 'BEGIN {printf("%.2f",(x-(35*y))/(100-(25*y)))}'`)
hsv=(`echo "h=(120+(128*$2))-((120+(128*$2))*$depth);" \
	"s=((1+($depth*$2))/(1+$2));" \
	"(1+$depth)/1.5;v=last;" \
	"i=floor(h/60);" \
	"f=h/60-i;" \
	"v*(1-s);" \
	"v*(1-f*s);" \
	"v*(1-(1-f)*s);scale=0;" \
	"i%6; floor(($depth*8)+2)*100" | bc -l /home/x/.local/func.bc`)
	i=${hsv[4]}; weight=${hsv[5]}
	hsv=(`awk  -v "v=${hsv[0]}" -v "p=${hsv[1]}" -v "q=${hsv[2]}" -v "t=${hsv[3]}" 'BEGIN{printf("%02x ", v*255)}BEGIN{printf("%02x ", p*255)}BEGIN{printf("%02x ", q*255)}BEGIN{printf("%02x ", t*255)}BEGIN{printf("%02x",191.25)}'`)
	#v p q t
case $i in
	0) rgb="#${hsv[0]}${hsv[3]}${hsv[1]}";;
	1) rgb="#${hsv[2]}${hsv[0]}${hsv[1]}";;
	2) rgb="#${hsv[1]}${hsv[0]}${hsv[1]}";;
	3) rgb="#${hsv[1]}${hsv[2]}${hsv[0]}";;
	4) rgb="#${hsv[3]}${hsv[1]}${hsv[0]}";;
	5) rgb="#${hsv[0]}${hsv[1]}${hsv[2]}";;
esac
}

Data
exit

Offline

#2 2013-03-22 08:19:25

swanson
Member
From: Sweden
Registered: 2011-02-05
Posts: 759

Re: First xfce4-panel-genmon bash script

Sorry for my ignorance but what is func.bc ?

Offline

#3 2013-03-22 23:22:06

themadxcow
Member
Registered: 2013-03-22
Posts: 3

Re: First xfce4-panel-genmon bash script

Oh sorry!  its just a floor function for bc:

define floor(x) {
  auto os,xx;os=scale;scale=0
  xx=x/1;if(xx>x).=xx--
  scale=os;return(xx)
}

Offline

#4 2013-03-23 07:23:23

swanson
Member
From: Sweden
Registered: 2011-02-05
Posts: 759

Re: First xfce4-panel-genmon bash script

Nice thank you!

Offline

#5 2013-03-23 12:10:05

digirium
Member
Registered: 2012-11-15
Posts: 51

Re: First xfce4-panel-genmon bash script

I was excited about your script because it uses what appeared be pango formatting! I've written a few genmon plugins (in C and in bash) before but never included any pango. I tried it out, got it to execute cleanly (just had to change the paths for the .mon file and the bc script) and checked that the script produced output. Then I added the script to a genmon on my panel to see what it did! I just got span written a lot of times across the panel! Next, I tried encapsulating the output with <txt> ... </txt> and then tried again with <tool> .. </tool>. But that didn't work either.

So I am puzzled now! I was keen to see how the output looked but I cannot figure out how this worked for you not me and I need an idea :s If you wish I can post an image of what it looked like on my panel? I am running XFCE4 (startxfce4) and replaced the window manager with compiz and emerald.

Offline

#6 2013-03-23 12:18:11

digirium
Member
Registered: 2012-11-15
Posts: 51

Re: First xfce4-panel-genmon bash script

Here is a screenshot: https://dl.dropbox.com/s/o60qsbuh446m7pa/ss-nv-hmmm.png

Here is output:

<u><span color='#ffffff'>|  	   GPU hz 	     |   	CPU 3400hz 	      |  MEM |</span></u>
<span color='#ffffff'>|</span>  <span weight='300' color='#51cc00'>	19.84%</span>  <span weight='0' color='#82528e'>+23°C</span>  <span color='#ffffff'>    |</span>  <span weight='200' color='#1db700'>    8.29%</span>  <span weight='-200' color='#5d4343'>1.4 3.3 5.0 12.2 34.0 22.0 83.5</span>  <span color='#ffffff'>    |</span>  <span weight='400' color='#9ae300'> 34%</span>  <span color='#ffffff'>|</span>

Offline

#7 2013-03-23 20:16:26

themadxcow
Member
Registered: 2013-03-22
Posts: 3

Re: First xfce4-panel-genmon bash script

Yes, it is Pango markup.  Unfortunately, I did not include any error handling, nor does Pango, so if a single variable is null then Pango gives up and walks away.

In your case the offender is the GPU frequency var and possibly the cpu temperature.  The script currently grabs that information from nvidia-smi and /proc/stat.

Here's what it should look like: SqLkJzT.png

You may have to play with it a bit to get it perfect, but replacing this part should help:

echo "<u><span color='#ffffff'>|  	   GPU 	     |" \
	"  	CPU ${CFREQ}hz 	      |"\
	" MEM |</span></u>"
	echo -n "<span color='#ffffff'>|</span>" \
	" <span weight='$gwt' color='$gcol'>	${GPU[0]}%</span>" \
	" <span weight='$gtwt' color='$gtemp'>+${GPU[1]}°C</span>" \ #<-- remove this line if it still doesnt load via Pango
	" <span color='#ffffff'>    |</span>" \
	" <span weight='$cwt' color='$ccol'>    $CUSE%</span>" \
	" <span color='#ffffff'>    |</span>" \
	" <span weight='$mwt' color='$mcol'> $MEM%</span>" \
	" <span color='#ffffff'>|</span>"

Offline

#8 2013-03-24 15:45:23

digirium
Member
Registered: 2012-11-15
Posts: 51

Re: First xfce4-panel-genmon bash script

Thanks, I did get it working. I have xfce4-genmon-plugin 3.4.0-1 and that version does actually seem to need <txt></txt> encapsulating the pango markup. Just doesnt work without it, wondering which version of genmon plugin you have? Also, ctwt was getting set to -200 this seemed to upset pango causing it to spit out the spans as raw markup instead of nicely formatting (overriding to 200 made it work).

Thank you for your nice script it opened my eyes to some new ideas. smile

Offline

Board footer

Powered by FluxBB