#!/usr/bin/ruby # # Copyright (c) Perforce Software, Inc., 1997-2015. All rights reserved # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE # SOFTWARE, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH # DAMAGE. # # # ## Support Disclaimer ## # # User contributed content on the Perforce Public Depot is not supported # by Perforce, although it may be supported by its author. This applies # to all contributions even those submitted by Perforce employees. # # ## Usage ## # # check_p4.rb [ -H hostname ] [ -p port_number ] [ -u p4user ] [ --ssl ] # [ -c critical_threshold_days ] [ -w warn_threshold_days ] require 'P4' require 'getoptlong' require 'date' ssl = '' host = nil port = nil user = 'root' critical = 0 warn = 0 trustfile='/var/lib/nagios/.p4trust' opts = GetoptLong.new( [ '-H', GetoptLong::REQUIRED_ARGUMENT ], [ '-p', GetoptLong::REQUIRED_ARGUMENT ], [ '-u', GetoptLong::REQUIRED_ARGUMENT ], [ '--ssl', GetoptLong::NO_ARGUMENT ], [ '-c', GetoptLong::OPTIONAL_ARGUMENT ], [ '-w', GetoptLong::OPTIONAL_ARGUMENT ] ) opts.each do |k,v| host = v if( k == '-H' ) port = v if( k == '-p' ) user = v if( k == '-u' ) ssl = 'ssl:' if( k == '--ssl' ) critical = v.to_i if( k == '-c' ) warn = v.to_i if( k == '-w' ) end # Set P4TRUST explicitly. The init script for the NRPE daemon # doesn't set $HOME so we can't depend on that. ENV[ 'P4TRUST'] = trustfile if !ENV.has_key?( 'P4TRUST' ) p4 = P4.new p4.port = "#{ssl}#{host}:#{port}" p4.user = user p4.debug = 0 p4.prog = 'check_p4(nagios)' p4.version = '$Change: 953917 $' begin p4.connect rescue P4Exception puts( "CRITICAL: Can't contact Perforce Server at #{p4.port}" ) exit( 2 ) end begin if( !ssl.empty? ) p4.run_trust('-y') end info = p4.run_info info = info.shift license = info[ 'serverLicense' ] licExpiry = nil if( license =~ /(support ends|expires) (\d+\/\d+\/\d+)/ ) licExpiry = Date.strptime( $2, '%Y/%m/%d' ) end certExpiry = nil info.keys.map{ |x| x if x.end_with?('CertExpire') }.compact.each do |tag| certExpiries = info[ tag ] if( !certExpiries.nil? ) expiry = DateTime.strptime( certExpiries, '%b %d %H:%M:%S %Y %Z' ) if( certExpiry.nil? || expiry < certExpiry ) certExpiry = expiry end end end rescue P4Exception puts( "CRITICAL: Can't get info from Perforce Server at #{p4.port}" ) exit( 2 ) end exitCode = 0 tags = [ 'OK', 'WARNING', 'CRITICAL' ] messages = [] if( licExpiry.nil? ) messages << "License does not expire" else diff = licExpiry - Date.today diff = diff.to_i if( diff < critical ) messages << "License expires in #{diff} days" exitCode = 2 elsif( diff < warn ) messages << "License expires in #{diff} days" exitCode = 1 else messages << "License expires in #{diff} days" end end if( !certExpiry.nil? ) diff = certExpiry - DateTime.now diff = diff.to_i puts diff if( diff < critical ) messages << "SSL certificate expires in #{diff} days" exitCode = 2 if exitCode < 2 elsif( diff < warn ) messages << "SSL certificate expires in #{diff} days" exitCode = 1 if exitCode < 1 else messages << "SSL certificate expires in #{diff} days" end end messages.each do |msg| puts "#{tags[exitCode]}: #{msg}" end exit( exitCode )