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.SLARuleInterface;
28  import org.miloss.fgsms.services.interfaces.common.MachinePerformanceData;
29  import org.miloss.fgsms.services.interfaces.common.NameValuePair;
30  import org.miloss.fgsms.services.interfaces.common.PolicyType;
31  import org.miloss.fgsms.services.interfaces.common.ProcessPerformanceData;
32  import org.miloss.fgsms.services.interfaces.datacollector.AddDataRequestMsg;
33  import org.miloss.fgsms.services.interfaces.datacollector.BrokerData;
34  import org.miloss.fgsms.services.interfaces.policyconfiguration.MachinePolicy;
35  import org.miloss.fgsms.services.interfaces.policyconfiguration.ProcessPolicy;
36  import org.miloss.fgsms.services.interfaces.policyconfiguration.ServicePolicy;
37  import org.miloss.fgsms.services.interfaces.status.SetStatusRequestMsg;
38  
39  /**
40   *
41   * @author AO
42   */
43  public class HighCPUUsage implements SLARuleInterface {
44  
45      @Override
46      public boolean CheckTransactionalRule(SetStatusRequestMsg req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
47          return false;
48      }
49  
50      @Override
51      public boolean CheckTransactionalRule(ProcessPerformanceData req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
52          return false;
53      }
54  
55      @Override
56      public boolean CheckTransactionalRule(MachinePerformanceData req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
57          if (nullableFaultMsg == null) {
58              nullableFaultMsg = new AtomicReference<String>();
59          }
60          if (req.getPercentusedCPU() == null) {
61              return false;
62          }
63          NameValuePair GetNameValuePairByName = Utility.getNameValuePairByName(params, "value");
64          long rate = -1;
65          if (GetNameValuePairByName.isEncrypted()) {
66              rate = Long.parseLong(Utility.DE(GetNameValuePairByName.getValue()));
67          } else {
68              rate = Long.parseLong(GetNameValuePairByName.getValue());
69          }
70          long faultrate = req.getPercentusedCPU().longValue();
71          if (faultrate < rate) {
72              nullableFaultMsg.set("The measured CPU Usage of " + faultrate + " is higher than " + rate + ", " + nullableFaultMsg.get());
73              return true;
74          }
75          return false;
76      }
77  
78      @Override
79      public boolean CheckTransactionalRule(AddDataRequestMsg req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
80          return false;
81      }
82  
83      @Override
84      public boolean CheckTransactionalRule(String url, List<BrokerData> data, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
85          return false;
86      }
87  
88      @Override
89      public boolean CheckNonTransactionalRule(ServicePolicy pol, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg, boolean pooled) {
90  
91          return false;
92      }
93  
94      @Override
95      public String GetDisplayName() {
96          return "High CPU Usage";
97      }
98  
99      @Override
100     public String GetHtmlFormattedHelp() {
101         return "This rule will trigger when the CPU usage is higher than the specified value."
102                 + "This rule is processed as data is sent from an agent and represents the instantaneous measured value. "
103                 + "This rule applies to machine policies only.<br><br>"
104                 + "Required parameters:"
105                 + "<ul>"
106                 + "<li>value - must be a positive integer. This is the threshold in a percentage 0-100.</li>"
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 
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 
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 || x > 100) {
145                         outError.set("The parameter 'value' must be greater than zero and 100 or less. " + 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         }
152         if (!foundLogger) {
153             outError.set("The parameter 'value' is required. " + outError.get());
154         }
155 
156         if (Utility.stringIsNullOrEmpty(outError.get())) {
157             return true;
158         } else {
159             return false;
160         }
161     }
162 
163     @Override
164     public org.miloss.fgsms.plugins.sla.AlertType GetType() {
165         return org.miloss.fgsms.plugins.sla.AlertType.Performance;
166     }
167     
168     @Override
169     public String GetHtmlFormattedDisplay(List<NameValuePair> params) {
170        NameValuePair mc = Utility.getNameValuePairByName(params, "value");
171         String item = UNDEFINED_VALUE;
172         if (mc != null) {
173             item = mc.getValue();
174             if (mc.isEncrypted() || mc.isEncryptOnSave()) {
175                 item = ENCRYPTED_MASK;
176             }
177         }
178         
179      
180         return Utility.encodeHTML(GetDisplayName() + " " + item);
181     }
182     
183      @Override
184     public List<PolicyType> GetAppliesTo() {
185          List<PolicyType> x = new ArrayList<PolicyType>();
186          x.add(PolicyType.MACHINE);
187          x.add(PolicyType.PROCESS);
188          return x;
189     }
190 }