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