Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > changing values in certain blocks

Reply
Thread Tools

changing values in certain blocks

 
 
alfonsobaldaserra
Guest
Posts: n/a
 
      02-03-2011
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
 
Reply With Quote
 
 
 
 
alfonsobaldaserra
Guest
Posts: n/a
 
      02-03-2011
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.
 
Reply With Quote
 
 
 
 
Helmut Schneider
Guest
Posts: n/a
 
      02-03-2011
alfonsobaldaserra wrote:

> 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
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      02-03-2011
On Thu, 3 Feb 2011 10:57:56 -0800 (PST), alfonsobaldaserra <> wrote:

>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
}

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      02-04-2011
alfonsobaldaserra wrote:
> 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
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
 
Reply With Quote
 
alfonsobaldaserra
Guest
Posts: n/a
 
      02-08-2011
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Methods and blocks - not that clear when blocks passed into Steven Taylor Ruby 9 04-27-2009 08:46 AM
How to distinguish blocks of certain arity in 1.8 bp50fathoms@gmail.com Ruby 4 04-24-2009 06:06 AM
"Building Blocks" are "Application Blocks" Arjen ASP .Net 3 02-27-2005 01:06 AM
procs/blocks - blocks with procs, blocks with blocks? matt Ruby 1 08-06-2004 01:33 AM



Advertisments
 



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