View Javadoc

1   package org.apache.helix.healthcheck;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.lang.management.ManagementFactory;
23  import java.lang.management.OperatingSystemMXBean;
24  import java.util.HashMap;
25  import java.util.Map;
26  import java.util.TreeMap;
27  
28  import org.apache.helix.ZNRecord;
29  import org.apache.log4j.Logger;
30  
31  
32  class DefaultHealthReportProvider extends HealthReportProvider
33  {
34    private static final Logger _logger = Logger
35        .getLogger(DefaultHealthReportProvider.class);
36  
37    public final static String _availableCPUs = "availableCPUs";
38    public final static String _freePhysicalMemory = "freePhysicalMemory";
39    public final static String _totalJvmMemory = "totalJvmMemory";
40    public final static String _freeJvmMemory = "freeJvmMemory";
41    public final static String _averageSystemLoad = "averageSystemLoad";
42  
43    public DefaultHealthReportProvider()
44    {
45    }
46  
47    @Override
48    public Map<String, String> getRecentHealthReport()
49    {
50      OperatingSystemMXBean osMxBean = ManagementFactory
51          .getOperatingSystemMXBean();
52      long freeJvmMemory = Runtime.getRuntime().freeMemory();
53      long totalJvmMemory = Runtime.getRuntime().totalMemory();
54      int availableCPUs = osMxBean.getAvailableProcessors();
55      double avgSystemLoad = osMxBean.getSystemLoadAverage();
56      long freePhysicalMemory = Long.MAX_VALUE;
57  
58      try
59      {
60        // if( osMxBean instanceof com.sun.management.OperatingSystemMXBean)
61        // {
62        // com.sun.management.OperatingSystemMXBean sunOsMxBean
63        // = (com.sun.management.OperatingSystemMXBean) osMxBean;
64        // freePhysicalMemory = sunOsMxBean.getFreePhysicalMemorySize();
65        // }
66      }
67      catch (Throwable t)
68      {
69        _logger.error(t);
70      }
71  
72      Map<String, String> result = new TreeMap<String, String>();
73  
74      result.put(_availableCPUs, "" + availableCPUs);
75      result.put(_freePhysicalMemory, "" + freePhysicalMemory);
76      result.put(_freeJvmMemory, "" + freeJvmMemory);
77      result.put(_totalJvmMemory, "" + totalJvmMemory);
78      result.put(_averageSystemLoad, "" + avgSystemLoad);
79  
80      return result;
81    }
82  
83    @Override
84    public Map<String, Map<String, String>> getRecentPartitionHealthReport()
85    {
86      Map<String, Map<String, String>> result = new HashMap<String, Map<String, String>>();
87      
88      result.put(getReportName(), getRecentHealthReport());
89      return result;
90    }
91  
92    @Override
93    public void resetStats()
94    {
95      // TODO Auto-generated method stub
96  
97    }
98  }