Converting from Netsaint flat-file to Nagios Template-based config files

Paulo Pinto pspa at gp.novis.pt
Tue Aug 27 00:41:50 CEST 2002


Hi all.

Here goes a small perl script to convert Netsaint flat-file config files to Nagios template-based ones.

It's not perfect, but it did help me on an 'urgent' migration (I needed some config tweaks only 'available' on the new style configs).

P.S. - improvements are welcome :)
P.S.2 - japh at ip.pt it's my 'personal' email ... I'm the same guy :-p
P.S.3 - I'm NOT a programmer ... the code is really dirty ... keep the flames to yerself, willya ? :-)

Hugz to all.

-- 
Paulo Pinto
Novis GSP

pspa at gp.novis.pt
-------------- next part --------------
#!/usr/bin/perl
#^^^^^^^^^^^^^^ <<--- You may need to change this to reflect the correct location of your perl interpreter


# TRANSLATOR - "Netsaint style" to "Nagios style" config file converter.
# Not Copyrighted - 2002/Paulo Pinto (japh at ip.pt)

# This file ONLY "translates" hosts.cfg AND commands.cfg. Any other config file is out of scope.

# WARNINGWARNINGWARNINGWARNINGWARNINGWARNINGWARNINGWARNINGWARNINGWARNING
# WARNINGWARNINGWARNINGWARNINGWARNINGWARNINGWARNINGWARNINGWARNINGWARNING
# WARNINGWARNINGWARNINGWARNINGWARNINGWARNINGWARNINGWARNINGWARNINGWARNING

# * THIS "SOFTWARE" IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
# * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 
# * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
# * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# * STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# * POSSIBILITY OF SUCH DAMAGE.

# YOU HAVE BEEN WARNED !!

# ==================================================================================================================================
# NOTE: I'm not a programmer ... this may have programming errors that can make any SERIOUS perl programmer rip his hair off ... :-)
# ==================================================================================================================================

# This "software" is provided without any guarantee that it will ever work, and if it does, that the results will be the ones that you expect.

# It was developed for my personal use but I guess that other people may have use for it.

# You are free to modify, erase, add ... i.e., do whatever you want with this file. ANYWAY, I'd be grateful if you provide me any changes 
# you may make to it.

# The usage is quite simple:

# - Create a new directory (we'll use /tmp/nagios for this example)
# - Copy your hosts.cfg to /tmp/nagios/hosts.old
# - Copy your commands.cfg to /tmp/nagios/commands.old
# - IMPORTANT ! -> MAKE SURE that both files have the ".old" extension before proceeding (FAILURE TO DO SO MAY CAUSE IMPREDICTABLE RESULTS !! )
# - Copy this file to /tmp/nagios
# - Execute this file  (you may have to chmod +x it ).

# You'll get (hopefully...) 9 output files with .cfg extension, in the 'template' Nagios format. 
# Remember that they NEED to be manually verified and, if needed, tweaked before going into production !
# The main cause is that I "assume" that some variables are the same for all hosts or services... THIS MAY NOT BE YOUR CASE !

# Again, you've been warned :-)

# Hopefully, this is where you stop reading and proceed to do something useful :-)


open(SOURCE1, "<"."hosts.old");
@source1 = <SOURCE1>;
close(SOURCE1);
open(SOURCE2, "<"."commands.old");
@source2 = <SOURCE2>;
close(SOURCE2);
open(DEST1, ">"."hosts.cfg");
open(DEST2, ">"."services.cfg");
open(DEST3, ">"."timeperiods.cfg");
open(DEST4, ">"."contacts.cfg");
open(DEST5, ">"."misccommands.cfg");
open(DEST6, ">"."contactgroups.cfg");
open(DEST7, ">"."hostgroups.cfg");
open(DEST8, ">"."escalations.cfg");
open(DEST9, ">"."checkcommands.cfg");

# "Needed" header stuff
# hosts.cfg stuff

                print(DEST1 "define host{"."\n");
                print(DEST1 "\t"."name"."\t"."\t"."\t"."\t"."generic-host"."\n");
                print(DEST1 "\t"."notifications_enabled"."\t"."\t"."0 ; Host notifications are NOT enabled"."\n");
                print(DEST1 "\t"."event_handler_enabled"."\t"."\t"."1 ; Host event handler is enabled"."\n");
                print(DEST1 "\t"."flap_detection_enabled"."\t"."\t"."1 ; Flap detection is enabled"."\n");
                print(DEST1 "\t"."process_perf_data"."\t"."\t"."0 ; Do NOT Process performance data"."\n");
                print(DEST1 "\t"."retain_status_information"."\t"."1 ; Retain status information across program restarts"."\n");
                print(DEST1 "\t"."retain_nonstatus_information"."\t"."1 ; Retain non-status information across program restarts"."\n");
                print(DEST1 "\t"."register"."\t"."\t"."\t"."0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!"."\n");
                print(DEST1 "\t"."}");
                print(DEST1 "\n\n");

#services.cfg stuff

                print(DEST2 "define service{"."\n");
                print(DEST2 "\t"."name"."\t"."\t"."\t"."\t"."generic-service"."\n");
                print(DEST2 "\t"."active_checks_enabled"."\t"."\t"."1 ; Active service checks are enabled"."\n");
                print(DEST2 "\t"."passive_checks_enabled"."\t"."\t"."0 ; Passive service checks are enabled/accepted"."\n");
                print(DEST2 "\t"."parallelize_check"."\t"."\t"."1 ; Active service checks should be parallelized (disabling this can lead to major performance problems)"."\n");
                print(DEST2 "\t"."obsess_over_service"."\t"."\t"."1 ; We should obsess over this service (if necessary)"."\n");
                print(DEST2 "\t"."check_freshness"."\t"."\t"."\t"."0 ; Default is to NOT check service 'freshness'"."\n");
		print(DEST2 "\t"."notifications_enabled"."\t"."\t"."0 ; Service notifications are NOT enabled"."\n");
                print(DEST2 "\t"."event_handler_enabled"."\t"."\t"."1 ; Service event handler is enabled"."\n");
                print(DEST2 "\t"."flap_detection_enabled"."\t"."\t"."1 ; Flap detection is enabled"."\n");
                print(DEST2 "\t"."process_perf_data"."\t"."\t"."0 ; Do NOT Process performance data"."\n");
                print(DEST2 "\t"."retain_status_information"."\t"."1 ; Retain status information across program restarts"."\n");
                print(DEST2 "\t"."retain_nonstatus_information"."\t"."1 ; Retain non-status information across program restarts"."\n");
                print(DEST2 "\t"."register"."\t"."\t"."\t"."0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!"."\n");
                print(DEST2 "\t"."}");
                print(DEST2 "\n\n");

# Rock and Roll ...

foreach $linha (@source1) {
chomp;
chomp($linha);
($a,$b)= split(/\]=/,$linha);
($b1,$b2)=split(/\[/,$a);
($a1,$a2,$a3,$a4,$a5,$a6,$a7,$a8,$a9,$a10,$a11,$a12,$a13,$a14,$a15)= split(/;/,$b);

if (substr($a,0,11) eq "timeperiod["){
		print(DEST3 "define timeperiod{"."\n");
		print(DEST3  "\t"."timeperiod_name ".$b2."\n");
		if (!($a1 eq "" )){print(DEST3  "\t"."alias ".$a1."\n")};
		if (!($a2 eq "" )){print(DEST3  "\t"."sunday ".$a2."\n")};
		if (!($a3 eq "" )){print(DEST3  "\t"."monday ".$a3."\n")};
		if (!($a4 eq "" )){print(DEST3  "\t"."tuesday ".$a4."\n")};
		if (!($a5 eq "" )){print(DEST3  "\t"."wednesday ".$a5."\n")};
		if (!($a6 eq "" )){print(DEST3  "\t"."thursday ".$a6."\n")};
		if (!($a7 eq "" )){print(DEST3  "\t"."friday ".$a7."\n")};
		if (!($a8 eq "" )){print(DEST3  "\t"."saturday ".$a8."\n")};
		print(DEST3  "\t"."}");
		print(DEST3  "\n\n");
		}

if (substr($a,0,8) eq "command["){
		print(DEST5 "define command{"."\n");
		print(DEST5 "\t"."command_name"."\t".$b2."\n");
		print(DEST5 "\t"."command_line"."\t".$b."\n");
		print(DEST5 "\t"."}");
		print(DEST5 "\n\n");
		}

if (substr($a,0,8) eq "contact["){

if ($a4 eq "1") {$t1 ="r,"};
if ($a5 eq "1") {$t2 ="c,"};
if ($a6 eq "1") {$t3 ="w,"};
if ($a7 eq "1") {$t4 ="r,"};
if ($a8 eq "1") {$t5 ="d,"};
if ($a9 eq "1") {$t6 ="u,"} ;
$temp1 = $t1.$t2.$t3;
$temp2 = $t4.$t5.$t6;
chomp($temp1);
chomp($temp2);
if ((substr($temp1,-1,1)) eq ",") { chop($temp1) };
if ((substr($temp2,-1,1)) eq ",") { chop($temp2) };


		print(DEST4 "define contact{"."\n");
		print(DEST4 "\t"."contact_name"."\t"."\t"."\t".$b2."\n");
		print(DEST4 "\t"."alias"."\t"."\t"."\t"."\t".$a1."\n");
		print(DEST4 "\t"."service_notification_period"."\t".$a2."\n");
		print(DEST4 "\t"."host_notification_period"."\t".$a3."\n");
                print(DEST4 "\t"."service_notification_options"."\t".$temp1."\n");
                print(DEST4 "\t"."host_notification_options"."\t".$temp2."\n");
		print(DEST4 "\t"."service_notification_commands"."\t".$a10."\n");
		print(DEST4 "\t"."host_notification_commands"."\t".$a11."\n");
		if (!($a12 eq "")){print(DEST4 "\t"."email"."\t"."\t"."\t"."\t".$a12."\n")};
		if (!($a13 eq "")){print(DEST4 "\t"."pager"."\t"."\t"."\t"."\t".$a13."\n")};
		print(DEST4 "\t"."}");
		print(DEST4 "\n\n");
		}

if (substr($a,0,8) eq "contactg"){
		print(DEST6 "define contactgroup{"."\n");
		print(DEST6 "\t"."contactgroup_name"."\t".$b2."\n");
		print(DEST6 "\t"."alias"."\t"."\t"."\t".$a1."\n");
		print(DEST6 "\t"."members"."\t"."\t"."\t".$a2."\n");
		print(DEST6 "\t"."}");
		print(DEST6 "\n\n");
		}

if (substr($a,0,5) eq "host["){


	if ($a8 eq "1") {$t1 ="r,"};
	if ($a9 eq "1") {$t2 ="d,"};
	if ($a10 eq "1") {$t3 ="u,"};
	$temp1 = $t1.$t2.$t3;
	chomp($temp1);
	if ((substr($temp1,-1,1)) eq ",") { chop($temp1) };

		print(DEST1 "define host{"."\n");
		print(DEST1 "\t"."use"."\t"."\t"."\t"."generic-host"."\n");
		print(DEST1 "\t"."host_name"."\t"."\t".$b2."\n");
		print(DEST1 "\t"."alias"."\t"."\t"."\t".$a1."\n");
		print(DEST1 "\t"."address"."\t"."\t"."\t".$a2."\n");
		if (!($a3 eq "" )){print(DEST1 "\t"."parents"."\t"."\t"."\t".$a3."\n")};
		print(DEST1 "\t"."check_command"."\t"."\t".$a4."\n");
		print(DEST1 "\t"."max_check_attempts"."\t".$a5."\n");
		print(DEST1 "\t"."notification_interval"."\t".$a6."\n");
		print(DEST1 "\t"."notification_period"."\t".$a7."\n");
		print(DEST1 "\t"."notification_options"."\t".$temp1."\n");
		print(DEST1 "\t"."}");
		print(DEST1 "\n\n");
		}

if (substr($a,0,10) eq "hostgroup["){
		print(DEST7 "define hostgroup{"."\n");
		print(DEST7 "\t"."hostgroup_name"."\t"."\t".$b2."\n");
		print(DEST7 "\t"."alias"."\t"."\t"."\t".$a1."\n");
		print(DEST7 "\t"."contact_groups"."\t"."\t".$a2."\n");
		print(DEST7 "\t"."members"."\t"."\t"."\t".$a3."\n");
		print(DEST7 "\t"."}");
		print(DEST7 "\n\n");
		}

if (substr($a,0,8) eq "service["){

        if ($a10 eq "1") {$t1 ="r,"};
        if ($a11 eq "1") {$t2 ="c,"};
        if ($a12 eq "1") {$t3 ="w,"};
        $temp1 = $t1.$t2.$t3;
        chomp($temp1);
        if ((substr($temp1,-1,1)) eq ",") { chop($temp1) };

		print(DEST2 "define service{"."\n");
		print(DEST2 "\t"."use"."\t"."\t"."\t"."generic-service"."\n");
		print(DEST2 "\t"."host_name"."\t"."\t".$b2."\n");
		print(DEST2 "\t"."service_description"."\t".$a1."\n");
		print(DEST2 "\t"."is_volatile"."\t"."\t".$a2."\n");
		print(DEST2 "\t"."check_period"."\t"."\t".$a3."\n");
		print(DEST2 "\t"."max_check_attempts"."\t".$a4."\n");
		print(DEST2 "\t"."normal_check_interval"."\t".$a5."\n");
		print(DEST2 "\t"."retry_check_interval"."\t".$a6."\n");
		print(DEST2 "\t"."contact_groups"."\t"."\t".$a7."\n");
		print(DEST2 "\t"."notification_interval"."\t".$a8."\n");
		print(DEST2 "\t"."notification_period"."\t".$a9."\n");
		print(DEST2 "\t"."notification_options"."\t".$temp1."\n");
		print(DEST2 "\t"."check_command"."\t"."\t".$a14."\n");
		print(DEST2 "\t"."}");
		print(DEST2 "\n\n");
		}


if (substr($a,0,18) eq "serviceescalation["){
		($c1,$c2)=split(/;/,$b2);
		($d1,$d2)= split(/-/,$a1);
		print(DEST8 "define serviceescalation{"."\n");
		print(DEST8 "\t"."host_name"."\t"."\t".$c1."\n");
		print(DEST8 "\t"."service_description"."\t".$c2."\n");
		print(DEST8 "\t"."first_notification"."\t".$d1."\n");
		print(DEST8 "\t"."last_notification"."\t".$d2."\n");
		print(DEST8 "\t"."contact_groups"."\t"."\t".$a2."\n");
		print(DEST8 "\t"."notification_interval"."\t".$a3."\n");
		print(DEST8 "\t"."}");
		print(DEST8 "\n\n");
		}

if (substr($a,0,20) eq "hostgroupescalation["){
                ($d1,$d2)= split(/-/,$a1);
		print(DEST8  "define hostgroupescalation{"."\n");
		print(DEST8  "\t"."hostgroup_name"."\t"."\t".$b2."\n");
		print(DEST8  "\t"."first_notification"."\t".$d1."\n");
		print(DEST8  "\t"."last_notification"."\t".$d2."\n");
		print(DEST8  "\t"."contact_groups"."\t"."\t".$a2."\n");
		print(DEST8  "\t"."notification_interval"."\t".$a3."\n");
		print(DEST8  "\t"."}");
		print(DEST8  "\n\n");
		}




}

foreach $linha (@source2) {
chomp;

($a,$b)= split(/\]=/,$linha);
($b1,$b2)=split(/\[/,$a);
($a1,$a2,$a3,$a4,$a5,$a6,$a7,$a8,$a9,$a10,$a11,$a12,$a13,$a14,$a15)= split(/;/,$b);


if (substr($a,0,8) eq "command["){
		print(DEST9 "define command{"."\n");
		print(DEST9 "\t"."command_name"."\t".$b2."\n");
		print(DEST9 "\t"."command_line"."\t".$b."\n");
		print(DEST9 "\t"."}");
		print(DEST9 "\n\n");
		}
}


close(DEST1);
close(DEST2);
close(DEST3);
close(DEST4);
close(DEST5);
close(DEST6);
close(DEST7);
close(DEST8);
close(DEST9);


More information about the Users mailing list