Aug 252011
 

What is finesse?

  • Finesse is all about getting the things fine.
  • It is about making a very discrete and canny move to do what you intend.
  • It is about making accurate decision based on little facts.
  • It is about making enough with just one step.
  • It is about getting a sharp move in a very hard situation.
  • It is about avoiding damage when hit.
  • It is about saving your own time. Your days in this world are limited.
  • It is about a lot of things.

But basically it is doing just ENOUGH to do what is needed. Not a step further, not a second later and not a watt power more than needed.

A guest in Darren Rowse’s Pro blog put a very cute explanation on what “enough” is. Do you know what boiling water is? It is boiling water. It can’t boil more. And if it is not boiling – than it is not boiling water.

So …

How to obtain finesse?

  • Sport. Cat’s grace. It is what finesse is! Sell your car and buy a bike. Exit the subway 1 station before your work and walk. WALK! Even if you are a bit overweight, soon you will start moving like a graceful big cat.
  • Try to learn something new every day. Finesse is not just moving smooth. It’s also being sharp.
  • Read an interesting book. Every morning. In the toilet. One page is enough.
  • Shower. Trim. Shave. Body smell is not finesse, neither is excess hair.
  • Sell your TV. You don’t need a pre-made stream of information. Everything you need and love in a TV is uploaded in the network. The only thing that a TV has more … is a waste of time. You don’t need to be told, when you can choose.
  • Cook your own food. Make yourself a rule, to eat no less than 300 and no more than 500 grams of food. If it is not enough – eat not 3 but 6 meals per day.
  • Abandon all sorts of beer. If you are alcohol junkie (like me) – drink wine. One-two glasses with the dinner will make you content without giving you huge belly. Bellies and finesse do not cope well together.
  • Get a dishwasher! You are NOT a house wife! It takes 20 minutes to wash your dinner plate and all utensils. Dishwasher costs around $300-$500 and will serve you for years. $18-$36 per week. Your time is more important and more expensive. (Ask your boss what do you cost him per hour if you don’t believe me.)
  • Stop being clumsy. Be aware of your surroundings. Have a look at Discovery and NatGeo films about big cats. They are aware of everything. They don’t make noise and don’t break stuff while they move. Adapt! Or you may lose something important.
  • Always use shortcuts. Why do you think they are created in the first place? They conserve time and energy. Not just pressing Ctrl+c and Ctrl+v instead of using mouse. Use all shortcuts that life provides.
  • Always bulk buy and never buy anything that you will not use at least to 70%. Mere possession is mental sickness. If you don’t need it – don’t buy it. If you know you will eat 10 steaks and drink 3 bottles of wine this week – go and get them @ weekend and put them in the fridge. Don’t go every evening. It’s pointless.

What to do with this excess 1 hour every day you just won?

  • Invest it in yourself. It’s your own time.
  • Go dance with your GF/BF.
  • Ride a bike. Feel free.
  • Walk in the park. Meet new people.
  • Embrace yourself. If you don’t like yourself, no one will.
 Posted by at 11:41 am

Little endian to Big endian (in Perl).

 c0de  Comments Off on Little endian to Big endian (in Perl).
Aug 232011
 

It is one of the problems some network programmers meet while coding. While debugging executable files You may have noticed that, when seen in hexadecimal editor, some data in the files is ordered backwards. First you see the 1 and then several zeroes. But you know for sure, that the value you encoded for a constant is actually “1” instead of 01 00 00 00.This is what Little endian byte order actually is.Have a look in the decimal value 305,419,896 in HEX. It is 0x12345678. If your machine is Big endian, you will see this value stored in a binary file looking the same way it is. But if your machine is little endian, The value stored in the file will look like 78 56 34 12. The least significant byte will be in front, and the most significant will be in back.

The reason for this goes back in the computer’s past, when it was more important to make MATH really fast in spite of easiness. Imagine you have 1 physical address. This address has the value 01 00 00 00. If you need to read this address as byte, word or long integer. You will ALWAYS get the value 1, which is the real value. If you are using Big endian byte order, you will need to get the address + 4 if you need byte, address + 2 if you need word. The little endian byte order is still used in network equipment and serial interface dialog, so it’s good to know how to cope with it… or you will get really annoyed 😉

01 00 00 00 (byte) = 1
01 00 00 00 (word) = 1
01 00 00 00 (long) = 1

Also, there is the need for speed. If your processor has only 3 real registers (X, Y and Accumulator), than it’s easier to compute the values if they are little endian.

Y = 0;
A = 1;
foreach byte do {
    X = readbyte;               # we read next byte
    if X > 0 do {               # Check if the computation is needed at all.
        Y = Y + (X * A);        # we use Y to collect the results of all bytes multiplied by A
    }
    SHL A << 8;                 # A grows from 1 to 256 to 65536 to 16777216 (2^0, 2^8, 2^16, 2^24)
}

This is of course an ideal case of what is now known as 64 bit processor. One that can use 64 bit words and registers. In case you are using Big endian byte order you cannot use the multiplication but integer division, which is slower.

If you are Linux user but unsure what endianness your equipment uses, execute this:

bash-4.1# perl -MConfig -e 'print "$Config{byteorder}\n";'

If you want this byte order reversed and shown using simple math, use this Perl script:

#!/usr/bin/perl -w

my $a1;
my $a2;
my $a3;
my $a4;
my $r;

if (! $ARGV[0]) {die "There are no args";}

$r = $ARGV[0];

$a1 = $r % 256;
$a2 = ($r - $a1) % 65536;
$a3 = ($r - $a1 - $a2) % 16777216;
$a4 = ($r - $a1 - $a2 - $a3);

$a2 = $a2 / 256;
$a3 = $a3 / 65536;
$a4 = $a4 / 16777216;

printf ("\n$r is equal to 0x%02x 0x%02x 0x%02x 0x%02x\n", $a4, $a3, $a2, $a1);
printf ("$r is reverted to 0x%02x 0x%02x 0x%02x 0x%02x\n\n", $a1, $a2, $a3, $a4);

This is just a rough example. There is easier way to make and explain it. Get the input value, the least significant byte can be obtained by getting the "modulo" of integer division from the input value, store this modulo in an array. After this subtract the modulo from this input value and divide the value by 256. This is the new input value. Repeat, until value is zero. Voila. You have an array of [most significant, less significant ... least significant] bytes.

The script will only work on Long integers, but can easily be made to work with 64 bit words. Made It short, because it's more easy to read. With Little endian, we will always see backward byte order:

bash-4.1# ./RevertByteOrder.pl 305419896

305419896 is equal to    0x12 0x34 0x56 0x78
305419896 is reverted to 0x78 0x56 0x34 0x12

bash-4.1# ./RevertByteOrder.pl 22136

22136 is equal to    0x00 0x00 0x56 0x78
22136 is reverted to 0x78 0x56 0x00 0x00

bash-4.1# ./RevertByteOrder.pl 4660

4660 is equal to    0x00 0x00 0x12 0x34
4660 is reverted to 0x34 0x12 0x00 0x00

bash-4.1#

And if you need to see how many bytes per integer and long integer is your equipment using, execute this:

bash-4.1# perl -V:{short,int,long{,long}}

I hope this explains the Little and Big endian byte order. You can check the articles I've used for more information.

* http://stackoverflow.com/questions/2610849/finding-if-the-system-is-little-endian-or-big-endian-with-perl

* http://docstore.mik.ua/orelly/perl/prog3/ch25_02.htm

 Posted by at 12:07 pm

How to watch the Switch CPU utilization with TCL.

 QA  Comments Off on How to watch the Switch CPU utilization with TCL.
Aug 172011
 

This is a small utility I wrote in the process of my work. It is a bit ugly and nonetheless non-elegant and non-reliable, because the actual CPU utilization it measures for the switch also has the CPU needed to execute the “show system cpu” command from switch side.

Will be much more elegant if the switch reports the data to SNMP trap collector, but anyway. It will help you help yourself write your own script for this. This one is made to monitor the CPU while you work on other tasks.

The utility uses Tcl:Expect, Tcl:Math and Tcl:Tk. All of them are included in TclLibs package of your chosen distribution. The code is set to use SSH to the switch, but easily can be modified to work also with Telnet. Just un-comment the corresponding lines and set the “proto” value to be “telnet”. The other important values are ip, user, pass, enable, enablepass and the actual command the switch needs to show the CPU utilization. You can also tweak values of Delay and Width. I use 1000 and  600, because those are 10 minutes in one band-graph 100 pixels high.

CPU Utilization

CPU Utilization

Few disclaimers.

  • The code is not very good and will raise exceptions if the switch does not respond in time.
  • The code assumes that the result line will end in 1 to 3 digits followed by %-sign. (e.g. the result must say “current cpu is 100%” or “CPU 33%”)
  • There is virtually no guarantee the code will show you accurate values. It is for monitoring only.
  • When 10 minutes pass, the bar graph will start to scroll from right to left. No data is kept anywhere.
  • Part of this code is copy&pasted verbatim from the Tcl forums and discussions
#!/usr/bin/wish

package require Expect

set proto ssh
set ip 10.3.155.50
set user "admin"
set pass "admin"
set enable "enable"
set enablepass "enableadmin"
set cpu_command "show system cpu"

# Uncomment next line in case of simple Telnet connection
# spawn $proto $ip
# Uncomment next line in case of SSH connection
spawn $proto $user@$ip

expect -re "name:"
exp_send "$user\r"
expect -re "word:"
exp_send "$pass\r"

# Uncomment in case you need "enable" and password
#expect -re ">"
#exp_send "$enable\r"
#expect -re "word:"
#exp_send "$enablepass\r"

	proc cpu {} {
		global cpu_command
		expect -re "#"
		exp_send "$cpu_command\r"
		expect -re "(\\d*)\\s*%"
		return $expect_out(1,string)
	}

  array set params \
  {
    title     {CPU utilization}
    width     600
    height    100
    delay     1000
    plot      [cpu]
    t0        0
    t1        -1
  }

  # compute heights
  set h $params(height)
  set h1 [expr {int($h * 0.5)}]  ;# grey line @ 50%
  set h2 [expr {$h1 + 1}]
  # create canvas & bindings
  canvas .c -width $params(width) -height $h \
            -xscrollincrement 1 -bg beige
  pack .c
  bind .  { exit }
  # plotting itself
  wm title . $params(title)
  .c xview scroll $params(t0) unit
  set t $params(t0)
  while {$t != $params(t1)} \
  {
    update
    after $params(delay)
    set v [expr ($h - $params(plot))]
    .c create line $t $h1 $t $h2 -fill gray
    .c create rectangle $t $v $t $h
    incr t
    if {$t > $params(width)} { .c xview scroll 1 unit }
  }
 Posted by at 3:25 pm