Authored by root

Add emr scanner

@@ -15,7 +15,7 @@ class EMRClusterScanner(object): @@ -15,7 +15,7 @@ class EMRClusterScanner(object):
15 def __init__(self,interval,secretId='',secretKey=''): 15 def __init__(self,interval,secretId='',secretKey=''):
16 self.interval = int(interval) if int(interval)> 30 else 30 #扫描时间间隔必须大于30秒,避免浪费cpu资源以及api访问过于频繁 单位:秒 16 self.interval = int(interval) if int(interval)> 30 else 30 #扫描时间间隔必须大于30秒,避免浪费cpu资源以及api访问过于频繁 单位:秒
17 if not secretId or not secretKey: 17 if not secretId or not secretKey:
18 - self.secretId,self.secretKey = open('~/.qcloud_config').read().strip('\n').split('\n') 18 + self.secretId,self.secretKey = open('/home/txyang/.qcloud_config').read().strip('\n').split('\n')
19 else: 19 else:
20 self.secretId,self.secretKey = secretId,secretKey 20 self.secretId,self.secretKey = secretId,secretKey
21 21
@@ -27,7 +27,7 @@ class EMRClusterScanner(object): @@ -27,7 +27,7 @@ class EMRClusterScanner(object):
27 def EmrDescribeCluster(self,PageNo=1): 27 def EmrDescribeCluster(self,PageNo=1):
28 #获取所有EMR集群数据 28 #获取所有EMR集群数据
29 #默认获取一页数据,每页最多显示20条集群信息 29 #默认获取一页数据,每页最多显示20条集群信息
30 - client = QcloudApi(secretId,secretKey) 30 + client = QcloudApi(self.secretId,self.secretKey)
31 params = { 31 params = {
32 "Action":"EmrDescribeCluster", 32 "Action":"EmrDescribeCluster",
33 "PageNo": PageNo, 33 "PageNo": PageNo,
@@ -102,15 +102,16 @@ class EMRClusterScanner(object): @@ -102,15 +102,16 @@ class EMRClusterScanner(object):
102 return hostsEntry,hostsInFile 102 return hostsEntry,hostsInFile
103 103
104 def gitUpdate(self): 104 def gitUpdate(self):
105 - cmd = 'cd /opt/projects/yoho-ansible-roles/;git add {0};git commit -m "Update bigdata inventory file"; git push;sleep(2)'.format(self.inventoryFile) 105 + cmd = 'cd /opt/projects/yoho-ansible-roles/;git pull;git add {0};git commit -m "Update bigdata inventory file"; git push'.format(self.inventoryFile)
106 os.popen(cmd) 106 os.popen(cmd)
  107 + time.sleep(20)
107 return True 108 return True
108 109
109 def updateEMRInventory(self): 110 def updateEMRInventory(self):
110 #比对当前与最新的Inventory ip列表,相同则pass不同则更新 111 #比对当前与最新的Inventory ip列表,相同则pass不同则更新
111 today = datetime.date.today().strftime('%Y%m%d') 112 today = datetime.date.today().strftime('%Y%m%d')
112 hostsData = open(self.inventoryFile).read() 113 hostsData = open(self.inventoryFile).read()
113 - hostsEntry,hostsInFile = getHostsInFile(hostsData) 114 + hostsEntry,hostsInFile = self.getHostsInFile(hostsData)
114 currentNodes = self.getCurrentNodes() 115 currentNodes = self.getCurrentNodes()
115 if set(currentNodes) == set(hostsInFile): 116 if set(currentNodes) == set(hostsInFile):
116 print 'Inventory already up to date' 117 print 'Inventory already up to date'
@@ -136,12 +137,12 @@ class EMRClusterScanner(object): @@ -136,12 +137,12 @@ class EMRClusterScanner(object):
136 try: 137 try:
137 result = self.updateEMRInventory() 138 result = self.updateEMRInventory()
138 if result: 139 if result:
139 - self.mailman.mail(rceivers=self.receivers,Content='EMR大数据Inventory已更新\n{0}\n详见 http://git.yoho.cn/ops/yoho-ansible-roles/blob/master/inventories/bigdata/hosts'.format(result),Title='EMR Inventory 更新结果') 140 + self.mailman.mail(receivers=self.receivers,Content='EMR大数据Inventory已更新\n{0}\n详见 http://git.yoho.cn/ops/yoho-ansible-roles/blob/master/inventories/bigdata/hosts'.format(result),Title='EMR Inventory 更新结果')
140 else: 141 else:
141 pass 142 pass
142 except Exception as e: 143 except Exception as e:
143 print e 144 print e
144 - self.mailman.mail(rceivers=self.receivers,Content='EMR大数据Inventory更新失败\n{0}\n5分钟后重试'.format(str(e)),Title='EMR Inventory 更新结果') 145 + self.mailman.mail(receivers=self.receivers,Content='EMR大数据Inventory更新失败\n{0}\n5分钟后重试'.format(str(e)),Title='EMR Inventory 更新结果')
145 time.sleep(300) 146 time.sleep(300)
146 pass 147 pass
147 time.sleep(self.interval) 148 time.sleep(self.interval)
@@ -149,6 +150,6 @@ class EMRClusterScanner(object): @@ -149,6 +150,6 @@ class EMRClusterScanner(object):
149 150
150 if __name__ == '__main__': 151 if __name__ == '__main__':
151 parser = argparse.ArgumentParser() 152 parser = argparse.ArgumentParser()
152 - parser.add_argument('interval',type=int,help='Checks interval in seconds') 153 + parser.add_argument('--interval',type=int,help='Checks interval in seconds')
153 args = parser.parse_args() 154 args = parser.parse_args()
154 EMRClusterScanner(args.interval).start_daemon() 155 EMRClusterScanner(args.interval).start_daemon()
  1 +#!/usr/bin/python
  2 +# -*- coding: UTF-8 -*-
  3 +
  4 +import smtplib
  5 +from email.mime.text import MIMEText
  6 +from email.header import Header
  7 +
  8 +
  9 +class mailman(object):
  10 + def __init__(self):
  11 + self.sender = 'robot@yohoops.cn'
  12 + self.smtpObj = smtplib.SMTP('localhost')
  13 +
  14 + def mail(self,receivers=[],Content='Test Mail',From ='Robot',To='YOHOOPS',Title='Test Mail'):
  15 + message = MIMEText(Content, 'plain', 'utf-8')
  16 + message['From'] = Header(From, 'utf-8')
  17 + message['To'] = Header(To, 'utf-8')
  18 + message['Subject'] = Header(Title, 'utf-8')
  19 +
  20 + try:
  21 + self.smtpObj.sendmail(self.sender, receivers, message.as_string())
  22 + print "邮件发送成功"
  23 + except smtplib.SMTPException:
  24 + print "Error: 无法发送邮件"
  25 +
  26 + return 0