|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Rackspace Cloud Monitoring Plug-In |
| 4 | +# Check the mtime of a file and how long it has been since it has been modified |
| 5 | +# |
| 6 | +# (c) 2015 Justin Gallardo <[email protected]> |
| 7 | +# All Rights Reserved. |
| 8 | +# |
| 9 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | +# not use this file except in compliance with the License. |
| 11 | +# You may obtain a copy of the License at |
| 12 | +# |
| 13 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +# |
| 15 | +# Unless required by applicable law or agreed to in writing, software |
| 16 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | +# See the License for the specific language governing permissions and |
| 19 | +# limitations under the License. |
| 20 | +# |
| 21 | +# Usage: |
| 22 | +# Place plug-in in /usr/lib/rackspace-monitoring-agent/plugins |
| 23 | +# |
| 24 | +# It accepts one argument, which should be the file you wish to check the mtime of. |
| 25 | +# |
| 26 | +# If the file is a symlink, it will dereference it. |
| 27 | +# |
| 28 | +# Returns 4 metrics: |
| 29 | +# - mtime: The time(unix epoch) the file was last modified |
| 30 | +# - age: The number of seconds that have elapsed since the file was modified |
| 31 | +# - size: The number of bytes of the file |
| 32 | +# - type: The type of file |
| 33 | +# |
| 34 | +# The following is an example 'criteria' for a Rackspace Monitoring Alarm: |
| 35 | +# |
| 36 | +# if (metric['age'] > 3600) { |
| 37 | +# return new AlarmStatus(CRITICAL, 'The file has not been modified in more than 1 hour. Last modified #{age} seconds ago'); |
| 38 | +# } |
| 39 | +# return new AlarmStatus(OK, 'The file was last modified #{age} seconds ago.'); |
| 40 | +# |
| 41 | +file=$1 |
| 42 | + |
| 43 | +if [ ! -e $file ]; then |
| 44 | + echo "status critical \"$file\" does not exist" |
| 45 | + exit 1 |
| 46 | +fi |
| 47 | + |
| 48 | +if [ ! -r $file ]; then |
| 49 | + echo "status critical \"$file\" is not readable" |
| 50 | + exit 1 |
| 51 | +fi |
| 52 | + |
| 53 | +mtime=$(stat --dereference -c%Y $file) |
| 54 | +now=$(date '+%s') |
| 55 | +age=$(( $now - $mtime )) |
| 56 | +size=$(stat --dereference -c%s $file) |
| 57 | +file_type=$(file -bL $file) |
| 58 | + |
| 59 | +echo "status ok file statted" |
| 60 | +echo "metric mtime uint64 $mtime" |
| 61 | +echo "metric age uint64 $age seconds" |
| 62 | +echo "metric size uint64 $size" |
| 63 | +echo "metric type string $file_type" |
| 64 | +exit 0 |
| 65 | + |
0 commit comments