Archive

Archive for March, 2010

Basic Package Management Operations Using Aptitude

March 24th, 2010 2 comments

debianaptitude is a featureful package manager for Debian GNU/Linux systems, based on the renowned apt package management infrastructure. aptitude provides the functionality of dselect and apt-get, as well as many additional features not found in either program.

For the package management operation which involves package installation or updates package metadata, you need to have root privilege.

Here are basic package management operations with commandline using aptitude(8).

Working With aptitude

Update package archive metadata.

aptitude update

Install candidate version of “foo” package with its dependencies.

aptitude install foo

Install candidate version of installed packages without removing any other packages.

aptitude upgrade

Install candidate version of installed packages while removing other packages if needed.

aptitude dist-upgrade

The difference between “safe-upgrade”/”upgrade” and “full-upgrade”/”dist-upgrade” only appears when new versions of packages stand in different dependency relationships from old versions of those packages. The “aptitude safe-upgrade” command does not install new packages nor remove installed packages.

Remove “foo” package while leaving its configuration files.

aptitude remove foo

Remove “foo” package and its configuration files.

aptitude purge foo

Clear out the local repository of retrieved package files completely.

aptitude clean

Clear out the local repository of retrieved package files for outdated packages.

aptitude autoclean

Display detailed information about “foo” package.

aptitude show foo

Search packages which match ‘regex’.

aptitude search <regex>

Explain the reason why ‘regex’ matching packages should be installed.

aptitude why <regex>

Explain the reason why ‘regex’ matching packages can not be installed.

aptitude why-not <regex>

Notable command options for aptitude

-s simulate the result of the command
-d download only but no install/upgrade
-D show brief explanations before the automatic installations and removals

List of the aptitude regex formula

~n match on package name
~d match on description
~t match on task name
~G match on debtag
~m match on maintainer
~s match on package section
~V match on package version
~A{sarge,etch,sid} match archive
~O{debian,…} match origin
~p{extra,important,optional,required,standard} match priority
~E match essential packages
~v match virtual packages
~N match new packages
~a{install,upgrade,downgrade,remove,purge,hold,keep} match with pending action
~i match installed packages
~M match installed packages with A-mark (auto installed package)
~i!~M match installed packages without A-mark (administrator selected package)
~U match installed and upgradable packages
~c match removed but not purged packages
~g match removed, purged or can-be-removed packages
~b match packages with broken relation
~B match packages with broken depends/predepends/conflict
~D[:] match packages from which relation is defined to package
~DB[:] match packages from which broken relation is defined to package
~R[:] match packages to which the package defines relation
~RB[:] match packages to which the package defines broken relation
~R~i match packages to which some other installed packages depend on
!~R~i match packages to which no other installed packages depend on
~R~i|~Rrecommends:~i match packages to which some other installed packages depend or recommend on
~S filter match package with filtered version
~T match all packages (true)
~F match no packages (false)

Python RRDTool Tutorial

March 22nd, 2010 No comments

pythonRRDtool is the OpenSource industry standard, high performance data logging and graphing system for time series data.

For this tutorial I’m going to assume you understand how to get RRDTool installed and working from the command line. If not you should take a look at this tutorial (if you are in a hurry look at the “A Real World Example” section) or any of the tutorials on this page.

Setup and introduction

Py-rrdtool wrapper implementation has been written from the scratch (without SWIG). So we will require python dev file for compiling the py-rrdtool module.

$ sudo apt-get install python-dev

Download the Python RRDTool from here.

$ tar xfz py-rrdtool-1.0b1.tar.gz
$ cd py-rrdtool-1.0b1
$ sudo python setup.py install

Creating a RRD

We first need to create a database for our data. For these examples we will be creating a database that could be used for generating network graphs. This database is created for updates every 5 minutes (300 seconds), has input and output counters that store data for the average and max counts and stores enough samples for hourly, daily, monthly and yearly graphs of both average and max.

import sys
import rrdtool
 
ret = rrdtool.create("net.rrd", "--step", "300", "--start", '0',
 "DS:input:COUNTER:600:U:U",
 "DS:output:COUNTER:600:U:U",
 "RRA:AVERAGE:0.5:1:600",
 "RRA:AVERAGE:0.5:6:700",
 "RRA:AVERAGE:0.5:24:775",
 "RRA:AVERAGE:0.5:288:797",
 "RRA:MAX:0.5:1:600",
 "RRA:MAX:0.5:6:700",
 "RRA:MAX:0.5:24:775",
 "RRA:MAX:0.5:444:797")
 
if ret:
 print rrdtool.error()

After running you will have a file called net.rrd in the current directory. This is your RRD and will contain all the samples for your graphs.

For more information on the options to rrd_create see: rrdcreate

Updating a RRD

The next step is to update your RRD on the frequency you set when you created it. In the case above the frequency was set to 5 minutes (300 seconds). The following script generates random input and output values as input to the update function, sleeps for 300 seconds and then loops.

import sys
import time
import random
import rrdtool
 
total_input_traffic = 0
total_output_traffic = 0
 
while 1:
 total_input_traffic += random.randrange(1000, 1500)
 total_output_traffic += random.randrange(1000, 3000)
 ret = rrdtool.update('speed.rrd','N:' + `total_input_traffic` + ':' + `total_output_traffic`);
 if ret:
 print rrdtool.error()
 time.sleep(300)

Displaying RRD data as a graph

Now that you have data in your RRD you will want to graph it. The following code will graph the average input and output for 1 day as well as the max for that day.

import sys
import rrdtool
 
ret = rrdtool.graph( "net.png", "--start", "-1d", "--vertical-label=Bytes/s",
 "DEF:inoctets=test1.rrd:input:AVERAGE",
 "DEF:outoctets=test1.rrd:output:AVERAGE",
 "AREA:inoctets#00FF00:In traffic",
 "LINE1:outoctets#0000FF:Out traffic\\r",
 "CDEF:inbits=inoctets,8,*",
 "CDEF:outbits=outoctets,8,*",
 "COMMENT:\\n",
 "GPRINT:inbits:AVERAGE:Avg In traffic\: %6.2lf %Sbps",
 "COMMENT:  ",
 "GPRINT:inbits:MAX:Max In traffic\: %6.2lf %Sbps\\r",
 "GPRINT:outbits:AVERAGE:Avg Out traffic\: %6.2lf %Sbps",
 "COMMENT: ",
 "GPRINT:outbits:MAX:Max Out traffic\: %6.2lf %Sbps\\r")

You should end up with a file named net.png in your working directory that looks something like:

net

You can also do weekly and monthly graphs by changing the start parameter to -1w or -1m:

net

net

For more information on the options to rrd_graph see: rrdgraph

Categories: PYTHON Tags: ,

Command Line Currency Converter For Linux

March 19th, 2010 2 comments

currency-convertMany times I prefer to just work from the shell. The ~/.bashrc file is a great place to put functions if you are using them regularly.

Let’s build a cool command line currency converter using google’s currency converter. You might require wget or curl and sed to do this.

Just run the following command in terminal.

wget -qO- "http://www.google.com/finance/converter?a=1&from=usd&to=inr&hl=es" |  sed '/res/!d;s/<[^>]*>//g';

This will convert 1 USD to INR (Indian Rupee).
Output:

1 USD = 45.4950 INR

What we are doing is, query google using wget and parse the output using sed.

You can use curl as well:

curl -s "http://www.google.com/finance/converter?a=1&from=usd&to=inr&hl=es" |  sed '/res/!d;s/<[^>]*>//g';

Isn’t that a long command. Lets create a cool bash function for the ease use:
Open you ~/.bashrc file in your favorite text editor and add the following lines and save.

currency_convert() {
  wget -qO- "http://www.google.com/finance/converter?a=$1&from=$2&to=$3&hl=es" |  sed '/res/!d;s/<[^>]*>//g';
}

Now Run:

$ currency_convert 10 usd inr
10 USD = 454.9500 INR

If you want to convert to more than one currencies at once, rewrite our function as bellow.

currency_convert() {
  for i in $3
  do
    wget -qO- "http://www.google.com/finance/converter?a=$1&from=$2&to=$i&hl=es" |  sed '/res/!d;s/<[^>]*>//g';
  done
}

Now run:

$ currency_convert 1 usd "inr gbp eur"
1 USD = 45.4950 INR
1 USD = 0.6603 GBP
1 USD = 0.7382 EUR

Happy currency converting :-)

Categories: LINUX Tags: ,

Switch to our mobile site