Created
April 15, 2016 17:46
-
-
Save meconlin/2e629e9cacc63169e44c53901c1820dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import sys | |
from datetime import datetime, timedelta | |
import boto | |
import yaml | |
DATEFORMAT = '%Y-%m-%dT%H:%M:%S.000Z' | |
if len(sys.argv) != 4: | |
print("Usage: {} bucket|level|s3appname".format(sys.argv[0])) | |
sys.exit(1) | |
def get_prefix(level, s3appname, actdate): | |
return '{0}/{1}/y={2:%Y}/m={2:%m}/d={2:%d}'.format( | |
level, s3appname, actdate) | |
def call(bucketname, level, s3appname): | |
c = boto.connect_s3() | |
yesterday_p = get_prefix(level, s3appname, | |
datetime.utcnow() - timedelta(days=1)) | |
today_p = get_prefix(level, s3appname, datetime.utcnow()) | |
bucket = c.get_bucket(bucketname).list(today_p) | |
latest = sorted(bucket, key=lambda x: x.last_modified, reverse=True) | |
if not latest: | |
bucket = c.get_bucket(bucketname).list(yesterday_p) | |
latest = sorted(bucket, | |
key=lambda x: x.last_modified, reverse=True) | |
if not latest: | |
raise Exception("No logs found on S3") | |
latestdate = latest[0].last_modified | |
latestdate = datetime.strptime(latestdate, DATEFORMAT) | |
diff = round((datetime.utcnow() - latestdate).seconds / 60, 2) | |
#print '{0} : {1} : {2}'.format(bucketname, latestdate, diff) | |
return diff | |
answer = call(sys.argv[1], sys.argv[2], sys.argv[3]) | |
print answer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment