Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import json
- import datetime
- import time
- from pprint import pprint
- import boto
- import MySQLdb
- from elasticsearch import Elasticsearch
- from elasticsearch.exceptions import ConnectionError, NotFoundError
- from botocore.exceptions import ClientError
- ignoredClients = ['merry', 'pippin', 'sam', 'frodo', 'bilbo', 'fm3', 'bvdemo', 'bvtraining', 'argos']
- body = {"sort": [{"content.collectedAt": {"order": "desc"}}],
- "query": {"filtered": {"query": {"match_all": {}},
- "filter": {"bool": {"must_not": {"term": {"content.channel": "custom"}}}}}}}
- try:
- es = Elasticsearch(
- ['internal-plloi-prod-fm3-2058221880.us-east-1.elb.amazonaws.com'],
- port=9200)
- # print "Connected", es.info()
- except Exception as ex:
- print "Error:", ex
- ec2 = boto.connect_ec2()
- rds = boto.connect_rds()
- def get_all_fm2_instances():
- instance_list = []
- filter = {"tag:fm2": "aio",'instance-state-name' : 'running'}
- try:
- instances = ec2.get_only_instances(filters=filter)
- except ClientError as e:
- print e
- for instance in instances:
- instance_list.append(instance.tags['Name'])
- return instance_list
- def get_clients_content_date():
- print "Getting last date of collected content"
- outdated_clients = dict()
- instance_list = get_all_fm2_instances()
- for client in instance_list:
- try:
- req = es.search(index='!curations:contentmetadata:' + client, size=1, body=body, request_timeout=240)
- json_res = json.dumps(req)
- content = json.loads(json_res)
- if content["hits"]["total"] > 0:
- last_date = content["hits"]["hits"][0]["_source"]["content"]["collectedAt"]
- yesterday = int(time.time()) - (24 * 60 * 60)
- seven_days = int(time.time()) - (24 * 60 * 60 * 7)
- if last_date < yesterday and last_date > seven_days and client not in ignoredClients:
- last_date_time = datetime.datetime.fromtimestamp(last_date).strftime('%Y-%m-%d')
- outdated_clients.setdefault('name', []).append(client)
- outdated_clients.setdefault('last_date_time', []).append(last_date_time)
- except NotFoundError:
- pass
- return outdated_clients
- def main():
- print "Creating clint's list of stalled collectors"
- outdated_clients = get_clients_content_date()
- for name, polloi_date in zip(outdated_clients['name'], outdated_clients['last_date_time']):
- rs = rds.get_all_dbinstances(instance_id=name + '-db')
- db_host = rs[0].endpoint[0]
- db = MySQLdb.connect(host=db_host, user="user", passwd="pass", db=name)
- cur = db.cursor()
- cur.execute("select created from content_update order by created desc limit 1")
- for row in cur.fetchall():
- db_content_date = datetime.datetime.strftime(row[0], "%Y-%m-%d")
- if polloi_date <= db_content_date:
- print name + ':' + polloi_date + ' ' + db_content_date
- db.close()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement