|
1 #! /usr/bin/perl |
|
2 |
|
3 use strict; |
|
4 use warnings; |
|
5 use feature qw(say); |
|
6 use Nagios::Check::DNS::check_tlsa_record; |
|
7 use File::Basename; |
|
8 use Monitoring::Plugin; |
|
9 |
|
10 my $ME = basename $0; |
|
11 my $blurb = 'This Plugin is intendet to check validity of TLSA Record'; |
|
12 my $url = 'http://www.schlittermann.de'; |
|
13 my $author = 'Heike Yvonne Pesch'; |
|
14 my $email = '<pesch@schlittermann.de>'; |
|
15 my $usage = 'Usage: %s [ -v|--verbose ] [-H <host>] [-t <timeout>] ' |
|
16 . '[ -c|--critical=<critical threshold> ] ' |
|
17 . '[ -w|--warning=<warning threshold> ] ' |
|
18 . '[ -p|--port=<portnumber> ] ' |
|
19 . '[ -q|--queryserver=<DNS-Server-IP> ] '; |
|
20 my $extra = <<_; |
|
21 |
|
22 NOTICE |
|
23 If you want to use a Hostlist, you have to put entrys like this: |
|
24 |
|
25 host |
|
26 host:port |
|
27 |
|
28 |
|
29 EXAMPLES |
|
30 $ME -H ssl.schlittermann.de |
|
31 $ME -H hh.schlittermann.de -p25 |
|
32 $ME -H hh.schlittermann.de:25 |
|
33 $ME -f hostlist.txt |
|
34 |
|
35 Author: $author $email |
|
36 For more information visit $url |
|
37 _ |
|
38 |
|
39 |
|
40 my $nagios_tlsa = Monitoring::Plugin->new( |
|
41 usage => $usage, |
|
42 blurb => $blurb, |
|
43 extra => $extra, |
|
44 url => $url, |
|
45 plugin => $ME, |
|
46 timeout => 120, |
|
47 |
|
48 ); |
|
49 $nagios_tlsa->add_arg( |
|
50 spec => 'host|H=s', |
|
51 help => q|Host/Domain to check|, |
|
52 required => 0, |
|
53 ); |
|
54 |
|
55 $nagios_tlsa->add_arg( |
|
56 spec => 'hostlist|f=s', |
|
57 help => q|Host/Domainlist in file to check|, |
|
58 required => 0, |
|
59 ); |
|
60 |
|
61 $nagios_tlsa->add_arg( |
|
62 spec => 'expiry|e', |
|
63 help => q|check expiry of Certificate|, |
|
64 required => 0, |
|
65 ); |
|
66 |
|
67 $nagios_tlsa->add_arg( |
|
68 spec => 'port|p=i', |
|
69 help => q|Port of Domain to check the TLSA (default: 443)|, |
|
70 required => 0, |
|
71 default => 443, |
|
72 ); |
|
73 |
|
74 $nagios_tlsa->add_arg( |
|
75 spec => 'queryserver|q=s', |
|
76 required => 0, |
|
77 help => |
|
78 q|DNS Server to ask to check the TLSA (default: defined in resolve.conf)|, |
|
79 |
|
80 ); |
|
81 |
|
82 $nagios_tlsa->add_arg( |
|
83 spec => 'protocol|P=s', |
|
84 help => q|Protocol to ask to check the TLSA record of domain (default: tcp)|, |
|
85 required => 0, |
|
86 default => 'tcp', |
|
87 ); |
|
88 |
|
89 $nagios_tlsa->getopts; |
|
90 |
|
91 my $domain = $nagios_tlsa->opts->host; |
|
92 my $port = $nagios_tlsa->opts->port; |
|
93 my $protocol = $nagios_tlsa->opts->protocol; |
|
94 my $domainlist = $nagios_tlsa->opts->hostlist; |
|
95 my $expiry = $nagios_tlsa->opts->expiry; |
|
96 my $pattern = '^(?<domain>\S*\.[a-z]{2,4}?):{0,1}(?<port>[0-9]*$)'; |
|
97 |
|
98 |
|
99 if (!$domain && !$domainlist) { |
|
100 my $script = basename $0; |
|
101 my $excuse = "Please set -H <domain> or -f <domainlist>\n" |
|
102 . "For all options try $script --help"; |
|
103 |
|
104 say $excuse; |
|
105 exit 1; |
|
106 } |
|
107 |
|
108 if ($domainlist) |
|
109 { |
|
110 get_domains(); |
|
111 exit 0; |
|
112 } |
|
113 |
|
114 if ($domain) |
|
115 { |
|
116 |
|
117 my $pattern = '^(?<domain>\S*\.[a-z]{2,4}?):{1}(?<port>[0-9]+$)'; |
|
118 if ($domain =~ /$pattern/gi) |
|
119 { |
|
120 $domain = $+{domain}; |
|
121 $port = $+{port}; |
|
122 } |
|
123 |
|
124 if (!$port || $port eq '') |
|
125 { |
|
126 $port = 443; |
|
127 } |
|
128 |
|
129 if (!$protocol || $protocol ne 'tcp' || $protocol ne 'udp') |
|
130 { |
|
131 $protocol = 'tcp'; |
|
132 } |
|
133 |
|
134 my $return = Nagios::Check::DNS::check_tlsa_record::main(($domain, $port, $protocol)); |
|
135 say $return; |
|
136 } |
|
137 |
|
138 sub get_domains { |
|
139 open(my $filehandle, '<', $domainlist); |
|
140 |
|
141 while (<$filehandle>) { |
|
142 if (/$pattern/ig) { |
|
143 $domain = $+{domain}; |
|
144 |
|
145 if ("$+{port}" =~ /^\s*$/) { $port = '443'; } |
|
146 else { $port = $+{port}; } |
|
147 |
|
148 my $return = Nagios::Check::DNS::check_tlsa_record::main(($domain, $port)); |
|
149 say $return; |
|
150 } |
|
151 else { |
|
152 die "$domainlist has wrong or malformed content\n"; |
|
153 } |
|
154 |
|
155 } |
|
156 } |
|
157 |
|
158 # vim: ft=perl ts=2 sw=2 |