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.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   * @author AO
43   */
44  public class HighThreadCount 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          if (nullableFaultMsg != null) {
54              nullableFaultMsg = new AtomicReference<String>();
55          }
56          NameValuePair value = Utility.getNameValuePairByName(params, "value");
57          int x = 0;
58          if (value.isEncrypted()) {
59              x = Integer.parseInt(Utility.DE(value.getValue()));
60          } else {
61              x = Integer.parseInt(value.getValue());
62          }
63          if (req.getNumberofActiveThreads() != null && x > req.getNumberofActiveThreads().longValue()) {
64              nullableFaultMsg.set("The number of active threads, " + req.getNumberofActiveThreads() + " is higher than the threshold of " + x + ", " + nullableFaultMsg.get());
65              return true;
66          }
67          return false;
68      }
69      
70      @Override
71      public boolean CheckTransactionalRule(MachinePerformanceData req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
72          if (nullableFaultMsg != null) {
73              nullableFaultMsg = new AtomicReference<String>();
74          }
75          NameValuePair value = Utility.getNameValuePairByName(params, "value");
76          int x = 0;
77          if (value.isEncrypted()) {
78              x = Integer.parseInt(Utility.DE(value.getValue()));
79          } else {
80              x = Integer.parseInt(value.getValue());
81          }
82          if (req.getNumberofActiveThreads() != null && x > req.getNumberofActiveThreads().longValue()) {
83              nullableFaultMsg.set("The number of active threads, " + req.getNumberofActiveThreads() + " is higher than the threshold of " + x + ", " + nullableFaultMsg.get());
84              return true;
85          }
86          return false;
87      }
88      
89      @Override
90      public boolean CheckTransactionalRule(AddDataRequestMsg req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
91          return false;
92      }
93      
94      @Override
95      public boolean CheckTransactionalRule(String url, List<BrokerData> data, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
96          return false;
97      }
98      
99      @Override
100     public boolean CheckNonTransactionalRule(ServicePolicy pol, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg, boolean pooled) {
101         return false;
102     }
103     
104     @Override
105     public String GetDisplayName() {
106         return "High Thread Count";
107     }
108     
109     @Override
110     public String GetHtmlFormattedHelp() {
111         return "This rule will trigger when a process or machine has a high number of threads running. This applies to machine and process service policies only.<br><br>"
112                 + "Required Parameters:"
113                 + "<ul>"
114                 + "<li>value - some positive integer value representing the threshold such as if the thread count is greater than the value, the rule is triggered.</li>"
115                 + "</ul>";
116         
117     }
118     
119     @Override
120     public List<NameValuePair> GetRequiredParameters() {
121         List<NameValuePair> items = new ArrayList<NameValuePair>();
122         items.add(Utility.newNameValuePair("value", null, false, false));
123         return items;
124     }
125     
126     @Override
127     public List<NameValuePair> GetOptionalParameters() {
128         return new ArrayList<NameValuePair>();
129     }
130     
131     @Override
132     public boolean ValidateConfiguration(List<NameValuePair> params, AtomicReference<String> outError,ServicePolicy policy) {
133         if (outError == null) {
134             outError = new AtomicReference<String>();
135         }
136         if (params == null || params.isEmpty()) {
137             outError.set("The parameter 'value'  is required. " + outError.get());
138         }
139              if (!(policy instanceof ProcessPolicy) && !(policy instanceof MachinePolicy)) {
140             outError.set("This rule only applies to Machine and Process Policies. " + outError.get());
141         }
142         boolean foundSubject = false;
143         
144         for (int i = 0; i < params.size(); i++) {
145             if (params.get(i).getName().equals("value")) {
146                 foundSubject = true;
147                 if (Utility.stringIsNullOrEmpty(params.get(i).getValue())) {
148                     outError.set("A value must be specified for the parameter 'value'. " + outError.get());
149                 }
150                 
151                 try {
152                     int x = -1;
153                     if (params.get(i).isEncrypted()) {
154                         x = Integer.parseInt(Utility.DE(params.get(i).getValue()));
155                     } else {
156                         x = Integer.parseInt((params.get(i).getValue()));
157                     }
158                     if (x < 0) {
159                         outError.set("The parameter 'value'  must be greater than zero. " + outError.get());
160                     }
161                 } catch (Exception ex) {
162                     outError.set("Could not parse the value of  'value'  to an integer. Error: " + ex.getMessage() + ". " + outError.get());
163                 }
164             }
165             
166         }
167         if (!foundSubject) {
168             outError.set("The parameter 'value'  is required. " + outError.get());
169         }
170         if (Utility.stringIsNullOrEmpty(outError.get())) {
171             return true;
172         } else {
173             return false;
174         }
175     }
176     
177        @Override
178     public AlertType GetType() {
179        return AlertType.Performance;
180     }
181        
182        @Override
183     public String GetHtmlFormattedDisplay(List<NameValuePair> params) {
184        NameValuePair mc = Utility.getNameValuePairByName(params, "value");
185         String item = UNDEFINED_VALUE;
186         if (mc != null) {
187             item = mc.getValue();
188             if (mc.isEncrypted() || mc.isEncryptOnSave()) {
189                 item = ENCRYPTED_MASK;
190             }
191         }
192 
193         return Utility.encodeHTML(GetDisplayName() + " " + item );
194     }
195        
196          @Override
197     public List<PolicyType> GetAppliesTo() {
198          List<PolicyType> x = new ArrayList<PolicyType>();
199          x.add(PolicyType.MACHINE);
200          x.add(PolicyType.PROCESS);
201          return x;
202     }
203 }