View Javadoc

1   package org.kuali.common.deploy;
2   
3   import java.util.ArrayList;
4   import java.util.Arrays;
5   import java.util.List;
6   import java.util.Properties;
7   
8   import org.kuali.common.util.Assert;
9   import org.kuali.common.util.FormatUtils;
10  import org.kuali.common.util.MonitorTextFileResult;
11  import org.kuali.common.util.PropertyUtils;
12  import org.kuali.common.util.secure.SecureChannel;
13  import org.slf4j.Logger;
14  import org.slf4j.LoggerFactory;
15  
16  public class AppDynamicsMonitoring implements Monitoring {
17  
18  	private static final Logger logger = LoggerFactory.getLogger(AppDynamicsMonitoring.class);
19  
20  	SecureChannel channel;
21  	String setEnvPropertyKey = "setenv.env.content";
22  	MachineAgent machineAgent;
23  	ServerAgent serverAgent;
24  	String user;
25  	String group;
26  	boolean enabled;
27  	Properties filterProperties;
28  
29  	@Override
30  	public void stop() {
31  		long start = System.currentTimeMillis();
32  		logger.info("[appdynamics:stopping]");
33  		DeployUtils.killMatchingProcesses(channel, user, machineAgent.getStartupCommand(), "machine agent");
34  		logger.info("[appdynamics:stopped] - {}", FormatUtils.getTime(System.currentTimeMillis() - start));
35  	}
36  
37  	@Override
38  	public void prepare() {
39  		long start = System.currentTimeMillis();
40  		logger.info("[appdynamics:preparing]");
41  		List<String> dirs = Arrays.asList(machineAgent.getLogsDir(), machineAgent.getTmpDir(), serverAgent.getLogsDir());
42  		List<String> chownDirs = getChownDirs(dirs);
43  		DeployUtils.delete(channel, dirs);
44  		DeployUtils.mkdirs(channel, dirs);
45  		DeployUtils.chown(channel, user, group, chownDirs);
46  		List<Deployable> deployables = Arrays.asList(machineAgent.getController(), serverAgent.getController());
47  		DeployUtils.copyFiles(channel, deployables, filterProperties);
48  		if (enabled) {
49  			String value = "\n" + serverAgent.getAppServerStartupOptions();
50  			PropertyUtils.appendToOrSetProperty(filterProperties, setEnvPropertyKey, value);
51  		}
52  		logger.info("[appdynamics:prepared] - {}", FormatUtils.getTime(System.currentTimeMillis() - start));
53  	}
54  
55  	@Override
56  	public void start() {
57  		if (!enabled) {
58  			logger.info("[appdynamics:start] - (skipped) - monitoring is not enabled");
59  			return;
60  		}
61  		long start = System.currentTimeMillis();
62  		logger.info("[appdynamics:starting]");
63  		startMachineAgent(channel, machineAgent);
64  		logger.info("[appdynamics:started] - {}", FormatUtils.getTime(System.currentTimeMillis() - start));
65  	}
66  
67  	protected void startMachineAgent(SecureChannel channel, MachineAgent machineAgent) {
68  		logger.info("[appdynamics:machineagent:starting]");
69  		boolean exists = channel.exists(machineAgent.getLogFile());
70  		Assert.isFalse(exists, "machine agent log file [" + machineAgent.getLogFile() + "] already exists");
71  		String command = DeployUtils.getNohupBackgroundProcess(user, machineAgent.getStartupCommand());
72  		logger.debug(command);
73  		channel.executeNoWait(command);
74  		String path = machineAgent.getLogFile();
75  		String token = machineAgent.getStartupToken();
76  		int intervalMillis = machineAgent.getLogFileIntervalMillis();
77  		int timeoutMillis = machineAgent.getStartupTimeoutMillis();
78  		String encoding = machineAgent.getLogFileEncoding();
79  		MonitorTextFileResult result = DeployUtils.monitorTextFile(channel, path, token, intervalMillis, timeoutMillis, encoding);
80  		if (!result.isContains()) {
81  			throw new IllegalStateException("Could not verify AppDynamics Machine Agent startup");
82  		} else {
83  			logger.info("[appdynamics:machineagent:started]");
84  		}
85  	}
86  
87  	protected List<String> getChownDirs(List<String> dirs) {
88  		List<String> chownDirs = new ArrayList<String>();
89  		chownDirs.addAll(dirs);
90  		chownDirs.add(machineAgent.getBaseDir());
91  		chownDirs.add(serverAgent.getBaseDir());
92  		return chownDirs;
93  	}
94  
95  	public String getUser() {
96  		return user;
97  	}
98  
99  	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 }