changing values in certain blocks

  • Thread starter alfonsobaldaserra
  • Start date
A

alfonsobaldaserra

hello again,

i have a bunch of nagios configuration files. the contents are as
follows

define service {
host_name chakra.example.com
service_description Disk Monitor
use disk_service
max_check_attempts 5
check_interval 5
retry_interval 1
notification_interval 60
contacts nagiosadmin
_xiwizard windows_server
register 1
}

define service {
host_name chakra.example.com
service_description Ping
use ping_service
max_check_attempts 5
check_interval 5
retry_interval 1
notification_interval 60
contacts nagiosadmin
_xiwizard windows_server
register 1
}

define service {
host_name chakra.example.com
service_description SNMP
use processes_service
check_command check_snmp_process!community!
snmp!0
max_check_attempts 5
check_interval 5
retry_interval 1
check_period xi_timeperiod_24x7
notification_interval 60
notification_period xi_timeperiod_24x7
contacts nagiosadmin
_xiwizard windows_server
register 1
}

the value of check_interval for every file is 5. i want to change
that to 60 but only for "Disk Monitor" blocks which could be anywhere
in the file. in this post that is at the top. i tried but failed so
i thought i might get some ideas from here :)

as always any pointers are appreciated. thank you
 
A

alfonsobaldaserra

i think i figured a working way to achieve this:

#!/usr/bin/perl

use strict;
use warnings;
use File::Copy;

$|++;

my $file = shift;
my $new = "${file}\.new";

open my $conf, "<", $file or die "$!\n";
local $/ = undef;
my $eggs = <$conf>;
close $conf;

$eggs =~ s/(.*disk_service.*?check_interval\s*)5(.*)/${1}60${2}/s;

open my $conf2, ">", $new;
print $conf2 $eggs;
close $conf2;

move $file, "${file}\.old";
move $new, $file;

__END__

i am not quite satisfied with $eggs substitution part, i believe there
should be a better way to do this. thanks.
 
H

Helmut Schneider

alfonsobaldaserra said:
i have a bunch of nagios configuration files. the contents are as
follows

define service {
host_name chakra.example.com
service_description Disk Monitor
use disk_service
max_check_attempts 5
check_interval 5
retry_interval 1
notification_interval 60
contacts nagiosadmin
_xiwizard windows_server
register 1
}

define service {
host_name chakra.example.com
service_description Ping
use ping_service
max_check_attempts 5
check_interval 5
retry_interval 1
notification_interval 60
contacts nagiosadmin
_xiwizard windows_server
register 1
}

define service {
host_name chakra.example.com
service_description SNMP
use processes_service
check_command check_snmp_process!community!
snmp!0
max_check_attempts 5
check_interval 5
retry_interval 1
check_period xi_timeperiod_24x7
notification_interval 60
notification_period xi_timeperiod_24x7
contacts nagiosadmin
_xiwizard windows_server
register 1
}

the value of check_interval for every file is 5. i want to change
that to 60 but only for "Disk Monitor" blocks which could be anywhere
in the file. in this post that is at the top. i tried but failed so
i thought i might get some ideas from here :)

as always any pointers are appreciated. thank you

Actually, you rather should include "check_interval 60" in the
"disk_service" template and remove it from the individual service
definition...

Helmut
 
S

sln

i think i figured a working way to achieve this:

#!/usr/bin/perl

use strict;
use warnings;
use File::Copy;

$|++;

my $file = shift;
my $new = "${file}\.new";

open my $conf, "<", $file or die "$!\n";
local $/ = undef;
my $eggs = <$conf>;
close $conf;

$eggs =~ s/(.*disk_service.*?check_interval\s*)5(.*)/${1}60${2}/s;

open my $conf2, ">", $new;
print $conf2 $eggs;
close $conf2;

move $file, "${file}\.old";
move $new, $file;

__END__

i am not quite satisfied with $eggs substitution part, i believe there
should be a better way to do this. thanks.

As long as you made an attempt, this might help.

-sln

-------------
use strict;
use warnings;

# Regex uses (?(condition)yes-pattern|no-pattern)
# Probably experimental, shouldn't use it.

my $rx = qr/
# Required '{', grp: 1
({[^}]+?)

(?: # Optional, grps: 2,3,4
( (?<=\s) check_interval \s+ ) (\d+) (?=\s) ([^}]*?)
)?
# Required, grp: 5
( (?<=\s) service_description \s+ Disk\ Monitor (?=[\s}]) [^}]*?)

(?(2)| # Required if grp 2 didn't capture, grps: 6,7
( (?<=\s) check_interval \s+ ) (\d+) (?=[\s}])
)
/xs;


my $datastring = join '', <DATA>;

my $count = $datastring =~ s/$rx/
$1 .
(defined $3 ? "${2}60${4}" : '') .
$5 .
(defined $7 ? "${6}60" : '')
/eg;

print "Changed $count item(s)\n";
print $datastring,"\n" if $count;

__DATA__

define service {
host_name chakra.example.com
service_description Disk Monitor
use disk_service
max_check_attempts 5
check_interval 5
retry_interval 1
notification_interval 60
contacts nagiosadmin
_xiwizard windows_server
register 1
}

define service {
host_name chakra.example.com
service_description Ping
use ping_service
max_check_attempts 5
check_interval 5
retry_interval 1
notification_interval 60
contacts nagiosadmin
_xiwizard windows_server
register 1
}

define service {
host_name chakra.example.com
service_description SNMP
use processes_service
check_command check_snmp_process!community!
snmp!0
max_check_attempts 5
check_interval 5
retry_interval 1
check_period xi_timeperiod_24x7
notification_interval 60
notification_period xi_timeperiod_24x7
contacts nagiosadmin
_xiwizard windows_server
register 1
}
 
J

John W. Krahn

alfonsobaldaserra said:
hello again,

i have a bunch of nagios configuration files. the contents are as
follows

define service {
host_name chakra.example.com
service_description Disk Monitor
use disk_service
max_check_attempts 5
check_interval 5
retry_interval 1
notification_interval 60
contacts nagiosadmin
_xiwizard windows_server
register 1
}

define service {
host_name chakra.example.com
service_description Ping
use ping_service
max_check_attempts 5
check_interval 5
retry_interval 1
notification_interval 60
contacts nagiosadmin
_xiwizard windows_server
register 1
}

define service {
host_name chakra.example.com
service_description SNMP
use processes_service
check_command check_snmp_process!community!
snmp!0
max_check_attempts 5
check_interval 5
retry_interval 1
check_period xi_timeperiod_24x7
notification_interval 60
notification_period xi_timeperiod_24x7
contacts nagiosadmin
_xiwizard windows_server
register 1
}

the value of check_interval for every file is 5. i want to change
that to 60 but only for "Disk Monitor" blocks which could be anywhere
in the file. in this post that is at the top. i tried but failed so
i thought i might get some ideas from here :)

as always any pointers are appreciated. thank you


perl -i.bak -00pe'/service_description\s+Disk Monitor/ &&
s/(check_interval\s+)5(?=\s)/${1}60/' yourfile



John
 
A

alfonsobaldaserra

perl -i.bak -00pe'/service_description\s+Disk Monitor/ &&
s/(check_interval\s+)5(?=\s)/${1}60/' yourfile

John
--


thank you everyone for your code, one liner and advise :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top