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