View Javadoc

1   /*
2    * Copyright 2010 Capgemini and Contributors
3    *
4    * Licensed under the Apache License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.appstatus.core.check.impl;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Properties;
21  
22  import net.sf.appstatus.core.AppStatus;
23  import net.sf.appstatus.core.check.AbstractCheck;
24  import net.sf.appstatus.core.check.IAppStatusAware;
25  import net.sf.appstatus.core.check.ICheckResult;
26  import net.sf.appstatus.core.services.IService;
27  
28  import org.apache.commons.lang3.StringUtils;
29  
30  /**
31   * @author Nicolas Richeton
32   *
33   */
34  public class ServicesFailureCheck extends AbstractCheck implements IAppStatusAware {
35  
36  	private AppStatus appStatus;
37  	private int limitError = 10;
38  	private int limitWarn = 5;
39  
40  	@Override
41  	public ICheckResult checkStatus() {
42  		List<IService> services = appStatus.getServiceManager().getServices();
43  		List<String> warns = new ArrayList<String>();
44  		List<String> errors = new ArrayList<String>();
45  
46  		for (IService s : services) {
47  			if (s.getHits() == 0) {
48  				continue;
49  			}
50  
51  			long failureRatio = (s.getFailures() * 100) / s.getHits();
52  			if (failureRatio > limitError) {
53  				errors.add("Service <b>" + s.getGroup() + "#" + s.getName() + "</b> failure ratio (" + failureRatio
54  						+ "%) is over error limit (" + limitError + "%)");
55  			} else if (failureRatio > limitWarn) {
56  				warns.add("Service <b>" + s.getGroup() + "#" + s.getName() + "</b> failure ratio (" + failureRatio
57  						+ "%) is over warn limit (" + limitWarn + "%)");
58  			}
59  		}
60  
61  		ICheckResult result = null;
62  		if (errors.size() > 0) {
63  			String description = StringUtils.join(errors, "<br/>");
64  			if (warns.size() > 0) {
65  				description = description + " <br/>Additional warnings: " + StringUtils.join(warns, "<br/>");
66  			}
67  			result = result(this).code(ICheckResult.ERROR).fatal().description(description).build();
68  		} else if (warns.size() > 0) {
69  			result = result(this).code(ICheckResult.ERROR).description(StringUtils.join(warns, "<br/>")).build();
70  		} else {
71  			result = result(this).code(ICheckResult.OK).description("All failure ratios under " + limitWarn + "%")
72  					.build();
73  		}
74  		return result;
75  	}
76  
77  	public String getGroup() {
78  		return "Services";
79  	}
80  
81  	public String getName() {
82  		return "Failures";
83  	}
84  
85  	public void setAppStatus(AppStatus appStatus) {
86  		this.appStatus = appStatus;
87  
88  	}
89  
90  	@Override
91  	public void setConfiguration(Properties configuration) {
92  		super.setConfiguration(configuration);
93  
94  		String error = getConfiguration().getProperty("serviceFailureCheck.limitError");
95  		if (error != null) {
96  			limitError = Integer.valueOf(error);
97  		}
98  
99  		String warn = getConfiguration().getProperty("serviceFailureCheck.limitWarn");
100 		if (warn != null) {
101 			limitWarn = Integer.valueOf(warn);
102 		}
103 	}
104 }