-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcheck_s3_file.py
executable file
·78 lines (63 loc) · 2.29 KB
/
check_s3_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/python
import argparse
import logging
import time
import os
import re
from boto.s3.connection import S3Connection
from urlparse import urlparse
from sys import exit
from datetime import datetime
parser = argparse.ArgumentParser(description='This is a tool to check files in AWS S3. You can use it to check Age, contents, existence. Returns "FOUND-OK" if everything is fine. Gives error with reason otherwise. Requires python boto. (sudo pip install boto) ')
parser.add_argument('--url', help='path to s3 file (ex: s3://zabbix-ops/monitors/check_table_partition.txt)',required = True)
parser.add_argument('--regex', help='Simple regex to apply to file contents')
parser.add_argument('--ttl', help='File age in seconds')
parser.add_argument('--access_key', help='AWS Access key ID')
parser.add_argument('--secret_key', help='AWS Secret Password')
parser.add_argument('--aws_profile', help='AWS profile to use from ~/.aws/credentials file')
parser.add_argument('--debug', help='Enable debug mode, this will show you all the json-rpc calls and responses', action="store_true")
args = parser.parse_args()
if args.debug:
logging.basicConfig(level = logging.DEBUG, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
logger = logging.getLogger(__name__)
def main():
global args
tmpfile = '/tmp/.s3.tmp'
o = urlparse(args.url)
if args.debug:
print o
if args.access_key and args.secret_key:
conn = S3Connection(args.access_key, args.secret_key)
elif args.aws_profile:
conn = S3Connection(profile_name=args.aws_profile)
else:
conn = S3Connection()
bucket = conn.get_bucket(o.netloc)
s3_file = bucket.get_key(o.path)
s3_file.get_contents_to_filename(tmpfile)
if args.debug:
print s3_file
if args.ttl:
stats = os.stat(tmpfile)
n = int(time.time())
delta = int(n - stats.st_mtime)
if args.debug:
print "Delta =",delta,", ttl =",args.ttl
if int(args.ttl) < int(delta):
print 'Error: file too old'
exit(-1)
if args.regex:
mys3temp = open(tmpfile,"r")
fc = mys3temp.read()
m = re.search(args.regex,fc)
if args.debug:
print fc
if m:
pass
else:
print "Error: regex failed to match '%s'" % args.regex
exit(-2)
os.remove(tmpfile)
print "FOUND-OK"
if __name__ == '__main__':
main()