1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.miloss.fgsms.sla.rules;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.concurrent.atomic.AtomicReference;
27 import org.miloss.fgsms.common.Utility;
28 import org.miloss.fgsms.plugins.sla.SLARuleInterface;
29 import org.miloss.fgsms.services.interfaces.common.MachinePerformanceData;
30 import org.miloss.fgsms.services.interfaces.common.NameValuePair;
31 import org.miloss.fgsms.services.interfaces.common.PolicyType;
32 import org.miloss.fgsms.services.interfaces.common.ProcessPerformanceData;
33 import org.miloss.fgsms.services.interfaces.datacollector.AddDataRequestMsg;
34 import org.miloss.fgsms.services.interfaces.datacollector.BrokerData;
35 import org.miloss.fgsms.services.interfaces.policyconfiguration.MachinePolicy;
36 import org.miloss.fgsms.services.interfaces.policyconfiguration.ProcessPolicy;
37 import org.miloss.fgsms.services.interfaces.policyconfiguration.ServicePolicy;
38 import org.miloss.fgsms.services.interfaces.status.SetStatusRequestMsg;
39 import org.miloss.fgsms.sla.NonTransactionalSLAProcessor;
40
41
42
43
44
45 public class HighCPUUsageOverTime implements SLARuleInterface {
46
47 @Override
48 public boolean CheckTransactionalRule(SetStatusRequestMsg req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
49 return false;
50 }
51
52 @Override
53 public boolean CheckTransactionalRule(ProcessPerformanceData req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
54 return false;
55 }
56
57 @Override
58 public boolean CheckTransactionalRule(MachinePerformanceData req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
59 return false;
60 }
61
62 @Override
63 public boolean CheckTransactionalRule(AddDataRequestMsg req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
64 return false;
65 }
66
67 @Override
68 public boolean CheckTransactionalRule(String url, List<BrokerData> data, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
69 return false;
70 }
71
72 @Override
73 public boolean CheckNonTransactionalRule(ServicePolicy pol, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg, boolean pooled) {
74 if (nullableFaultMsg == null) {
75 nullableFaultMsg = new AtomicReference<String>();
76 }
77 NameValuePair GetNameValuePairByName = Utility.getNameValuePairByName(params, "value");
78 long rate = Long.parseLong(GetNameValuePairByName.getValue());
79 GetNameValuePairByName = Utility.getNameValuePairByName(params, "duration");
80 long duration = Long.parseLong(GetNameValuePairByName.getValue());
81 long faultrate = NonTransactionalSLAProcessor.GetCPUUsageOverTime(pol.getURL(),duration, pooled);
82 if (faultrate > rate) {
83 nullableFaultMsg.set("CPU Usage measured value of " + faultrate + " is greater than the threshold of " + rate + ", " + nullableFaultMsg.get());
84 return true;
85 }
86 return false;
87 }
88
89 @Override
90 public String GetDisplayName() {
91 return "High CPU Usage over time";
92 }
93
94 @Override
95 public String GetHtmlFormattedHelp() {
96 return "This rule will trigger when the CPU utilization is greater than the specified value for the specified duration."
97 + "This rule is processed periodically as part of the Non-Transactional SLA Processor, who execution cycle is controled by the administrator. "
98 + "This rule applies to both Machine and Process level policies.<br><br>"
99 + "Required parameters:"
100 + "<ul>"
101 + "<li>value - must be a positive integer. This is the threshold in percentage, 0-100</li>"
102 + "<li>duration - must be a positive integer or long. This represents time in milliseconds for the duration. It must be one of the values from which"
103 + "statistics are aggregated. By default, the following durations are calculated 5 minutes (300000ms), 15 minutes (900000ms), 1 hour (3600000ms),"
104 + "and 24 hours (1440000ms). Administrators can add additional time periods for aggregation via General Settings."
105 + "</il>"
106 + "</ul>";
107 }
108
109 @Override
110 public List<NameValuePair> GetRequiredParameters() {
111 ArrayList<NameValuePair> arrayList = new ArrayList<NameValuePair>();
112 arrayList.add(Utility.newNameValuePair("value", null, false, false));
113 arrayList.add(Utility.newNameValuePair("duration", null, false, false));
114
115 return arrayList;
116 }
117
118 @Override
119 public List<NameValuePair> GetOptionalParameters() {
120 return new ArrayList<NameValuePair>();
121 }
122
123 @Override
124 public boolean ValidateConfiguration(List<NameValuePair> params, AtomicReference<String> outError,ServicePolicy policy) {
125 if (outError == null) {
126 outError = new AtomicReference<String>();
127 }
128 if (params == null || params.isEmpty()) {
129 outError.set("The parameter 'value' is required. " + outError.get());
130 }
131 if (!(policy instanceof ProcessPolicy) && !(policy instanceof MachinePolicy)) {
132 outError.set("This rule only applies to Machine and Process Policies. " + outError.get());
133 }
134 boolean foundLogger = false;
135 boolean foundduration = false;
136 for (int i = 0; i < params.size(); i++) {
137 if (params.get(i).getName().equals("value")) {
138 foundLogger = true;
139 if (Utility.stringIsNullOrEmpty(params.get(i).getValue())) {
140 outError.set("A value must be specified for the parameter 'value'. " + outError.get());
141 }
142 try {
143 long x = Long.parseLong(params.get(i).getValue());
144 if (x <= 0 || x > 100) {
145 outError.set("The parameter 'value' must be greater than zero and 100 or less. " + outError.get());
146 }
147 } catch (Exception ex) {
148 outError.set("Bad value for parameter 'value'. It must be an integer or long. Message:" + ex.getMessage() + ". " + outError.get());
149 }
150 }
151 if (params.get(i).getName().equals("duration")) {
152 foundduration = true;
153 if (Utility.stringIsNullOrEmpty(params.get(i).getValue())) {
154 outError.set("A value must be specified for the parameter 'duration'. " + outError.get());
155 }
156 try {
157 long x = Long.parseLong(params.get(i).getValue());
158 if (x <= 0) {
159 outError.set("The parameter 'duration' must be greater than zero. " + outError.get());
160 }
161 } catch (Exception ex) {
162 outError.set("Bad value for parameter 'duration'. It must be an integer or long. Message:" + ex.getMessage() + ". " + outError.get());
163 }
164 }
165 }
166 if (!foundLogger) {
167 outError.set("The parameter 'value' is required. " + outError.get());
168 }
169 if (!foundduration) {
170 outError.set("The parameter 'duration' is required. " + outError.get());
171 }
172 if (Utility.stringIsNullOrEmpty(outError.get())) {
173 return true;
174 } else {
175 return false;
176 }
177 }
178
179 @Override
180 public org.miloss.fgsms.plugins.sla.AlertType GetType() {
181 return org.miloss.fgsms.plugins.sla.AlertType.Performance;
182 }
183
184 @Override
185 public String GetHtmlFormattedDisplay(List<NameValuePair> params) {
186 NameValuePair mc = Utility.getNameValuePairByName(params, "value");
187 String item = UNDEFINED_VALUE;
188 if (mc != null) {
189 item = mc.getValue();
190 if (mc.isEncrypted() || mc.isEncryptOnSave()) {
191 item = ENCRYPTED_MASK;
192 }
193 }
194
195 NameValuePair mc2 = Utility.getNameValuePairByName(params, "duration");
196 String item2 = UNDEFINED_VALUE;
197 if (mc2 != null) {
198 item2 = mc2.getValue();
199 if (mc2.isEncrypted() || mc2.isEncryptOnSave()) {
200 item2 = ENCRYPTED_MASK;
201 }
202 }
203 return Utility.encodeHTML(GetDisplayName() + " " + item + "/" + item2+"ms");
204 }
205
206 @Override
207 public List<PolicyType> GetAppliesTo() {
208 List<PolicyType> x = new ArrayList<PolicyType>();
209 x.add(PolicyType.MACHINE);
210 x.add(PolicyType.PROCESS);
211 return x;
212 }
213 }
214