dd1442e66aa76a01af345e1400d9e20691ca3962
[dmaap/messagerouter/mirroragent.git] / src / main / java / com / att / nsa / 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
23 package com.att.nsa.dmaapMMAgent.utils;
24
25 import java.io.BufferedReader;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28
29 import org.apache.log4j.Logger;
30
31 import com.att.nsa.dmaapMMAgent.MirrorMakerAgent;
32
33
34 public class MirrorMakerProcessHandler {
35         static final Logger logger = Logger.getLogger(MirrorMakerProcessHandler.class);
36
37         public static boolean checkMirrorMakerProcess(String agentname) {
38                 try {
39                         Runtime rt = Runtime.getRuntime();
40                         Process mmprocess = null;
41
42                         if (System.getProperty("os.name").contains("Windows")) {
43                                 String args = "";
44                                 args = "wmic.exe process where \"commandline like '%agentname=" + agentname
45                                                 + "~%' and caption='java.exe'\"";
46                                 mmprocess = rt.exec(args);
47                         } else {
48                                 String args[] = { "/bin/sh", "-c", "ps -ef |grep java |grep agentname=" + agentname + "~" };
49                                 mmprocess = rt.exec(args);
50                         }
51
52                         InputStream is = mmprocess.getInputStream();
53                         InputStreamReader isr = new InputStreamReader(is);
54                         BufferedReader br = new BufferedReader(isr);
55                         String line;
56                         while ((line = br.readLine()) != null) {
57                                 // System.out.println(line);
58                                 if (line.contains("agentname=" + agentname) && line.contains("/bin/sh -c") == false) {
59                                         return true;
60                                 }
61                         }
62                 } catch (Exception e) {
63                         logger.error("Error at checkMirrorMakerProcess method:" + e.getMessage());
64                 }
65                 return false;
66         }
67
68         public static void stopMirrorMaker(String agentname) {
69                 try {
70                         Runtime rt = Runtime.getRuntime();
71                         Process killprocess = null;
72
73                         if (System.getProperty("os.name").contains("Windows")) {
74                                 String args = "wmic.exe process where \"commandline like '%agentname=" + agentname
75                                                 + "~%' and caption='java.exe'\" call terminate";
76                                 killprocess = rt.exec(args);
77                         } else {
78                                 String args[] = { "/bin/sh", "-c",
79                                                 "kill -9 $(ps -ef |grep java |grep agentname=" + agentname + "~| awk '{print $2}')" };
80                                 // args = "kill $(ps -ef |grep java |grep agentname=" +
81                                 // agentname + "~| awk '{print $2}')";
82                                 killprocess = rt.exec(args);
83                         }
84
85                         InputStream is = killprocess.getInputStream();
86                         InputStreamReader isr = new InputStreamReader(is);
87                         BufferedReader br = new BufferedReader(isr);
88                         String line;
89                         while ((line = br.readLine()) != null) {
90                                 // System.out.println(line);
91                         }
92
93                         logger.info("Mirror Maker " + agentname + " Stopped");
94                 } catch (Exception e) {
95                         logger.error("Error at stopMirrorMaker method:" + e.getMessage());
96                 }
97
98         }
99
100         public static void startMirrorMaker(String mmagenthome, String kafkaHome, String agentName, String consumerConfig,
101                         String producerConfig, String whitelist) {
102                 try {
103                         Runtime rt = Runtime.getRuntime();
104
105                         if (System.getProperty("os.name").contains("Windows")) {
106                                 String args = kafkaHome + "/bin/windows/kafka-run-class.bat -Dagentname=" + agentName
107                                                 + "~ kafka.tools.MirrorMaker --consumer.config " + consumerConfig + " --producer.config "
108                                                 + producerConfig + " --whitelist '" + whitelist + "' > " + mmagenthome + "/logs/" + agentName
109                                                 + "_MMaker.log";
110                                 final Process process = rt.exec(args);
111                                 new Thread() {
112                                         public void run() {
113                                                 try {
114                                                         InputStream is = process.getInputStream();
115                                                         InputStreamReader isr = new InputStreamReader(is);
116                                                         BufferedReader br = new BufferedReader(isr);
117                                                         String line;
118                                                         while ((line = br.readLine()) != null) {
119                                                                 // System.out.println(line);
120                                                         }
121                                                 } catch (Exception anExc) {
122                                                         logger.error("Error at startMirrorMaker method:" + anExc.getMessage());
123                                                 }
124                                         }
125                                 }.start();
126                         } else {
127                                 String args[] = { "/bin/sh", "-c",
128                                                 kafkaHome + "/bin/kafka-run-class.sh -Dagentname=" + agentName
129                                                                 + "~ kafka.tools.MirrorMaker --consumer.config " + consumerConfig
130                                                                 + " --producer.config " + producerConfig + " --whitelist '" + whitelist + "' >"
131                                                                 + mmagenthome + "/logs/" + agentName + "_MMaker.log 2>&1" };
132                                 final Process process = rt.exec(args);
133                                 new Thread() {
134                                         public void run() {
135                                                 try {
136                                                         InputStream is = process.getInputStream();
137                                                         InputStreamReader isr = new InputStreamReader(is);
138                                                         BufferedReader br = new BufferedReader(isr);
139                                                         String line;
140                                                         while ((line = br.readLine()) != null) {
141                                                                 // System.out.println(line);
142                                                         }
143                                                 } catch (Exception anExc) {
144                                                         logger.error("Exception at startMirrorMaker method else part run method:" + anExc.getMessage());
145                                                 }
146                                         }
147                                 }.start();
148                         }
149
150                         logger.info("Mirror Maker " + agentName + " Started" + " WhiteListing:" + whitelist);
151
152                 } catch (Exception e) {
153                         e.printStackTrace();
154                 }
155         }
156 }