Skip to contentSkip to conversation

Patch and Upstart file for healthd.sh

lm-sensors vbooh 2 participants 5 messages Feb 26, 2015 – Mar 5, 2015

lore
Share & tools
lore

vbooh

author
#0 lore raw

Hello.

I want to propose a patch for healthd.sh and also an Upstart init file for it. A patch is for a decreasing of the resource consumption by healthd.sh; it replaces the external commands with the built-in Bash commands. It did not make a huge difference, but it was still useful in my case.

Thank you.

healthd.sh +7 -5
@@ -25,7 +25,8 @@
 
 ADMIN_EMAIL="root@localhost"
 
-if [ -n "`sensors | grep ALARM`" ]
+sensors_state=$(sensors)
+if [[ "$sensors_state" =~ 'ALARM' ]]
 then
         echo "Pending Alarms on start up!  Exiting!"
         exit
@@ -33,10 +34,11 @@
 
 while true
 do
- sleep 15
- if [ -n "`sensors | grep ALARM`" ]
+ read -t 15 -N 0
+ sensors_state=$(sensors)
+ if [[ "$sensors_state" =~ 'ALARM' ]]
  then
-        sensors | mail -s "**** Hardware Health Warning ****"  $ADMIN_EMAIL
-        sleep 600
+        echo "$sensors_state" | mail -s '**** Hardware Health Warning ****' $ADMIN_EMAIL
+        read -t 600 -N 0 
  fi
 done

# Upstart init file. Installation:

# cp --interactive --verbose /usr/share/doc/lm-sensors/examples/daemon/healthd.sh /usr/local/sbin/healthd
# cp --interactive --verbose /usr/share/doc/lm-sensors/examples/daemon/healthd.conf /etc/init/
# sed --in-place '1,7d' /etc/init/healthd.conf
# initctl reload-configuration
# service healthd start
# healthd - shell script daemon, that sends via email alerts of "sensors" utility
#
# This is a simple daemon, which can be used to alert you in the
# event of a hardware health monitoring alarm by sending an
# email to the root.

description "sends via email alerts of sensors utility"

start on runlevel [2345]
stop on runlevel [!2345]

respawn

exec healthd

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#1 lore raw

Hi vbooh,

Quoted 1 line · vbooh

I want to propose a patch for healthd.sh and also an Upstart init file for it.

I didn't know people were still using that old piece of code :-D

I don't want to commit the Upstart init file, because such files depend on the distribution. So I think it is better to let each distribution maintain its own initialization files.

Quoted 1 line

A patch is for a decreasing of the resource consumption by healthd.sh; it replaces the external commands with the built-in Bash commands. It did not make a huge difference, but it was still useful in my case.

This is a sane goal, in fact we did similar changes to fancontrol some times ago for the same reason. Patch committed, thanks for your contribution. The read trick is very nice, I didn't know about it, I'll probably do the same in fancontrol as well as the current implementation was reported to cause problems.

Signature · 7 lines
Jean Delvare
SUSE L3 Support

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#2 lore raw
In reply to vbooh
Quoted 8 lines · Jean Delvare
Quoted 1 line · vbooh

A patch is for a decreasing of the resource consumption by healthd.sh; it replaces the external commands with the built-in Bash commands. It did not make a huge difference, but it was still useful in my case.

This is a sane goal, in fact we did similar changes to fancontrol some times ago for the same reason. Patch committed, thanks for your contribution. The read trick is very nice, I didn't know about it, I'll probably do the same in fancontrol as well as the current implementation was reported to cause problems.

Err, did you check the CPU consumption after applying your changes? I did similar changes to fancontrol and it results in an increased CPU consumption (37% of one CPU, compared to 0.3% before the changes) when fancontrol is started as a daemon by systemd. Strangely I do not observe this when running the script manually. I suppose "read" is angry if stdin is closed :(

Signature · 7 lines
Jean Delvare
SUSE L3 Support

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#3 lore raw
In reply to vbooh
Quoted 10 lines · Jean Delvare
Quoted 8 lines · Jean Delvare
Quoted 1 line · vbooh

A patch is for a decreasing of the resource consumption by healthd.sh; it replaces the external commands with the built-in Bash commands. It did not make a huge difference, but it was still useful in my case.

This is a sane goal, in fact we did similar changes to fancontrol some times ago for the same reason. Patch committed, thanks for your contribution. The read trick is very nice, I didn't know about it, I'll probably do the same in fancontrol as well as the current implementation was reported to cause problems.

Quoted 6 lines

Err, did you check the CPU consumption after applying your changes? I did similar changes to fancontrol and it results in an increased CPU consumption (37% of one CPU, compared to 0.3% before the changes) when fancontrol is started as a daemon by systemd. Strangely I do not observe this when running the script manually. I suppose "read" is angry if stdin is closed :(

I reverted to using regular sleep calls. However bash has optional builtin functions which can be loaded dynamically, and sleep is one of them. So we can try to load it if it is available, which should solve your problem. On openSUSE it is available in package bash-loadables, I don't know if other distributions package it or not.

The patch is here:
http://www.lm-sensors.org/changeset/6275

If you care about minor performance optimizations, you may also be interested in this cleanup:
http://www.lm-sensors.org/changeset/6276

Signature · 7 lines
Jean Delvare
SUSE L3 Support

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#4 lore raw
In reply to vbooh

Hi vbooh,

Adding the list back...

Quoted 2 lines · vbooh

Yes, I have checked a CPU load after applying the patch, but mostly using a manual launch in a command line('perf stat', 'time' external utility). In a daemon mode in my case it really increased CPU load(from 0.1% to 4.5%), but until you said I just did not notice that, since it is not so much. My bad. You were right, it looks like the 'read' command goes crazy without an attached terminal. I have found a pretty dirty hack around it, it requires some additional external utilities, but they are launched just once, not every 15 seconds, all the changes are in the attached patch. Now in a daemon mode 'healthd.sh' in my case consumes 0.0% of a CPU load.

I've looked around for such hacks and found several of them, including this one indeed. I agree it works and is probably the least ugly and unreliable, but it's still awkward compared to just calling sleep, and in my opinion it's not nice enough to go upstream. We may run in all kinds of compatibility and portability issues with that kind of code.

Quoted 1 line

Yes, the 'sleep' external utility can be compiled in 'bash', it was the very first tought of mine. But in Debian this must be done separately after an installation of an additional packages and in CentOS 7 there are even not all the needed files in the repository, so I have striked out that idea.

As far as I can see, Debian ships the loadable commands as source files, not binaries, in package bash-builtins, right? That doesn't seem very useful :(

I agree that the availability of the loadable builtins is limited. However I think this is a kind of chicken and egg problem:
distributions don't care much because there aren't many users of that feature, and application authors don't want to use it because distributions don't support it.

I'll talk to the upstream maintainers of bash about integrating it better in their build system, to get the ball rolling. I really think this is the way to go in the long term.

Quoted 1 line

Anyway thank you for your work.

You're welcome :)

Signature · 7 lines
Jean Delvare
SUSE L3 Support

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors