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