View Javadoc
1   /*
2    * Copyright (c) 2006-2007 Hyperic, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * 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,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.miloss.fgsms.osagent;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import org.miloss.fgsms.services.interfaces.common.NetworkAdapterInfo;
21  import org.hyperic.sigar.NetInterfaceConfig;
22  import org.hyperic.sigar.SigarException;
23  import org.hyperic.sigar.cmd.Shell;
24  
25  /**
26   * Display network info.
27   */
28  public class NetInfo extends SigarCommandBase implements Closable{
29  
30      public NetInfo(Shell shell) {
31          super(shell);
32      }
33  
34      public NetInfo() {
35          super();
36      }
37  
38  
39      public List<NetworkAdapterInfo> GetNetworkInfo() throws SigarException {
40          List<NetworkAdapterInfo> info = new ArrayList<NetworkAdapterInfo>();
41          String[] names = this.sigar.getNetInterfaceList();
42          NetworkAdapterInfo d = null;
43          for (int i = 0; i < names.length; i++) {
44              d = new NetworkAdapterInfo();
45              NetInterfaceConfig config = this.sigar.getNetInterfaceConfig(names[i]);
46              
47              try {
48                  org.hyperic.sigar.NetInfo netinfo = this.sigar.getNetInfo();
49                  d.setDefaultGateway(netinfo.getDefaultGateway());
50                  d.getDns().add(netinfo.getPrimaryDns());
51                  d.getDns().add(netinfo.getSecondaryDns());
52              } catch (Exception ex) {
53                  ex.printStackTrace();
54                  d.setDefaultGateway("unavailable");
55              }
56              d.setSubnetMask(config.getNetmask());
57              d.setAdapterName(config.getName());
58              d.setAdapterDescription(config.getDescription());
59              d.setMtu(config.getMtu());
60              d.setMac(config.getHwaddr());
61              d.getIp().add(config.getAddress());
62              // if (!ContainsMax(info, d.getMac()))
63              info.add(d);
64          }
65  
66  
67  
68          return info;
69      }
70  
71     
72  
73      private boolean ContainsMax(List<NetworkAdapterInfo> info, String mac) {
74          for (int i = 0; i < info.size(); i++) {
75              if (info.get(i).getMac().equalsIgnoreCase(mac)) {
76                  return true;
77              }
78          }
79          return false;
80      }
81          @Override
82      public void close() throws Exception{
83          if (sigar != null) {
84              sigar.close();
85              sigar = null;
86          }
87      }
88      @Override
89      protected void finalize() throws Throwable
90      {
91          
92            if (sigar != null) {
93                System.out.println("WARN,. finalize called without closing sigar first"+ this.getClass().getCanonicalName());
94              sigar.close();
95          }
96          super.finalize();
97      }
98  
99      
100 }