27367 total geeks with 3535 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
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: Jan 31
January Goal: $30.00
Gross: $0.00
Net Balance: $0.00
Left to go: $30.00
Contributors


News Feeds
The Register
Outlook for iOS
does security
STUPIDLY says dev
Alibaba"s magic
cave empties as it
misses revenue
target
EMC grows new
"Enterprise Content
Division"
Wham, bam...
premium rate scam:
Grindr users hit
with fun-killing
charges
Quantum adds cloud
tier to StorNext
A docket, tweet and
selfie can reveal
your identity,
boffins find
OH HAPPY DAY!
Lawyers replaced by
AI
Reddit: Don"t
worry, we didn"t
tell that foreign
government about
your /r/brony
addic
Hey, America.
Canada"s watchdog
just slapped net
neutrality rules on
wireless internet
We take bots down,
but they get up
again ? you"re
never going to keep
them down
Slashdot
UK Broadcaster Sky
To Launch Mobile
Service
Microsoft Launches
Outlook For Android
and iOS
FDA Wants To
Release Millions of
Genetically
Modified Mosquitoes
In Florida
Alibaba Face Off
With Chinese
Regulator Over Fake
Products
Microsoft To Invest
In Rogue Android
Startup Cyanogen
LibreOffice Gets a
Streamlined
Makeover With 4.4
Release
D-Link Routers
Vulnerable To DNS
Hijacking
Spire Plans To Use
Tiny Satellites For
More Accurate
Weather Forecasts
FSF-Endorsed
Libreboot X200
Laptop Comes With
Intel"s AMT Removed
"Anonymized" Credit
Card Data Not So
Anonymous, MIT
Study Shows
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 14493 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