View Javadoc

1   /*
2    * Copyright (c) 2003 Peter Antman, Teknik i Media  <peter.antman@tim.se>
3    *
4    * $Id: DiskCheckTest.java,v 1.1.1.1 2004/05/19 12:33:41 pra Exp $
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version
10   * 
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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;//2 percent left until only root can write!
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        } // end of if ()
71        
72     }
73  
74     public void testDiskSpace() throws Exception {
75        Drive[] drives = null;
76        if ( include.size() == 1) {
77           // Only check that one disk
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           } // end of else
84  
85        } else {
86           // Check all disk
87           drives = Drive.getDrives();
88        } // end of else
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             } // end of if ()
105             
106          } // end of if ()
107          
108       } // end of for ()
109       if ( foundErrors ) {
110          throw new AlertError("Disks are full: " + errors.toString());
111       } // end of if ()
112    
113    }
114 
115    private boolean check(Drive d) {
116       // Check if its excluded first
117       if ( isExcluded( d.getDriveName()) ) {
118          return false;
119       } else if ( isExcluded( d.getFile().getPath() )) {
120          return false;
121       } // end of else if ()
122 
123       // if we are here it is not excluded, is it included
124       if ( isIncluded( d.getDriveName()) ) {
125          return true;
126       } else if ( isIncluded( d.getFile().getPath() )) {
127          return true;
128       } else {
129          return false;
130       } // end of else
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       // Included is only activated if include is > 0
141       if ( include.size() > 0) {
142          return isFound(include, name);
143       } else {
144          return true;
145       } // end of else
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          } // end of if ()
161          
162       } // end of while ()
163       return false;
164    }
165    
166 }// DiskCheckTest