Debian GNU Linux Woody / Bash 2.05a / X Free Version 4.1.0.1 / i386


What do we have here ?
--------------------

A simple apm status logger, built of three bash-script modules:
powermon... message & logging manager (cronjob) 
powerlog.... logging battery status on short frequence to specified devices
powermesg ... wall warnings at 'low' and 'critical' & send informations to specified
devices ( console / X )

There were two completely different goals i tried to put into one.

1) Especially for aged batteries the battery/bios percentage is rather incorrect,
and i also cannot trust in the builtin bios emergency 'countdown'.
It occured two times the system abruptly shut off without any 'hardware' warning, at about 18% reported from apm.
Maybe a battery bug....
I had to find out safe values for what is 'low' and 'critical'.
To test this, I needed a log on console -without X-, and into file.


2) I installed a laptop to give it away to some newbie 'out of my reach'.
So, the other purpose was to have a reliable daemon-warning on X, 
with output space for basic information, to remember any user about what's the matter.
Since i don't know about xmessage tools on woody, i choosed xterm.
The xterm can also be used to shutdown directly if you're on the rim ;-)


What does it do ?
----------------

'powermesg' is called passing the parameters:
power=$1	# status = 'low' or 'critical'
rmain=$2	# remaining minutes (calculted guess)
TTY=$3		# output device, e.g. tty8
PTS=$4 		# e.g. /dev/console on xterm -C

It doesn't open any tty or xterm itself.
Within the script, one can setup messages specified to the status 'low' or 'critical',
or whether to X or textconsole. A one-line-message will be 'walled' to all open devices.

'powerlog' gets the parameters
INTERVAL 		# the logging frequency in seconds
[device1], [device2] 	# output devices (optional, overriding the defaults)
or 'off' 		# kills all running powerlog processes.
It goes into a loop, logging the apm-percentage and calculating a guess about the
remainig minutes, based on the parameters
CUT=18			# 'cutoff' apm % when the machine powerfails
U=72			# how many seconds lasts 1 apm %
To find out this values the first time, i used the same tool and let it run til crash.

Calculating the remaining time linear - but discharging isn't. So it's just a rough guess ! 
I would appreciate you having an idea to improve it (e.g. involving ibam) !
powerlog also logs to the defined logfile.
It terminates when detecting on-ac-power.


'powermon' is settled as a cronjob, and manages invoking the other two.
In the script previously specified parameters are: 

# -----------This values change with battery aging ------------------
CUT=18			# 'cutoff' apm % when the machine powerfails
U=72			# how many seconds lasts 1 apm %
LOW=15			# First warning level. Value = remainig minutes until power off
CRITICAL=5		# Second warning + start logging
Note:
Please consider that cron might check the apm status a millisecond before 'critical', thus
activating critical reaction at the next check, which will be (cronjob-frequency) minutes
later ! You need to put your desired safe periode + adding the cronjob frequency here.

# -------------------------------------------------------------------
INTERVAL=30 		# Critical-logging frequence in seconds.
TTY=/dev/tty8 		# first log device (textconsole)
TTY_ALT=/dev/tty7	# where to log instead if not on X at all
PTS=/dev/console 	# second log device (xterm -C)
GEOMETRY="84x32"	# xterm-window size (chars)
TITLE="Battery power warning:" # xterm-window title

powermon checks if there's X up,  and sends
 messages / logging through an xterm.
Anyway to the specified textconsole. Tries to recognize hibernation.


INSTALLING
----------

If you want try, please note there's NOTHING GUARANTED !
It's only extensiv tested. Probably there still are bugs.

Be careful to safe all your data, close all xsessions, move to textconsole and do 'sync'
before let your system powerfail crash !

The way, i installed it was:
Placed the scripts into /usr/local/bin.
Check if this is in your PATH (~/.bash_profile and/or .bashrc).

Set up a call from crondaemon in /etc/crontab:
*/3  *  ***   root   on_ac_power || powermon
with a checking frequency of every 3 minutes here.
Check if /usr/local/bin is in the crontab PATH.
Else, you can place something like PATH=$PATH:/usr/bin/X11:/usr/local/bin
into powermon. (The X11 is needed for xauth stuff)

To test things, you can uncomment some prepared lines in powermon,
to type in values for apm-% manually, and for some parameter debugging output. 

I use X 4.1, and don't know if X-Security is unchanged in X 4.2.

 
Unsolved:

1) One silly problem is:
I don't know how to prevent cron from mailing to root everytime when powermon is terminating
powerlog ?

2) By now detects if X-Session is up only with a "ps| grep" hack.
And: how to get the session-user's name ? Depends on kdm by now.

3) xterm opens a root shell on userspace....
for private use, maybe no problem.
But is'nt there a better way.

4) Estimate remaining time more exactly.  Invoking ibam ?



I'm not used to bash or coding at all. 
Was my first work bigger than 3 lines.... e.g. i don't know  about functions.
If you like to improve something, please do it !
I would appreciate any feedback anyway.


HTH

Micha.







