1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.backsource.alert.agent;
21 import java.util.List;
22 import java.util.Iterator;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import org.backsource.alert.PropertyUtil;
28 import org.backsource.alert.AlertError;
29 import org.backsource.alert.util.Drive;
30 /***
31 * Test for disk usage.
32 *
33 * <p>The test may be run without any property settings, in which case it will
34 * check that no disks on the system exceds a 93% diskusage. It is also possible to configure it with the following properties: </p>
35 * <ul>
36 <li>{@link #INCLUDE}, a space separated list of disks or filesystems to inlude, if only one disk is mentioned, only that one is checked, if no one is mention ass is checked that are not int the exclude list. Excluded disks has precedence ower included disks.
37 </li>
38 <li>{@link #EXCLUDE}, a space separated list of disks or filesystems to exclude from the check, could for example be cdrom.
39 </li>
40 <li>{@link #MAX_PERCENT}, max percent usage of disk allowed.</li>
41 </ul>
42 *
43 * @author <a href="mailto:pra@tim.se">Peter Antman</a>
44 * @version $Revision: 1.1.1.1 $
45 */
46
47 public class DiskCheckTest extends BaseCase {
48 static final Log log = LogFactory.getLog( DiskCheckTest.class);
49 /*** agent.disk.include */
50 public static String INCLUDE = "agent.disk.include";
51 /*** agent.disk.exclude */
52 public static String EXCLUDE = "agent.disk.exclude";
53 /*** agent.disk.maxPercentUsed */
54 public static String MAX_PERCENT= "agent.disk.maxPercentUsed";
55 private List include;
56 private List exclude;
57 private int maxPercent = 93;
58
59 public DiskCheckTest (String name){
60 super(name);
61 }
62
63 protected void setUp() throws Exception{
64 super.setUp();
65 include = PropertyUtil.getValues( prop.getProperty(INCLUDE) );
66 exclude = PropertyUtil.getValues( prop.getProperty(EXCLUDE) );
67 String tmp = prop.getProperty(MAX_PERCENT);
68 if ( tmp != null) {
69 maxPercent = Integer.parseInt( tmp );
70 }
71
72 }
73
74 public void testDiskSpace() throws Exception {
75 Drive[] drives = null;
76 if ( include.size() == 1) {
77
78 Drive d = Drive.getDrive( (String)include.get(0) );
79 if ( d != null) {
80 drives = new Drive[] { d };
81 } else {
82 throw new AlertError("drive not found: " + include.get(0));
83 }
84
85 } else {
86
87 drives = Drive.getDrives();
88 }
89
90
91 StringBuffer errors = new StringBuffer();
92 boolean foundErrors = false;
93 for ( int i = 0;i<drives.length;i++) {
94 Drive d = drives[i];
95 if ( check(d) ) {
96 log.debug("Checking drive " + d.getDriveName());
97 if ( d.getPercentUsed() > maxPercent) {
98 foundErrors = true;
99 errors.append("Disk usage exceeds ").
100 append(maxPercent).
101 append("% usage for drive ").
102 append(d.getDriveName()).
103 append(" (").append(d).append(")\n");
104 }
105
106 }
107
108 }
109 if ( foundErrors ) {
110 throw new AlertError("Disks are full: " + errors.toString());
111 }
112
113 }
114
115 private boolean check(Drive d) {
116
117 if ( isExcluded( d.getDriveName()) ) {
118 return false;
119 } else if ( isExcluded( d.getFile().getPath() )) {
120 return false;
121 }
122
123
124 if ( isIncluded( d.getDriveName()) ) {
125 return true;
126 } else if ( isIncluded( d.getFile().getPath() )) {
127 return true;
128 } else {
129 return false;
130 }
131
132
133
134 }
135
136 /***
137 * Check if disk is inlcuded, an empty include list means all disks are inclued.
138 */
139 private boolean isIncluded(String name) {
140
141 if ( include.size() > 0) {
142 return isFound(include, name);
143 } else {
144 return true;
145 }
146
147 }
148
149 private boolean isExcluded(String name) {
150 return isFound(exclude,name);
151 }
152
153
154 private boolean isFound(List haystack, String needle) {
155 Iterator i = haystack.iterator();
156 while ( i.hasNext() ) {
157 String check = (String)i.next();
158 if ( check.equals(needle)) {
159 return true;
160 }
161
162 }
163 return false;
164 }
165
166 }