OK denn kommt hier die volle Dröhnung ;)
das Problematische Script:
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/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(<DIR>)
{
$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();
die Klasse:
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
package Host2;
use Switch;
# Konstruktor
sub new
{
# Perl uebergibt zuert die Bezeichnung der Klasse
my $class = $_[0];
# aufbau eines assoziativen Arrays um Speicher zu Allokieren
my $this = {
hostname => $_[1],
net => $_[2],
parents => $_[3],
};
# this wird die Klasse zugeurdnete
bless $this, $class;
# gibt die Referenz auf this(Instanz) zurueck
return $this;
}
# fuegt eine Eigenschaft hinzu
sub set
{
# Referenz auf Instantz
my $this = shift;
# zu schriebende Variable
my $name = shift;
# Wert der Variable
my $value = shift;
# speichert Variable und Wert in der Instanz
$this->{$name} = $value;
}
# gibt eine Eigenschaft zurueck
sub get
{
# Referenz auf Instantz
my $this = shift;
# zu lesende Variable
my $name = shift;
# Rueckgabe des angefragten Werts
return $this->{$name};
}
1;
-----------
Wer Rechstschreibfehler findet, darf diese behalten und bei Ebay versteigern!!!
-----------
Nidar mied där Rächtschraibunk!!!