001 package org.kuali.common.deploy; 002 003 import java.util.ArrayList; 004 import java.util.Arrays; 005 import java.util.List; 006 import java.util.Properties; 007 008 import org.kuali.common.util.Assert; 009 import org.kuali.common.util.FormatUtils; 010 import org.kuali.common.util.MonitorTextFileResult; 011 import org.kuali.common.util.PropertyUtils; 012 import org.kuali.common.util.secure.SecureChannel; 013 import org.slf4j.Logger; 014 import org.slf4j.LoggerFactory; 015 016 public class AppDynamicsMonitoring implements Monitoring { 017 018 private static final Logger logger = LoggerFactory.getLogger(AppDynamicsMonitoring.class); 019 020 SecureChannel channel; 021 String setEnvPropertyKey = "setenv.env.content"; 022 MachineAgent machineAgent; 023 ServerAgent serverAgent; 024 String user; 025 String group; 026 boolean enabled; 027 Properties filterProperties; 028 029 @Override 030 public void stop() { 031 long start = System.currentTimeMillis(); 032 logger.info("[appdynamics:stopping]"); 033 DeployUtils.killMatchingProcesses(channel, user, machineAgent.getStartupCommand(), "machine agent"); 034 logger.info("[appdynamics:stopped] - {}", FormatUtils.getTime(System.currentTimeMillis() - start)); 035 } 036 037 @Override 038 public void prepare() { 039 long start = System.currentTimeMillis(); 040 logger.info("[appdynamics:preparing]"); 041 List<String> dirs = Arrays.asList(machineAgent.getLogsDir(), machineAgent.getTmpDir(), serverAgent.getLogsDir()); 042 List<String> chownDirs = getChownDirs(dirs); 043 DeployUtils.delete(channel, dirs); 044 DeployUtils.mkdirs(channel, dirs); 045 DeployUtils.chown(channel, user, group, chownDirs); 046 List<Deployable> deployables = Arrays.asList(machineAgent.getController(), serverAgent.getController()); 047 DeployUtils.copyFiles(channel, deployables, filterProperties); 048 if (enabled) { 049 String value = "\n" + serverAgent.getAppServerStartupOptions(); 050 PropertyUtils.appendToOrSetProperty(filterProperties, setEnvPropertyKey, value); 051 } 052 logger.info("[appdynamics:prepared] - {}", FormatUtils.getTime(System.currentTimeMillis() - start)); 053 } 054 055 @Override 056 public void start() { 057 if (!enabled) { 058 logger.info("[appdynamics:start] - (skipped) - monitoring is not enabled"); 059 return; 060 } 061 long start = System.currentTimeMillis(); 062 logger.info("[appdynamics:starting]"); 063 startMachineAgent(channel, machineAgent); 064 logger.info("[appdynamics:started] - {}", FormatUtils.getTime(System.currentTimeMillis() - start)); 065 } 066 067 protected void startMachineAgent(SecureChannel channel, MachineAgent machineAgent) { 068 logger.info("[appdynamics:machineagent:starting]"); 069 boolean exists = channel.exists(machineAgent.getLogFile()); 070 Assert.isFalse(exists, "machine agent log file [" + machineAgent.getLogFile() + "] already exists"); 071 String command = DeployUtils.getNohupBackgroundProcess(user, machineAgent.getStartupCommand()); 072 logger.debug(command); 073 channel.executeNoWait(command); 074 String path = machineAgent.getLogFile(); 075 String token = machineAgent.getStartupToken(); 076 int intervalMillis = machineAgent.getLogFileIntervalMillis(); 077 int timeoutMillis = machineAgent.getStartupTimeoutMillis(); 078 String encoding = machineAgent.getLogFileEncoding(); 079 MonitorTextFileResult result = DeployUtils.monitorTextFile(channel, path, token, intervalMillis, timeoutMillis, encoding); 080 if (!result.isContains()) { 081 throw new IllegalStateException("Could not verify AppDynamics Machine Agent startup"); 082 } else { 083 logger.info("[appdynamics:machineagent:started]"); 084 } 085 } 086 087 protected List<String> getChownDirs(List<String> dirs) { 088 List<String> chownDirs = new ArrayList<String>(); 089 chownDirs.addAll(dirs); 090 chownDirs.add(machineAgent.getBaseDir()); 091 chownDirs.add(serverAgent.getBaseDir()); 092 return chownDirs; 093 } 094 095 public String getUser() { 096 return user; 097 } 098 099 public void setUser(String user) { 100 this.user = user; 101 } 102 103 public SecureChannel getChannel() { 104 return channel; 105 } 106 107 public void setChannel(SecureChannel channel) { 108 this.channel = channel; 109 } 110 111 public String getGroup() { 112 return group; 113 } 114 115 public void setGroup(String group) { 116 this.group = group; 117 } 118 119 public boolean isEnabled() { 120 return enabled; 121 } 122 123 public void setEnabled(boolean enabled) { 124 this.enabled = enabled; 125 } 126 127 public MachineAgent getMachineAgent() { 128 return machineAgent; 129 } 130 131 public void setMachineAgent(MachineAgent machineAgent) { 132 this.machineAgent = machineAgent; 133 } 134 135 public ServerAgent getServerAgent() { 136 return serverAgent; 137 } 138 139 public void setServerAgent(ServerAgent serverAgent) { 140 this.serverAgent = serverAgent; 141 } 142 143 public Properties getFilterProperties() { 144 return filterProperties; 145 } 146 147 public void setFilterProperties(Properties filterProperties) { 148 this.filterProperties = filterProperties; 149 } 150 151 public String getSetEnvPropertyKey() { 152 return setEnvPropertyKey; 153 } 154 155 public void setSetEnvPropertyKey(String setEnvPropertyKey) { 156 this.setEnvPropertyKey = setEnvPropertyKey; 157 } 158 159 }