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
40
41
42
43
44 public class HighMemoryUsage implements SLARuleInterface {
45
46 @Override
47 public boolean CheckTransactionalRule(SetStatusRequestMsg req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
48 return false;
49 }
50
51 @Override
52 public boolean CheckTransactionalRule(ProcessPerformanceData req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
53 return false;
54 }
55
56 @Override
57 public boolean CheckTransactionalRule(MachinePerformanceData req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
58 if (nullableFaultMsg == null) {
59 nullableFaultMsg = new AtomicReference<String>();
60 }
61 if (req.getBytesusedMemory() == null) {
62 return false;
63 }
64 NameValuePair GetNameValuePairByName = Utility.getNameValuePairByName(params, "value");
65 long rate = Long.parseLong(GetNameValuePairByName.getValue());
66 long faultrate = req.getBytesusedMemory().longValue();
67 if (faultrate < rate) {
68 nullableFaultMsg.set("The measured CPU Usage of " + faultrate + " is higher than " + rate + ", " + nullableFaultMsg.get());
69 return true;
70 }
71 return false;
72 }
73
74 @Override
75 public boolean CheckTransactionalRule(AddDataRequestMsg req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
76 return false;
77 }
78
79 @Override
80 public boolean CheckTransactionalRule(String url, List<BrokerData> data, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
81 return false;
82 }
83
84 @Override
85 public boolean CheckNonTransactionalRule(ServicePolicy pol, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg, boolean pooled) {
86
87 return false;
88 }
89
90 @Override
91 public String GetDisplayName() {
92 return "High CPU Usage";
93 }
94
95 @Override
96 public String GetHtmlFormattedHelp() {
97 return "This rule will trigger when the Memory usage is higher than the specified value."
98 + "This rule is processed as data is sent from an agent and represents the instantaneous measured value. "
99 + "This rule applies to machine policies only.<br><br>"
100 + "Required parameters:"
101 + "<ul>"
102 + "<li>value - must be a positive integer. This is the threshold in bytes.</li>"
103 + "</ul>";
104 }
105
106 @Override
107 public List<NameValuePair> GetRequiredParameters() {
108 ArrayList<NameValuePair> arrayList = new ArrayList<NameValuePair>();
109 arrayList.add(Utility.newNameValuePair("value", null, false, false));
110
111 return arrayList;
112 }
113
114 @Override
115 public List<NameValuePair> GetOptionalParameters() {
116 return new ArrayList<NameValuePair>();
117 }
118
119 @Override
120 public boolean ValidateConfiguration(List<NameValuePair> params, AtomicReference<String> outError,ServicePolicy policy) {
121 if (outError == null) {
122 outError = new AtomicReference<String>();
123 }
124 if (params == null || params.isEmpty()) {
125 outError.set("The parameter 'value' is required. " + outError.get());
126 }
127 if (!(policy instanceof ProcessPolicy) && !(policy instanceof MachinePolicy)) {
128 outError.set("This rule only applies to Machine and Process Policies. " + outError.get());
129 }
130 boolean foundLogger = false;
131
132 for (int i = 0; i < params.size(); i++) {
133 if (params.get(i).getName().equals("value")) {
134 foundLogger = true;
135 if (Utility.stringIsNullOrEmpty(params.get(i).getValue())) {
136 outError.set("A value must be specified for the parameter 'value'. " + outError.get());
137 }
138 try {
139 long x = Long.parseLong(params.get(i).getValue());
140 if (x <= 0) {
141 outError.set("The parameter 'value' must be greater than zero. " + outError.get());
142 }
143 } catch (Exception ex) {
144 outError.set("Bad value for parameter 'value'. It must be an integer or long. Message:" + ex.getMessage() + ". " + outError.get());
145 }
146 }
147 }
148 if (!foundLogger) {
149 outError.set("The parameter 'value' is required. " + outError.get());
150 }
151
152 if (Utility.stringIsNullOrEmpty(outError.get())) {
153 return true;
154 } else {
155 return false;
156 }
157
158 }
159
160 @Override
161 public org.miloss.fgsms.plugins.sla.AlertType GetType() {
162 return org.miloss.fgsms.plugins.sla.AlertType.Performance;
163 }
164
165 @Override
166 public String GetHtmlFormattedDisplay(List<NameValuePair> params) {
167 NameValuePair mc = Utility.getNameValuePairByName(params, "value");
168 String item = UNDEFINED_VALUE;
169 if (mc != null) {
170 item = mc.getValue();
171 if (mc.isEncrypted() || mc.isEncryptOnSave()) {
172 item = ENCRYPTED_MASK;
173 }
174 }
175
176
177 return Utility.encodeHTML(GetDisplayName() + " " + item );
178 }
179
180 @Override
181 public List<PolicyType> GetAppliesTo() {
182 List<PolicyType> x = new ArrayList<PolicyType>();
183 x.add(PolicyType.MACHINE);
184 x.add(PolicyType.PROCESS);
185 return x;
186 }
187 }
188