Skip to content

Instantly share code, notes, and snippets.

@lazyfrosch
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lazyfrosch/10246612 to your computer and use it in GitHub Desktop.
Save lazyfrosch/10246612 to your computer and use it in GitHub Desktop.
LConf Custom mid.pl to replace some macros in the service name
# (c) 2013 NETWAYS GmbH, Markus Frosch <markus.frosch@netways.de>
sub CustomMid {
my $CLIENTS = shift;
# process each host
foreach my $client (keys %{$CLIENTS}) {
# process each service
foreach my $service (keys %{$CLIENTS->{$client}->{'SERVICES'}}) {
###
# Replace some macros in the service name
###
if(my @matches = $CLIENTS->{$client}->{'SERVICES'}->{$service}->{'cn'} =~ m/(\$[A-Z0-9]+\$)/gi) {
beVerbose("INFO", "Found ".scalar(@matches)." macros in service name $service") if defined $opt->{verbose};
foreach(@matches) {
my $match = $_;
my $replacement;
my $ignore;
if (defined $opt->{verbose}) {
beVerbose("INFO", "Found macro $match in service name (service=$service - client=$client)")
}
if ($match eq '$HOSTNAME$') {
$replacement = $CLIENTS->{$client}->{'cn'};
}
elsif ($match eq '$HOSTADDRESS$') {
$replacement = $CLIENTS->{$client}->{'lconfaddress'};
}
elsif ($match eq '$HOSTALIAS$') {
$replacement = $CLIENTS->{$client}->{'lconfalias'};
}
else {
print "ERROR: macro $match unavailable for replacement (service=$service - client=$client)\n";
$ignore = 1;
}
if($replacement) {
$CLIENTS->{$client}->{'SERVICES'}->{$service}->{'cn'} =~ s/\Q$match\E/$replacement/;
if (defined $opt->{verbose}) {
beVerbose("INFO", "replacing macro $match with '$replacement' in service name".
"(service=$service - client=$client)");
}
}
elsif(!$ignore) {
print "ERROR: no replacement for macro $match (service=$service - client=$client)\n";
}
}
}
}
}
return $CLIENTS;
}
1;
# vi: expandtab ts=4 sw=4 :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment