View Javadoc
1   /**
2    * This Source Code Form is subject to the terms of the Mozilla Public
3    * License, v. 2.0. If a copy of the MPL was not distributed with this
4    * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5    *
6    * If it is not possible or desirable to put the notice in a particular
7    * file, then You may include the notice in a location (such as a LICENSE
8    * file in a relevant directory) where a recipient would be likely to look
9    * for such a notice.
10  
11   * 
12   */
13   
14  /*  ---------------------------------------------------------------------------
15   *  U.S. Government, Department of the Army
16   *  Army Materiel Command
17   *  Research Development Engineering Command
18   *  Communications Electronics Research Development and Engineering Center
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   * @author AO
44   */
45  public class HighMemoryUsageOverTime 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.GetMemoryUsageOverTime(pol.getURL(),duration, pooled);
82          if (faultrate > rate) {
83              nullableFaultMsg.set("The measured Memory usage of " + faultrate + " is greater than " + rate + ", " + nullableFaultMsg.get());
84              return true;
85          }
86          return false;
87      }
88  
89      @Override
90      public String GetDisplayName() {
91          return "High Disk I/O Rates";
92      }
93  
94      @Override
95      public String GetHtmlFormattedHelp() {
96          return "This rule will trigger when the Memory utilization rate 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 policies.<br><br>"
99                  + "Required parameters:"
100                 + "<ul>"
101                 + "<li>value - must be a positive integer. This is the threshold in KB</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) {
145                         outError.set("The parameter 'value' must be greater than zero. " + 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                 @Override
179     public org.miloss.fgsms.plugins.sla.AlertType GetType() {
180        return org.miloss.fgsms.plugins.sla.AlertType.Performance;
181     }
182                 
183                 @Override
184     public String GetHtmlFormattedDisplay(List<NameValuePair> params) {
185        NameValuePair mc = Utility.getNameValuePairByName(params, "value");
186         String item = UNDEFINED_VALUE;
187         if (mc != null) {
188             item = mc.getValue();
189             if (mc.isEncrypted() || mc.isEncryptOnSave()) {
190                 item = ENCRYPTED_MASK;
191             }
192         }
193         
194         NameValuePair mc2 = Utility.getNameValuePairByName(params, "duration");
195         String item2 = UNDEFINED_VALUE;
196         if (mc2 != null) {
197             item2 = mc2.getValue();
198             if (mc2.isEncrypted() || mc2.isEncryptOnSave()) {
199                 item2 = ENCRYPTED_MASK;
200             }
201         }
202         return Utility.encodeHTML(GetDisplayName() + " " + item + "/" + item2+"ms");
203     }
204                 
205                   @Override
206     public List<PolicyType> GetAppliesTo() {
207          List<PolicyType> x = new ArrayList<PolicyType>();
208          x.add(PolicyType.MACHINE);
209          x.add(PolicyType.PROCESS);
210          return x;
211     }
212 }
213 
214