Authored by root

Add emr scanner

... ... @@ -15,7 +15,7 @@ class EMRClusterScanner(object):
def __init__(self,interval,secretId='',secretKey=''):
self.interval = int(interval) if int(interval)> 30 else 30 #扫描时间间隔必须大于30秒,避免浪费cpu资源以及api访问过于频繁 单位:秒
if not secretId or not secretKey:
self.secretId,self.secretKey = open('~/.qcloud_config').read().strip('\n').split('\n')
self.secretId,self.secretKey = open('/home/txyang/.qcloud_config').read().strip('\n').split('\n')
else:
self.secretId,self.secretKey = secretId,secretKey
... ... @@ -27,7 +27,7 @@ class EMRClusterScanner(object):
def EmrDescribeCluster(self,PageNo=1):
#获取所有EMR集群数据
#默认获取一页数据,每页最多显示20条集群信息
client = QcloudApi(secretId,secretKey)
client = QcloudApi(self.secretId,self.secretKey)
params = {
"Action":"EmrDescribeCluster",
"PageNo": PageNo,
... ... @@ -102,15 +102,16 @@ class EMRClusterScanner(object):
return hostsEntry,hostsInFile
def gitUpdate(self):
cmd = 'cd /opt/projects/yoho-ansible-roles/;git add {0};git commit -m "Update bigdata inventory file"; git push;sleep(2)'.format(self.inventoryFile)
cmd = 'cd /opt/projects/yoho-ansible-roles/;git pull;git add {0};git commit -m "Update bigdata inventory file"; git push'.format(self.inventoryFile)
os.popen(cmd)
time.sleep(20)
return True
def updateEMRInventory(self):
#比对当前与最新的Inventory ip列表,相同则pass不同则更新
today = datetime.date.today().strftime('%Y%m%d')
hostsData = open(self.inventoryFile).read()
hostsEntry,hostsInFile = getHostsInFile(hostsData)
hostsEntry,hostsInFile = self.getHostsInFile(hostsData)
currentNodes = self.getCurrentNodes()
if set(currentNodes) == set(hostsInFile):
print 'Inventory already up to date'
... ... @@ -136,12 +137,12 @@ class EMRClusterScanner(object):
try:
result = self.updateEMRInventory()
if result:
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 更新结果')
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 更新结果')
else:
pass
except Exception as e:
print e
self.mailman.mail(rceivers=self.receivers,Content='EMR大数据Inventory更新失败\n{0}\n5分钟后重试'.format(str(e)),Title='EMR Inventory 更新结果')
self.mailman.mail(receivers=self.receivers,Content='EMR大数据Inventory更新失败\n{0}\n5分钟后重试'.format(str(e)),Title='EMR Inventory 更新结果')
time.sleep(300)
pass
time.sleep(self.interval)
... ... @@ -149,6 +150,6 @@ class EMRClusterScanner(object):
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('interval',type=int,help='Checks interval in seconds')
parser.add_argument('--interval',type=int,help='Checks interval in seconds')
args = parser.parse_args()
EMRClusterScanner(args.interval).start_daemon()
... ...
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
class mailman(object):
def __init__(self):
self.sender = 'robot@yohoops.cn'
self.smtpObj = smtplib.SMTP('localhost')
def mail(self,receivers=[],Content='Test Mail',From ='Robot',To='YOHOOPS',Title='Test Mail'):
message = MIMEText(Content, 'plain', 'utf-8')
message['From'] = Header(From, 'utf-8')
message['To'] = Header(To, 'utf-8')
message['Subject'] = Header(Title, 'utf-8')
try:
self.smtpObj.sendmail(self.sender, receivers, message.as_string())
print "邮件发送成功"
except smtplib.SMTPException:
print "Error: 无法发送邮件"
return 0
... ...