File: convert

package info (click to toggle)
mrtg 2.5.4c-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 956 kB
  • ctags: 488
  • sloc: perl: 4,995; ansic: 2,133; makefile: 204; csh: 49; sh: 40
file content (155 lines) | stat: -rwxr-xr-x 4,216 bytes parent folder | download
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/local/gnu/bin/perl5
#mode: -*-perl-*-
require 5.002;
#
###############################################################
# This converts mrtg 1.x logfiles to 2.x format
###############################################################
# $Id: convert,v 2.0 1997/01/26 10:48:37 oetiker Exp $
#
# $Log: convert,v $
# Revision 2.0  1997/01/26 10:48:37  oetiker
# 2.0 release
#
#
#$debug=1

die "

Usage: convert  <logfile>

convert reads a mrtg 1.x <logfile>.log and converts it's contens
to mrtg 2.x format. The 1.x logfile gets stored in <logfile>.1.x-log

" unless (@ARGV && -R "$ARGV[0].log");


@step_width=(300,1800,7200,24*3600);
@number_of_steps=(600,600,600,732);

$total_steps=(600+600+600+732);

open(OLD,"<$ARGV[0].log") || die "Can't open $ARGV[0].log\n";

# get the first bytes ....

chop($ol_current=<OLD>);
chop($ol_inbytes=<OLD>);
chop($ol_outbytes=<OLD>);
chop($ol_lower=<OLD>);
&sanity;

open(NEW,">$ARGV[0].new.$$") || die "Can't open $ARGV[0].new\n";

$nl_first=int(time/$step_width[0])*$step_width[0];
$nl_current=$nl_first;
$nl_lower=$nl_current-$step_width[0];

# we trick rateup into beleving that the counter wrapped ...
# this gives a nice transition from old to new log ...

print NEW "$nl_current 999999999999999 999999999999999\n";

$nl_in=0;
$nl_out=0;


while(! eof OLD && $total_steps){
  # see wether we have to change gears

  if ( ($#step_width >= 1) &&
       ($nl_current <=  $nl_first - $step_width[0]*$number_of_steps[0]) &&

       # forget the clutch ... we do it at the right moment ...
       ($nl_current ==  int($nl_current/$step_width[1])*$step_width[1])
     )
    {
      shift @step_width;
      shift @number_of_steps;
      $nl_first = $nl_current;
      $nl_lower = $nl_current-$step_width[0];
      print "SHIFTING ... step: $step_width[0]\n";
    }
 
  # get the data ...


  #is part of the chunk still within our scope ?
  #     OOOOOOOOOOOOOOOOOOOOOOOOO
  # NOW           NNNNNNNNN
  if (($nl_lower>=$ol_lower) && ($nl_current<=$ol_current)) {
    $factor = $step_width[0]/($ol_current-$ol_lower);
   $nl_in += $ol_in* $factor;
    $nl_out += $ol_out* $factor;
  }

  #is part of the chunk still within our scope ?
  #                  OOOO
  # NOW           NNNNNNNNN
  elsif (($nl_lower<=$ol_lower) && ($nl_current>=$ol_current)) {
    $nl_in += $ol_in;
    $nl_out += $ol_out;
  }
  #is part of the chunk still within our scope ?
  #                  OOOOOOOOOOOOOOOOOOOOOOOOO
  # NOW           NNNNNNNNN
  elsif (($nl_lower>=$ol_lower) && ($nl_current>=$ol_current)) {
    $factor = ($ol_current-$nl_lower)* ($ol_current-$ol_lower);
    $nl_in += $ol_in* $factor;
    $nl_out += $ol_out* $factor;
  }
  #is part of the chunk still within our scope ?
  #          OOOOOOOOOOOOOOOOOOOOO
  # NOW                     NNNNNNNNN
  elsif (($nl_lower<=$ol_lower) && ($nl_current<=$ol_current)) {
    $factor = ($nl_current-$ol_lower)* ($ol_current-$ol_lower);
    $nl_in += $ol_in* $factor;
    $nl_out += $ol_out* $factor;
  }
  #                                      OOOOOOOOOOOOOOOOOOOOO
  # NOW                     NNNNNNNNN
  # is the gobeling sufficient, to move to the next slot nl?
  if ($nl_lower >= $ol_lower) {
    print NEW "$nl_current ".int($nl_in/$step_width[0])." ".
      int($nl_out/$step_width[0])."\n";
    $total_steps--;
    $nl_current=$nl_lower;
    $nl_lower -= $step_width[0];
    $nl_in=0;
    $nl_out=0;
    print "NL Step: $nl_current - $step_width[0]\n" if $debug;
  } 
  #

  #         OOOOOOOOOOOOOOOOOOOOO
  # NOW                       NNNNNNNNN
  # do we need more ol data to compleate the slot?
  elsif ($nl_lower <= $ol_lower) {
    $ol_current=$ol_lower;
    chop($ol_in=<OLD>);
    chop($ol_out=<OLD>);
    chop($ol_lower=<OLD>);
    &sanity;
    print "OL Step: $ol_current\n" if $debug;
  } 
  
}

close NEW;

rename "$ARGV[0].log", "$ARGV[0].1.x-log";
rename "$ARGV[0].new.$$","$ARGV[0].log";
 
print "Conversion successful ... A copy of the old logfile is in $ARGV[0].old\n";

exit 0;

sub sanity{ 
  if (($ol_lower >= $ol_current) ||
      ($ol_lower+0 != int($ol_lower))) {
    die ("\nERROR at line $.: ".
	 "$ARGV[0].log does NOT look like a 1.x mrtg logfile\n\n");
    close NEW;
    unlink "$ARGV[0].new.$$";
  }
}