[icinga-checkins] icinga.org: icinga2/mfriedrich/config: convertv1->v2 initial import

git at icinga.org git at icinga.org
Tue Mar 12 17:40:53 CET 2013


Module: icinga2
Branch: mfriedrich/config
Commit: 331f7154fb28f4b375f12364d2ed72b8bf03a10b
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=331f7154fb28f4b375f12364d2ed72b8bf03a10b

Author: Michael Friedrich <michael.friedrich at netways.de>
Date:   Tue Mar 12 17:40:53 2013 +0100

convertv1->v2 initial import

---

 contrib/configconvert/convertv1_v2.pl |  266 +++++++++++++++++++++++++++++++++
 1 files changed, 266 insertions(+), 0 deletions(-)

diff --git a/contrib/configconvert/convertv1_v2.pl b/contrib/configconvert/convertv1_v2.pl
new file mode 100644
index 0000000..0eac1db
--- /dev/null
+++ b/contrib/configconvert/convertv1_v2.pl
@@ -0,0 +1,266 @@
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+
+use Data::Dumper;
+use File::Find;
+use feature 'say';
+
+# stubs
+sub strip($);
+sub errlog ($$);
+sub slurp($);
+sub parse_cfg($$);
+sub debug($$);
+sub get_key_from_main_cfg($$);
+
+sub convert_2x ($);
+
+# FIXME getopts
+my $icinga_cfg = "/etc/icinga/icinga.cfg";
+my $debug_level = 1;
+
+my $type_cnt;
+
+# TODO
+# also fetch some basic configs from main config, which may be interesting
+
+# get all included cfg_dir/cfg_file paths and create a list
+my @cfg_files = get_key_from_main_cfg($icinga_cfg, "cfg_file");
+my @cfg_dirs = get_key_from_main_cfg($icinga_cfg, "cfg_dir");
+
+# helper function for find
+sub find_cfg_files {
+    my $file = $File::Find::name;
+    return if -d $file;
+    if ($file =~ /\.cfg$/) {
+        push @cfg_files, $file;
+    }
+}
+
+foreach my $cfg_dir (@cfg_dirs) {
+    find(\&find_cfg_files, $cfg_dir);
+}
+
+#print "@cfg_files";
+
+# now fetch all the config information into our global hash ref
+my $cfg_obj = {};
+
+foreach my $cfg_file (@cfg_files) {
+    $cfg_obj = parse_cfg($cfg_obj, $cfg_file);
+}
+
+#say Dumper($cfg_obj);
+say Dumper($cfg_obj->{'service'});
+
+
+# the conversion magic inside
+my $cfg_obj_v2 = convert_2x($cfg_obj);
+
+
+
+
+
+
+
+
+################################################################################
+# FUNCTIONS
+################################################################################
+
+sub convert_2x ($) {
+# v1 -> v2
+# register 0 == template
+# use == inherits template
+# dependency == ...
+# escalation == ...
+# check_command => command_name => define command == command in service
+
+    # hashref is selectable by type first
+    my $cfg_obj_v1 = shift;
+
+    my $cfg_obj_v2;
+
+    ######################################
+    # HOSTS
+    # use => template
+    # check_command => undef
+    ######################################
+    foreach my $host_obj_v1 (@$cfg_obj_v1{'host'}) {
+
+    }
+
+    ######################################
+    # SERVICES
+    #
+    ######################################
+
+    ######################################
+    # 
+    ######################################
+
+    ######################################
+    # 
+    ######################################
+
+    ######################################
+    # 
+    ######################################
+
+    ######################################
+    # 
+    ######################################
+
+    ######################################
+    # 
+    ######################################
+
+    return $cfg_obj_v2;
+}
+
+
+sub parse_cfg ($$) {
+    my $cfg_obj = shift;
+    my $file = shift;
+
+    
+    my $obj = {}; #hashref
+    my $in_define = 0;
+    my $type;
+    my $append; # this is a special case where multiple lines are appended with \ - not sure if we support THAT.
+    my $inline_comment;
+
+    my $cnt;
+    my $attr;
+    my $val;
+
+    my @cfg = slurp($file);
+
+    foreach my $line (@cfg) {
+        $line = strip($line);
+
+        # skip comments and empty lines
+        next if ($line eq "" || !defined($line) || $line =~ /^\s+$/);
+        next if ($line =~ /^[#;]/ || $line =~ /;.*/);
+        
+        #debug($debug_level, $line);
+
+        # end of def
+        if ($line =~ /}(\s*)$/) {
+            $in_define = 0;
+            $type = "";
+            next;
+        }
+        # start of def
+        elsif ($line =~ /define\s+(\w+)\s*{?(.*)$/) {
+            $type = $1;
+            $append = $2;
+
+            # we're ready to process entries
+            $in_define = 1;
+            # save the current type counter, being our unique key here
+            $cnt = $type_cnt->{$type} += 1;
+            next;
+        }
+        # in def
+        elsif ($in_define == 1) {
+
+            # first, remove the annoying inline comments after ';'
+            $line =~ s/\s*;(.*)$//;
+            $inline_comment = $1;
+
+            # then split it and save it by type->cnt->attr->val
+            ($attr, $val) = split (/\s+/, $line, 2); # important - only split into 2 elements 
+            # ignore empty values
+            next if (!defined($val));
+            next if ($val eq "");
+            #debug($debug_level, "line: '$line'");
+            #debug($debug_level, "type: $type");
+            #debug($debug_level, "attr: $attr");
+            #debug($debug_level, "val: $val");
+            #debug($debug_level, "\n");
+
+            $cfg_obj->{$type}->{$cnt}->{$attr} = $val;
+
+            # ignore duplicated attributes, last one wins
+        }
+        else {
+            $in_define = 0;
+        }
+
+    }
+
+    return $cfg_obj;
+
+}
+
+
+sub strip($) {
+    my $str = shift;
+
+    #strip trailing and leading whitespaces
+    $str =~ s/^\s+//;
+    $str =~ s/\s+$//;
+
+    return $str;
+}
+
+sub errlog($$) {
+    my $err_lvl = shift;
+    my $log_str = shift;
+
+    if ($err_lvl > 0) {
+        print STDERR color("red"), "$log_str\n";;
+    } else {
+        print "$log_str\n";
+    }
+    
+}
+
+sub debug($$) {
+    my $dbg_lvl = shift;
+    my $dbg_str = shift;
+
+    if ($dbg_lvl > 0) {
+        print "$dbg_str\n";
+    }
+}
+
+sub slurp($) {
+    my $file = shift;
+
+    if ( -f $file ) {
+        open ( my $fh, "<", $file ) or die "Could not open $file: $!";
+        return do {
+            <$fh>;
+        }
+    } else {
+        die "$file does not exist";
+    }
+}
+
+sub get_key_from_main_cfg($$) {
+    my ( $file, $key) = @_;
+
+    my @key_arr = ();
+
+    if ( !-f $file) {
+        errlog(1, "cfg file $file does not exist!");
+        return;
+    }
+
+    if ( open ( my $fh, '<', $file ) ) {
+        while ( my $line = <$fh> ) {
+            chomp($line);
+            $line =~ s/#.*//;
+            if ($line =~ /^\s*$key=([^\s]+)/) {
+                push @key_arr, $1; # we may have multiple occurences
+            }
+        }
+    }
+
+    return @key_arr;
+}
+





More information about the icinga-checkins mailing list