1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
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=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
23 package com.att.nsa.dmaapMMAgent.utils;
25 import java.io.BufferedReader;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
29 import org.apache.log4j.Logger;
31 import com.att.nsa.dmaapMMAgent.MirrorMakerAgent;
33 public class MirrorMakerProcessHandler {
34 static final Logger logger = Logger.getLogger(MirrorMakerProcessHandler.class);
36 public static boolean checkMirrorMakerProcess(String agentname) {
38 Runtime rt = Runtime.getRuntime();
39 Process mmprocess = null;
41 if (System.getProperty("os.name").contains("Windows")) {
43 args = "wmic.exe process where \"commandline like '%agentname=" + agentname
44 + "~%' and caption='java.exe'\"";
45 mmprocess = rt.exec(args);
47 String args[] = { "/bin/sh", "-c", "ps -ef |grep java |grep agentname=" + agentname + "~" };
48 mmprocess = rt.exec(args);
51 InputStream is = mmprocess.getInputStream();
52 InputStreamReader isr = new InputStreamReader(is);
53 BufferedReader br = new BufferedReader(isr);
55 while ((line = br.readLine()) != null) {
56 // System.out.println(line);
57 if (line.contains("agentname=" + agentname) && line.contains("/bin/sh -c") == false) {
61 } catch (Exception e) {
67 public static void stopMirrorMaker(String agentname) {
69 Runtime rt = Runtime.getRuntime();
70 Process killprocess = null;
72 if (System.getProperty("os.name").contains("Windows")) {
73 String args = "wmic.exe process where \"commandline like '%agentname=" + agentname
74 + "~%' and caption='java.exe'\" call terminate";
75 killprocess = rt.exec(args);
77 String args[] = { "/bin/sh", "-c",
78 "kill -9 $(ps -ef |grep java |grep agentname=" + agentname + "~| awk '{print $2}')" };
79 // args = "kill $(ps -ef |grep java |grep agentname=" +
80 // agentname + "~| awk '{print $2}')";
81 killprocess = rt.exec(args);
84 InputStream is = killprocess.getInputStream();
85 InputStreamReader isr = new InputStreamReader(is);
86 BufferedReader br = new BufferedReader(isr);
88 while ((line = br.readLine()) != null) {
89 // System.out.println(line);
92 logger.info("Mirror Maker " + agentname + " Stopped");
93 } catch (Exception e) {
99 public static void startMirrorMaker(String mmagenthome, String kafkaHome, String agentName, String consumerConfig,
100 String producerConfig, String whitelist) {
102 Runtime rt = Runtime.getRuntime();
104 if (System.getProperty("os.name").contains("Windows")) {
105 String args = kafkaHome + "/bin/windows/kafka-run-class.bat -Dagentname=" + agentName
106 + "~ kafka.tools.MirrorMaker --consumer.config " + consumerConfig + " --producer.config "
107 + producerConfig + " --whitelist '" + whitelist + "' > " + mmagenthome + "/logs/" + agentName
109 final Process process = rt.exec(args);
113 InputStream is = process.getInputStream();
114 InputStreamReader isr = new InputStreamReader(is);
115 BufferedReader br = new BufferedReader(isr);
117 while ((line = br.readLine()) != null) {
118 // System.out.println(line);
120 } catch (Exception anExc) {
121 anExc.printStackTrace();
126 String args[] = { "/bin/sh", "-c",
127 kafkaHome + "/bin/kafka-run-class.sh -Dagentname=" + agentName
128 + "~ kafka.tools.MirrorMaker --consumer.config " + consumerConfig
129 + " --producer.config " + producerConfig + " --whitelist '" + whitelist + "' >"
130 + mmagenthome + "/logs/" + agentName + "_MMaker.log 2>&1" };
131 final Process process = rt.exec(args);
135 InputStream is = process.getInputStream();
136 InputStreamReader isr = new InputStreamReader(is);
137 BufferedReader br = new BufferedReader(isr);
139 while ((line = br.readLine()) != null) {
140 // System.out.println(line);
142 } catch (Exception anExc) {
143 anExc.printStackTrace();
149 logger.info("Mirror Maker " + agentName + " Started" + " WhiteListing:" + whitelist);
151 } catch (Exception e) {