Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file age check #160

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
90 changes: 90 additions & 0 deletions src/check_file_age.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/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 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
# 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 parse_args():
"""Get argument parser -> ArgumentParser"""

parser = ArgumentParser()

parser.add_argument(
'--path', '-p',
required=True,
Comment on lines +40 to +41
Copy link
Contributor

Choose a reason for hiding this comment

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

Can be just:

Suggested change
'--path', '-p',
required=True,
'--path', '-p', required=True,

help='path of file to check'
)
parser.add_argument(
'--warning', '-w',
default=30, type=int,
Comment on lines +45 to +46
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
'--warning', '-w',
default=30, type=int,
'--warning', '-w', type=int, default=30,

help='warning threshold in minutes for file age'
)
parser.add_argument(
'--critical', '-c',
default=60, type=int,
Comment on lines +50 to +51
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
'--critical', '-c',
default=60, type=int,
'--critical', '-c', type=int, default=60,

help='critical threshold in minutes for file age'
)

return parser.parse_args()


def main():
"""Main entrypoint, performs check of file age"""

args = parse_args()

try:
age = time.time() - os.path.getmtime(args.path)
Morthy marked this conversation as resolved.
Show resolved Hide resolved
except OSError:
print(
'UNKNOWN - {} is not present or no permissions'.format(args.path)
)
Comment on lines +66 to +68
Copy link
Contributor

Choose a reason for hiding this comment

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

We rather go with this multiline style:

Suggested change
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'
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)
)
Comment on lines +82 to +84
Copy link
Contributor

Choose a reason for hiding this comment

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

Try if you like this way more:

Suggested change
'{} - {} last changed {} minutes ago'.format(
status, args.path, round(age / 60)
)
'{} - {} last changed {} minutes ago'
.format(status, args.path, round(age / 60))

)
exit(code)
Copy link
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.



if __name__ == '__main__':
main()