Archive for May, 2009

Python-RPM

If you ever get to use this library, it’s design is horrible and it’s documentation is worse. If you want to retrieve the basic RPM data, I have a code snippet you can use:

python-rpm

As long as at the top of your file you had import rpm that should work file. That gives a few basic fields. To get a full list of fields, I actually used a reference from a perl module: http://cpansearch.perl.org/src/RJRAY/Perl-RPM-1.51/RPM/Constants.pm.

If you expect more information, it really isn’t out there. Every mailing list post tells you to visit slides at people.redhat.com, on a webpage which no longer exists from 2004. I was able to find it again, so this may help you also: http://www.ukuug.org/events/linux2004/programme/paper-PNasrat-1/rpm-python-slides/toc.html. ActiveState’s website also has a snippet showing a bit of what is contained in that hdr class, which really helped trying to decipher this: http://code.activestate.com/recipes/576767/.

And for all future python programmers out there, THERE IS NO NEED TO ABBREVIATE VARIABLE NAMES. We have big computers, lots of memory, and naming things ‘hdr’ and ‘ts’ help nobody, and make your code horrible for others to use. Seriously, expand them out.

Tags: , ,

To the Anti-Ubuntuone trademark brigade

Instead of focusing on UbuntuOne, how about taking a look at this website: http://www.hmrgroup.co.uk/

Their logo is the trademarked Ubuntu logo. Now thats productive.

Etc

Bought 20 Ubuntu CD’s from the US Store. I have to wonder, for those of you using shipit, if you have the means why not actually buy them?

cds

UbuntuOne
I think this is a really cool initiative from Canonical. I don’t understand people complaining about being closed source, because the client is fully open sourced from what I can see. So all the code I installed on my system is open source, no problem. If I utilize their service, then it isn’t, as the backend isn’t open source. But the fact that I can edit the client files, or make a compatible backend myself since the client is open means I have absolutely no issue with that. I think people complain to complain, if they only used that energy to write an open backend instead. Oh well.

Tags: , ,

Cherokee / Django tip: Timeout value

The default Cherokee timeout value is 15 seconds. I write server control dashboards which may have views which take longer than 15 seconds to render because they launch processes etc. Using Jquery / Ajax, I noticed these processes would spawn a bunch. This is because the of the default cherokee timeout. I increased this to 2400 seconds and now my problem went away. Hope that helps someone.

Tags: , ,

Creating graphs in python

Slow Friday today, so was digging through my $HOME and found some graphs I was generating a few months ago. I wanted to create some graphs showing the number of connections to a server, and stumbled on CairoPlot.

Show me pretty pictures
Graph generated by CairoPlot

Unfortunately, I believe I hacked my CairoPlot.py file to make the dots and pulled some stuff from other repositories, so the code might not give the same results. You may want to try the trunk. But the general point still exists that it is really easy to use, and the library is really easy to hack. To make the graph above I just ran:


#!/usr/bin/python
colors = [ (0.2, .3, .65), (0.5, 0.7, .1), (.35, .2, .45), ]
graphData = {}
graphData['server1'] = [ 20, 12, 42, 14, 11, 35 ]
graphData['server2'] = [ 18, 23, 10, 17, 23, 25 ]
CairoPlot.dot_line_plot("./graphs/blog", graphData, 500, 500, axis = True, grid = True, dots = True, series_colors = colors)

Obviously this is really easy to script, so you can parse your syslog files, append them to the graphData dictionary corresponding to the server, and bam you have a full report of everything happening etc. What I did was use the datetime module to sort events into time buckets that were then used as graphs, giving a view of 24 hours or whatever the period entered was.

Parsing syslog
I will give you a hand here too. In python, to parse syslog, I used a module called pyparsing. It uses a parsing language which is pretty easy to understand if you give it 20 minutes or so. Ie to parse the syslog lines I was looking for, I did the following:


month = Word(string.uppercase, string.lowercase, exact=3)
integer = Word( nums )
ipAddress = delimitedList( integer, ".", combine=True )
serverDateTime = Combine( month + " " + integer + " " + integer + ":" + integer + ":" + integer )
hostname = Word( alphas + nums + "_" + "-" )
daemon = Word(alphas) + Suppress("[") + integer + Suppress("]:")
ip = Suppress("remote IP address ") + ipAddress
bnf = serverDateTime + ipAddress + daemon + ip

for line in syslogFile:
try:
fields = bnf.parseString(line)
...

Keep in mind I did this very quick, so I am sure it can be refactored a bunch, just an example.

Tags: , , ,