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 org.onap.dmaap.messagerouter.mirroragent.nsa.dmaapMMAgent.utils;
 
  25 import java.io.BufferedReader;
 
  26 import java.io.InputStream;
 
  27 import java.io.InputStreamReader;
 
  29 import org.apache.log4j.Logger;
 
  30 import org.onap.dmaap.messagerouter.mirroragent.nsa.dmaapMMAgent.MirrorMakerAgent;
 
  32 public class MirrorMakerProcessHandler {
 
  33         static final Logger logger = Logger.getLogger(MirrorMakerProcessHandler.class);
 
  35         public static boolean checkMirrorMakerProcess(String agentname) {
 
  37                         Runtime rt = Runtime.getRuntime();
 
  38                         Process mmprocess = null;
 
  40                         if (System.getProperty("os.name").contains("Windows")) {
 
  42                                 args = "wmic.exe process where \"commandline like '%agentname=" + agentname
 
  43                                                 + "~%' and caption='java.exe'\"";
 
  44                                 mmprocess = rt.exec(args);
 
  46                                 String args[] = { "/bin/sh", "-c", "ps -ef |grep java |grep agentname=" + agentname + "~" };
 
  47                                 mmprocess = rt.exec(args);
 
  50                         InputStream is = mmprocess.getInputStream();
 
  51                         InputStreamReader isr = new InputStreamReader(is);
 
  52                         BufferedReader br = new BufferedReader(isr);
 
  54                         while ((line = br.readLine()) != null) {
 
  55                                 // System.out.println(line);
 
  56                                 if (line.contains("agentname=" + agentname) && line.contains("/bin/sh -c") == false) {
 
  60                 } catch (Exception e) {
 
  66         public static void stopMirrorMaker(String agentname) {
 
  68                         Runtime rt = Runtime.getRuntime();
 
  69                         Process killprocess = null;
 
  71                         if (System.getProperty("os.name").contains("Windows")) {
 
  72                                 String args = "wmic.exe process where \"commandline like '%agentname=" + agentname
 
  73                                                 + "~%' and caption='java.exe'\" call terminate";
 
  74                                 killprocess = rt.exec(args);
 
  76                                 String args[] = { "/bin/sh", "-c",
 
  77                                                 "kill -9 $(ps -ef |grep java |grep agentname=" + agentname + "~| awk '{print $2}')" };
 
  78                                 // args = "kill $(ps -ef |grep java |grep agentname=" +
 
  79                                 // agentname + "~| awk '{print $2}')";
 
  80                                 killprocess = rt.exec(args);
 
  83                         InputStream is = killprocess.getInputStream();
 
  84                         InputStreamReader isr = new InputStreamReader(is);
 
  85                         BufferedReader br = new BufferedReader(isr);
 
  87                         while ((line = br.readLine()) != null) {
 
  88                                 // System.out.println(line);
 
  91                         logger.info("Mirror Maker " + agentname + " Stopped");
 
  92                 } catch (Exception e) {
 
  98         public static void startMirrorMaker(String mmagenthome, String kafkaHome, String agentName, String consumerConfig,
 
  99                         String producerConfig, String whitelist) {
 
 101                         Runtime rt = Runtime.getRuntime();
 
 103                         if (System.getProperty("os.name").contains("Windows")) {
 
 104                                 String args = kafkaHome + "/bin/windows/kafka-run-class.bat -Dagentname=" + agentName
 
 105                                                 + "~ kafka.tools.MirrorMaker --consumer.config " + consumerConfig + " --producer.config "
 
 106                                                 + producerConfig + " --whitelist '" + whitelist + "' > " + mmagenthome + "/logs/" + agentName
 
 108                                 final Process process = rt.exec(args);
 
 112                                                         InputStream is = process.getInputStream();
 
 113                                                         InputStreamReader isr = new InputStreamReader(is);
 
 114                                                         BufferedReader br = new BufferedReader(isr);
 
 116                                                         while ((line = br.readLine()) != null) {
 
 117                                                                 // System.out.println(line);
 
 119                                                 } catch (Exception anExc) {
 
 120                                                         anExc.printStackTrace();
 
 125                                 String args[] = { "/bin/sh", "-c",
 
 126                                                 kafkaHome + "/bin/kafka-run-class.sh -Dagentname=" + agentName
 
 127                                                                 + "~ kafka.tools.MirrorMaker --consumer.config " + consumerConfig
 
 128                                                                 + " --producer.config " + producerConfig + " --whitelist '" + whitelist + "' >"
 
 129                                                                 + mmagenthome + "/logs/" + agentName + "_MMaker.log 2>&1" };
 
 130                                 final Process process = rt.exec(args);
 
 134                                                         InputStream is = process.getInputStream();
 
 135                                                         InputStreamReader isr = new InputStreamReader(is);
 
 136                                                         BufferedReader br = new BufferedReader(isr);
 
 138                                                         while ((line = br.readLine()) != null) {
 
 139                                                                 // System.out.println(line);
 
 141                                                 } catch (Exception anExc) {
 
 142                                                         anExc.printStackTrace();
 
 148                         logger.info("Mirror Maker " + agentName + " Started" + " WhiteListing:" + whitelist);
 
 150                 } catch (Exception e) {