]>
Commit | Line | Data |
---|---|---|
522d1836 | 1 | #!/moira/bin/perl -Tw |
2 | ||
3 | # $Id$ | |
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 | $outdir = '/moira/dcm/nagios-wsh'; | |
10 | ||
11 | use DBI; | |
12 | ||
13 | $dbh = DBI->connect("dbi:Oracle:moira", "moira", "moira") | |
14 | || exit $MR_DBMS_ERR; | |
15 | ||
16 | $sth0 = $dbh->prepare("SELECT l.list_id, m.name " . | |
17 | "FROM list l, machine m, serverhosts sh " . | |
18 | "WHERE sh.value3 = l.name AND sh.service = " . | |
19 | "'NAGIOS-WSH' AND m.mach_id = sh.mach_id") | |
20 | || exit $MR_DBMS_ERR; | |
21 | $sth0->execute; | |
22 | ||
23 | while (($root_list_id, $hostname) = $sth0->fetchrow_array) { | |
24 | umask 022; | |
25 | open(OUT, ">$outdir/$hostname") || exit $MR_OCONFIG; | |
26 | print OUT "# This file is automatically generated by Moira. Do not edit.\n"; | |
c21d764b | 27 | $sth = $dbh->prepare("SELECT m.name, i.tag FROM machine m, imembers i " . |
522d1836 | 28 | "WHERE i.list_id = " . $dbh->quote($root_list_id) . |
29 | "AND i.member_type = 'MACHINE' AND m.status = 1 " . | |
30 | "AND i.member_id = m.mach_id AND i.direct = 1 ORDER BY m.name") | |
31 | || exit $MR_DBMS_ERR; | |
32 | $sth->execute; | |
33 | ||
c21d764b | 34 | while (($name, $tag) = $sth->fetchrow_array) { |
522d1836 | 35 | next if $name eq "[NONE]"; |
36 | $name = lc($name); | |
37 | push(@allwshhosts, $name); | |
38 | print OUT <<END; | |
39 | define host{ | |
40 | host_name $name | |
41 | alias $name | |
42 | address $name | |
43 | use generic-host | |
44 | } | |
45 | ||
46 | define service{ | |
47 | host_name $name | |
48 | contact_groups wsh | |
49 | use ping-service | |
50 | } | |
51 | ||
52 | define hostescalation{ | |
53 | host_name $name | |
54 | contact_groups wsh,wsh-mail | |
55 | first_notification 2 | |
56 | last_notification 0 | |
57 | notification_interval 0 | |
58 | } | |
59 | ||
60 | define serviceescalation{ | |
61 | host_name $name | |
62 | contact_groups wsh,wsh-mail | |
63 | service_description PING | |
64 | first_notification 2 | |
65 | last_notification 0 | |
66 | notification_interval 0 | |
67 | } | |
68 | ||
69 | END | |
70 | ||
c21d764b | 71 | next if $tag == 0; |
72 | $sth1 = $dbh->prepare("SELECT s.string " . | |
73 | "FROM strings s " . | |
74 | "WHERE s.string_id = " . $tag) | |
75 | || exit $MR_DBMS_ERR; | |
76 | $sth1->execute; | |
77 | my @tags = split /\s+/,$sth1->fetchrow_array; | |
78 | $sth1->finish; | |
79 | my %services; | |
80 | while (<@tags>) { | |
81 | my $service = $_; | |
82 | chomp $service; | |
83 | if ($service =~ /^(FTP|NFS|HTTP|HTTPS|SMTP|SSHD|TELNET|TNS)$/i) { | |
84 | my $ucservice = $service; | |
85 | $ucservice =~ tr/a-z/A-Z/; | |
86 | my $lcservice = $service; | |
87 | $lcservice =~ tr/A-Z/a-z/; | |
88 | $services{$ucservice}++; | |
89 | print OUT <<END; | |
90 | define service{ | |
91 | host_name $name | |
92 | contact_groups wsh | |
93 | use $lcservice-service | |
94 | } | |
95 | ||
96 | define serviceescalation{ | |
97 | host_name $name | |
98 | contact_groups wsh,wsh-mail | |
99 | service_description $ucservice | |
100 | first_notification 2 | |
101 | last_notification 0 | |
102 | notification_interval 0 | |
103 | } | |
104 | ||
105 | END | |
106 | } elsif ($service =~ /^HTTPS-CERT$/i) { | |
107 | $services{'HTTPS-CERT'}++; | |
108 | print OUT <<END; | |
109 | define service{ | |
110 | host_name $name | |
111 | contact_groups wsh-mail | |
112 | use https-cert-service | |
113 | } | |
114 | ||
115 | END | |
e5ac2c18 | 116 | } else { # Hopefully this is a URL of some sort |
117 | my $protocol; | |
118 | my $server; | |
119 | my $path; | |
120 | if ($service =~ /^([a-z]*):\/\/([^\/]*)(.*)$/ ) { | |
121 | $protocol = $1; | |
122 | $server = $2; | |
123 | $path = $3; | |
124 | $protocol =~ tr/a-z/A-Z/; | |
125 | $server =~ tr/A-Z/a-z/; | |
126 | } | |
127 | if ($protocol =~ /^HTTP(|S)$/) { | |
128 | $path = "/" if ($path eq ""); | |
129 | if ($server eq $name && $path eq "/") { # this is a simple service | |
130 | push @tags,$protocol; | |
131 | } | |
132 | # prep a bunch of variables for the text we'll spit out | |
133 | my $description = "$protocol-$server-$path"; | |
134 | $description =~ s/-\//-/g; | |
135 | $description =~ s/\/-/-/g; | |
136 | $description =~ s/\//-/g; | |
137 | $description =~ s/-$//g; | |
138 | $description =~ tr/A-Z/a-z/; | |
139 | my $ucdescription = $description; | |
140 | $ucdescription =~ tr/a-z/A-Z/; | |
141 | my $SSL = ""; | |
142 | if ($protocol eq "HTTPS") { | |
143 | $SSL = "--SSL -k /var/nagios/etc/cert.pem"; | |
144 | } | |
145 | print OUT <<END; | |
146 | define command{ | |
147 | command_name check_$name-$description | |
148 | command_line \$USER1\$/check_http -H $server $SSL -u $path | |
149 | } | |
150 | ||
151 | define service{ | |
152 | host_name $name | |
153 | name $description-service | |
154 | service_description $ucdescription | |
155 | contact_groups wsh | |
156 | check_command check_$name-$description | |
157 | use generic-service | |
158 | } | |
159 | ||
160 | define serviceescalation{ | |
161 | host_name $name | |
162 | contact_groups wsh,wsh-mail | |
163 | service_description $ucdescription | |
164 | first_notification 2 | |
165 | last_notification 0 | |
166 | notification_interval 0 | |
167 | } | |
168 | ||
169 | END | |
170 | if ($services{$protocol}) { | |
171 | print OUT <<END; | |
172 | define servicedependency{ | |
173 | dependent_host_name $name | |
174 | dependent_service_description $ucdescription | |
175 | host_name $name | |
176 | service_description $protocol | |
177 | execution_failure_criteria n | |
178 | notification_failure_criteria w,u,c | |
179 | } | |
180 | ||
181 | END | |
182 | } | |
183 | } else { | |
184 | printf STDERR "Machine %s has unknown service %s; ignoring\n", $name, $service; | |
185 | } | |
186 | } # if SERVICE else URL | |
c21d764b | 187 | |
188 | if ($services{'HTTPS'} && $services{'HTTPS-CERT'}) { | |
189 | print OUT <<END; | |
190 | define servicedependency{ | |
191 | dependent_host_name $name | |
192 | dependent_service_description HTTPS-CERT | |
193 | host_name $name | |
194 | service_description HTTPS | |
195 | execution_failure_criteria n | |
196 | notification_failure_criteria w,u,c | |
197 | } | |
198 | ||
199 | END | |
200 | } | |
201 | } | |
202 | } # while $sth->fetchrow_array | |
522d1836 | 203 | |
204 | print OUT <<END; | |
205 | define hostgroup{ | |
206 | hostgroup_name wsh-hosts | |
207 | alias wsh-hosts | |
208 | contact_groups wsh | |
209 | END | |
210 | ||
211 | print OUT "\tmembers\t\t\t"; | |
212 | ||
213 | $maybecomma = ""; | |
214 | foreach $host (@allwshhosts) { | |
215 | print OUT "$maybecomma$host"; | |
216 | $maybecomma = ","; | |
217 | } | |
218 | ||
219 | print OUT "\n\t}\n\n"; | |
220 | ||
221 | close(OUT); | |
222 | } | |
223 | ||
224 | $dbh->disconnect; | |
225 | ||
226 | exit 0; | |
227 | ||
228 |