bin/check_tlsa-record
changeset 12 c111a724d89c
parent 9 2041bac74e8d
child 13 198dd181ba37
equal deleted inserted replaced
10:1c9ab700c340 12:c111a724d89c
    10 my $ME      = basename $0;
    10 my $ME      = basename $0;
    11 my $blurb   = 'This Plugin is intendet to check validity of TLSA Record';
    11 my $blurb   = 'This Plugin is intendet to check validity of TLSA Record';
    12 my $url     = 'http://www.schlittermann.de';
    12 my $url     = 'http://www.schlittermann.de';
    13 my $author  = 'Heike Yvonne Pesch';
    13 my $author  = 'Heike Yvonne Pesch';
    14 my $email   = '<pesch@schlittermann.de>';
    14 my $email   = '<pesch@schlittermann.de>';
    15 my $usage   = 'Usage: %s [ -v|--verbose ] [-H <host>] [-t <timeout>] '
    15 my $usage   = <<_;
    16             . '[ -c|--critical=<critical threshold> ] '
    16 Usage: %s [ -v|--verbose ] -H <host> [-t <timeout>] 
    17             . '[ -w|--warning=<warning threshold> ] '
    17                          [ -f|--hostlist=<hostlist> ] 
    18             . '[ -p|--port=<portnumber> ] '
    18                          [ -c|--critical=<critical threshold> ] 
    19             . '[ -q|--queryserver=<DNS-Server-IP> ] ';
    19                          [ -w|--warning=<warning threshold> ] 
       
    20                          [ -p|--port=<portnumber> ] 
       
    21                          [ -q|--queryserver=<DNS-Server-IP> ] 
       
    22 _
       
    23 
    20 my $extra   = <<_;
    24 my $extra   = <<_;
    21 
    25 
    22 NOTICE
    26 NOTICE
    23 If you want to use a Hostlist, you have to put entrys like this:
    27 If you want to use a Hostlist, you have to put entrys like this:
    24 
    28 
    44   url     => $url,
    48   url     => $url,
    45   plugin  => $ME,
    49   plugin  => $ME,
    46   timeout => 120,
    50   timeout => 120,
    47 
    51 
    48 );
    52 );
       
    53 
       
    54 #@TODO exit 1 &Co in eigenes die || oh_shit
    49 $nagios_tlsa->add_arg(
    55 $nagios_tlsa->add_arg(
    50   spec     => 'host|H=s',
    56   spec     => 'host|H=s',
    51   help     => q|Host/Domain to check|,
    57   help     => 'Host/Domain to check',
    52   required => 0,
    58   required => 0,
    53 );
    59 );
    54 
    60 
    55 $nagios_tlsa->add_arg(
    61 $nagios_tlsa->add_arg(
    56   spec     => 'hostlist|f=s',
    62   spec     => 'hostlist|f=s',
    57   help     => q|Host/Domainlist in file to check|,
    63   help     => 'Host/Domainlist in file to check',
    58   required => 0,
    64   required => 0,
    59 );
    65 );
    60 
    66 
    61 $nagios_tlsa->add_arg(
    67 $nagios_tlsa->add_arg(
    62   spec     => 'expiry|e',
    68   spec     => 'expiry|e',
    63   help     => q|check expiry of Certificate|,
    69   help     => 'check expiry of Certificate',
    64   required => 0,
    70   required => 0,
    65 );
    71 );
    66 
    72 
    67 $nagios_tlsa->add_arg(
    73 $nagios_tlsa->add_arg(
    68   spec     => 'port|p=i',
    74   spec     => 'port|p=i',
    69   help     => q|Port of Domain to check the TLSA (default: 443)|,
    75   help     => 'Port of Domain to check the TLSA (default: 443)',
    70   required => 0,
    76   required => 0,
    71   default  => 443,
    77   default  => 443,
    72 );
    78 );
    73 
    79 
    74 $nagios_tlsa->add_arg(
    80 $nagios_tlsa->add_arg(
    75   spec     => 'queryserver|q=s',
    81   spec     => 'queryserver|q=s',
    76   required => 0,
    82   required => 0,
    77   help     =>
    83   help     =>
    78   q|DNS Server to ask to check the TLSA (default: defined in resolve.conf)|,
    84   'DNS Server to ask to check the TLSA (default: defined in resolve.conf)',
    79 
    85 
    80 );
    86 );
    81 
    87 
    82 $nagios_tlsa->add_arg(
    88 $nagios_tlsa->add_arg(
    83   spec     => 'protocol|P=s',
    89   spec     => 'protocol|P=s',
    84   help     => q|Protocol to ask to check the TLSA record of domain (default: tcp)|,
    90   help     => 'Protocol to ask to check the TLSA record of domain (default: tcp)',
    85   required => 0,
    91   required => 0,
    86   default  => 'tcp',
    92   default  => 'tcp',
       
    93 );
       
    94 
       
    95 $nagios_tlsa->add_arg(
       
    96   spec     => 'timeout|t=i',
       
    97   help     => 'Timeout in seconds for check (default: 120)',
       
    98   required => 0,
       
    99   default  => 120,
    87 );
   100 );
    88 
   101 
    89 $nagios_tlsa->getopts;
   102 $nagios_tlsa->getopts;
    90 
   103 
    91 my $domain     = $nagios_tlsa->opts->host;
   104 my $domain     = $nagios_tlsa->opts->host;
    92 my $port       = $nagios_tlsa->opts->port;
   105 my $port       = $nagios_tlsa->opts->port;
    93 my $protocol   = $nagios_tlsa->opts->protocol;
   106 my $protocol   = $nagios_tlsa->opts->protocol;
    94 my $domainlist = $nagios_tlsa->opts->hostlist;
   107 my $domainlist = $nagios_tlsa->opts->hostlist;
    95 my $expiry     = $nagios_tlsa->opts->expiry;
   108 my $expiry     = $nagios_tlsa->opts->expiry;
    96 my $pattern    = '^(?<domain>\S*\.[a-z]{2,4}?):{0,1}(?<port>[0-9]*$)';
       
    97 
       
    98 
   109 
    99 if (!$domain && !$domainlist) {
   110 if (!$domain && !$domainlist) {
   100     my $script = basename $0;
   111     my $script = basename $0;
   101     my $excuse = "Please set -H <domain> or -f <domainlist>\n"
   112     say "Please set -H <domain> or -f <domainlist>\n"
   102     . "For all options try $script --help";
   113     . "For all options try $script --help";
   103 
   114 
   104     say $excuse;
       
   105     exit 1;
   115     exit 1;
   106 }
   116 }
   107 
   117 
   108 if ($domainlist)
   118 if ($domainlist)
   109 {
   119 {
   110   get_domains();
   120   get_domains();
   111   exit 0;
   121   exit 0;
   112 }
   122 }
   113 
   123 
   114 if ($domain)
   124 if ($domain) {
   115 {
       
   116 
   125 
   117   my $pattern    = '^(?<domain>\S*\.[a-z]{2,4}?):{1}(?<port>[0-9]+$)';
   126   if ($domain =~ /^(?<domain>\S*\.[a-z]{2,4}?):{1}(?<port>[0-9]+$)/gi) {
   118   if ($domain =~ /$pattern/gi)
       
   119   {
       
   120     $domain = $+{domain};
   127     $domain = $+{domain};
   121     $port   = $+{port};
   128     $port   = $+{port};
   122   }
   129   }
   123 
   130 
   124   if (!$port || $port eq '')
   131   if (not $port or $port eq '') {
   125   {
       
   126     $port = 443;
   132     $port = 443;
   127   }
   133   }
   128 
   134 
   129   if (!$protocol || $protocol ne 'tcp' || $protocol ne 'udp')
   135   if (not $protocol or $protocol ne 'tcp' or $protocol ne 'udp') {
   130   {
       
   131     $protocol = 'tcp';
   136     $protocol = 'tcp';
   132   }
   137   }
   133 
   138 
   134   my $return = Nagios::Check::DNS::check_tlsa_record::main(($domain, $port, $protocol));
   139   my $return = Nagios::Check::DNS::check_tlsa_record::main(($domain, $port, $protocol));
   135   say $return;
   140   say $return;
   138 
   143 
   139 sub get_domains {
   144 sub get_domains {
   140     open(my $filehandle, '<', $domainlist);
   145     open(my $filehandle, '<', $domainlist);
   141 
   146 
   142     while (<$filehandle>) {
   147     while (<$filehandle>) {
   143         if (/$pattern/ig) {
   148         if (/^(?<domain>\S*\.[a-z]{2,4}?):{0,1}(?<port>[0-9]*$)/ig) {
   144             $domain = $+{domain};
   149             $domain = $+{domain};
   145 
   150 
   146             if ("$+{port}" =~ /^\s*$/) { $port = '443'; }
   151             if ("$+{port}" =~ /^\s*$/) { $port = '443'; }
   147             else { $port   = $+{port}; }
   152             else { $port   = $+{port}; }
   148 
   153