1 | #!/moira/bin/perl -Tw |
---|
2 | |
---|
3 | # $Id: nagios-cluster.gen,v 1.2 2009-06-17 14:45:33 jweiss Exp $ |
---|
4 | # The following exit codes are defined and MUST BE CONSISTENT with the |
---|
5 | # error codes the library uses: |
---|
6 | $MR_DBMS_ERR = 47836421; |
---|
7 | $MR_OCONFIG = 47836460; |
---|
8 | |
---|
9 | $outfile = '/moira/dcm/nagios-cluster.out'; |
---|
10 | |
---|
11 | use DBI; |
---|
12 | |
---|
13 | umask 022; |
---|
14 | open(OUT, ">$outfile") || exit $MR_OCONFIG; |
---|
15 | print OUT "# This File is automatically generated by Moira. Do not edit.\n\n"; |
---|
16 | |
---|
17 | |
---|
18 | $dbh = DBI->connect("dbi:Oracle:moira", "moira", "moira") |
---|
19 | || exit $MR_DBMS_ERR; |
---|
20 | |
---|
21 | |
---|
22 | $sth1 = $dbh->prepare("SELECT m.name FROM machine m, clusters c, mcmap p " . |
---|
23 | "WHERE m.status=1 AND c.name='cluster' AND " . |
---|
24 | "(m.mach_id=p.mach_id AND p.clu_id=c.clu_id)") |
---|
25 | || exit $MR_DBMS_ERR; |
---|
26 | $sth1->execute || exit $MR_DBMS_ERR; |
---|
27 | |
---|
28 | my %clusters; |
---|
29 | while (($name) = $sth1->fetchrow_array) { |
---|
30 | next if $name eq "[NONE]"; |
---|
31 | $name = lc($name); |
---|
32 | my $cluster = $name; |
---|
33 | $cluster =~ s/\..*$//; |
---|
34 | $cluster =~ s/\-[^\-]*$//; |
---|
35 | # print "$name\t$cluster\n"; |
---|
36 | push @$cluster, $name; |
---|
37 | $clusters{$cluster}++; |
---|
38 | |
---|
39 | |
---|
40 | print OUT <<"END"; |
---|
41 | define host{ |
---|
42 | host_name $name |
---|
43 | alias $name |
---|
44 | address $name |
---|
45 | contact_groups noone |
---|
46 | use generic-host |
---|
47 | } |
---|
48 | |
---|
49 | define service{ |
---|
50 | host_name $name |
---|
51 | contact_groups noone |
---|
52 | use ping-service |
---|
53 | } |
---|
54 | |
---|
55 | define service{ |
---|
56 | host_name $name |
---|
57 | contact_groups noone |
---|
58 | use no-ssh-service |
---|
59 | } |
---|
60 | |
---|
61 | define service{ |
---|
62 | host_name $name |
---|
63 | contact_groups noone |
---|
64 | use no-telnet-service |
---|
65 | } |
---|
66 | |
---|
67 | define service{ |
---|
68 | host_name $name |
---|
69 | contact_groups noone |
---|
70 | use no-ftp-service |
---|
71 | } |
---|
72 | |
---|
73 | define service{ |
---|
74 | host_name $name |
---|
75 | contact_groups noone |
---|
76 | use public-service |
---|
77 | } |
---|
78 | |
---|
79 | END |
---|
80 | } |
---|
81 | |
---|
82 | foreach $key (sort (keys %clusters)) { |
---|
83 | print OUT <<END; |
---|
84 | define hostgroup { |
---|
85 | hostgroup_name $key |
---|
86 | alias $key |
---|
87 | END |
---|
88 | print OUT "\tmembers\t\t"; |
---|
89 | $maybecomma = ""; |
---|
90 | foreach $machine (@$key) { |
---|
91 | print OUT "$maybecomma$machine"; |
---|
92 | $maybecomma = ","; |
---|
93 | } |
---|
94 | print OUT "\n\t}\n\n"; |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | print OUT "\n### Printers\n\n"; |
---|
99 | |
---|
100 | $sth0 = $dbh->prepare("SELECT UNIQUE m.name FROM machine m, printers p " . |
---|
101 | "WHERE (p.type = 'DORM' OR p.type ='CLUSTER') " . |
---|
102 | "AND p.mach_id = m.mach_id AND m.status=1") |
---|
103 | || exit $MR_DBMS_ERR; |
---|
104 | |
---|
105 | $sth0->execute || exit $MR_DBMS_ERR; |
---|
106 | |
---|
107 | while (($name) = $sth0->fetchrow_array) { |
---|
108 | next if $name eq "[NONE]"; |
---|
109 | $name = lc($name); |
---|
110 | push(@allprinters, $name); |
---|
111 | print OUT <<"END"; |
---|
112 | define host{ |
---|
113 | host_name $name |
---|
114 | alias $name |
---|
115 | address $name |
---|
116 | contact_groups noone |
---|
117 | use generic-host |
---|
118 | } |
---|
119 | |
---|
120 | define service{ |
---|
121 | host_name $name |
---|
122 | contact_groups noone |
---|
123 | use ping-service |
---|
124 | } |
---|
125 | |
---|
126 | define service{ |
---|
127 | host_name $name |
---|
128 | contact_groups noone |
---|
129 | use hpjd-service |
---|
130 | } |
---|
131 | |
---|
132 | END |
---|
133 | |
---|
134 | } |
---|
135 | |
---|
136 | print OUT <<END; |
---|
137 | define hostgroup{ |
---|
138 | hostgroup_name printers |
---|
139 | alias printers |
---|
140 | END |
---|
141 | |
---|
142 | print OUT "\tmembers\t\t"; |
---|
143 | |
---|
144 | $maybecomma = ""; |
---|
145 | foreach $printer (@allprinters) { |
---|
146 | print OUT "$maybecomma$printer"; |
---|
147 | $maybecomma = ","; |
---|
148 | } |
---|
149 | |
---|
150 | print OUT "\n\t}\n\n"; |
---|
151 | |
---|
152 | close(OUT); |
---|
153 | $dbh->disconnect; |
---|
154 | |
---|
155 | exit 0; |
---|