37e0981be79d18e6c3a24da7dbf8bde24ec9250f
[dmaap/messagerouter/mirroragent.git] / src / main / java / org / onap / dmaap / mr / dmaapMMAgent / utils / MirrorMakerProcessHandler.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *  
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  *
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 package org.onap.dmaap.mr.dmaapMMAgent.utils;
23
24 import java.io.BufferedReader;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27
28 import org.apache.log4j.Logger;
29
30 public class MirrorMakerProcessHandler {
31         static final Logger logger = Logger.getLogger(MirrorMakerProcessHandler.class);
32         static String mmagenthome = System.getProperty("MMAGENTHOME");
33
34         public static boolean checkMirrorMakerProcess(String agentname, boolean enablelogCheck, String grepLog) throws Exception {
35                 String line,linelog;
36                 try {
37                         Runtime rt = Runtime.getRuntime();
38                         Process mmprocess = null;
39
40                         if (System.getProperty("os.name").contains("Windows")) {
41                                 String args = "";
42                                 args = "wmic.exe process where \"commandline like '%agentname=" + agentname
43                                                 + "~%' and caption='java.exe'\"";
44                                 mmprocess = rt.exec(args);
45                         } else {
46                                 //String args[] = { "/bin/sh", "-c", "ps -ef |grep java |grep agentname=" + agentname + "~" };
47                                 
48                                 String args[] = { "/bin/sh", "-c", "ps -ef | grep `ps -ef |grep agentname=" + agentname + "~ | egrep -v 'grep|java' | awk '{print $2}' `| egrep -v '/bin/sh|grep' "};
49                                 logger.info("CheckMM process->"+args[2]);
50                                 mmprocess = rt.exec(args);
51                         }
52
53                         InputStream is = mmprocess.getInputStream();
54                         InputStreamReader isr = new InputStreamReader(is);
55                         BufferedReader br = new BufferedReader(isr);
56                          
57                         while ((line = br.readLine()) != null) {
58                                 logger.info(line);
59                 //              if (line.contains("agentname=" + agentname) && line.contains("/bin/sh -c") == false) {
60                                         
61                                         //If enablelogCheck Check MirrorMaker log for errors and restart mirrormaker
62                                         if(enablelogCheck) {
63                                                 logger.info("Check if MM log contains any errors");
64                                                 String args2[];
65                                                 args2 = new String[] { "/bin/sh", "-c", "grep -i ERROR "+ mmagenthome + "/logs/" + agentname + "_MMaker.log"};
66                                                 if(null!=grepLog && !grepLog.isEmpty()) 
67                                                 {
68                                                          args2 = new String[]{ "/bin/sh", "-c", grepLog +" " +  mmagenthome + "/logs/" + agentname + "_MMaker.log"};
69                                                 }
70                                                 logger.info("Grep log args-- "+args2[2]);                               
71                                                 mmprocess = rt.exec(args2);
72                                                 InputStream islog = mmprocess.getInputStream();
73                                                 InputStreamReader isrlog = new InputStreamReader(islog);
74                                                 BufferedReader brlog = new BufferedReader(isrlog);
75                                                         
76                                                         while ((linelog = brlog.readLine()) != null) 
77                                                         {
78                                                                 logger.info("Error from MM log--"+linelog);
79                                                                         
80                                                                                 if (linelog.toLowerCase().contains("ERROR".toLowerCase()) ||
81                                                                                                 linelog.toLowerCase().contains("Issue".toLowerCase())   ) 
82                                                                                 {
83                                                                                         logger.info("MM log contains error Stop MM and restart");
84                                                                                         stopMirrorMaker(agentname);
85                                                                                         isrlog.close();
86                                                                                         brlog.close();
87                                                                                         return false;
88                                                                                 } 
89                                                                                 
90                                                                                 
91                                                         }
92                                                         isrlog.close();
93                                                         brlog.close();
94                                         }
95                                                 
96                                                 return true;                                    
97                         //      }
98                         }
99                 } catch (Exception e) {
100                         e.printStackTrace();
101                 }
102                 return false;
103         }
104
105         public static void stopMirrorMaker(String agentname) {
106                 try {
107                         Runtime rt = Runtime.getRuntime();
108                         Process killprocess = null;
109
110                         if (System.getProperty("os.name").contains("Windows")) {
111                                 String args = "wmic.exe process where \"commandline like '%agentname=" + agentname
112                                                 + "~%' and caption='java.exe'\" call terminate";
113                                 killprocess = rt.exec(args);
114                         } else {
115                                 //String args[] = { "/bin/sh", "-c",
116                                         //      "kill -9 $(ps -ef |grep java |grep agentname=" + agentname + "~| awk '{print $2}')" };
117                                 
118                                 //String args[] = { "/bin/sh", "-c",
119                                 //              "kill -9 `ps -ef |grep agentname=" + agentname + "~| egrep -v 'grep|java' | awk '{print $2}'` | egrep -v '/bin/sh|grep'"};
120                                 String args[] = { "/bin/sh", "-c",
121                                                 "for i in `ps -ef |grep agentname="+ agentname + "~ | egrep -v 'grep|java' | awk '{print $2}'`;do kill -9 `ps -eaf | grep $i | egrep -v '/bin/sh|grep' | awk '{print $2}'` ;done"};
122                                 logger.info ("Stop MM ->"+args[2]);                             
123                                 // args = "kill $(ps -ef |grep java |grep agentname=" +
124                                 // agentname + "~| awk '{print $2}')";
125                                 killprocess = rt.exec(args);
126                         }
127
128                         InputStream is = killprocess.getInputStream();
129                         InputStreamReader isr = new InputStreamReader(is);
130                         BufferedReader br = new BufferedReader(isr);
131                         String line;
132                         while ((line = br.readLine()) != null) {
133                                 // System.out.println(line);
134                         }
135
136                         logger.info("Mirror Maker " + agentname + " Stopped");
137                 } catch (Exception e) {
138                         e.printStackTrace();
139                 }
140
141         }
142
143         public static void startMirrorMaker(String mmagenthome, String kafkaHome, String agentName, String consumerConfig,
144                         String producerConfig, int numStreams,  String whitelist) {
145                 try {
146                         Runtime rt = Runtime.getRuntime();
147
148                         if (System.getProperty("os.name").contains("Windows")) {
149                                 String args = kafkaHome + "/bin/windows/kafka-run-class.bat -Dagentname=" + agentName
150                                                 + "~ kafka.tools.MirrorMaker --consumer.config " + consumerConfig + " --producer.config "
151                                                 + producerConfig +" --num.streams " + numStreams + "  --abort.on.send.failure true" +" --whitelist '" + whitelist + "' > " + mmagenthome + "/logs/" + agentName
152                                                 + "_MMaker.log";
153                                 final Process process = rt.exec(args);
154                                 new Thread() {
155                                         @Override
156                                         public void run() {
157                                                 try {
158                                                         InputStream is = process.getInputStream();
159                                                         InputStreamReader isr = new InputStreamReader(is);
160                                                         BufferedReader br = new BufferedReader(isr);
161                                                         String line;
162                                                         while ((line = br.readLine()) != null) {
163                                                                 // System.out.println(line);
164                                                         }
165                                                 } catch (Exception anExc) {
166                                                         anExc.printStackTrace();
167                                                 }
168                                         }
169                                 }.start();
170                         } else {
171                                 String args[] = { "/bin/sh", "-c",
172                                                 kafkaHome + "/bin/kafka-run-class.sh -Dagentname=" + agentName
173                                                                 + "~ kafka.tools.MirrorMaker --consumer.config " + consumerConfig
174                                                                 + " --producer.config " + producerConfig + " --num.streams " + numStreams + "  --abort.on.send.failure true" + " --whitelist '" + whitelist + "' >"
175                                                                 + mmagenthome + "/logs/" + agentName + "_MMaker.log 2>&1" };
176                                 final Process process = rt.exec(args);
177                                 new Thread() {
178                                         public void run() {
179                                                 try {
180                                                         InputStream is = process.getInputStream();
181                                                         InputStreamReader isr = new InputStreamReader(is);
182                                                         BufferedReader br = new BufferedReader(isr);
183                                                         String line;
184                                                         while ((line = br.readLine()) != null) {
185                                                                 // System.out.println(line);
186                                                         }
187                                                 } catch (Exception anExc) {
188                                                         anExc.printStackTrace();
189                                                 }
190                                         }
191                                 }.start();
192                         }
193
194                         logger.info("Mirror Maker " + agentName + " Started" + " WhiteListing:" + whitelist);
195
196                 } catch (Exception e) {
197                         e.printStackTrace();
198                 }
199         }
200 }

© 2017 ONAP. Copyright © The Linux Foundation ®. All Rights Reserved.
The Linux Foundation has registered trademarks and uses trademarks.
For a list of trademarks of The Linux Foundation, please see our Trademark Usage page.
Linux is a registered trademark of Linus Torvalds.
Privacy Policy and Terms of Use