I have the some code and the following is not producing the output I need.
I'm expecting a line in my OUTFILE to look like this...
02<summary>ALC: - RT304 Short truncated version of Error Mes</summary>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Where X are spaces
I'm getting
02<summary>ALC: - 1 Short truncated version of Error Mes</summary>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
I'm struggling to find our where my problem lies.
I've not failed! Just found 100 ways that don't work...yet!
Code:
# The next print is destined for the Problem Summary field which has a max
# length of 50chr. The first 4 will be 'ALC:' and then we need the short hostname/IP.
# This is followed by ' - '. Then whathever is left of the 50 chars can be used by
# $EVENT->{msg}. Finish off with </summary> and then pad out with spaces until 102
# chars. There is no newline at the end required.
my $hostNshort;
# $hostNshort store either the IP or the short version of hostname e.g. RT304.mycom.org.uk will become RT304 while
# 10.32.54.76 will remain untouched
my $hostNpassed = trim( $EVENT->{hostname}->[0]);
# $hostNpassed has the hostname as passed from XPort
# m//x ---- the x allows the RE to be eXtended to allow embedded comments
if ( $hostNpassed =~ / ( # start capturing to $1
# NOTE look for something like an IP address first as the RE engine
# "prefers" the Longest, Leftmost match
# ie in alternation the first alternative has precedence
# and the hostname part or the RE would match against the 1st octet of an IP addr
\d{1,3} # 1-3 digit chars 0-9
\. # a literal
\d{1,3}
\.
\d{1,3}
\.
\d{1.3} # NOTE: this is an imperfect IP matching RE, as it allows many invalid IP
# addresses to pass through 999.888.777.666 etc etc, however the perfect RE for
# matching IP addrs is mindblowing complex, and doesn't really offer any
# advantage here
| # OR
[A-Za-z0-9]+ # 1 or more of A-Za-z0-9 (almost the same as \w but that includes _
) # end capturing to $1
/x) {
$hostNshort = $1;
}
else{
$hostNshort = "ERROR"; #set sorthostname to ERROR
}
$mStr = join( "", "02<summary>ALC:", $hostNshort, " - ", trim( $EVENT->{msg}->[0]));
# $mStr now has 02 prefix, opening tag, ALC:, short hostname or ip, - , and msg.
# Need to truncate so that the data after the opening tags is no more that 50 chars.
# (02<summary>ALC: = 15 chars) so we take the the first 65 chars and then add the closing tag.
$mStr = substr($mStr, 0, 65). "</summary>";
print OUTFILE pad102($mStr);
# Sub that will take the sting parameter and add spaces to the end to make the string a length of 102 chars $LineLen has been defined earlier and equals 102
sub pad102{
my $p = shift;
$p = $p. " " x ($LineLen - length($p));
return $p;
}
I'm expecting a line in my OUTFILE to look like this...
02<summary>ALC: - RT304 Short truncated version of Error Mes</summary>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Where X are spaces
I'm getting
02<summary>ALC: - 1 Short truncated version of Error Mes</summary>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
I'm struggling to find our where my problem lies.
I've not failed! Just found 100 ways that don't work...yet!