From 7674d4133f4feaa123d510d99e868f475ece8452 Mon Sep 17 00:00:00 2001 From: Meik Milevczik Date: Wed, 19 Jun 2019 13:27:09 +0200 Subject: [PATCH 1/3] Add file age check --- src/check_file_age.py | 82 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/check_file_age.py diff --git a/src/check_file_age.py b/src/check_file_age.py new file mode 100644 index 00000000..287bb455 --- /dev/null +++ b/src/check_file_age.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python3 +"""InnoGames Monitoring Plugins - File Age Check + +This script checks the age of a file and raises a warning or critical state +depending on the thresholds specified as arguments + +Copyright 2019 InnoGames GmbH +""" +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +from argparse import ArgumentParser +import os +import time + +def main(): + """Main entrypoint, performs check of file age""" + + args = get_parser().parse_args() + + if not args.path: + print('UNKNOWN - no path supplied') + exit(3) + + age = 0 + try: + age = time.time() - os.path.getmtime(args.path) + except OSError: + print('UNKNOWN - {} is not present or no permissions'.format( + args.path)) + exit(3) + + status = 'OK' + code = 0 + + if age > args.critical*60: + code = 2 + status = 'CRITICAL' + elif age > args.warning*60: + code = 1 + status = 'WARNING' + + print('{} - {} last changed {} minutes ago'.format( + status, args.path, round(age/60))) + exit(code) + + +def get_parser(): + """Get argument parser -> ArgumentParser""" + + parser = ArgumentParser() + + parser.add_argument('--path', '-p', help='path of file to check') + parser.add_argument( + '--warning', '-w', + help='warning threshold in minutes for file age', + default=30, type=int) + parser.add_argument( + '--critical', '-c', + help='critical threshold in minutes for file age', + default=60, type=int) + + return parser + + +if __name__ == '__main__': + main() From 0a7c121b9cdb6a5cb7a6db235a88237c225e13c5 Mon Sep 17 00:00:00 2001 From: Jon D <5537959+Morthy@users.noreply.github.com> Date: Tue, 3 Mar 2020 14:22:54 +0100 Subject: [PATCH 2/3] Add file age check --- src/check_file_age.py | 66 ++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/src/check_file_age.py b/src/check_file_age.py index 287bb455..578be854 100644 --- a/src/check_file_age.py +++ b/src/check_file_age.py @@ -1,10 +1,12 @@ #!/usr/bin/env python3 + + """InnoGames Monitoring Plugins - File Age Check This script checks the age of a file and raises a warning or critical state depending on the thresholds specified as arguments -Copyright 2019 InnoGames GmbH +Copyright 2020 InnoGames GmbH """ # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -28,21 +30,42 @@ import os import time + +def parse_args(): + """Get argument parser -> ArgumentParser""" + + parser = ArgumentParser() + + parser.add_argument( + '--path', '-p', + required=True, + help='path of file to check' + ) + parser.add_argument( + '--warning', '-w', + default=30, type=int, + help='warning threshold in minutes for file age' + ) + parser.add_argument( + '--critical', '-c', + default=60, type=int, + help='critical threshold in minutes for file age' + ) + + return parser.parse_args() + + def main(): """Main entrypoint, performs check of file age""" - args = get_parser().parse_args() - - if not args.path: - print('UNKNOWN - no path supplied') - exit(3) + args = parse_args() - age = 0 try: age = time.time() - os.path.getmtime(args.path) except OSError: - print('UNKNOWN - {} is not present or no permissions'.format( - args.path)) + print( + 'UNKNOWN - {} is not present or no permissions'.format(args.path) + ) exit(3) status = 'OK' @@ -55,28 +78,13 @@ def main(): code = 1 status = 'WARNING' - print('{} - {} last changed {} minutes ago'.format( - status, args.path, round(age/60))) + print( + '{} - {} last changed {} minutes ago'.format( + status, args.path, round(age/60) + ) + ) exit(code) -def get_parser(): - """Get argument parser -> ArgumentParser""" - - parser = ArgumentParser() - - parser.add_argument('--path', '-p', help='path of file to check') - parser.add_argument( - '--warning', '-w', - help='warning threshold in minutes for file age', - default=30, type=int) - parser.add_argument( - '--critical', '-c', - help='critical threshold in minutes for file age', - default=60, type=int) - - return parser - - if __name__ == '__main__': main() From c1e8a13a9a54d4e2a36cefd7f240548577041159 Mon Sep 17 00:00:00 2001 From: Jon Dawson <5537959+Morthy@users.noreply.github.com> Date: Tue, 3 Mar 2020 15:16:17 +0100 Subject: [PATCH 3/3] Add check for file age --- src/check_file_age.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/check_file_age.py b/src/check_file_age.py index 578be854..5b250d91 100644 --- a/src/check_file_age.py +++ b/src/check_file_age.py @@ -71,16 +71,16 @@ def main(): status = 'OK' code = 0 - if age > args.critical*60: + if age > args.critical * 60: code = 2 status = 'CRITICAL' - elif age > args.warning*60: + elif age > args.warning * 60: code = 1 status = 'WARNING' print( '{} - {} last changed {} minutes ago'.format( - status, args.path, round(age/60) + status, args.path, round(age / 60) ) ) exit(code)