File: col2tlviz.pl

package info (click to toggle)
collectl 3.7.4-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,624 kB
  • ctags: 119
  • sloc: perl: 14,928; sh: 429; makefile: 11
file content (113 lines) | stat: -rwxr-xr-x 2,604 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/perl -w

# Copyright 2003-2009 Hewlett-Packard Development Company, L.P.

# Revision history (some of)
#1.2.0 Convert alphabetic fields to 0s, which at this point are only for DSK and NET

use Config;
use Getopt::Std;
use File::Basename;
use strict;

my $Version="1.2.0";
my $Copyright='Copyright 2003-2008 Hewlett-Packard Development Company, L.P.';

my $pcFlag=($Config{"osname"}=~/MSWin32/) ? 1 : 0;
my $SEP=($pcFlag) ? '\\' : '/';

our ($opt_h, $opt_i, $opt_o, $opt_v);
my  ($inspec, $outdir);
getopts('hi:o:v');
$inspec=$opt_i    if defined($opt_i);
$outdir=$opt_o    if defined($opt_o);

if (defined($opt_v))
{
  print "col2tlviz V$Version\n";
  print "$Copyright\n";
  exit;
}

if (defined($opt_h) || !defined($inspec))
{
  print "usage: col2tlv.pl -i filespec [-o dirname] [-v]\n";
  print "$Copyright\n";
  exit;
}

error("output directory doesn't exist")    if defined($outdir) && !-e $outdir;

my @files;
my $glob=$inspec;
my $skipped=0;
@files=glob($glob);
foreach my $file (@files)
{
  if ($file!~/tab$|cpu$|dsk$|net$|nfs$/)
  {
    $skipped++;
    next;
  }

  open IN, "<$file" or error("Couldn't open '$file'");

  my $outfile="$file.csv";
  $outfile="$outdir$SEP".basename($outfile)    if defined($outdir);
  open OUT, ">$outfile" or error("Couldn't create '$outfile'");
  print "Creating: $outfile\n";

  my $state=0;
  my $header='';
  while (my $line=<IN>)
  {
    if ($line=~/^#Date/)
    {
      cvtHeader(\$header);

      $state=1;
      $line=~s/ /,/g;
      $line=~s/#Date,Time(.*),?$/Sample Time$1/;  # also get rid of optional trailing comma!
      print OUT $line;
      next;
    }

    if ($state==0)
    {
      $header.=$line;
      next;
    }

    my ($date, $time, $therest)=split(/ /, $line, 3);
    my $year=substr($date, 0, 4);
    my $mon= substr($date, 4, 2);
    my $day=substr($date, 6, 2);

    my $month=substr('JanFebMarAprMayJunJulAugSepOctNovDec', ($mon-1)*3, 3);
    my $newdate="$day-$month-$year";

    $therest=~s/ /,/g;
    $therest=~s/[a-z].*?,/0,/g;    # for DSK and NET files, this will convert instance names to 0's
    print OUT "$newdate $time,$therest";
  }
}
print "skipped $skipped file(s) that did not extension(s): tab,cpu,dsk,net,nfs\n"    if $skipped;

sub cvtHeader
{
  my $hdrref=shift;

  $$hdrref=~s/##.*//g;
  $$hdrref=~s/#\s+(.*)/"$1",/g;
  $$hdrref=~s/[\n\r]+//g;
  $$hdrref=~s/,$//;
  $$hdrref=~/Host:\s+(\S+)/;
  $$hdrref="$1,$$hdrref\n";
  print OUT $$hdrref;
}

sub error
{
  print "$_[0]\n";
  exit;
}