906fb3e11aeb7898f64ac7787402b23e439f01c9
[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 public class MirrorMakerProcessHandler {
34         static final Logger logger = Logger.getLogger(MirrorMakerProcessHandler.class);
35
36         public static boolean checkMirrorMakerProcess(String agentname) {
37                 try {
38                         Runtime rt = Runtime.getRuntime();
39                         Process mmprocess = null;
40
41                         if (System.getProperty("os.name").contains("Windows")) {
42                                 String args = "";
43                                 args = "wmic.exe process where \"commandline like '%agentname=" + agentname
44                                                 + "~%' and caption='java.exe'\"";
45                                 mmprocess = rt.exec(args);
46                         } else {
47                                 String args[] = { "/bin/sh", "-c", "ps -ef |grep java |grep agentname=" + agentname + "~" };
48                                 mmprocess = rt.exec(args);
49                         }
50
51                         InputStream is = mmprocess.getInputStream();
52                         InputStreamReader isr = new InputStreamReader(is);
53                         BufferedReader br = new BufferedReader(isr);
54                         String line;
55                         while ((line = br.readLine()) != null) {
56                                 // System.out.println(line);
57                                 if (line.contains("agentname=" + agentname) && line.contains("/bin/sh -c") == false) {
58                                         return true;
59                                 }
60                         }
61                 } catch (Exception e) {
62                         e.printStackTrace();
63                 }
64                 return false;
65         }
66
67         public static void stopMirrorMaker(String agentname) {
68                 try {
69                         Runtime rt = Runtime.getRuntime();
70                         Process killprocess = null;
71
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);
76                         } else {
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);
82                         }
83
84                         InputStream is = killprocess.getInputStream();
85                         InputStreamReader isr = new InputStreamReader(is);
86                         BufferedReader br = new BufferedReader(isr);
87                         String line;
88                         while ((line = br.readLine()) != null) {
89                                 // System.out.println(line);
90                         }
91
92                         logger.info("Mirror Maker " + agentname + " Stopped");
93                 } catch (Exception e) {
94                         e.printStackTrace();
95                 }
96
97         }
98
99         public static void startMirrorMaker(String mmagenthome, String kafkaHome, String agentName, String consumerConfig,
100                         String producerConfig, String whitelist) {
101                 try {
102                         Runtime rt = Runtime.getRuntime();
103
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
108                                                 + "_MMaker.log";
109                                 final Process process = rt.exec(args);
110                                 new Thread() {
111                                         public void run() {
112                                                 try {
113                                                         InputStream is = process.getInputStream();
114                                                         InputStreamReader isr = new InputStreamReader(is);
115                                                         BufferedReader br = new BufferedReader(isr);
116                                                         String line;
117                                                         while ((line = br.readLine()) != null) {
118                                                                 // System.out.println(line);
119                                                         }
120                                                 } catch (Exception anExc) {
121                                                         anExc.printStackTrace();
122                                                 }
123                                         }
124                                 }.start();
125                         } else {
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);
132                                 new Thread() {
133                                         public void run() {
134                                                 try {
135                                                         InputStream is = process.getInputStream();
136                                                         InputStreamReader isr = new InputStreamReader(is);
137                                                         BufferedReader br = new BufferedReader(isr);
138                                                         String line;
139                                                         while ((line = br.readLine()) != null) {
140                                                                 // System.out.println(line);
141                                                         }
142                                                 } catch (Exception anExc) {
143                                                         anExc.printStackTrace();
144                                                 }
145                                         }
146                                 }.start();
147                         }
148
149                         logger.info("Mirror Maker " + agentName + " Started" + " WhiteListing:" + whitelist);
150
151                 } catch (Exception e) {
152                         e.printStackTrace();
153                 }
154         }
155 }