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.AlertType;
29  import org.miloss.fgsms.plugins.sla.SLARuleInterface;
30  import org.miloss.fgsms.services.interfaces.common.MachinePerformanceData;
31  import org.miloss.fgsms.services.interfaces.common.NameValuePair;
32  import org.miloss.fgsms.services.interfaces.common.PolicyType;
33  import org.miloss.fgsms.services.interfaces.common.ProcessPerformanceData;
34  import org.miloss.fgsms.services.interfaces.datacollector.AddDataRequestMsg;
35  import org.miloss.fgsms.services.interfaces.datacollector.BrokerData;
36  import org.miloss.fgsms.services.interfaces.policyconfiguration.ServicePolicy;
37  import org.miloss.fgsms.services.interfaces.policyconfiguration.TransactionalWebServicePolicy;
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 MeanTimeBetweenFailureLessThan  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  
78          NameValuePair GetNameValuePairByName = Utility.getNameValuePairByName(params, "duration");
79          long duration = Long.parseLong(GetNameValuePairByName.getValue());
80             NameValuePair rater = Utility.getNameValuePairByName(params, "value");
81          long rate = Long.parseLong(rater.getValue());
82          //    long rate = x.getFaults();//) / (double) (Utility.durationToTimeInMS(x.getTime())));
83          long faultrate = NonTransactionalSLAProcessor.GrabMTBF(duration, pol.getURL());
84          if (faultrate < duration) {
85              nullableFaultMsg.set("MTBF measured value of " + faultrate + " is less than " + rate + ", " + nullableFaultMsg.get());
86              return true;
87          }
88          return false;
89      }
90  
91      @Override
92      public String GetDisplayName() {
93          return "MTBF is less than (value)";
94      }
95  
96      @Override
97      public String GetHtmlFormattedHelp() {
98          return "This rule will trigger when the Mean Time Between Failure is less than the specified duration."
99                  + "This rule is processed periodically as part of the Non-Transactional SLA Processor, who execution cycle is controled by the administrator. "
100                 + "This rule applies to transactional service policies only.<Br><br>"
101                 + "Required parameters:"
102                 + "<ul>"
103                      + "<li>value - must be a positive integer. This is the threshold</li>"
104                 + "<li>duration - must be a positive integer or long. This represents time in milliseconds for the duration for search through. It must be one of the values from which"
105                 + "statistics are aggregated. By default, the following durations are calculated 5 minutes (300000ms), 15 minutes (900000ms), 1 hour (3600000ms),"
106                 + "and 24 hours (1440000ms). Administrators can add additional time periods for aggregation via General Settings."
107                 + "</il>"
108                 + "</ul>";
109     }
110 
111     @Override
112     public List<NameValuePair> GetRequiredParameters() {
113         ArrayList<NameValuePair> arrayList = new ArrayList<NameValuePair>();
114         //    arrayList.add(Utility.newNameValuePair("value", null, false, false));
115         arrayList.add(Utility.newNameValuePair("duration", null, false, false));
116 
117         return arrayList;
118     }
119 
120     @Override
121     public List<NameValuePair> GetOptionalParameters() {
122         return new ArrayList<NameValuePair>();
123     }
124 
125     @Override
126     public boolean ValidateConfiguration(List<NameValuePair> params, AtomicReference<String> outError,ServicePolicy policy) {
127         if (outError == null) {
128             outError = new AtomicReference<String>();
129         }
130         if (params == null || params.isEmpty()) {
131             outError.set("The parameter 'value' is required. " + outError.get());
132         }
133               if (!(policy instanceof TransactionalWebServicePolicy)) {
134             outError.set("This rule only applies to Transactional Service Policies. " + outError.get());
135         }
136         //   boolean foundLogger = false;
137         boolean foundduration = false;
138         for (int i = 0; i < params.size(); i++) {
139 
140             if (params.get(i).getName().equals("duration")) {
141                 foundduration = true;
142                 if (Utility.stringIsNullOrEmpty(params.get(i).getValue())) {
143                     outError.set("A value must be specified for the parameter 'duration'. " + outError.get());
144                 }
145                 try {
146                     long x = Long.parseLong(params.get(i).getValue());
147                     if (x <= 0) {
148                         outError.set("The parameter 'duration' must be greater than zero. " + outError.get());
149                     }
150                 } catch (Exception ex) {
151                     outError.set("Bad value for parameter 'duration'. It must be an integer or long. Message:" + ex.getMessage() + ". " + outError.get());
152                 }
153             }
154         }
155 
156         if (!foundduration) {
157             outError.set("The parameter 'duration' is required. " + outError.get());
158         }
159         if (Utility.stringIsNullOrEmpty(outError.get())) {
160             return true;
161         } else {
162             return false;
163         }
164     }
165     
166        @Override
167     public AlertType GetType() {
168        return AlertType.Performance;
169     }
170        
171        @Override
172     public String GetHtmlFormattedDisplay(List<NameValuePair> params) {
173         NameValuePair mc = Utility.getNameValuePairByName(params, "value");
174         String item = UNDEFINED_VALUE;
175         if (mc != null) {
176             item = mc.getValue();
177             if (mc.isEncrypted() || mc.isEncryptOnSave()) {
178                 item = ENCRYPTED_MASK;
179             }
180         }
181         return Utility.encodeHTML(GetDisplayName() + " " + item + " ms");
182     }
183        
184               @Override
185     public List<PolicyType> GetAppliesTo() {
186          List<PolicyType> x = new ArrayList<PolicyType>();
187          x.add(PolicyType.TRANSACTIONAL);
188         
189          
190          return x;
191     }
192 }