1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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 }