27028 total geeks with 3521 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
timsattemme
[b][url=http ://www.web-i ntera.com/]t iffa<strong> <a href="http:/ /www.web-int era.com/">ti ffany jewelry</a>< /strong> <br > <strong><a href="http:/ /www.web-int era.com/">ti ffany & co</a></stro ng> <br>
timsattemme
<ul><li><str ong><a href="http:/ /www.web-int era.com/">ti ffany outlet store</a></s trong> </li> <li><strong> <a href="http:/ /www.web-int era.com/">ti ffany outlet</a></ strong> </li ><li><strong ><a href="http:/ /www.web-int era.com/">ti ffany jewelry outlet</a></ stro
timsattemme
[b]<a href="http:/ /www.fieldsc rm.com/">ome ga <strong><a href="http:/ /www.fieldsc rm.com/">ome ga watches on sale</a></st rong> <br> <strong><a href="http:/ /www.fieldsc rm.com/">ome ga watches replica</a>< /strong> <br >
timsattemme
<strong><a href="http:/ /www.montbla ncpensonline store.com/"> montblanc fountain</a> </strong> | <strong><a href="http:/ /www.montbla ncpensonline store.com/"> montblanc pen</a></str ong> | <strong><a href="http:/ /www.montbla ncpensonline store.com/"> mont blanc</a
timsattemme
<ul><li><str ong><a href="http:/ /www.watches omegahot.com /">best omega watches replica</a>< /strong> </l i><li><stron g><a href="http:/ /www.watches omegahot.com /">watches</ a></strong> </li><li><st rong><a href="http:/ /www.watches omegahot.com /">watch</a> </strong>

Donate
Donate and help us fund new challenges
Donate!
Due Date: Sep 30
September Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
Google Apple
grapple brings
crypto cop block to
Android
Who"s that at the
door, storage box
flingers? It"s the
hard drive makers.
No, they are n
Stray positrons
caught on ISS hint
at DARK MATTER
source
Oi, London thief.
We KNOW what you"re
doing - our
PRECRIME system
warned us
Monitors monitor"s
monitoring finds
touch screens have
0.4% market share
Why Oracle CEO
Larry Ellison had
to go ... Except he
hasn"t
iPhone 6: Will it
blend? We don"t
know, but it sure
CAN"T FLY
Gigantic bazaar
Alibaba WILL turn
share price up to
11, er, $68 for
biggest IPO ever in<
Samsung unlocks
Knox at zero bucks
Apple iStuff saved
by Beer
Slashdot
The Myths and
Realities of
Synthetic
Bioweapons
Europeans Came From
Three Ancestry
Groupings
Study: Chimpanzees
Have Evolved To
Kill Each Other
Slashdot Asks:
What"s In Your Home
Datacenter?
Dealership
Commentator:
Tesla"s Going To
Win In Every State
Microsoft Lays Off
2,100, Axes Silicon
Valley Research
Apple"s "Warrant
Canary" Has Died
Mystery Signal
Could Be Dark
Matter Hint In ISS
Detector
Dremel Releases 3D
Printer
A Beginner"s Guide
To Programming With
Swift
Article viewer

ACPI Battery Check



Written by:ateam
Published by:optikal
Published on:2005-04-10 21:53:02
Topic:Perl
Search OSI about Perl.More articles by ateam.
 viewed 14208 times send this article printer friendly

Digg this!
    Rate this article :
One of my Christmas presents this past year was a nice, 4 lbs. laptop. I use the Enlightenment window manager under Linux, thus, when my laptop's battery is getting too low, I don't have any application in my face warning me about it, like with KDE or Windows. Thus, I wrote a script to check the status of my battery and determine whether or not anything needs to be done about it. This is a short guide on using my script with your laptop.

ACPI Battery Check

Requirements: Linux kernel with ACPI support

This guide assumes you have a laptop, Linux, and something between your ears (preferably a brain). The script I've written will do the job on any machine with any battery running any Linux distribution. I'm wonderful, I know.

I feel its right to explain why I wrote this script in the first place, so here it goes... One day I was sitting in class playing a game on my laptop when suddenly it turned off. This came without warning, so I didn't get a chance to save my game, successfully making those past 20 minutes a waste of time. Perhaps I was being tought a lesson from you know who that in class, I should be doing something other than playing computer games. However, I'd like to believe it was just a way to get me to finally write a script to check my battery state and do something about it if need be!

Now, I know that there are tons of full-featured ACPI scripts out there. Most, if not ALL, of these have things written into them that I either don't want or that my laptop doesn't support. XScreenSaver already takes care of putting my display on standby and such and my ACPI modules already take care of fan usage, so why bother using some huge, bloated script? If you want a script like that, then what I've written isn't for you.

Okay, enough talk, its time for some code...

Once you have ACPI support in your kernel, /etc/acpi should be created automatically. If not, create it and an events directory and file as root like so:

mkdir /etc/acpi
mkdir /etc/acpi/events
touch /etc/acpi/events/default


If /etc/acpi and the other files already exist, then doing that is not needed. Regardless, /etc/acpi/events/default must be modified. Add the following lines into that file as root:

event=.*
action=/etc/acpi/battery_check.pl


Now it comes time to put that battery_check.pl script in /etc/acpi, as specified in the event-rules above. First, create /etc/acpi/battery_check.pl as root like this:

touch /etc/acpi/battery_check.pl


Then, open /etc/acpi/battery_check.pl with your favorite text-editor and paste the following into it:

#!/usr/bin/perl
#
#battery_check.pl
#
#Used to check ACPI battery state in /proc/acpi
#and warn users or halsts system if charge is
#too low.
#
#Written by ateam (derek@backdrifts.net)

$warning_message = "ACPI - Battery critically low!";
$halt_message = "ACPI - Battery too low, halting system NOW!";

chop (@pts = `ls /dev/pts/`);
($pts_length = @pts) --;

chop (@bat_state = `cat /proc/acpi/battery/BAT0/state`);
chop (@bat_info = `cat /proc/acpi/battery/BAT0/info`);

$charge_state = @bat_state[2];
$charge_state =~ s#(charging state: )##g;

$remaining = @bat_state[4];
$remaining =~ s#(remaining capacity: )##g;
$remaining =~ s#( mAh)##g;

$warning = @bat_info[5];
$warning =~ s#(design capacity warning: )##g;
$warning =~ s#( mAh)##g;

$low = @bat_info[6];
$low =~ s#(design capacity low: )##g;
$low =~ s#( mAh)##g;

if ($charge_state eq "discharging" && $remaining <= $warning && $remaining > $low) {
        for ($i = 0; $i <= $pts_length; $i ++) {
                system "echo \"$warning_message\" > /dev/pts/@pts[$i]";
        }
        system "echo \"$warning_message\" > /dev/tty1";
        system "logger \"$warning_message\"";
} elsif ($charge_state eq "discharging" && $remaining <= $low) {
        for ($i = 0; $i <= $pts_length; $i ++) {
                system "echo \"$halt_message\" > /dev/pts/@pts[$i]";
        }
        system "echo \"$halt_message\" > /dev/tty1";
        system "logger \"$halt_message\"";
        system "/sbin/init 0";
} else {
        system "logger \"ACPI - battery ok, no warning sent, system not halting\"";
}


If your perl interpreter binary is located somewhere other than /usr/bin/perl, then modify the code appropriately.

In my opinion, code is worth nothing to a user if he deson't understand how it works. In addition, I don't want you to be running some script on your machine that you're unfamiliar with. So, I'll explain.

Very simply, the script collects information regarding your battery's capacity, warning level, low level and state. When an ACPI event occurs, which is less than every minute, this script is run, checking your battery status. If your battery has reached its warning level, then a message is sent to tty1 and all virtual terminals (pts/*) warning you with "ACPI - Battery critically low!" If your battery is so low that your system needs to be halted in order to protect your hard disks, then the message will read "ACPI - Battery too low, halting system NOW!" All activity is logged in /var/log/messages to ensure that you know what's going on, or more importantly, what went on.

About modification of this script; if you don't wish to monitor BAT0, then change the script accordingly. In addition, you may not want your machine to be halted for you if your battery is about to die. I reccomend that you do, however.

Enjoy :)

Did you like this article? There are hundreds more.

Comments:
optikal
2005-04-11 04:07:07
Useful, well written article :-) keep it up ateam. If you get your apache log monitor done that would make a great article I think :-)
bb
2005-04-11 10:39:55
on a slightly different but unrelated note .. i found myself questioning the 'stepping' of my dell notebook the other week, i was convinced it was running at the wrong cpu speed when i thought i needed some muscle doing some software building.

whilst i was routing around looking for information, i found this application called speedswitchXP which allows you to override the windows xp cpu stepping, and switch your cpu into full, low or dynamic performance manually.

im not sure its good for the life of my notebook, or my power consumption, but theres something nice about thinking you can ramp your CPU up to the max.
mercurus
2005-04-25 09:47:30
Although it is cool to see this written in perl, are there not bash script versions produced with the acpi packages for most linux distributions ?

Only I would imagine that a bash script would use less CPU cycles, and therefore less power ... which is an issue if you have a speedstep processor and you're running the script every minute or so. I'll test it out on my Thinkpad a little later ...

That said ... there are a whole lot of other ACPI events on which it would be cool to run perl scripts :)
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
bb
List the number of files in a directory and each subdirectory on Fri 29th Sep 11am
I needed to get the number of files in a directory and recurse through all subdirectories doing the same. I nedded it so i could compare two sets of directory trees for something I was working on. My Irish chum Enstyne came up with this Perl solution w

Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
Perl: The basics by sefo

Basic knowledge on often used Perl features


     
Your Ad Here
 
Copyright Open Source Institute, 2006