XFCE4 — Using the General Monitor (Genmon)

[Note that in the examples that follow, all quotes, single and double should be straight. It is wordpress that makes them look like left and right quotes!!]

The XFCE4 desktop is known as a lightweight, fast, functional desktop, yet it has many features. It has a Desktop Settings Manager which calls a number of applets, not as complex or as many the KDE Settings Manager, but all that most people will ever need. There are a nuScreenshot_2016-07-23_13-59-12mber of Panel apps that can be added. The Launcher is the most flexible I have seen, it can appear as a single icon to launch one program, or as an icon with a small arrow, which can have a drop-down menu, and this is done simply by adding more than one program to the Launcher. The top launcher will be the main icon, and if you want the program to re-appear in the drop down, just add it again.

As you can see from the screenshot here, I have put all parts of LibreOffice onto a single launcher.

And there’s more… unlike some desktop panel program launchers, you are not totally tied to menu items. When you add an item, it can be from your usual startup menu, but you can also create it from scratch if you know where to find the executable file, icon, and you can use your own text in the name and the description. Or you can create your own by amending it from the standard entry.

And what about GenMon?

With all this power you’d be amazed that XFCE4 runs so smoothly. One panel applet it does lack, however, is a CPU temperature monitor. So I am going to design one using GenMon. What I am about to write may not work for you, this is about technique. You will need to install psensor, available from Synaptic in PCLinuxOS. You may also have to root around in a folder called /sys/class/hwmon/ which is a “virtual folder” from which you can read stuff about your computer’s hardware monitoring.

GenMon is a panel applet that runs a shell script that you write. The script must cause up to four lines to be echoed (printed). The lines are in this format:

<txt>Text</txt>
<tool>Tooltip</tool>
<img>image.png</img>
<click>program</click>

Any of these can be variables, so that a number of conditions can be measured, and choices made as to the content of what is seen on the screen. The image will be an icon, the click will be a program that is run only where there is an icon to click on, the text can be any text, and the tooltip can also be any text.

My computer’s processor is an Athlon FX4100. Its hardware monitor returns only two numbers, some return loads of them! The first is the temperature of the core of the processor. The second is the power being dissipated from the processor by its heatsink. Open your file manager at /sys/class/hwmon/hwmon0 and you may see this:
hwmon0.png

The virtual text file, temp1_input, contains the CPU core temperature, in 1000ths of a degree Celsius. You cannot open this file with a normal text editor, but you can read its contents at any given time. Open up a terminal and type in

cat /sys/class/hwmon/hwmon0/temp1_input

You will get a figure, probably in the 10,000s. Or maybe you may have to look somewhere else for this file. But I am assuming for now that this is the right one. In order to capture that value into a variable in a script file, your line would be like this:

temp1=`cat /sys/class/hwmon/hwmon0/temp1_input`

Note that the reverse apostrophes cause the text output from a command to be returned so that you can allocate it to a variable, in this case, temp1.

The folder /sys/class/hwmon/hwmon1/ contains a similar file called “power1_input”. If you type into your terminal:

cat /sys/class/hwmon/hwmon1/power1_input

You will get a number in the 10,000,000s, indicating the power dissipation of the heat from the CPU. Many CPUs do not return this number, instead returning a fan speed. You will find something, somewhere in the hardware monitors folders which might be useful. But for me, this is useful, so I will add another line to my script file.

power1=`cat /sys/class/hwmon/hwmon1/power1_input`

Now, the question is to manipulate those two numbers so that they can show useful information on your Genmon applet on your panel. If the “cat” commands above do not return figures that may be usable to you, you’ll need to find out more about your hardware. The stuff is all there, somewhere!

I did come up with some multi-line stuff, which manipulated the numbers, separating the division and the remainder and putting a decimal point between. What I wanted was 4 significant figures with a decimal point included, mainly so that the number would not change length, always consisting of 5 characters, i.e., 5.030, 54.37, or 120.2. Here is one way of going about it, dividing temp1 by 1000:

#First we divide by 1000, but this loses any decimal points.
temp2=$(($temp1/1000)) 
#Now we get the remainder.
temp3=$(($temp1%1000))
#Now we have to add leading zeroes to the remainder
temp4=$((1000+$temp3))
temp3=${temp4:1:3}
#And finally…
temp4=$temp2″.”$temp3
#and to 4 significant figures:
temp1={temp4:0:5}”°C”

It works, and works well. But I wanted to find something  tidier so I had a look around on the net to see if there was a tidier way to do floating point arithmetic. I chose awk rather than bc because bc does not use a leading zero on numbers below 1.

awk is actually a quick and dirty programming language (you learn something new every day). It normally works from a file, but you can work it from a variable. I’ve not studied all of it yet, but from the little I read, I worked out how to do a floating point division. This page was very helpful: Awk Primer — print and printf()

Anyway, the line I had to use was this (I’ll explain what it all means in a minute)

temp3=$(awk -v m=$temp1 ‘BEGIN { printf(“%-.3f”, m/1000) }’)
temp1=${temp3:0:5}

The awk command returns a string in this case, so by placing the command inside brackets, preceded by a dollar sign, we can allocate the result to a string. The “-v m=$temp1” is important because it defines a variable to be used by the command, otherwise it will require a file. ‘BEGIN {….} ‘ is part of the syntax for awk, I don’t pretend to understand it yet. printf() is a formatted string and the page I referred to above was very helpful in understanding it. The first, quoted, string inside the brackets, defines the format, in this case, %-.3f means left justified, floating point, to 3 decimal places. The following line just reduced the result to 4 significant figures (5 characters including the decimal point)

OK, with this knowledge, I can build a nice and short script, tailored to my own computer. I did this, then one day it returned an error! Why? Well due to the linux system’s hardware detection, it allocated hwmon0 and hwmon1 the other way around! So I added a test for this, and here is the final script.

#!/bin/sh

if [ -e /sys/class/hwmon/hwmon1/temp1_input ] ;
then
     temp1=`cat /sys/class/hwmon/hwmon1/temp1_input`
     power1=`cat /sys/class/hwmon/hwmon0/power1_input`
else
     temp1=`cat /sys/class/hwmon/hwmon0/temp1_input`
     power1=`cat /sys/class/hwmon/hwmon1/power1_input`
fi

#4 more lines later added here, see below

temp3=$(awk -v m=$temp1 ‘BEGIN { printf(“%-.3f”, m/1000) }’)
temp1=${temp3:0:5}”°C”

power2=$(awk -v m=$power1 ‘BEGIN { printf(“%-.3f”, m/1000000 )}’)
power1=${power2:0:5}”W”

echo “<img>/usr/share/icons/temperature-s.png</img>”
echo “<txt>$temp1</txt>”
echo “<tool>Heat Transfer: $power1</tool>”
echo “<click>psensor</click>”

This brought about a further problem. The temperature returned by my Athlon FX4100 CPU is unrealistically low. This is because the figure it returns is designed to show how much stress the CPU is under, and doesn’t become accurate until the processor starts getting hot. Telling me that the processor is running at 8°C on a hot summer’s day, is clearly wrong. I picked a number off the top of my head, and decided that the figure becomes accurate at 60°C, and that I would add a third of the difference between the recorded temperature and the 60. For most processors this would not be a problem, but of course, when you write your own script, you can do what you like!

So I added these few lines where indicated in the script.

tempdiff1=$((60000-$temp1))
discrim=$(($tempdiff1>0))
tempdiff2=$(($tempdiff1*$discrim/3))
temp2=$(($temp1+$tempdiff2))

These lines measure the difference if the figure returned is under 60, and if it is, divide it by 3 and add it to the temperature provided by the sensors. Note you’re left with “temp2” here so you would have to make a change the following line, starting “temp3=”,  change “m=$temp1” to “m=$temp2”.

genmon1Another thing you can do, if you prefer Fahrenheit, is, in the line starting “temp3=”, change m/1000 to 32+9*m/5000. And in the following line, change “°C” to “°F”.

You need to save the file, I’ve called mine “correct-temp” in the /usr/local/bin folder, and make it executable, but before you attach it to a GenMon, you need to check that it works. If you run it in a terminal, you should see four lines of output, as shown further up this article. You will also need to have an icon called temperature-s.png in your /usr/bin/icons folder. There is one here for you: 

temperature-s  https://dl.dropboxusercontent.com/u/81695069/temperature-s.png

genmon2To add the script to GenMon, first you have to install xfce4-genmon-plugin from Synaptic, then add it to a panel. Right click on it, then select Properties, you just need to put the name of the script into the box labelled “Command” and set the time you want it to repeat, in seconds.

I hope that you can find a way of using genmon to expand the possibilities of XFCE4 on your desktop.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: