August 30, 2008

Easy dual monitor setup with xrandr

I wanted to automatically configure second monitor plugged into my laptop as above internal LCD, and I was prepared to allocate few days for this task. But, in recent X.org servers this has gotten much better, so it's enough just to add following to .xinitrc:

xrandr --output DVI-0 --auto --above LVDS
xrandr --output VGA-0 --above LVDS

There is also grandr (with package in Debian) if you want point-and-click functionality.

Don't work on single monitor, having dual monitor again is such productivity boost :-)

Update: this works only on cards which report more than one output connected with xrandr -q:

Screen 0: minimum 320 x 200, current 1920 x 2250, maximum 1920 x 2250
VGA-0 disconnected (normal left inverted right x axis y axis)
LVDS connected 1400x1050+0+1200 (normal left inverted right x axis y axis) 305mm x 228mm
DVI-0 connected 1920x1200+0+0 (normal left inverted right x axis y axis) 474mm x 296mm
Im my case, it's ATI Technologies Inc M52 [Mobility Radeon X1300] with radeon driver. To make this work, I also needed to create virtual desktop which is large enough to accomodate both screens by adding Virtual to /etc/X11/xorg.conf:
Section "Screen"
        Identifier "MyScreen"
        Device     "MyCard"
        DefaultDepth     24
        SubSection "Display"
                Virtual 1920 2250
        EndSubSection
EndSection

August 15, 2008

10 minutes to interactive php debugger in vim

Let's assume you have a huge (or legacy, sic!) application written in php which is result of 5+ years of development (read: spagetty code, php3-5 code style and so on).

But, something doesn't work, and you can't figure it out by just looking into code. If you abstract problem a bit, ability to single step through php code might be generally useful, especially if it integrates well into my console-vim-firefox environment.

So, does it? It sees that it does...

First, install Xdebug extension:

$ apt-get install php5-xdebug
Then enable DBGP debugger in /etc/php5/conf.d/xdebug.ini
xdebug.remote_enable = 1
xdebug.remote_port = 9000
xdebug.remote_host = localhost
Now hop to Firefox Add-on site and install Xdebug Helper. This is small extension to add two icons (debug and profile) to Firefox status line.

Now you need interface, so download DBGp client for vim (take care of Mac hidden files inside zip) into your ~/.vim. There is also very nice tutorial now to install everything, but if you are using Debian (lenny or unstable) you can get all requirements from packages (thanks DDs!)

How enable debugging in Firefox by clicking on Xdebug Helper icon (or add ?XDEBUG_SESSION_START=1 to URL of your application) and reload page once (this will install debug Cookie in Firefox session).

Finally you are ready to start debug session: start empty vim press F5 and reload your page again in next 5 seconds.

You will end up in window with loaded source code which you can single step, put breakpoints or examine variables. All in under 10 minutes (if you didn't dooze off while reading this post...)

xdebug-vim.png

In next installment of introduction to php development for 21th century we'll take a look at profiling and fun stuff with DBGp protocol using perl client :-)

July 25, 2008

OSCON 2008 - where is Open Source going?

Few years ago, at our local Croatian Linux User Conference I said: "we won, it's over". However, since then I have been noticing that all interesting problems are already solved.

There are few strange people (like me) who like to do stuff which is needed (and for which people are willing to pay money regardless if it's open source or not) but aside from that it seems that there is more interest it writing third twitter or one hundred twenty fourth wiki than solving some of open problem with open source software.

Having said that, OSCON 2008 is near end, and I just left keynote speech in which Microsoft announced donation to Apache foundation. It really seems that new leadership in Microsoft will change this company in same way that IBM changed and embraced Open Source. I should have anticipated that when we got lecture about open standards from Microsoft on our last CLUC conference, but it didn't occurred to me that this is a global thrend in Microsoft.

That brings us, Open Source community, into interesting position: we no longer have clear external enemy (and comming from former Yugoslavia let me tell you how important external enemy is :-)

This situation puts me in interesting position: I could ask myself do I want to work for Microsoft (that question until now was easily answered with big bold NO). I don't have such clear answer any more (no, I'm not leaving for Microsoft any time soon :-)

It seems that people don't care any more about freedom (in FSF sense of free software) and any "open" license is just good enough. That model worked well for BSD folks for quite some time, but I do believe that critical component of Linux success was GPL license.

All in all, OSCON 2008 was good, but traveling half way around the world doesn't give me same pleasure as it once was. It might be that podcasts from lectures turned out to be good enough for me to consume conference materials or that OSCON is not right conference for me (I loved YAPC last year, and I'm not going this year which is a bit shame).

July 18, 2008

Automounting SD card on EeePC

For some time, I'm seeing strange messages in powertop with mounted SD card:

A USB device is active 100.0% of the time:
USB device  5-5 : UB6225 (ENE)

Since I'm leaving for OSCON (and will mostly work on eee while there) I wanted to be conservative about my battery usage, so I took a closer look at this problem.

At first, installing autofs seemed like good idea. However, autofs can't run scripts (and I really want to toggle card reader's power) so I took a looked at afuse which seemed nice, but I didn't want to add another layer of indirection (missing timeout feature could be solved by script which sleeps in background).

Than I thought about it a bit more and decided that best solution is probably simplest possible: do a small shell script. I usually know when I want to access files on SD card anyway (otherwise, I couldn't really power off card because I would be using files on it).

#!/bin/sh

dir=/rest

if mount | grep $dir >/dev/null ; then
umount $dir && sudo sh -c "echo 0 > /proc/acpi/asus/cardr"
sudo rmmod usb-storage
sudo dmesg -c
else
if [ `cat /proc/acpi/asus/cardr` -eq 1 ] ; then
mount $dir && df $dir
else
sudo modprobe usb-storage delay_use=0
sudo sh -c "echo 1 > /proc/acpi/asus/cardr"
sudo dmesg -c
sleep 2
sudo dmesg -c
mount $dir && df $dir
fi
fi

You might want to remove dmesg calls if you don't care about output or don't want this script to mess with your kernel logs.

June 3, 2008

Subversion downgrade and permission errors

We had a double-disk failure on RAID5 array last week (yes, it does happen). One of things that got affected was our source code repository.


So, we reinstalled etch and installed subversion, did svnadmin verify on repository and all seemed well until we tried to commit using SVN DAV. Apache's error log was full of errors like this:


Could not create activity /svn/repo/!svn/act/9526cf2d-4893-4b35-a843-4c0f2d9f8bc6. [500, #0]
could not open dbm files. [500, #120022]
Can't open activity db: APR does not understand this error code [500, #120022]

But, permissions where correct! In utter desperation, I even tried chmod 777 on repository (kids, don't try this at home!) but it didn't help.


What did help was:


mkdir repo.new
svnadmin create repo.new
svnadmin dump repo | svnadmin load repo.new
mv repo repo.old
mv repo.new repo

Checking created repository with diff showed that it was indeed changed:

Binary files repo.old/dav/activities and repo/dav/activities differ
diff -rw repo.old/db/current repo/db/current
1c1
< 59 r9 1
---
> 62 ra 1
Only in repo/db/revprops: 60
Only in repo/db/revprops: 61
Only in repo/db/revprops: 62
diff -rw repo.old/db/revs/10 repo/db/revs/10
4c4
< id: aq.0.r10/17
---
> id: e1.0.r10/17
6c6
< pred: aq.0.r8/1443323
---
> pred: e1.0.r8/1443490
16c16
< dir b0.0.r8/1443476
---
> dir av.0.r8/1443643

and so on, for pages and pages.


I don't feel very good about changes, but repository was created with newer version from backports, and this time we used version from etch (thus downgrading from 1.4.6 to 1.4.2).


The whole point of this post is to help some random Google user with downgrade of subversion. So, here you have it!

May 22, 2008

Group by data in shell pipes

My mind is just too accustomed to RDBMS engines to accept that I can't have GROUP BY in my shell pipes. So I wrote one groupby.pl.


Aside from fact that it somewhat looks like perl golfing (which I'm somewhat proud of), let's see how does it look:


dpavlin@llin:~/private/perl$ ps axv | ./groupby.pl 'sum:($6+$7+$8),10,count:10,min:($6+$7+$8),max:($6+$7+$8)' | sort -k1 -nr | head -10 | align
440947 /usr/lib/iceweasel/firefox-bin 1 440947 440947
390913 /usr/sbin/apache2 11 22207 39875
180943 /usr/bin/X 1 180943 180943
135279 /usr/bin/pinot-dbus-daemon 1 135279 135279
122254 mocp 2 25131 97123
84887 pinot 1 84887 84887
78279 postgres: 5 10723 21971
70030 /usr/bin/perl 6 6959 15615
50213 /bin/bash 7 6351 7343
49266 /usr/lib/postgresql/8.2/bin/postgres 2 24631 24635

This will display total usage for process, it's name, number of such processes and range of memory usage. We can then use old friend sum.pl to produce console graph, but I already wrote about it.


So, let's move to another example, this time for OpenVZ. Let's see how much memory is each virtual machine using (and get number of processes for free):



$ vzps -E axv --no-headers | ./groupby.pl 'sum:($7+$8+$9*1024),1,count:1'
2209504127 0 265
611768242 212024 38
162484775 212037 19
170797534 212052 38
104853258 212226 26
712007227 212253 21

But wouldn't it be nice to display hostnames instead of VEID numbers? We can, using --join and --on options (which are really backticks on steroids):

$ vzps -E axv --no-headers | ./groupby.pl 'sum:($7+$8+$9*1024),1,count:1' --join 'sudo vzlist -H -o veid,hostname' --on 2
2146263206 0 259
675835528 saturn.ffzg.hr 40
162484775 arh.rot13.org 19
170797534 koha-dev.rot13.org 38
104853258 koha.ffzg.hr 26
712011323 zemlja.ffzg.hr 21

Which brings us to final result:

$ vzps -E axv --no-headers | ./groupby.pl 'sum:($7+$8+$9*1024),1,count:1' --join 'sudo vzlist -H -o veid,hostname' --on 2 | sort -rn | align | ./sum.pl -h
0 260 2105M OOOOOOOOOOOOOOOOOOO 2105M
zemlja.ffzg.hr 21 679M OOOOOO------------------- 2784M
saturn.ffzg.hr 35 512M OOOO-------------------------- 3296M
koha-dev.rot13.org 38 162M O------------------------------ 3459M
arh.rot13.org 19 154M O-------------------------------- 3614M
koha.ffzg.hr 26 99M ---------------------------------- 3714M

So, here you have it: SQL like query language for your shell pipes.

May 15, 2008

Bag of useful scripts

Why does console refuse to die?
- It's because of pipes!
This post is result of my long addiction to console applications. Somehow, when I want to get a quick view of things on my system, I always turn to pipes and do something with them. In that process, I developed few of useful scripts for use within shell pipes, and I would like to introduce my readership to them.

PostgreSQL database size

When I want to see size of all databases on my system or size of tables in one database I turn to pg_size. It's a short and sweet script which will do a little shell magic (take a look in it) and display size of all databases on system (without any options) or size of all tables in database and number of rows (when used like pg_size database_name) like this:
dpavlin@llin:~$ pg_size dellstore2 | grep -v sql_
4890624 customers 20000
3153920 orderlines 60350
2678784 cust_hist 60350
991232 products 10000
966656 orders 12000
450560 inventory 10000
8192 categories 16
0 reorder 0
This is all nice and well, but doesn't really gives us the right overview, so move along for...

Nice console graphs

First, a caveat: this tools assumes that it will get number, space, and optional description. Output above seems to fit into this description, so let's try it:
dpavlin@llin:~$ COLUMNS=80 pg_size dellstore2 | grep -v sql_ | sum.pl -h
customers 20000    4776k OOOOOOOOOOOOOOOO                                 4776k
orderlines 60350   3080k OOOOOOOOOO-----------------                      7856k
cust_hist 60350    2616k OOOOOOOOO---------------------------               10M
products 10000      968k OOO-------------------------------------           11M
orders 12000        944k OOO----------------------------------------        12M
inventory 10000     440k O-------------------------------------------       12M
categories 16      8192b ---------------------------------------------      12M
reorder 0              0 ---------------------------------------------      12M
This gives us nice output: description (followed by number of rows from above output), and running total of size in human readable form (if you don't like it, remove -h flag and you will get raw numbers).

Let's take another example (if you are still reading this and not interested in PostgreSQL database size). Let's see how much traffic did pppd transfer over very slow GPRS link on 8-day vacation:

dpavlin@llin:~$ grep 'pppd.*: Sent' /var/log/messages | awk '{ print $7 + $10 " " $1 " " $2 }' | sum.pl -h
May 5         0                                                               0
May 5       39k                                                             39k
May 5     7512k OO                                                        7551k
May 6     6352b --                                                        7558k
May 6       20k --                                                        7579k
May 6     1183k ---                                                       8762k
May 8     6869k OO---                                                       15M
May 8       70k -----                                                       15M
May 9     3596k O------                                                     18M
May 9     1998k -------                                                     20M
May 10      32M OOOOOOOOOOOO--------                                        53M
May 10      13k --------------------                                        53M
May 11      44M OOOOOOOOOOOOOOOOO--------------------                       98M
May 12      12M OOOO--------------------------------------                 111M
May 13    7120k OO-------------------------------------------              118M
May 13      20M OOOOOOO----------------------------------------------      139M
Much more interesting! A long time ago, I had a bunch of quick one-lines which used sum.pl to produce output from various other system counters, but somehow it got lost.

As I get only few comments on my blog, if you find this useful, leave one. I have few other examples, like the one which shows top 5 memory eaters on my system:

dpavlin@llin:~$ ps v | awk '{ print $8 " " $9 " " $10 }' | sort -rn | ~/private/perl/sum.pl | head -5
# RSS %MEM COMMAND
10.6 /usr/lib/iceweasel/firefox-bin 165092 OOOOOOOOOOOOOO                165092
4.3 perl                             67240 OOOOOO---------------         232332
0.5 awesome                           8504 ---------------------         240836
0.4 irssi                             6632 ----------------------        247468
0.3 vi                                5888 ----------------------        253356
but, if this is not interested to my readership, tell me so, and I will stop spamming your already full RSS reader with console output! :-)

April 30, 2008

State of linux wifi (first week with OLPC)

So It has been a week from time when borrowed OLPC entered my family of computers. I have Thinkpad T60 with Atheros AR5212 (which works with atk5k driver from 2.6.25, nice work!) and Eee PC with Atheros (which works with special madwifi patch).

Since 802.11s just landed into upstream kernel git, I was eager to take a look at this mash network thing. Oh, how ignorant I was. OLPC uses 802.11s protocol which is different from official implementation of 802.11s and with good reason: they are using embedded processor in wifi card do to mash protocol for them (saving power and enabling mash to work when laptop is suspended). I could have installed olsr on OLPC, but I'm really trying to have bigger mash which is compatible with unmodified OLPCs.

Because my time is limited, I would like to work in user-land if at all possible, and since wpa_supplicant can work on unmodified kernels, it would be nice to have that level of support for OLPC mash also. After a lot of browsing (and reading few really great wifi hacking sites), I concluded that only hope is radiotap which is more-or-less supported on every pcmcia wifi card that I have (prism based 802.11b card and rt2500). I had also found simpliest possible code which uses radiotap to start with.

Now, I would just need another OLPC to save some network traces and start experimenting :-)

Aside from that, I switched totally to OLPC for this week, and amazingly enough, I didn't miss my Eee PC one tiny bit. Although a bit slower than Eee, OLPC screen is bigger (and better in black and write mode on sunlight) which helps a lot with web pages. Browser performance is amazing, so I have little doubt that we will be able to support most of web sites on OLPC without much problem. OOH, I did notice a couple of excessive round-tips on one of my web sites, while surfing on it, but that's for best anyway :-)

Update: According to message on libertas-dev mail list there is effort to use kernel's 802.11s implementation which makes my effort in supporting OLPC variant obsolete.

April 23, 2008

First day with OLPC

Part of my day job (and reason why I love it) is working with guys from OLPC Croatia in spreading a word and intention of this project. Yesterday we had great presentation in Varaždin and they are interested in pilot project.

With news like this it seems that we are fighting windmills. I hope that we are not wrong to invest time and effort into this since my first day of experience with little green thing is very positive.

For a start let me clear some doubts: it is slow machine. Keyboard is really different (much worse than Eee PC for example), but if you don't press it harder than needed (which was my problem) it's actually O.K. It's also heavier than Eee PC (which somehow I noticed carrying it around) but other than that it great machine.

Having said that, I had problems with associating to my AP (so I used scripts for that), but as a real Linux user, I decided to take a look at it. From that point on, things improved: first i noticed very nice upgrade mechanism with allowed me to upgrade to build 703 which works great. This build is not final, so it comes without any Actions (think of them as applications installed on OLPC), but installation of Action pack from USB stick is really easy (it's also possible to upgrade system using USB stick if you don't have network).

The only show-stopper problem that I now have is how to buy OLPC? I don't even know if we can order just 2000 units for pilot program, but I would really love to have ability to buy device for me and other interested developers in CARNet. Having said that, if you do have OLPC which you are not using, it can find new home with interested developer.

Aside from that, hi-res black and white screen is just beautiful, machine performs extremely well if you have in mind that it's basically 430Mhz pentium machine with 256Mb or RAM and 1Gb of flash. It seems like too little, but if you use OLPC as network connected digital notebook (with sharing!) it works beautifully. It best mobile device I had since palm back in last century.

Every little thing of this machine is designed great: kudos do development team. One small thing is that you can rotate screen in both directions (Toshiba, take note of this!). Hardware keys on both sides of display actually make usage a joy (and decrease a need to touch keyboard).

If we from moment move on from hardware to Sugar, it really great user interface which I would love to see on other laptops (before you ask, I did try it on Debian and it does work, I will take another look after I will have to return laptop).

I really hope that this project will live on and enable us to deploy units to elementary schools and hopefully achieve some benefit for students (which I'm somehow sure we'll have :-)

April 15, 2008

First Croatian Perl Workshop

First of all, we had first ever Croatian perl workshop. Thanks to all the people who showed up, we had attendance of about ten.

Organizing a workshop event turned out to be much more work then I anticipated, and various other tasks stopped me from preparing for it as good as I should. Also, small number of people force me to re-consider my lectures about perl. On one hand, I really, really, tried to spread perl (and had good fortune of being at right place at right time to get Zagreb.pm off the ground), but with such low attendance, I must conclude that perl is used only by about 20 people in Zagreb. This seems somehow disturbing. Comparing size of Zagreb with Moscow turned out to show about same proportion, so I was just overly optimistic.

I also gave half an hour presentation about Jifty, based on Building a Jifty app in a jiffy by Kevin Falcone and showed some examples of my jifty apps (I actually didn't talk about last one, just mentioned it as integration of external javascript -- CodePress in this example).

I also have to thank to Andrew Shitov from Moscow.pm who have managed to prepare several very interesting topics which, in my opinion, made this event worthwhile. If it wasn't free I would ask my money back :-\