Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions src/check_file_age.py
Original file line number Diff line number Diff line change
@@ -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')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not making the argument required then?

exit(3)

age = 0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to neither explicitly initialise nor declare it. age will be available outside the try scope too.

try:
age = time.time() - os.path.getmtime(args.path)
Comment thread
Morthy marked this conversation as resolved.
except OSError:
print('UNKNOWN - {} is not present or no permissions'.format(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apply Multiline style code:

func(
    argument_1, argument_2,
    'some small string format'.format(...),
    'some bigger bigger bigger string format'
    .format(...)
    even_more_arguments, ...
)

Comment thread
Morthy marked this conversation as resolved.
Outdated
args.path))
exit(3)

status = 'OK'
code = 0

if age > args.critical*60:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8: Spaces around operators.

code = 2
status = 'CRITICAL'
elif age > args.warning*60:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8: Spaces around operators.

code = 1
status = 'WARNING'

print('{} - {} last changed {} minutes ago'.format(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apply Multiline style code:

func(
    argument_1, argument_2,
    'some small string format'.format(...),
    'some bigger bigger bigger string format'
    .format(...)
    even_more_arguments, ...
)

status, args.path, round(age/60)))
exit(code)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use sys.exit. quit and exit are rather used for interactive shells. You will also need to import sys for that.



def get_parser():
"""Get argument parser -> ArgumentParser"""

parser = ArgumentParser()

parser.add_argument('--path', '-p', help='path of file to check')
parser.add_argument(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apply Multiline style code:

func(
    argument_1, argument_2,
    'some small string format'.format(...),
    'some bigger bigger bigger string format'
    .format(...)
    even_more_arguments, ...
)

'--warning', '-w',
help='warning threshold in minutes for file age',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make help being the last argument standing alone.

default=30, type=int)
parser.add_argument(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apply Multiline style code:

func(
    argument_1, argument_2,
    'some small string format'.format(...),
    'some bigger bigger bigger string format'
    .format(...)
    even_more_arguments, ...
)

'--critical', '-c',
help='critical threshold in minutes for file age',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make help being the last argument standing alone.

default=60, type=int)

return parser


if __name__ == '__main__':
main()