001/* 002 * Copyright 2013 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.runtime.plugins.admin.jvmstatus.monitoring.sample; 017 018import java.util.HashMap; 019import java.util.Map; 020 021import org.hyperic.sigar.FileSystem; 022import org.hyperic.sigar.FileSystemUsage; 023import org.hyperic.sigar.Sigar; 024import org.hyperic.sigar.SigarException; 025import org.hyperic.sigar.SigarProxy; 026import org.hyperic.sigar.SigarProxyCache; 027 028/** 029 * Sigar-based implementation of {@link DiskIOMonitor}. 030 */ 031public class SigarDiskIOMonitor implements DiskIOMonitor 032{ 033 private final Sigar _sigar; 034 private final SigarProxy _proxy; 035 036 private long _reads; 037 private long _readBytes; 038 private long _writes; 039 private long _writeBytes; 040 041 /** 042 * Constructor. 043 */ 044 public SigarDiskIOMonitor() 045 { 046 _sigar = new Sigar(); 047 _proxy = SigarProxyCache.newInstance(_sigar); 048 049 refresh(); 050 } 051 052 @Override 053 public void refresh() 054 { 055 _reads = 0; 056 _readBytes = 0; 057 _writes = 0; 058 _writeBytes = 0; 059 060 FileSystem[] fsList; 061 062 try 063 { 064 fsList = _proxy.getFileSystemList(); 065 } 066 catch (SigarException e) 067 { 068 return; 069 } 070 071 for (FileSystem fs : fsList) 072 { 073 if (fs.getType() == FileSystem.TYPE_LOCAL_DISK) 074 { 075 try 076 { 077 FileSystemUsage usage = _sigar.getFileSystemUsage(fs.getDirName()); 078 079 _reads += usage.getDiskReads(); 080 _readBytes += usage.getDiskReadBytes(); 081 _writes += usage.getDiskWrites(); 082 _writeBytes += usage.getDiskWriteBytes(); 083 } 084 catch (SigarException e) 085 { 086 continue; 087 } 088 } 089 } 090 } 091 092 @Override 093 public double getReads() 094 { 095 return _reads; 096 } 097 098 @Override 099 public double getReadBytes() 100 { 101 return _readBytes; 102 } 103 104 @Override 105 public double getWrites() 106 { 107 return _writes; 108 } 109 110 @Override 111 public double getWriteBytes() 112 { 113 return _writeBytes; 114 } 115 116 @Override 117 public Map<String, Double> toMap() 118 { 119 Map<String, Double> map = new HashMap<>(); 120 121 map.put(DiskIOMonitor.READS, getReads()); 122 map.put(DiskIOMonitor.READ_BYTES, getReadBytes()); 123 map.put(DiskIOMonitor.WRITES, getWrites()); 124 map.put(DiskIOMonitor.WRITE_BYTES, getWriteBytes()); 125 126 return map; 127 } 128}