#!/usr/bin/perl use strict; use DBI; use lib "/usr/local/nagios"; use Host2; use Switch; my $dbserver1 = "dbserver1"; my $username1 = "test"; my $password1 = "test-pw"; my $db1 = "rechnerpool"; my $dbserver2 = "dbserver2"; my $username2 = "test"; my $password2 = "test-pc"; my $db2 = "switchpool"; my @dirs; $dirs[0] = "/etc/nagios/objects/hosts/linux/"; $dirs[1] = "/etc/nagios/objects/hosts/netview/"; $dirs[2] = "/etc/nagios/objects/hosts/switches/"; $dirs[3] = "/etc/nagios/objects/hosts/windows/"; my $temp = ""; my $net = ""; my $parents = ""; my $hostname = ""; my $i = 0; my %hosts = {}; my $dbh1 = DBI->connect("DBI:mysql:database=".$db1.";host=".$dbserver1, $username1, $password1); my $dbh2 = DBI->connect("DBI:mysql:database=".$db2.";host=".$dbserver2, $username2, $password2); my $mapdir = "/usr/local/nagios/share/nagvis/nagvis/etc/maps/"; foreach my $dir (@dirs) { open(DIR, "ls ".$dir."|"); while() { $parents = ""; $net = ""; $hostname = ""; $i = 0; # $_ hat den inhalt Rechner000.cfg $_ =~/([0-9a-zA-Z]*)\.*/; $hostname = $1; my $sth1 = $dbh1->prepare("SELECT netz FROM hosts WHERE hostname LIKE \"%".$hostname."%\""); $sth1->execute(); while(my $ref1 = $sth1->fetchrow_hashref()) { $net = $ref1->{klasse}; } $sth1->finish(); my $sth2 = $dbh2->prepare("SELECT hostns FROM swports WHERE system LIKE \"%".$hostname."%\" AND system NOT LIKE \"%DUMMY%\""); $sth2->execute(); while(my $ref2 = $sth2->fetchrow_hashref()) { if($i == 0) { $parents = $ref2->{hostns}; $i++; } else { $parents .= ",".$ref2->{hostns}; } } my $host = Host2->new($hostname, $net, $parents); #hash{key} = $value $hosts{$hostname} = $host; } close(DIR); } my $x = 0; my $y = 0; my @nets = {}; $nets[0] = "NetzA"; $nets[1] = "NetzB"; $nets[2] = "NetzC"; for(my $i=0; $i < $#nets; $i++) { $x = 0; $y = 0; open(FILE, ">".$mapdir.$nets[$i].".cfg"); print FILE "define global {\n"; print FILE "usegdlibs=1\n"; print FILE "allowed_user=nagios\n"; print FILE "allowed_for_config=nagios\n"; print FILE "iconset=std_medium\n"; print FILE "map_image=grey.png\n"; print FILE "}\n"; foreach $hostname (sort keys %hosts) { print $hostname."\n"; $temp = $hosts{$hostname}; if($temp->get("net") eq $nets[$i]) { print FILE "define host {\n"; print FILE "only_hard_states=1\n"; print FILE "recognize_services=1\n"; print FILE "backend_id=ndomy_1\n"; print FILE "host_name=".$temp->get("hostname")."\n"; print FILE "x=".$x."\n"; print FILE "y=".$y."\n"; print FILE "}\n"; $x+=40; if($x%400 == 0) { $y+=25; $x=0; } } } close(FILE); } system("chown wwwrun:www ".$mapdir."*"); system("chmod 0664 ".$mapdir."*"); $dbh1->disconnect(); $dbh2->disconnect();