Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import json
  4. import datetime
  5. import time
  6. from pprint import pprint
  7. import boto
  8. import MySQLdb
  9. from elasticsearch import Elasticsearch
  10. from elasticsearch.exceptions import ConnectionError, NotFoundError
  11. from botocore.exceptions import ClientError
  12.  
  13. ignoredClients = ['merry', 'pippin', 'sam', 'frodo', 'bilbo', 'fm3', 'bvdemo', 'bvtraining', 'argos']
  14.  
  15. body = {"sort": [{"content.collectedAt": {"order": "desc"}}],
  16. "query": {"filtered": {"query": {"match_all": {}},
  17. "filter": {"bool": {"must_not": {"term": {"content.channel": "custom"}}}}}}}
  18.  
  19. try:
  20. es = Elasticsearch(
  21. ['internal-plloi-prod-fm3-2058221880.us-east-1.elb.amazonaws.com'],
  22. port=9200)
  23.  
  24. # print "Connected", es.info()
  25. except Exception as ex:
  26. print "Error:", ex
  27.  
  28. ec2 = boto.connect_ec2()
  29. rds = boto.connect_rds()
  30.  
  31.  
  32. def get_all_fm2_instances():
  33. instance_list = []
  34. filter = {"tag:fm2": "aio",'instance-state-name' : 'running'}
  35. try:
  36. instances = ec2.get_only_instances(filters=filter)
  37. except ClientError as e:
  38. print e
  39. for instance in instances:
  40. instance_list.append(instance.tags['Name'])
  41. return instance_list
  42.  
  43.  
  44. def get_clients_content_date():
  45. print "Getting last date of collected content"
  46. outdated_clients = dict()
  47. instance_list = get_all_fm2_instances()
  48. for client in instance_list:
  49. try:
  50. req = es.search(index='!curations:contentmetadata:' + client, size=1, body=body, request_timeout=240)
  51. json_res = json.dumps(req)
  52. content = json.loads(json_res)
  53. if content["hits"]["total"] > 0:
  54. last_date = content["hits"]["hits"][0]["_source"]["content"]["collectedAt"]
  55. yesterday = int(time.time()) - (24 * 60 * 60)
  56. seven_days = int(time.time()) - (24 * 60 * 60 * 7)
  57. if last_date < yesterday and last_date > seven_days and client not in ignoredClients:
  58. last_date_time = datetime.datetime.fromtimestamp(last_date).strftime('%Y-%m-%d')
  59. outdated_clients.setdefault('name', []).append(client)
  60. outdated_clients.setdefault('last_date_time', []).append(last_date_time)
  61. except NotFoundError:
  62. pass
  63. return outdated_clients
  64.  
  65.  
  66. def main():
  67. print "Creating clint's list of stalled collectors"
  68. outdated_clients = get_clients_content_date()
  69. for name, polloi_date in zip(outdated_clients['name'], outdated_clients['last_date_time']):
  70. rs = rds.get_all_dbinstances(instance_id=name + '-db')
  71. db_host = rs[0].endpoint[0]
  72. db = MySQLdb.connect(host=db_host, user="user", passwd="pass", db=name)
  73. cur = db.cursor()
  74. cur.execute("select created from content_update order by created desc limit 1")
  75. for row in cur.fetchall():
  76. db_content_date = datetime.datetime.strftime(row[0], "%Y-%m-%d")
  77. if polloi_date <= db_content_date:
  78. print name + ':' + polloi_date + ' ' + db_content_date
  79. db.close()
  80.  
  81.  
  82. if __name__ == '__main__':
  83. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement