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;
34 public class MirrorMakerProcessHandler {
35 static final Logger logger = Logger.getLogger(MirrorMakerProcessHandler.class);
37 public static boolean checkMirrorMakerProcess(String agentname) {
39 Runtime rt = Runtime.getRuntime();
40 Process mmprocess = null;
42 if (System.getProperty("os.name").contains("Windows")) {
44 args = "wmic.exe process where \"commandline like '%agentname=" + agentname
45 + "~%' and caption='java.exe'\"";
46 mmprocess = rt.exec(args);
48 String args[] = { "/bin/sh", "-c", "ps -ef |grep java |grep agentname=" + agentname + "~" };
49 mmprocess = rt.exec(args);
52 InputStream is = mmprocess.getInputStream();
53 InputStreamReader isr = new InputStreamReader(is);
54 BufferedReader br = new BufferedReader(isr);
56 while ((line = br.readLine()) != null) {
57 // System.out.println(line);
58 if (line.contains("agentname=" + agentname) && line.contains("/bin/sh -c") == false) {
62 } catch (Exception e) {
68 public static void stopMirrorMaker(String agentname) {
70 Runtime rt = Runtime.getRuntime();
71 Process killprocess = null;
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);
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);
85 InputStream is = killprocess.getInputStream();
86 InputStreamReader isr = new InputStreamReader(is);
87 BufferedReader br = new BufferedReader(isr);
89 while ((line = br.readLine()) != null) {
90 // System.out.println(line);
93 logger.info("Mirror Maker " + agentname + " Stopped");
94 } catch (Exception e) {
100 public static void startMirrorMaker(String mmagenthome, String kafkaHome, String agentName, String consumerConfig,
101 String producerConfig, String whitelist) {
103 Runtime rt = Runtime.getRuntime();
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
110 final Process process = rt.exec(args);
114 InputStream is = process.getInputStream();
115 InputStreamReader isr = new InputStreamReader(is);
116 BufferedReader br = new BufferedReader(isr);
118 while ((line = br.readLine()) != null) {
119 // System.out.println(line);
121 } catch (Exception anExc) {
122 anExc.printStackTrace();
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);
136 InputStream is = process.getInputStream();
137 InputStreamReader isr = new InputStreamReader(is);
138 BufferedReader br = new BufferedReader(isr);
140 while ((line = br.readLine()) != null) {
141 // System.out.println(line);
143 } catch (Exception anExc) {
144 anExc.printStackTrace();
150 logger.info("Mirror Maker " + agentName + " Started" + " WhiteListing:" + whitelist);
152 } catch (Exception e) {