use of com.cloud.usage.UsageVmDiskVO in project cloudstack by apache.
the class VmDiskUsageParser method parse.
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Parsing all Vm Disk usage events for account: " + account.getId());
}
if ((endDate == null) || endDate.after(new Date())) {
endDate = new Date();
}
// - query usage_disk table for all entries for userId with
// event_date in the given range
SearchCriteria<UsageVmDiskVO> sc = s_usageVmDiskDao.createSearchCriteria();
sc.addAnd("accountId", SearchCriteria.Op.EQ, account.getId());
sc.addAnd("eventTimeMillis", SearchCriteria.Op.BETWEEN, startDate.getTime(), endDate.getTime());
List<UsageVmDiskVO> usageVmDiskVOs = s_usageVmDiskDao.search(sc, null);
Map<String, VmDiskInfo> vmDiskUsageByZone = new HashMap<String, VmDiskInfo>();
// Calculate the bytes since last parsing
for (UsageVmDiskVO usageVmDisk : usageVmDiskVOs) {
long zoneId = usageVmDisk.getZoneId();
String key = "" + zoneId;
if (usageVmDisk.getVmId() != 0) {
key += "-Vm-" + usageVmDisk.getVmId() + "-Disk-" + usageVmDisk.getVolumeId();
}
VmDiskInfo vmDiskInfo = vmDiskUsageByZone.get(key);
long ioRead = usageVmDisk.getIORead();
long ioWrite = usageVmDisk.getIOWrite();
long bytesRead = usageVmDisk.getBytesRead();
long bytesWrite = usageVmDisk.getBytesWrite();
if (vmDiskInfo != null) {
ioRead += vmDiskInfo.getIORead();
ioWrite += vmDiskInfo.getIOWrite();
bytesRead += vmDiskInfo.getBytesRead();
bytesWrite += vmDiskInfo.getBytesWrite();
}
vmDiskUsageByZone.put(key, new VmDiskInfo(zoneId, usageVmDisk.getVmId(), usageVmDisk.getVolumeId(), ioRead, ioWrite, bytesRead, bytesWrite));
}
List<UsageVO> usageRecords = new ArrayList<UsageVO>();
for (String key : vmDiskUsageByZone.keySet()) {
VmDiskInfo vmDiskInfo = vmDiskUsageByZone.get(key);
long ioRead = vmDiskInfo.getIORead();
long ioWrite = vmDiskInfo.getIOWrite();
long bytesRead = vmDiskInfo.getBytesRead();
long bytesWrite = vmDiskInfo.getBytesWrite();
if ((ioRead > 0L) || (ioWrite > 0L) || (bytesRead > 0L) || (bytesWrite > 0L)) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Creating vm disk usage record, io read:" + ioRead + ", io write: " + ioWrite + "bytes read:" + bytesRead + ", bytes write: " + bytesWrite + "for account: " + account.getId() + " in availability zone " + vmDiskInfo.getZoneId() + ", start: " + startDate + ", end: " + endDate);
}
Long vmId = null;
Long volumeId = null;
// Create the usage record for disk I/O read (io requests)
String usageDesc = "disk I/O read (io requests)";
if ((vmDiskInfo.getVmId() != 0) && (vmDiskInfo.getVolumeId() != 0)) {
vmId = vmDiskInfo.getVmId();
volumeId = vmDiskInfo.getVolumeId();
usageDesc += " for Vm: " + vmId + " and Volume: " + volumeId;
}
UsageVO usageRecord = new UsageVO(vmDiskInfo.getZoneId(), account.getId(), account.getDomainId(), usageDesc, ioRead + " io read", UsageTypes.VM_DISK_IO_READ, new Double(ioRead), vmId, null, null, null, vmDiskInfo.getVolumeId(), startDate, endDate, "VirtualMachine");
usageRecords.add(usageRecord);
// Create the usage record for disk I/O write (io requests)
usageDesc = "disk I/O write (io requests)";
if ((vmDiskInfo.getVmId() != 0) && (vmDiskInfo.getVolumeId() != 0)) {
usageDesc += " for Vm: " + vmId + " and Volume: " + volumeId;
}
usageRecord = new UsageVO(vmDiskInfo.getZoneId(), account.getId(), account.getDomainId(), usageDesc, ioWrite + " io write", UsageTypes.VM_DISK_BYTES_WRITE, new Double(ioWrite), vmId, null, null, null, vmDiskInfo.getVolumeId(), startDate, endDate, "VirtualMachine");
usageRecords.add(usageRecord);
// Create the usage record for disk I/O read (bytes)
usageDesc = "disk I/O read (bytes)";
if ((vmDiskInfo.getVmId() != 0) && (vmDiskInfo.getVolumeId() != 0)) {
usageDesc += " for Vm: " + vmId + " and Volume: " + volumeId;
}
usageRecord = new UsageVO(vmDiskInfo.getZoneId(), account.getId(), account.getDomainId(), usageDesc, bytesRead + " bytes read", UsageTypes.VM_DISK_BYTES_READ, new Double(bytesRead), vmId, null, null, null, vmDiskInfo.getVolumeId(), startDate, endDate, "VirtualMachine");
usageRecords.add(usageRecord);
// Create the usage record for disk I/O write (bytes)
usageDesc = "disk I/O write (bytes)";
if ((vmDiskInfo.getVmId() != 0) && (vmDiskInfo.getVolumeId() != 0)) {
usageDesc += " for Vm: " + vmId + " and Volume: " + volumeId;
}
usageRecord = new UsageVO(vmDiskInfo.getZoneId(), account.getId(), account.getDomainId(), usageDesc, bytesWrite + " bytes write", UsageTypes.VM_DISK_BYTES_WRITE, new Double(bytesWrite), vmId, null, null, null, vmDiskInfo.getVolumeId(), startDate, endDate, "VirtualMachine");
usageRecords.add(usageRecord);
} else {
// Don't charge anything if there were zero bytes processed
if (s_logger.isDebugEnabled()) {
s_logger.debug("No vm disk usage record (0 bytes used) generated for account: " + account.getId());
}
}
}
s_usageDao.saveUsageRecords(usageRecords);
return true;
}
use of com.cloud.usage.UsageVmDiskVO in project cloudstack by apache.
the class UsageVmDiskDaoImpl method saveUsageVmDisks.
@Override
public void saveUsageVmDisks(List<UsageVmDiskVO> usageVmDisks) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
try {
txn.start();
String sql = INSERT_USAGE_VM_DISK;
PreparedStatement pstmt = null;
// in reality I just want CLOUD_USAGE dataSource connection
pstmt = txn.prepareAutoCloseStatement(sql);
for (UsageVmDiskVO usageVmDisk : usageVmDisks) {
pstmt.setLong(1, usageVmDisk.getAccountId());
pstmt.setLong(2, usageVmDisk.getZoneId());
pstmt.setLong(3, usageVmDisk.getVmId());
pstmt.setLong(4, usageVmDisk.getVolumeId());
pstmt.setLong(5, usageVmDisk.getIORead());
pstmt.setLong(6, usageVmDisk.getIOWrite());
pstmt.setLong(7, usageVmDisk.getAggIORead());
pstmt.setLong(8, usageVmDisk.getAggIOWrite());
pstmt.setLong(9, usageVmDisk.getBytesRead());
pstmt.setLong(10, usageVmDisk.getBytesWrite());
pstmt.setLong(11, usageVmDisk.getAggBytesRead());
pstmt.setLong(12, usageVmDisk.getAggBytesWrite());
pstmt.setLong(13, usageVmDisk.getEventTimeMillis());
pstmt.addBatch();
}
pstmt.executeBatch();
txn.commit();
} catch (Exception ex) {
txn.rollback();
s_logger.error("error saving usage_vm_disk to cloud_usage db", ex);
throw new CloudRuntimeException(ex.getMessage());
}
}
use of com.cloud.usage.UsageVmDiskVO in project cloudstack by apache.
the class UsageVmDiskDaoImpl method getRecentVmDiskStats.
@Override
public Map<String, UsageVmDiskVO> getRecentVmDiskStats() {
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
String sql = SELECT_LATEST_STATS;
PreparedStatement pstmt = null;
try {
pstmt = txn.prepareAutoCloseStatement(sql);
ResultSet rs = pstmt.executeQuery();
Map<String, UsageVmDiskVO> returnMap = new HashMap<String, UsageVmDiskVO>();
while (rs.next()) {
long accountId = rs.getLong(1);
long zoneId = rs.getLong(2);
long vmId = rs.getLong(3);
Long volumeId = rs.getLong(4);
long ioRead = rs.getLong(5);
long ioWrite = rs.getLong(6);
long aggIORead = rs.getLong(7);
long aggIOWrite = rs.getLong(8);
long bytesRead = rs.getLong(9);
long bytesWrite = rs.getLong(10);
long aggBytesRead = rs.getLong(11);
long aggBytesWrite = rs.getLong(12);
long eventTimeMillis = rs.getLong(13);
if (vmId != 0) {
returnMap.put(zoneId + "-" + accountId + "-Vm-" + vmId + "-Disk-" + volumeId, new UsageVmDiskVO(accountId, zoneId, vmId, volumeId, ioRead, ioWrite, aggIORead, aggIOWrite, bytesRead, bytesWrite, aggBytesRead, aggBytesWrite, eventTimeMillis));
} else {
returnMap.put(zoneId + "-" + accountId, new UsageVmDiskVO(accountId, zoneId, vmId, volumeId, ioRead, ioWrite, aggIORead, aggIOWrite, bytesRead, bytesWrite, aggBytesRead, aggBytesWrite, eventTimeMillis));
}
}
return returnMap;
} catch (Exception ex) {
s_logger.error("error getting recent usage disk stats", ex);
} finally {
txn.close();
}
return null;
}
Aggregations