2011

HOW TO : Combining Perl and Zoho to produce reports

This HOW TO is more for my notes. We had a request at work, where we had to parse some log files and create a graph from the data in the log files.

The log files looked like this

[bash]
0m0.107s
0m0.022s
0m0.015s
2011-01-05_02_22
0m0.102s
0m0.024s
0m0.014s
2011-01-05_02_23
[/bash]

I wrote the following perl script to get the log file to look as such

[bash]| 0m0.107s| 0m0.022s| 0m0.015s| 2011-01-05 | 02:22

| 0m0.102s| 0m0.024s| 0m0.014s| 2011-01-05 | 02:23 [/bash]

perl script

[perl]
#!/usr/bin/perl
# Modules to load
# use strict;
use warnings;

# Variables
my $inputFile = ‘input.txt’;
my $version = 0.1;

my $logFile = ‘parsed_input.csv’;

# Sub Functions
sub Log($$$);
sub Trim($);

# Clear the screen
system $^O eq ‘MSWin32’ ? ‘cls’ : ‘clear’;

# Open the output log file
open(LOGFILE,"> $logFile") || die "Couldn’t open $logFile, exiting $!\n";

# Open the input file
open(INPUTFILE,"< $inputFile") || die "Couldn’t open $inputFile, exiting $!\n";

# Process the input file, one line at a time
while (defined ($line = <INPUTFILE>)) {
chomp $line;
# Check for blank line
if ($line =~ /^$/)
{
# Start a new line in the output
print LOGFILE "\n";
}
else
{
# Split the date and time
if ($line =~ /2011/)
{
@date = split (/_/,$line);
print LOGFILE "| $date[0] | $date[1]:$date[2]";
}
else
{
# Write the value to the output
print LOGFILE "| $line";
}
}
}
[/perl]
I then took the parsed log files and imported them into the cloud based reporting engine provided by Zoho at http://reports.zoho.com

The final result are these reports

SERVER1

SERVER2

Did I say, I love technology? 🙂

HOW TO : Find out which network port a program is using in linux

Quick way to figure out, which ports a particular program is using in linux

[bash] netstat -plan | grep -i PROGRAM_NAME [/bash]

Example : Check which ports SSH is listening on

[bash]

samurai@samurai:~$ sudo /bin/netstat -plan | grep sshd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      5257/sshd
tcp        0     52 123.123.123.123:22      124.124.124.124:32846     ESTABLISHED 3551/sshd: samurai
tcp6       0      0 :::22                   :::*                    LISTEN      5257/sshd
unix  3      [ ]         STREAM     CONNECTED     5893     3551/sshd: samurai
unix  2      [ ]         DGRAM                    5849     3551/sshd: samurai

[/bash]

HOW TO : Manage startup services in Ubuntu

Most Redhat/Fedora users are used to chkconfig and service for controlling the services/programs that startup at boot time. Here is how you do it in Ubuntu

  • Check status of a particular service

[bash] sudo SERVICE_NAME status [/bash]

Example : Check the status of Apache Web Service

[bash]samurai@samurai:~$ sudo service apache2 status
Apache is running (pid 3496).[/bash]

  • Add a service to start on bootup

[bash] update-rc.d SERVICE_NAME add [/bash]

Example : Configure squid to start on bootup

[bash] update-rc.d squid add [/bash]

  • Stop a service from starting on bootup

[bash] update-rc.d SERVICE_NAME remove [/bash]

Example : Configure squid to NOT start on bootup

[bash] update-rc.d squid remove [/bash]

NOTE : You need to have a startup script in /etc/init.d for the service to ensure update-rc.d works fine.

New lens and my mom..

Picture of my mom taken with my new toy (Canon 50mm F/1.8 lens). My mom is usually a shy person and this is probably the first time I saw her being comfortable in front of a camera.

I took a photography class recently and am finally enlightened to what a good lens can do :).  Prior to the class, I was always of the opinion that a zoom lens is the way to go. But my instructor opened my eyes to the world of fixed (and expensive) lenses :).

More pictures from this photo shoot at http://www.flickr.com/photos/kudithipudi/sets/72157625773287053/with/5385052525/

HOW TO : Check IO speed on a Linux Machine

For my notes.. if you ever want to check the IO capability of a disk (local or network) on a linux machine, use the following command

[bash] dd if=/dev/zero of=test.file bs=4M count=1000 [/bash]

The above command make a copy of the output from /dev/zero to a file called test.file (you can locate the file on the disk you want to measure) with a block size of 4M for a total file size of 4000Mb.

2011 Resolutions..

For the few folks that keep in touch with me through this website.. Happy New Year!! :). Hope this new year brings you health and love.

I am usually not a resolutions guy :).. But I wanted to give it a try this year. And I am making them public, so that I have more incentive (read pressure) to reach them. Without future adieu, here they are

  1. Take a look at this picture No.. not the part, where I am eating Jhanvi’s head :).. But that big fat gut hanging out for everyone’s viewing pleasure. Loosing that is my first resolution.
  2. Second picture..This is a picture of the traffic to this site, according to Google. I want to double that by the end of this year. In other words, write more often :).
  3. The third resolution doesn’t have a picture :). I have always wanted to get a CISSP certification. Well, this year, I am going to stop thinking and act on it.
  4. And finally, the fourth one is to go on a decent vacation. Ideally, some place exotic :)..And hopefully, we will have a visa to visit it.