added lwes-filter-listener
[lwes.git] / src / lwes-calculate-max-event-size
blobc2938b6f3129adb92430c5b06ceb59df5fd79d04
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
6 use Data::Dumper;
8 if (scalar (@ARGV) < 1)
10 print STDERR "lwes-calculate-max-size <esf_files>\n";
11 exit 1;
14 my $events = {};
16 my $current_event = undef;
17 my $in_event = 0;
18 my $in_meta = 0;
20 my $lines = 1;
21 # parse the file
22 open (FH, "< $ARGV[0]");
23 while (<FH>)
25 my $line = $_;
27 # kill leading and trailing white-space
28 $line =~ s/^\s*//g;
29 $line =~ s/\s*$//g;
31 # skip comment lines
32 next if $line =~ m/^#/;
33 # skip blank lines
34 next if $line eq "";
36 if ($in_event)
38 if ($line eq "}")
40 $in_event = 0;
41 $current_event = undef;
43 else
45 if ($line =~ /^(\w+)\s+(\w+);\s+#\s+(\w+)\s+(\w+),\s+(\w+)(,\s+(\w+))?/)
47 my ($type, $sname, $lname, $contract, $source, $max_length)
48 = ($1, $2, $3, $4, $5, $7);
49 if ($contract ne "OPTIONAL"
50 && $contract ne "REQUIRED")
52 print STDERR "ERROR: line $lines\n";
53 print STDERR "ERROR: '$contract' should be one of\n";
54 print STDERR "\tOPTIONAL\n";
55 print STDERR "\tREQUIRED\n";
56 exit 1;
58 foreach my $src (split /\|/, $source)
60 if ( $src ne "COMMAND_ARGS"
61 && $src ne "FUNCTION_ARGS"
62 && $src ne "CALCULATED"
63 && $src ne "HTTP_REQ"
64 && $src ne "XARGS"
67 print STDERR "ERROR: line $lines\n";
68 print STDERR "ERROR: '$src' should be one or"
69 ." more of\n";
70 print STDERR "\tCOMMAND_ARGS\n";
71 print STDERR "\tFUNCTION_ARGS\n";
72 print STDERR "\tCALCULATED\n";
73 print STDERR "\tHTTP_REQ\n";
74 print STDERR "\tXARGS\n";
75 exit 1;
78 if (defined ($current_event))
80 $events->{$current_event}->{$sname} =
82 'type' => $type,
83 'lname' => $lname,
84 'contract' => $contract,
85 'source' => $source,
87 if (defined ($max_length))
89 $events->{$current_event}->{$sname}->{'max_length'} =
90 $max_length;
96 else
98 if ($line eq "{")
100 $in_event = 1;
102 else
104 unless (exists ($events->{$line}))
106 $current_event = $line;
107 $events->{$current_event} = { };
109 else
111 print STDERR "ERROR: line $lines\n";
112 print STDERR "ERROR: $line event exists twice in file!\n";
113 exit 1;
118 close (FH);
120 # calculate the sizes
122 # calculate meta size
123 my $meta_attributes_size = 0;
124 foreach my $attr (keys %{$events->{'MetaEventInfo'}})
126 # ignore fields set by the listener or journaller
127 next if $attr eq "SenderIP";
128 next if $attr eq "SenderPort";
129 next if $attr eq "ReceiptTime";
130 next if $attr eq "SiteID";
131 $meta_attributes_size +=
132 calculateEventEntrySize
133 ($events->{'MetaEventInfo'}->{$attr}->{'type'},
134 $attr,
135 $events->{'MetaEventInfo'}->{$attr}->{'max_length'});
138 foreach my $event (keys %{$events})
140 next if $event eq "MetaEventInfo";
141 my $event_max_size = $meta_attributes_size;
142 $event_max_size += calculateEventHeaderSize ($event);
143 foreach my $attr (keys %{$events->{$event}})
145 $event_max_size +=
146 calculateEventEntrySize
147 ($events->{$event}->{$attr}->{'type'},
148 $attr,
149 $events->{$event}->{$attr}->{'max_length'});
151 print "$event\t$event_max_size\n";
154 sub calculateEventHeaderSize
156 my ($eventname) = @_;
157 my $size = 0;
159 # one byte for event name length
160 $size += 1;
162 # length bytes for event name
163 $size += length $eventname;
165 # two bytes for number of attributes
166 $size += 2;
168 return $size;
171 sub calculateEventEntrySize
173 my ($type, $attr, $max_size) = @_;
175 my $size = 0;
177 # add one byte for attribute name length
178 $size += 1;
180 # add length bytes for attribute name
181 $size += length $attr;
183 # add one byte for attribute type id
184 $size += 1;
186 # now add bytes for data types
187 if ($type eq "boolean")
189 $size += 1;
191 elsif ($type eq "uint16")
193 $size += 2;
195 elsif ($type eq "int16")
197 $size += 2;
199 elsif ($type eq "uint32")
201 $size += 4;
203 elsif ($type eq "int32")
205 $size += 4;
207 elsif ($type eq "uint64")
209 $size += 8;
211 elsif ($type eq "int64")
213 $size += 8;
215 elsif ($type eq "ip_addr")
217 $size += 4;
219 elsif ($type eq "string")
221 # two bytes of length
222 $size += 2;
224 # add max size bytes of length
225 unless (defined ($max_size))
227 print STDERR "ERROR: must specify a max size for string types\n";
228 exit 1;
230 if ($max_size > 65535)
232 print STDERR "ERROR: max size must be less than 65535\n";
233 exit 1;
235 $size += $max_size;
237 else
239 print STDERR "ERROR: unknown type [$type]\n";
240 exit 1;
243 return $size;
246 exit 0;