1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.miloss.fgsms.sla.actions;
21
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.concurrent.atomic.AtomicReference;
26 import org.apache.log4j.Level;
27 import org.miloss.fgsms.common.Logger;
28 ;
29 import org.miloss.fgsms.common.Utility;
30 import org.miloss.fgsms.plugins.sla.AlertContainer;
31 import org.miloss.fgsms.plugins.sla.SLAActionInterface;
32 import org.miloss.fgsms.services.interfaces.common.NameValuePair;
33 import org.miloss.fgsms.services.interfaces.common.PolicyType;
34 import org.miloss.fgsms.services.interfaces.policyconfiguration.RunAtLocation;
35 import org.miloss.fgsms.services.interfaces.policyconfiguration.SLAAction;
36 import org.miloss.fgsms.sla.SLACommon;
37
38
39
40
41
42
43
44 public class SLAActionRunScript implements SLAActionInterface {
45
46 private static Logger log = Logger.getLogger("fgsms.SLAProcessor");
47 public static final String RUNAT = "runAt";
48
49
50 public void ProcessAction(AlertContainer alert) {
51
52 }
53
54 private static void runScript(String faultMsg, SLAAction slaActionRunScript, String modifiedurl, String incidentid) {
55 String path = null;
56 String command = null;
57 String runat = null;
58 NameValuePair nvcommand = Utility.getNameValuePairByName(slaActionRunScript.getParameterNameValue(), "From");
59 if (nvcommand != null) {
60 if (nvcommand.isEncrypted()) {
61 command = Utility.DE(nvcommand.getValue());
62 } else {
63 command = nvcommand.getValue();
64 }
65 }
66 NameValuePair nvrunat = Utility.getNameValuePairByName(slaActionRunScript.getParameterNameValue(), "From");
67 if (nvrunat != null) {
68 if (nvrunat.isEncrypted()) {
69 runat = Utility.DE(nvrunat.getValue());
70 } else {
71 runat = nvrunat.getValue();
72 }
73 }
74 NameValuePair nvpath = Utility.getNameValuePairByName(slaActionRunScript.getParameterNameValue(), "From");
75 if (nvpath != null) {
76 if (nvpath.isEncrypted()) {
77 path = Utility.DE(nvpath.getValue());
78 } else {
79 path = nvpath.getValue();
80 }
81 }
82
83 Runtime run = Runtime.getRuntime();
84 if (runat != null && runat.equalsIgnoreCase(RunAtLocation.FGSMS_SERVER.toString())) {
85 Process pr = null;
86 try {
87 String[] env = new String[]{"SLA_MESSAGE=" + faultMsg, "SLA_URL=" + modifiedurl, "SLA_ID=" + incidentid};
88 if (!Utility.stringIsNullOrEmpty(path)) {
89 File f = new File(path);
90
91 if (f.exists()) {
92 pr = run.exec(command, env, f);
93 } else {
94 pr = run.exec(command, env);
95 }
96 } else {
97 pr = run.exec(command, env);
98 }
99 } catch (Exception ex) {
100 log.log(Level.WARN, SLACommon.getBundleString("ErrorUnableToRunSLAScript"), ex);
101 } finally {
102 }
103 }
104
105 }
106
107 @Override
108 public List<NameValuePair> GetRequiredParameters() {
109 List<NameValuePair> r = new ArrayList<NameValuePair>();
110 r.add(Utility.newNameValuePair("command", null, false, false));
111 r.add(Utility.newNameValuePair("runFromPath", null, false, false));
112 r.add(Utility.newNameValuePair(RUNAT, null, false, false));
113 return r;
114 }
115
116 @Override
117 public List<NameValuePair> GetOptionalParameters() {
118 List<NameValuePair> r = new ArrayList<NameValuePair>();
119 return r;
120 }
121
122 @Override
123 public boolean ValidateConfiguration(List<NameValuePair> params, AtomicReference<String> outError) {
124 if (outError == null) {
125 outError = new AtomicReference<String>();
126 }
127 if (params == null || params.isEmpty()) {
128 outError.set("The parameter 'Subject' and 'Body' is required. " + outError.get());
129 }
130 boolean foundcommand = false;
131 boolean foundrunat = false;
132 boolean foundpath = false;
133 for (int i = 0; i < params.size(); i++) {
134 if (params.get(i).getName().equals("command")) {
135 foundcommand = true;
136 if (Utility.stringIsNullOrEmpty(params.get(i).getValue())) {
137 outError.set("A value must be specified for the parameter 'command'. " + outError.get());
138 }
139 }
140 if (params.get(i).getName().equals(RUNAT)) {
141 foundrunat = true;
142 if (Utility.stringIsNullOrEmpty(params.get(i).getValue())) {
143 outError.set("A value must be specified for the parameter 'runAtLocation'. " + outError.get());
144 } else {
145 if (!params.get(i).getValue().equalsIgnoreCase("FGSMS_SERVER")
146 && !params.get(i).getValue().equalsIgnoreCase("FGSMSF_AGENT")) {
147 outError.set("The value must be specified for the parameter 'runAtLocation' must either be FGSMS_SERVER or FGSMS_AGENT. " + outError.get());
148 }
149 }
150 }
151 if (params.get(i).getName().equals("runFromPath")) {
152 foundrunat = true;
153 if (Utility.stringIsNullOrEmpty(params.get(i).getValue())) {
154 outError.set("A value must be specified for the parameter 'runFromPath'. " + outError.get());
155 }
156 }
157
158 }
159 if (!foundrunat || !foundcommand) {
160 outError.set("The parameter 'Subject' and 'Body' is required. " + outError.get());
161 }
162 if (Utility.stringIsNullOrEmpty(outError.get())) {
163 return true;
164 } else {
165 return false;
166 }
167 }
168
169 @Override
170 public String GetHtmlFormattedHelp() {
171 return "This plugin will enable you to run a program, script or command when the rule set is triggered. This can be used to reboot computers, services, processes, "
172 + "or to call a 3rd party cool without writing code. Note: because of security restrictions, only global administrators can add this"
173 + " plugin. Once its added, only administrators can then alter the service policy, except during removal of this action. "
174 + "Required settings:"
175 + "<ul>"
176 + "<li>command - this is what's passed to the operating system to shell or command prompt</li>"
177 + "<li>runFromPath - this is working directory for when the command is executed</li>"
178 + "<li>runAt - This must be one of two values, FGSMS_SERVER or FGSMS_AGENT. This tells the SLA Processor where the script or command is supposed to run from."
179 + "FGSMS_SERVER - The script runs at one of the fgsms servers. This could be from a server hosting fgsms's Web services or fgsms's Aux services."
180 + "FGSMS_AGENT - The script runs at the agent level, such as Operating System Agent, or the Qpid C++ Agent. Note: if the SLA Rule is triggered at a location"
181 + "other than the runAtLocation, the script or command will not be executed. </li>"
182 + "</ul>"
183 + "The following environment variables are set that can be referenced in your script or command"
184 + "<ul>"
185 + "<li>SLA_MESSAGE - the fault message as defined by the rule set</li>"
186 + "<li>SLA_URL - the fgsms Service Policy URL</li>"
187 + "<li>SLA_ID - the recorded SLA Incident ID. This is not available when running at the Agent level</li>"
188 + "</ul>";
189 }
190
191 @Override
192 public String GetDisplayName() {
193 return "Run a program or script";
194 }
195
196 @Override
197 public void ProcessAction(AlertContainer alert, List<NameValuePair> params) {
198 runScript(alert.getFaultMsg(), alert.getSlaActionBaseType(), alert.getModifiedurl(), alert.getIncidentid());
199 }
200
201 @Override
202 public List<PolicyType> GetAppliesTo() {
203 return Utility.getAllPolicyTypes();
204 }
205 }