Skip to content

Instantly share code, notes, and snippets.

@openfirmware
Created October 13, 2011 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save openfirmware/1285476 to your computer and use it in GitHub Desktop.
Save openfirmware/1285476 to your computer and use it in GitHub Desktop.
Nagios check rsnapshot
#!/usr/bin/env ruby
#
# Nagios check for rsnapshot activity and errors
# Author: James Badger <jamesbadger@gmail.com>
#
# == Documentation
#
# This script will check rsnapshot's log file to determine if it is actively
# running and that it is running without errors.
#
# Will return a critical error if:
# * The rsnapshot log file doesn't exist
# * The rsnapshot log file hasn't been updated in over CRIT_TIME seconds
# * The rsnapshot log file has reported an error since the last run began
#
# Will return a warning error if:
# * The rsnapshot log file hasn't been updated in over WARN_TIME seconds, but
# less than CRIT_TIME seconds
# * The rsnapshot log file could not be opened for reading
#
# Else, it will return an OK message.
#
# === Assumptions
#
# Assumes rsnapshot log_file has been setup. Assumes you are running rsnapshot
# every 4 hours (for hourly snapshots). Assumes rsnapshot log location. Assumes
# rsnapshot log file format will use "rsnapshot [interval]: started" to mark
# when a rsnapshot run was initiated, and will use "ERROR" when it encounters
# a fatal error.
#
# Copyright 2011, GeoSensor Web Lab
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'time'
require 'date'
EXIT_OK = 0
EXIT_WARNING = 1
EXIT_CRITICAL = 2
EXIT_UNKNOWN = 3
LOG_FILE = "/var/log/rsnapshot/rsnapshot"
CRIT_TIME = 6 * 3600
WARN_TIME = 4 * 3600
# Check rsnapshot log existence
unless File.exist?(LOG_FILE)
puts "RSNAPSHOT CRITICAL: Log file not found"
exit(EXIT_CRITICAL)
end
# Check rsnapshot log activity
mtime = File.mtime(LOG_FILE)
if mtime < Time.now - CRIT_TIME
puts "RSNAPSHOT CRITICAL: Log file not touched in > 6 hours"
exit(EXIT_CRITICAL)
elsif mtime < Time.now - WARN_TIME
puts "RSNAPSHOT WARNING: Log file not touched in > 4 hours"
exit(EXIT_WARNING)
end
# Check rsnapshot log errors
if File.readable?(LOG_FILE)
log = IO.read(LOG_FILE).split("\n").reverse
last_run_line = log.find_index { |l| l =~ /rsnapshot ([^)]+): started/ }
last_error_line = log.find_index { |l| l =~ /ERROR/ }
if last_error_line && (last_error_line < last_run_line)
puts "RSNAPSHOT CRITICAL: Last run had errors"
exit(EXIT_CRITICAL)
end
else
puts "RSNAPSHOT WARNING: Log file is unreadable"
exit(EXIT_WARNING)
end
puts "RSNAPSHOT OK"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment