programming task #2!

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.
Post Reply
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

programming task #2!

Post by lambda »

this one is pretty easy, too.

suppose you have to monitor interfaces on a phone switch. it has two interfaces, one for incoming calls, one for outgoing. you have a computer connected to the switch, and you can run software to monitor the interfaces, to see what sort of events are occurring. the software produces a log file in this format:

Code: Select all

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!                                                                                   !!!!
!!!!    Cisco Packet Telephony Center Monitoring and Troubleshooting Tool (PTC-MT)     !!!!
!!!!                                                                                   !!!!
!!!!    The software is for use only by or under the direct supervision of             !!!!
!!!!    authorized personnel or an authorized agent of Cisco Systems, Inc.  If you     !!!!
!!!!    are not an authorized Cisco agent or are not using the software under the      !!!!
!!!!    direct supervision of a Cisco agent, Cisco grants you no right or license to   !!!!
!!!!    this software, and you must immediately terminate your use of the software     !!!!
!!!!    and delete or return the software to Cisco. By continuing to use the           !!!!
!!!!    software, you represent that you are an agent of Cisco or that you are under   !!!!
!!!!    the direct supervision of an agent of Cisco authorized to use this             !!!!
!!!!    software.                                                                      !!!!
!!!!                                                                                   !!!!
!!!!    Please wait a little.... PTC-MT will start in less than 5 seconds.             !!!!
!!!!                                                                                   !!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


********************************************************** 
* 07 PTC-MT WARNING: Profile file "master.cfg" not found *
********************************************************** 
******************************************************** 
* 07 PTC-MT WARNING: Profile file "user.cfg" not found *
******************************************************** 
************************************************************* 
* 07 PTC-MT WARNING: Profile file "exception.cfg" not found *
************************************************************* 
*************************************************************** 
* 03 PTC-MT INFO: PTC-MT is listening on interface "bge1".... *
*************************************************************** 
 
 ---------------------- 
 SS7 MESSAGES DISPLAY   
 ---------------------- 
 
                                                                Ser
Time stamp       OPC                  DPC                  Var  Ind.     Msg  Data    
------------------------------------------------------------------------------------------------------- 
First packet received - 03/06/2007

13:05:48.987595  0-041-2[00330]       0-048-5[00389]       ITU  ISUP. -> ACM (06) CIC=00015  
                                                                         SLS=15 Pr:0 Ni:NTL 
13:05:49.127631  0-042-4[00340]       0-048-5[00389]       ITU  ISUP. -> RLC (10) CIC=00035  
                                                                         SLS=03 Pr:0 Ni:NTL 
13:05:49.225719  0-041-2[00330]       0-048-5[00389]       ITU  ISUP. -> IAM (01) CIC=00055  CDPN=0560311324F REDIRECTING=569102813
                                                                         SLS=07 Pr:0 Ni:NTL CGPN=563863936
13:05:49.245555  0-048-5[00389]       0-041-2[00330]       ITU  ISUP. -> CFN (2f) CIC=00055  
                                                                         SLS=07 Pr:0 Ni:NTL 
13:05:49.315770  0-048-5[00389]       0-041-2[00330]       ITU  ISUP. -> ACM (06) CIC=00055  
                                                                         SLS=07 Pr:0 Ni:NTL 
13:05:49.335440  0-048-5[00389]       0-041-2[00330]       ITU  ISUP. -> CPG (2c) CIC=00055  EVENT=Alerting
                                                                         SLS=07 Pr:0 Ni:NTL 
13:05:49.473668  0-063-1[00505]       0-048-5[00389]       ITU  ISUP. -> IAM (01) CIC=00500  CDPN=141499912F CGPN=569871451
                                                                         SLS=04 Pr:0 Ni:NTL 
13:05:49.552544  0-042-4[00340]       0-048-5[00389]       ITU  ISUP. -> REL (0c) CIC=00211  Cause 16 = Normal Call Clearing
                                                                         SLS=03 Pr:0 Ni:NTL 
13:05:49.565458  0-048-5[00389]       0-042-4[00340]       ITU  ISUP. -> RLC (10) CIC=00206  
                                                                         SLS=14 Pr:0 Ni:NTL 
13:05:49.854074  0-041-2[00330]       0-048-5[00389]       ITU  ISUP. -> CPG (2c) CIC=00015  EVENT=Alerting
                                                                         SLS=15 Pr:0 Ni:NTL 
13:05:50.394305  0-037-6[00302]       0-048-5[00389]       ITU  SNM.. -> RST  (15) PC=0-050-7[00407] 
                                                                         Pr:0 Ni:NTL 
13:05:50.446115  0-042-4[00340]       0-048-5[00389]       ITU  ISUP. -> IAM (01) CIC=00035  CDPN=141499911F CGPN=568550740
                                                                         SLS=03 Pr:0 Ni:NTL 
13:05:50.492999  0-063-1[00505]       0-048-5[00389]       ITU  ISUP. -> IAM (01) CIC=00657  CDPN=141499921F CGPN=560371114
                                                                         SLS=01 Pr:0 Ni:NTL 
the problem is that the software monitors one interface at a time. if you want to monitor both interfaces, you have to run two copies of the software. that means you end up with two files that you must read to figure out what's going on. so, to make things easier, you decide to merge the two files.

your task is to merge two files that look like the above file. if file1 looks like

Code: Select all

13:05:48.987595  0-041-2[00330]       0-048-5[00389]       ITU  ISUP. -> ACM (06) CIC=00015  
                                                                         SLS=15 Pr:0 Ni:NTL 
13:05:49.127631  0-042-4[00340]       0-048-5[00389]       ITU  ISUP. -> RLC (10) CIC=00035  
                                                                         SLS=03 Pr:0 Ni:NTL 
13:05:49.225719  0-041-2[00330]       0-048-5[00389]       ITU  ISUP. -> IAM (01) CIC=00055  CDPN=0560311324F REDIRECTING=569102813
                                                                         SLS=07 Pr:0 Ni:NTL CGPN=563863936
and file2 looks like

Code: Select all

13:05:48.954074  0-041-2[00330]       0-048-5[00389]       ITU  ISUP. -> CPG (2c) CIC=00015  EVENT=Alerting
                                                                         SLS=15 Pr:0 Ni:NTL 
13:05:49.114305  0-037-6[00302]       0-048-5[00389]       ITU  SNM.. -> RST  (15) PC=0-050-7[00407] 
                                                                         Pr:0 Ni:NTL 
13:05:49.246115  0-042-4[00340]       0-048-5[00389]       ITU  ISUP. -> IAM (01) CIC=00035  CDPN=141499911F CGPN=568550740
                                                                         SLS=03 Pr:0 Ni:NTL 
then the output of the merge operation should look like

Code: Select all

13:05:48.954074  0-041-2[00330]       0-048-5[00389]       ITU  ISUP. -> CPG (2c) CIC=00015  EVENT=Alerting
                                                                         SLS=15 Pr:0 Ni:NTL 
13:05:48.987595  0-041-2[00330]       0-048-5[00389]       ITU  ISUP. -> ACM (06) CIC=00015  
                                                                         SLS=15 Pr:0 Ni:NTL 
13:05:49.114305  0-037-6[00302]       0-048-5[00389]       ITU  SNM.. -> RST  (15) PC=0-050-7[00407] 
                                                                         Pr:0 Ni:NTL 
13:05:49.127631  0-042-4[00340]       0-048-5[00389]       ITU  ISUP. -> RLC (10) CIC=00035  
                                                                         SLS=03 Pr:0 Ni:NTL 
13:05:49.225719  0-041-2[00330]       0-048-5[00389]       ITU  ISUP. -> IAM (01) CIC=00055  CDPN=0560311324F REDIRECTING=569102813
                                                                         SLS=07 Pr:0 Ni:NTL CGPN=563863936
13:05:49.246115  0-042-4[00340]       0-048-5[00389]       ITU  ISUP. -> IAM (01) CIC=00035  CDPN=141499911F CGPN=568550740
                                                                         SLS=03 Pr:0 Ni:NTL 
got it? good.
LinuxFreaK
Site Admin
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
Location: Karachi
Contact:

Re:

Post by LinuxFreaK »

Dear lambda,
Salam,

Will love to do this but i do not have much time for now. May be some one else can do this :)

Best Regards.
Farrukh Ahmed
LinuxFreaK
Site Admin
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
Location: Karachi
Contact:

Re:

Post by LinuxFreaK »

Dear lambda,
Salam,

Would not took more then 5 minutes :)

Code: Select all

#!/bin/bash

if [ $# -ne 2 ]
then
    echo 1>&2 Usage: $0 "First File", "Second File"
    exit 1
fi

TMP="merge.txt.$$"
RES="merge.txt"

cat $1 > $TMP
cat $2 >> $TMP

cat $TMP | sort > $RES

rm -f "$TMP"
Best Regards.
Farrukh Ahmed
LinuxFreaK
Site Admin
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
Location: Karachi
Contact:

Re:

Post by LinuxFreaK »

Dear PLUCian's,
Salam,

One can also solve this programming task with the help you lambda post :)

FYI, http://www.linuxpakistan.net/forum2x/vi ... php?t=6772

Best Regards.
Farrukh Ahmed
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Re:

Post by lambda »

LinuxFreaK wrote:Would not took more then 5 minutes :)
and incorrect. remember, you have to sort each pair of lines -- the line with a timestamp entry and the next line are related. your code separates them.
LinuxFreaK
Site Admin
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
Location: Karachi
Contact:

Re:

Post by LinuxFreaK »

Dear lambda,
Salam,

Okay. I will see when i get some time ;)

May be some one else will give you answer :)

Best Regards.
Farrukh Ahmed
LinuxFreaK
Site Admin
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
Location: Karachi
Contact:

Re:

Post by LinuxFreaK »

Dear lambda,
Salam,

Code: Select all

#!/usr/bin/env perl

use strict;
use warnings;
use diagnostics;

if (@ARGV != 2) {
       print STDERR "usage: $0 LOGFILE_1 LOGFILE_2\n";
       exit(1);
}

my ($logfile1, $logfile2) = @ARGV;

my %map = (parse_logfile($logfile1), parse_logfile($logfile2));

foreach my $key (sort keys %map) {
       print $map{$key};
}

sub parse_logfile {
       my %map;
       my $line;

       open LOGFILE, $_[0] or die "$0: can't open file '$_[0]': $!";

       while (defined($line = <LOGFILE>) &&
               $line !~ /^\d{2}(:\d{2}){2}\.\d{6}/) {
       }

       my $stop = 0;

       do {
               if (defined $line &&
               $line =~ /^(\d{2}(:\d{2}){2}\.\d{6})/) {
                       my $key = $1;
                       $map{$key} = $line;

                       while (defined($line = <LOGFILE>) &&
                               $line !~ /^\d{2}(:\d{2}){2}\.\d{6}/) {
                               $map{$key} .= $line;
                       }
               } else {
                       $stop = 1;
               }
       } while (!$stop);

       close LOGFILE or die "$0: can't close file '$_[0]': $!";

       return %map;
}
Best Regards.
Farrukh Ahmed
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Post by lambda »

looks like it works. congratulations!
LinuxFreaK
Site Admin
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
Location: Karachi
Contact:

Re:

Post by LinuxFreaK »

Dear lambda,
Salam,

Check this out.

Code: Select all

#!/bin/bash

if [ $# -ne 2 ]
then
    echo 1>&2 Usage: $0 "First File", "Second File"
    exit 1
fi

fold() { sed -e :a -e '$!{N;/\n[[:blank:]]/{s/\n/@@@/;ba' -e } -e } -e 'P;D'; }
unfold() { sed -e 's/@@@/\
/g'; }

TMP="merge.txt.$$"
RES="merge.txt"

cat $1 > $TMP
cat $2 >> $TMP

cat $TMP | fold | sort | unfold  > $RES

rm -f "$TMP"
Best Regards.
Farrukh Ahmed
kragen
Cadet
Posts: 8
Joined: Sun Mar 25, 2007 3:56 am
Contact:

No need to get complicated

Post by kragen »

You just want to sort pairs of lines from some number of input files given on the command line, right? This works:

Code: Select all

#!/usr/bin/perl -w
use strict;
my @entries;
while (<>) {
        my $secondline = <>;
        push @entries, $_ . $secondline;
}
print sort @entries;
It has some problems:
  • What is it going to do when the time crosses midnight? It's going to interleave the entries from multiple days.
  • How about if a newline is missing from the end of one of the files? It might run two lines together.
  • Is the hour guaranteed to have two digits?
But it might work well enough as is, and if not, it's fairly straightforward to add handling for those things.
kragen
Cadet
Posts: 8
Joined: Sun Mar 25, 2007 3:56 am
Contact:

Post by kragen »

I should point out that the three problems with my script apply equally to Farrukh's. However, should there be multiple lines following the timestamped line (instead of just one), his script will keep them together, and my script will put things into a totally bizarre order. Maybe it would be good to add a line that says

Code: Select all

die "unexpected line format: $_" if !/\A\d\d:\d\d:\d\d\.\d{6}/ for @entries;
On the other hand, I notice that his script will silently drop input items if there is more than one with exactly the same timestamp, down to the microsecond --- either from the same logfile or from different log files.
Post Reply