Posts tagged ‘perl’

Playing arround with X11::Aosd

So in my desire to map some ACPI buttons on my thinkpad, I noticed this ‘battery’ button (Fn+F2). I thought it would be nice if I get this nice graph showing me my battery state.

Sadly osd_cat only does bars and text, and I wanted a nice pie or something. So I looked around for perl modules which could do more. In comes X11::Aosd.

X11::Aosd is a Cairo powered OSD interface which works quite nicely. As it’s an XS module it works fast and efficient, but as usual lacks in -proper- usage documenation.

The script I ended up writing (which called by another script that actually read the battery info) became as followed:

#!/usr/bin/perl -w
use strict;
use Getopt::Long;
use X11::Aosd ':all';

my $per   = 100;
my $mx    = 200;
my $my    = 200;
my $text;

my $res = GetOptions("percentage=i" => \$per,
                     "xoffset=i"    => \$mx,
                     "yoffset=i"    => \$my,
                     "text=s"       => \$text,
);

my $pi = 3.14159265358979;
my $rad = ((2*$pi) / 100) * $per;

my $aosd = X11::Aosd->new;

$aosd->set_transparency(TRANSPARENCY_COMPOSITE);

$aosd->set_position_with_offset(
    COORDINATE_CENTER,
    COORDINATE_CENTER,
    200, 200, 0, 0
);
$aosd->set_renderer(sub {
    my ($cr) = @_;
    $cr->set_source_rgba (1, 0.6, 0, 1) if($per < 30);
    $cr->set_source_rgba (1, 0, 0, 1)   if($per < 10);
    $cr->set_source_rgba (0, 1, 0, 1)   if($per >= 30);

    $cr->set_line_width(10);
    $cr->set_line_cap('round');
    $cr->arc(100, 75, 70, 0, $rad);

    $cr->line_to(100,75);
    $cr->line_to(170,75) if($per < 100);
    $cr->stroke;
    $cr->fill;

    if($text) {
        $cr->move_to(0, 175);
        $cr->select_font_face("Sans", 'normal', 'bold');
        $cr->set_font_size (23);
        $cr->show_text($text);
        $cr->fill;
    }
});

$aosd->show;
$aosd->loop_once;

It’s a simple piece but it works. I kinda like X11:Aosd even though it’s less then perfect docs, toghether with the original cairo docs you get a nice idea. Maybe more examples later.

btw, Thinkpad F<n> acpi buttons(Fn+Function) are quite simple in their acpi mapping: 00000080 00001003 is F3, 00000080 00001002 is F2, etc

Another Thinkpad script, volume.

I was missing my volume buttons on my thinkpad, I’m sure the default flavours of (XU|KU|U)buntu handle this nicely (can’t say I checked though ;). But since I run ion3 I get none of that fluffyness.

There are ofcourse already some onelines and scripts outthere that fix this problem, and I certainly copied from them what was useful, but I wanted my OSD feedback. So I wrote the following script, not fancy but it works.
It depends on the following packages:

  • perl
  • alsa-utils
  • xosd-bin

/usr/local/bin/ibmvolume.pl

#!/usr/bin/perl -w
use strict;
## Script made for Lenovo Thinkpad X61s
## for different cards you might need a different numid.

my $nvol  = '28';
my $nmute = '29';

my $volinfo  = `amixer cget numid=28`;
my $muteinfo = `amixer cget numid=29`;

my ($min, $max, $cur) =
    $volinfo =~ /.*min=(.+?),.*max=(.+?),.*values=([0-9]+).*/si;
my $per = int($cur / ($max / 100));

my ($mute)  = $muteinfo =~ /.*values=([a-z]+).*/si;
my $invmute = ($mute eq 'on') ? 'off' : 'on';

if($#ARGV < 0) {
    print "Min: $min, Max: $max, Cur: $cur($per%), Mute: $mute($invmute)\n";
    print "Usage: $0 (up|down|mute|show)\n";
    exit 1;
}

my $command = shift;

my %controls = (
    up   => "amixer -q cset numid=$nvol ".($cur+1),
    down => "amixer -q cset numid=$nvol ".($cur-1),
    mute => "amixer -q cset numid=$nmute $invmute",
);

system($controls{$command}) if $controls{$command};

my $cmute = ($command eq 'mute') ? $invmute : $mute;

my $font = '*-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1';

system("osd_cat -b percentage -f $font  -c green -A center -o 600 -d 1 -T 'Volume($cmute):' -P $per &");

Sadly the osd_cat tool can wait no shorter than 1 second, I would’ve liked something more like 0.5 second.

This now works from the commandline, taking commands like ‘up’, ‘down’, ‘mute’ and ’show’. Now one can use something like ‘xbindkeys’ to link it to key events like ‘XF86AudioRaiseVolume’ and ‘XF86AudioLowerVolume’.

But I just put the following code in my (lua) config for ion3:

~/.ion3/cfg_bindings.lua

defbindings("WMPlex.toplevel", {
    kpress("XF86AudioRaiseVolume", "ioncore.exec_on(_, '/usr/local/bin/ibmvolume.pl up')"),
    kpress("XF86AudioLowerVolume", "ioncore.exec_on(_, '/usr/local/bin/ibmvolume.pl down')"),
    kpress("XF86AudioMute", "ioncore.exec_on(_, '/usr/local/bin/ibmvolume.pl mute')"),
})

And don’t forget to reload ion3 afterwards :)

Dutch Perl Workshop 2009

Sadly the Dutch Perl Workshop or Nederlandse Perl Workshop is over. :(

Even though I was very tired to begin it all with the event was a lot of fun.

The workshop itself was just on friday, but on thursday evening we worked until 01:00 to get the network working. At the event location, Hotspot wireless was provided by the ‘StayOkay’, but this only allowed traffic over port 80 and 443, and was just crappy. So we connected a spare laptop to cisco switch + accespoints we could borrow and let that route all traffic over a OpenVPN link to a freshly setup Virtual Server. Worked like a charm.

On friday the day started early with the first talk at 9.15 (10 minutes late, as was planned :). With a few breaks for coffee/tea and lunch in the afternoon, we had one full track of talks ranging from 5 to 20 minutes. Topics where diverse aswell, from in-depth perl hacks to generic coding strategies and application showcases. A lot of fun, but quite intense.

At the end of the day we had an auction for the Perl Promotion foundation, which organises the event and a fun quiz featuring obscure Perl issues.

Dinner was included and tasty, and the rest of the evening was filled with discussions, playing fluxx and drinking whiskey or beer.

I’d say I can’t wait until next year, but since I’m quite exhausted from this one, I can now :)
I took some pictures with my Practica MTL3, as soon as the film is developed, I’ll post some here.

Nederlandse Perl Workshop

Beetje laat, maar aanstaande vrijdag de 6e Maart 2009. Is dus de Jaarlijkse Nederlandse Perl Workshop. Meestal boeiend, altijd gezellig. In middels is het programma ook redelijk vol en online te bezichtigen.

http://www.perlworkshop.nl/nlpw2009/schedule

Verder geef ik dit jaar zelf ook een praatje over m’n HealthMatrix, waarvan ik voor vrijdag nog hoop een release te doen. De huidige release is niet heel optimaal.

Mocht je vrijdag dus nog tijd hebben, schrijf je dan nog vooral in!

Fosdem & Perl Workshop

This weekend I’m hanging out on Fosdem which is quite fun, although mainly from a social point of view.

The ADA talks where intresting and I might take a look at the language. I felt they left out a lot during the talks. Which made me a bit confused as to how abstract the language really is. It looks powerful, but also very limiting.

Koen gave an small overview over a migration from Informix to PostgreSQL. This talk was again followed by a intresting talk on how to run ’software meetings’ in a somewhat centralised way. And currently watched a talk on ext4 which was fun and now watching a FreeBSD performance talk.

Overall I’ve mainly running into familiar faces and enjoying nioce belgian beers in de evening. The talks aren’t bad, but I do see it more as a social event. The network on the otherhand is quite crap, with ports being blocked, accespoints requiring users to press buttons on pages before you get any routing, and just entire parts not working.

On anoter note, the Dutch Perl Workshop is comming up soon. On the 6th of March this year you can spent an entire day following cool perl related talks, talking to fellow perl coders and playing Fluxx. A program isn’t online yet, but it’s bound to be fun anyway.

Ext4 talk at Fosdem 2009

Ext4 talk at Fosdem 2009

Remote controlling machines

As most system administrators with many machines in their herd, I am in need of a way of mass maintaining my systems. There are ofcourse quite a bit off tools for this job already available. Most are aimed at fancy enterprise setup where money is no problem, or large clusters where all your machines are the same. They usually also require a pre-installed client on the machine.

I don’t like daemons running on my server. If I had my way I’d turn everything on the servers off. Every port open, is an extra door that needs to be secured and can contain holes.

For the public services I don’t have much choice, my boss wants to provide certain things to certain people. So those I can’t close.

On the administrative side I’ve got more control. Most of my servers now have 2 ‘backdoors’ SSH and Munin, both are secured. Still, I’d rather have 1 ‘backdoor’, certainly not 3.

Munin doesn’t allow you to control anything on the machine, only read. which is fine. None of the tools seem to provide a way to read munin data, so those are out of the question. I used to use SSH + RSA-keys, this gave way too much power to 1 machine in my network though.

So I came up with a simple solution. I haven’t found anything only that does this, but I haven’t looked very hard either.

A very limited shell, a configuration file defines what you can do (can be updated remotely, when given a password). Runs as a simple user, powers can be extended with sudo if desired. This still requires deployment ofcourse, but I like it’s simplicity.

The script can be downloaded from here: http://u2m.nl/software/perl/mcp-client. It’s written in Perl and only depends on Digest::MD5, which is delivered by default in perl 5.8 and later.

Comments & Sugestions welcome ofcourse.