Skip to content

Commit c388de3

Browse files
committed
add check-mtime-follow-symlink.sh
1 parent bd8e9c7 commit c388de3

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

check-mtime-follow-symlink.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 follow it and return mtime/age/size of actual file.
27+
#
28+
# Returns 3 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+
#
33+
# The following is an example 'criteria' for a Rackspace Monitoring Alarm:
34+
#
35+
# if (metric['age'] > 3600) {
36+
# return new AlarmStatus(CRITICAL, 'The file has not been modified in more than 1 hour. Last modified #{age} seconds ago');
37+
# }
38+
# return new AlarmStatus(OK, 'The file was last modified #{age} seconds ago.');
39+
#
40+
file=$1
41+
42+
if [ ! -e $file ]; then
43+
echo "status critical \"$file\" does not exist"
44+
exit 1
45+
fi
46+
47+
if [ ! -r $file ]; then
48+
echo "status critical \"$file\" is not readable"
49+
exit 1
50+
fi
51+
52+
if [ -h $file ]; then
53+
file=$(realpath $file)
54+
fi
55+
56+
mtime=$(stat -c%Y $file)
57+
now=$(date '+%s')
58+
age=$(( $now - $mtime ))
59+
size=$(stat -c%s $file)
60+
61+
echo "status ok file statted"
62+
echo "metric mtime uint64 $mtime"
63+
echo "metric age uint64 $age seconds"
64+
echo "metric size uint64 $size"
65+
exit 0
66+

0 commit comments

Comments
 (0)