Showing
1 changed file
with
47 additions
and
37 deletions
@@ -21,7 +21,13 @@ class EMRClusterScanner(object): | @@ -21,7 +21,13 @@ class EMRClusterScanner(object): | ||
21 | 21 | ||
22 | self.inventoryFile = '/opt/projects/yoho-ansible-roles/inventories/bigdata/hosts' | 22 | self.inventoryFile = '/opt/projects/yoho-ansible-roles/inventories/bigdata/hosts' |
23 | self.mailman = mailman() | 23 | self.mailman = mailman() |
24 | - self.receivers = ['tiexin.yang@yoho.cn'] | 24 | + self.receivers = ['tiexin.yang@yoho.cn','chunhua.zhang@yoho.cn'] |
25 | + self.emrNameMap = { | ||
26 | + "emr-rt": "emr-r6bhtb5v", | ||
27 | + "emr-ops": "emr-iaeloyc2", | ||
28 | + "emr-dw": "emr-r6bhtb5v", | ||
29 | + "emr-recom": "emr-ilj72ynu" | ||
30 | + } | ||
25 | 31 | ||
26 | 32 | ||
27 | def EmrDescribeCluster(self,PageNo=1): | 33 | def EmrDescribeCluster(self,PageNo=1): |
@@ -50,27 +56,25 @@ class EMRClusterScanner(object): | @@ -50,27 +56,25 @@ class EMRClusterScanner(object): | ||
50 | result = client.do_query(params,"emr.api.qcloud.com/v2/index.php") | 56 | result = client.do_query(params,"emr.api.qcloud.com/v2/index.php") |
51 | return result | 57 | return result |
52 | 58 | ||
53 | - def getCurrentNodes(self): | 59 | + def getCurrentNodes(self,clusterId): |
54 | #获取当前所有EMR集群下的所有节点(不区分节点名称) | 60 | #获取当前所有EMR集群下的所有节点(不区分节点名称) |
55 | - clusterList = self.getCurrentEmrs() | 61 | + #clusterList = self.getCurrentEmrs() |
56 | emrNodeIps = [] | 62 | emrNodeIps = [] |
57 | nodesCnt = 0 | 63 | nodesCnt = 0 |
58 | PageNo = 1 | 64 | PageNo = 1 |
59 | - for clusterId in clusterList: | ||
60 | - while True: | ||
61 | - print 'Getting nodes from',clusterId,'page',PageNo | ||
62 | - result = self.EmrDescribeClusterNode(clusterId,"all",PageNo) | ||
63 | - nodeList = result['data']['nodeList'] | ||
64 | - for node in nodeList: | ||
65 | - emrNodeIps.append(node['ip']) | ||
66 | - nodesCnt += len(nodeList) | ||
67 | - totalCnt = result['data']['totalCnt'] | ||
68 | - if nodesCnt < totalCnt: | ||
69 | - PageNo+=1 | ||
70 | - continue | ||
71 | - else: | ||
72 | - break | ||
73 | - PageNo = 1 | 65 | + while True: |
66 | + print 'Getting nodes from',clusterId,'page',PageNo | ||
67 | + result = self.EmrDescribeClusterNode(clusterId,"all",PageNo) | ||
68 | + nodeList = result['data']['nodeList'] | ||
69 | + for node in nodeList: | ||
70 | + emrNodeIps.append(node['ip']) | ||
71 | + nodesCnt += len(nodeList) | ||
72 | + totalCnt = result['data']['totalCnt'] | ||
73 | + if nodesCnt < totalCnt: | ||
74 | + PageNo+=1 | ||
75 | + continue | ||
76 | + else: | ||
77 | + break | ||
74 | return emrNodeIps | 78 | return emrNodeIps |
75 | 79 | ||
76 | 80 | ||
@@ -94,11 +98,14 @@ class EMRClusterScanner(object): | @@ -94,11 +98,14 @@ class EMRClusterScanner(object): | ||
94 | return emrClusterList | 98 | return emrClusterList |
95 | 99 | ||
96 | 100 | ||
97 | - def getHostsInFile(self,data): | 101 | + def getHostsInFile(self,data,prefix): |
98 | #读取当前的Inventory信息,截取emr-recom下的所有ip数据用于和最新采集的ip列表进行匹配 | 102 | #读取当前的Inventory信息,截取emr-recom下的所有ip数据用于和最新采集的ip列表进行匹配 |
99 | - rex = re.compile(r'\[emr-recom\]([\d+\.\d+\.\d+\.\d+\s*]*)',re.S) | ||
100 | - hostsEntry = rex.findall(data)[0].strip('\n') | ||
101 | - hostsInFile = hostsEntry.split('\n') | 103 | + print 'prefix:',prefix |
104 | + rexContent = re.compile(r'(\[{0}\][\d+\.\d+\.\d+\.\d+\s*]*)'.format(prefix),re.S) | ||
105 | + rexIp = re.compile(r'(\d+\.\d+\.\d+\.\d+)') | ||
106 | + content = rexContent.findall(data) | ||
107 | + hostsEntry = content[0].strip('\n') | ||
108 | + hostsInFile = rexIp.findall(hostsEntry) | ||
102 | return hostsEntry,hostsInFile | 109 | return hostsEntry,hostsInFile |
103 | 110 | ||
104 | def gitUpdate(self): | 111 | def gitUpdate(self): |
@@ -106,22 +113,24 @@ class EMRClusterScanner(object): | @@ -106,22 +113,24 @@ class EMRClusterScanner(object): | ||
106 | os.popen(cmd) | 113 | os.popen(cmd) |
107 | return True | 114 | return True |
108 | 115 | ||
109 | - def updateEMRInventory(self): | 116 | + def updateEMRInventory(self, emrName): |
110 | #比对当前与最新的Inventory ip列表,相同则pass不同则更新 | 117 | #比对当前与最新的Inventory ip列表,相同则pass不同则更新 |
111 | today = datetime.date.today().strftime('%Y%m%d') | 118 | today = datetime.date.today().strftime('%Y%m%d') |
112 | hostsData = open(self.inventoryFile).read() | 119 | hostsData = open(self.inventoryFile).read() |
113 | - hostsEntry,hostsInFile = self.getHostsInFile(hostsData) | ||
114 | - currentNodes = self.getCurrentNodes() | 120 | + hostsEntry,hostsInFile = self.getHostsInFile(hostsData,prefix=emrName) |
121 | + currentNodes = self.getCurrentNodes(clusterId=self.emrNameMap[emrName]) | ||
115 | if set(currentNodes) == set(hostsInFile): | 122 | if set(currentNodes) == set(hostsInFile): |
116 | print 'Inventory already up to date' | 123 | print 'Inventory already up to date' |
117 | return 0 | 124 | return 0 |
118 | else: | 125 | else: |
126 | + print '\n'.join(currentNodes) | ||
119 | N_toAdd = len(set(currentNodes) - set(hostsInFile)) | 127 | N_toAdd = len(set(currentNodes) - set(hostsInFile)) |
120 | N_toDel = len(set(hostsInFile) - set(currentNodes)) | 128 | N_toDel = len(set(hostsInFile) - set(currentNodes)) |
129 | + print '{0} to add,{1} to del'.format(N_toAdd,N_toDel) | ||
121 | print 'Taking backup for current inventory file...' | 130 | print 'Taking backup for current inventory file...' |
122 | shutil.copyfile(self.inventoryFile,self.inventoryFile+'.'+today) | 131 | shutil.copyfile(self.inventoryFile,self.inventoryFile+'.'+today) |
123 | print 'Replacing inventory file content...' | 132 | print 'Replacing inventory file content...' |
124 | - newInventoryContent = hostsData.replace(hostsEntry,'\n'.join(list(set(currentNodes)))) | 133 | + newInventoryContent = hostsData.replace(hostsEntry,'[{0}]\n'.format(emrName)+'\n'.join(list(set(currentNodes)))) |
125 | with open(self.inventoryFile,'w') as f: | 134 | with open(self.inventoryFile,'w') as f: |
126 | f.write(newInventoryContent) | 135 | f.write(newInventoryContent) |
127 | f.close() | 136 | f.close() |
@@ -133,17 +142,18 @@ class EMRClusterScanner(object): | @@ -133,17 +142,18 @@ class EMRClusterScanner(object): | ||
133 | def start_daemon(self): | 142 | def start_daemon(self): |
134 | #以守护进程的状态运行此EMR扫描程序 | 143 | #以守护进程的状态运行此EMR扫描程序 |
135 | while True: | 144 | while True: |
136 | - try: | ||
137 | - result = self.updateEMRInventory() | ||
138 | - if result: | ||
139 | - 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 | - pass | ||
142 | - except Exception as e: | ||
143 | - print e | ||
144 | - self.mailman.mail(receivers=self.receivers,Content='EMR大数据Inventory更新失败\n{0}\n5分钟后重试'.format(str(e)),Title='EMR Inventory 更新结果') | ||
145 | - time.sleep(300) | ||
146 | - pass | 145 | + for emrName in self.emrNameMap: |
146 | + #try: | ||
147 | + result = self.updateEMRInventory(emrName) | ||
148 | + if result: | ||
149 | + self.mailman.mail(receivers=self.receivers,Content='EMR大数据Inventory [{0}] 已更新\n{1}\n详见 http://git.yoho.cn/ops/yoho-ansible-roles/blob/master/inventories/bigdata/hosts'.format(emrName,result),Title='EMR Inventory 更新结果') | ||
150 | + else: | ||
151 | + pass | ||
152 | + #except Exception as e: | ||
153 | + # print e | ||
154 | + # self.mailman.mail(receivers=self.receivers,Content='EMR大数据Inventory更新失败\n{0}\n5分钟后重试'.format(str(e)),Title='EMR Inventory 更新结果') | ||
155 | + # time.sleep(300) | ||
156 | + # pass | ||
147 | time.sleep(self.interval) | 157 | time.sleep(self.interval) |
148 | 158 | ||
149 | 159 |
-
Please register or login to post a comment