Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Tutorials
[Linux] Scripting with xfce4-panel's "Generic Monitor" applet
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="griimnak" data-source="post: 456580" data-attributes="member: 35695"><p style="text-align: center"><strong><span style="font-size: 26px">Scripting on the xfce4-panel</span></strong></p> <p style="text-align: center">The xfce4-panel is extremely customizable, offering a wide variety of applets and addons to satisfy the user's needs.</p> <p style="text-align: center"></p> <p style="text-align: center">HOWEVER, if you truly desire to make the panel your own, the most important applet, in my opinion atleast, is the<strong> Generic Monitor</strong> applet.</p> <p style="text-align: center"></p> <p style="text-align: center"><img src="https://i.imgur.com/1ko94KC.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p> <p style="text-align: center">All it takes is minutes of basic scripting knowledge to transform your panel with this simple applet.</p> <p style="text-align: center"></p> <p style="text-align: center">Here's an example using my setup:</p> <p style="text-align: center"><img src="https://i.imgur.com/Xt68faP.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p> <p style="text-align: center"></p><p><span style="font-size: 26px"><strong>Tutorial</strong></span></p><p>Starting with a brand new panel:</p><p></p><p><img src="https://i.imgur.com/8tArjRL.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p></p><p><u>Click</u> → + (Add new item to panel)</p><p><u>Search </u>→ "Generic Monitor"</p><p><u>Click </u>→ Add</p><p></p><p>[SPOILER=Added to panel]</p><p><img src="https://i.imgur.com/KBJyyNb.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p>[/SPOILER]</p><p></p><p>You should now see the default text for the applet on your bar (genmon)XXX</p><p><u>Right-click</u> → "(genmon)XXX" → <u>Select </u>"Properties"</p><p></p><p><img src="https://i.imgur.com/ybnlv2U.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p><strong><span style="font-size: 18px">Now the magic happens.</span></strong></p><p>For starters, you can try simple shell commands such as uname -r.</p><p>These less complex commands such as uname -r may be ran without the creation of an external shell script.</p><p></p><p>[SPOILER=Output of uname -r:]</p><p><img src="https://i.imgur.com/gNPdOud.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p>[/SPOILER]</p><p></p><p><span style="font-size: 18px"><strong>Now let's increase the complexity.</strong></span></p><p></p><p>We need a place to store our more complex scripts, I'm going to choose a directory called [ICODE]~/.genmonscripts[/ICODE], you can use any location and name you wish as long as it's accessible to your linux user.</p><p>[CODE=sh]</p><p>mkdir ~/.genmonscripts && cd ~/.genmonscripts</p><p>touch freemem.sh</p><p>[/CODE]</p><p></p><p>The main idea is to perform your work in the script and finally <strong>echo</strong> or <strong>print </strong>out the string you wish to display on the xfce4-panel.</p><p>freemem.sh:</p><p>[CODE=bash]</p><p>#!/bin/bash</p><p># create variable FREE_MEM, then pipe data from free -h into awk which selects the part of the text you would want</p><p>FREE_MEM=`free -h | awk '/^Mem:/ {print $3 "/" $2}'`</p><p>echo "ram $FREE_MEM "</p><p># printed out to panel</p><p>[/CODE]</p><p></p><p>Once you have the script written, we can test if it works on our panel.</p><p><strong>Note:</strong> the <strong>~</strong> alias <u>will not work</u>, you must use the real path. (typically <strong>/home/yourusername/</strong>)</p><p></p><p>[CODE=sh]sh /home/griimnak/.genmonscripts/freemem.sh</p><p>[/CODE]</p><p>(don't forget sh at the beginning)</p><p><img src="https://i.imgur.com/eZZOltN.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p>And that's basically it!</p><p>This may seem simple, but someone with advanced scripting knowledge could do some really impressive things with this applet.</p><p>For example, you could write a script that checks how many new emails you have, etc.</p><p></p><p><strong><span style="font-size: 26px">Linting your scripts</span></strong></p><p>Linting your scripts are just generally considered good practice and minimize memory leaks from inefficient or invalid scripts.</p><p></p><p><img src="https://github.com/koalaman/shellcheck/raw/master/doc/terminal.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p>Download and install <a href="https://github.com/koalaman/shellcheck" target="_blank">ShellCheck</a></p><p></p><p><a href="https://github.com/koalaman/shellcheck" target="_blank"><strong>ShellCheck</strong></a> is a static linting tool for shell scripts. </p><p>Installation guide: <a href="https://github.com/koalaman/shellcheck#installing" target="_blank">https://github.com/koalaman/shellcheck#installing</a></p><p></p><p><strong><span style="font-size: 26px">My scripts</span></strong></p><p>To assist you further with understanding how to write scripts for genmon, i'll be providing the scripts i personally use.</p><p></p><p><strong>name:</strong> cputemp.sh</p><p>desc: returns the temp of the cpu package</p><p>useage: sh cputemp.sh</p><p>[CODE=bash]</p><p>#!/bin/bash</p><p></p><p></p><p>if which sensors > /dev/null; then</p><p> sensors | grep Core | awk '{print $3;}' | grep -oEi '[0-9]+.[0-9]+' | awk '{total+=$1; count+=1} END {print total/count,"C "}'</p><p>else</p><p> "??"</p><p>fi</p><p>[/CODE]</p><p></p><p></p><p><strong>name:</strong> sysload.sh</p><p>desc: returns both current cpu usage % and used ram/maxram ratio in one string.</p><p>usage: sh sysload.sh</p><p>[CODE=bash]</p><p>#!/bin/bash</p><p>CPU=`eval $(awk '/^cpu /{print "previdle=" $5 "; prevtotal=" $2+$3+$4+$5 }' /proc/stat); sleep 0.4; eval $(awk '/^cpu /{print "idle=" $5 "; total=" $2+$3+$4+$5 }' /proc/stat); intervaltotal=$((total-${prevtotal:-0})); echo "$((100*( (intervaltotal) - ($idle-${previdle:-0}) ) / (intervaltotal) ))"`</p><p>FREE_MEM=`free -h | awk '/^Mem:/ {print $3 "/" $2}'`</p><p>printf "cpu %.f%% " $CPU</p><p>echo "ram $FREE_MEM "</p><p>[/CODE]</p><p></p><p></p><p><strong>name:</strong> netload.sh</p><p>desc: returns current network usage (up and down)</p><p>usage: sh netload.sh <adapter></p><p>Note: Set update interval to 1.6 or above on genmon</p><p>[CODE=bash]</p><p>#!/bin/bash</p><p></p><p></p><p>if [ -z "$1" ]; then</p><p> echo</p><p> echo usage: $0 network-interface</p><p> echo</p><p> echo e.g. $0 eth0</p><p> echo</p><p> exit</p><p>fi</p><p></p><p>IF=$1</p><p></p><p>R1=`cat /sys/class/net/$1/statistics/rx_bytes`</p><p>T1=`cat /sys/class/net/$1/statistics/tx_bytes`</p><p>sleep 1</p><p>R2=`cat /sys/class/net/$1/statistics/rx_bytes`</p><p>T2=`cat /sys/class/net/$1/statistics/tx_bytes`</p><p>TBPS=`expr $T2 - $T1`</p><p>RBPS=`expr $R2 - $R1`</p><p>TKBPS=`expr $TBPS / 1024`</p><p>RKBPS=`expr $RBPS / 1024`</p><p>printf "%s: ↓%dK ↑%dK " $1 $RKBPS $TKBPS</p><p>[/CODE]</p><p></p><p>Hopefully this was useful to someone out there, Happy scripting!</p></blockquote><p></p>
[QUOTE="griimnak, post: 456580, member: 35695"] [CENTER][B][SIZE=7]Scripting on the xfce4-panel[/SIZE][/B] The xfce4-panel is extremely customizable, offering a wide variety of applets and addons to satisfy the user's needs. HOWEVER, if you truly desire to make the panel your own, the most important applet, in my opinion atleast, is the[B] Generic Monitor[/B] applet. [IMG]https://i.imgur.com/1ko94KC.png[/IMG] All it takes is minutes of basic scripting knowledge to transform your panel with this simple applet. Here's an example using my setup: [IMG]https://i.imgur.com/Xt68faP.png[/IMG] [/CENTER] [SIZE=7][B]Tutorial[/B][/SIZE] Starting with a brand new panel: [IMG]https://i.imgur.com/8tArjRL.png[/IMG] [U]Click[/U] → + (Add new item to panel) [U]Search [/U]→ "Generic Monitor" [U]Click [/U]→ Add [SPOILER=Added to panel] [IMG]https://i.imgur.com/KBJyyNb.png[/IMG] [/SPOILER] You should now see the default text for the applet on your bar (genmon)XXX [U]Right-click[/U] → "(genmon)XXX" → [U]Select [/U]"Properties" [IMG]https://i.imgur.com/ybnlv2U.png[/IMG] [B][SIZE=5]Now the magic happens.[/SIZE][/B] For starters, you can try simple shell commands such as uname -r. These less complex commands such as uname -r may be ran without the creation of an external shell script. [SPOILER=Output of uname -r:] [IMG]https://i.imgur.com/gNPdOud.png[/IMG] [/SPOILER] [SIZE=5][B]Now let's increase the complexity.[/B][/SIZE] We need a place to store our more complex scripts, I'm going to choose a directory called [ICODE]~/.genmonscripts[/ICODE], you can use any location and name you wish as long as it's accessible to your linux user. [CODE=sh] mkdir ~/.genmonscripts && cd ~/.genmonscripts touch freemem.sh [/CODE] The main idea is to perform your work in the script and finally [B]echo[/B] or [B]print [/B]out the string you wish to display on the xfce4-panel. freemem.sh: [CODE=bash] #!/bin/bash # create variable FREE_MEM, then pipe data from free -h into awk which selects the part of the text you would want FREE_MEM=`free -h | awk '/^Mem:/ {print $3 "/" $2}'` echo "ram $FREE_MEM " # printed out to panel [/CODE] Once you have the script written, we can test if it works on our panel. [B]Note:[/B] the [B]~[/B] alias [U]will not work[/U], you must use the real path. (typically [B]/home/yourusername/[/B]) [CODE=sh]sh /home/griimnak/.genmonscripts/freemem.sh [/CODE] (don't forget sh at the beginning) [IMG]https://i.imgur.com/eZZOltN.png[/IMG] And that's basically it! This may seem simple, but someone with advanced scripting knowledge could do some really impressive things with this applet. For example, you could write a script that checks how many new emails you have, etc. [B][SIZE=7]Linting your scripts[/SIZE][/B] Linting your scripts are just generally considered good practice and minimize memory leaks from inefficient or invalid scripts. [IMG]https://github.com/koalaman/shellcheck/raw/master/doc/terminal.png[/IMG] Download and install [URL='https://github.com/koalaman/shellcheck']ShellCheck[/URL] [URL='https://github.com/koalaman/shellcheck'][B]ShellCheck[/B][/URL] is a static linting tool for shell scripts. Installation guide: [URL]https://github.com/koalaman/shellcheck#installing[/URL] [B][SIZE=7]My scripts[/SIZE][/B] To assist you further with understanding how to write scripts for genmon, i'll be providing the scripts i personally use. [B]name:[/B] cputemp.sh desc: returns the temp of the cpu package useage: sh cputemp.sh [CODE=bash] #!/bin/bash if which sensors > /dev/null; then sensors | grep Core | awk '{print $3;}' | grep -oEi '[0-9]+.[0-9]+' | awk '{total+=$1; count+=1} END {print total/count,"C "}' else "??" fi [/CODE] [B]name:[/B] sysload.sh desc: returns both current cpu usage % and used ram/maxram ratio in one string. usage: sh sysload.sh [CODE=bash] #!/bin/bash CPU=`eval $(awk '/^cpu /{print "previdle=" $5 "; prevtotal=" $2+$3+$4+$5 }' /proc/stat); sleep 0.4; eval $(awk '/^cpu /{print "idle=" $5 "; total=" $2+$3+$4+$5 }' /proc/stat); intervaltotal=$((total-${prevtotal:-0})); echo "$((100*( (intervaltotal) - ($idle-${previdle:-0}) ) / (intervaltotal) ))"` FREE_MEM=`free -h | awk '/^Mem:/ {print $3 "/" $2}'` printf "cpu %.f%% " $CPU echo "ram $FREE_MEM " [/CODE] [B]name:[/B] netload.sh desc: returns current network usage (up and down) usage: sh netload.sh <adapter> Note: Set update interval to 1.6 or above on genmon [CODE=bash] #!/bin/bash if [ -z "$1" ]; then echo echo usage: $0 network-interface echo echo e.g. $0 eth0 echo exit fi IF=$1 R1=`cat /sys/class/net/$1/statistics/rx_bytes` T1=`cat /sys/class/net/$1/statistics/tx_bytes` sleep 1 R2=`cat /sys/class/net/$1/statistics/rx_bytes` T2=`cat /sys/class/net/$1/statistics/tx_bytes` TBPS=`expr $T2 - $T1` RBPS=`expr $R2 - $R1` TKBPS=`expr $TBPS / 1024` RKBPS=`expr $RBPS / 1024` printf "%s: ↓%dK ↑%dK " $1 $RKBPS $TKBPS [/CODE] Hopefully this was useful to someone out there, Happy scripting! [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Tutorials
[Linux] Scripting with xfce4-panel's "Generic Monitor" applet
Top