Some weeks ago, my laptop suddenly turned off, because its
battery was completely flat. To prevent this from happening again,
I wrote a small script that notifies me, when a critical charge
state is reached. Additionally it shuts the computer down before
the battery does. ;-)
Please note that this isn't meant to be a general purpose solution,
so feel free to adapt it to your own needs.