Search in sources :

Example 21 with UsageVO

use of com.cloud.usage.UsageVO 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:" + toHumanReadableSize(ioRead) + ", io write: " + toHumanReadableSize(ioWrite) + ", bytes read:" + toHumanReadableSize(bytesRead) + ", bytes write: " + toHumanReadableSize(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;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UsageVO(com.cloud.usage.UsageVO) Date(java.util.Date) UsageVmDiskVO(com.cloud.usage.UsageVmDiskVO)

Example 22 with UsageVO

use of com.cloud.usage.UsageVO in project CloudStack-archive by CloudStack-extras.

the class VPNUserUsageParser method createUsageRecord.

private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long userId, String userName, long zoneId) {
    // Our smallest increment is hourly for now
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Total running time " + runningTime + "ms");
    }
    float usage = runningTime / 1000f / 60f / 60f;
    DecimalFormat dFormat = new DecimalFormat("#.######");
    String usageDisplay = dFormat.format(usage);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Creating VPN user:" + userId + " usage record, usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
    }
    // Create the usage record
    String usageDesc = "VPN User: " + userName + ", Id: " + userId + " usage time";
    UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), null, null, null, null, userId, null, startDate, endDate);
    m_usageDao.persist(usageRecord);
}
Also used : DecimalFormat(java.text.DecimalFormat) UsageVO(com.cloud.usage.UsageVO)

Example 23 with UsageVO

use of com.cloud.usage.UsageVO in project CloudStack-archive by CloudStack-extras.

the class IPAddressUsageParser method createUsageRecord.

private static void createUsageRecord(long zoneId, long runningTime, Date startDate, Date endDate, AccountVO account, long IpId, String IPAddress, boolean isSourceNat, boolean isSystem) {
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Total usage time " + runningTime + "ms");
    }
    float usage = runningTime / 1000f / 60f / 60f;
    DecimalFormat dFormat = new DecimalFormat("#.######");
    String usageDisplay = dFormat.format(usage);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Creating IP usage record with id: " + IpId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
    }
    String usageDesc = "IPAddress: " + IPAddress;
    // Create the usage record
    UsageVO usageRecord = new UsageVO(zoneId, account.getAccountId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", UsageTypes.IP_ADDRESS, new Double(usage), IpId, (isSystem ? 1 : 0), (isSourceNat ? "SourceNat" : ""), startDate, endDate);
    m_usageDao.persist(usageRecord);
}
Also used : DecimalFormat(java.text.DecimalFormat) UsageVO(com.cloud.usage.UsageVO)

Example 24 with UsageVO

use of com.cloud.usage.UsageVO in project CloudStack-archive by CloudStack-extras.

the class LoadBalancerUsageParser method createUsageRecord.

private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long lbId, long zoneId) {
    // Our smallest increment is hourly for now
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Total running time " + runningTime + "ms");
    }
    float usage = runningTime / 1000f / 60f / 60f;
    DecimalFormat dFormat = new DecimalFormat("#.######");
    String usageDisplay = dFormat.format(usage);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Creating Volume usage record for load balancer: " + lbId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
    }
    // Create the usage record
    String usageDesc = "Load Balancing Policy: " + lbId + " usage time";
    //ToDo: get zone id
    UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), null, null, null, null, lbId, null, startDate, endDate);
    m_usageDao.persist(usageRecord);
}
Also used : DecimalFormat(java.text.DecimalFormat) UsageVO(com.cloud.usage.UsageVO)

Example 25 with UsageVO

use of com.cloud.usage.UsageVO in project cloudstack by apache.

the class VMInstanceUsageParser method createUsageRecord.

private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long vmId, String vmName, long zoneId, long serviceOfferingId, long templateId, String hypervisorType, Long cpuCores, Long cpuSpeed, Long memory) {
    // Our smallest increment is hourly for now
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Total running time " + runningTime + "ms");
    }
    float usage = runningTime / 1000f / 60f / 60f;
    DecimalFormat dFormat = new DecimalFormat("#.######");
    String usageDisplay = dFormat.format(usage);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Creating VM usage record for vm: " + vmName + ", type: " + type + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
    }
    // Create the usage record
    String usageDesc = vmName;
    if (type == UsageTypes.ALLOCATED_VM) {
        usageDesc += " allocated";
    } else {
        usageDesc += " running time";
    }
    usageDesc += " (ServiceOffering: " + serviceOfferingId + ") (Template: " + templateId + ")";
    UsageVO usageRecord = new UsageVO(Long.valueOf(zoneId), account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), Long.valueOf(vmId), vmName, cpuCores, cpuSpeed, memory, Long.valueOf(serviceOfferingId), Long.valueOf(templateId), Long.valueOf(vmId), startDate, endDate, hypervisorType);
    s_usageDao.persist(usageRecord);
}
Also used : DecimalFormat(java.text.DecimalFormat) UsageVO(com.cloud.usage.UsageVO)

Aggregations

UsageVO (com.cloud.usage.UsageVO)31 DecimalFormat (java.text.DecimalFormat)21 ArrayList (java.util.ArrayList)6 Date (java.util.Date)6 QuotaUsageVO (org.apache.cloudstack.quota.vo.QuotaUsageVO)5 AccountVO (com.cloud.user.AccountVO)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 List (java.util.List)3 QuotaAccountVO (org.apache.cloudstack.quota.vo.QuotaAccountVO)3 UsageNetworkVO (com.cloud.usage.UsageNetworkVO)2 Pair (com.cloud.utils.Pair)2 DomainVO (com.cloud.domain.DomainVO)1 UsageBackupVO (com.cloud.usage.UsageBackupVO)1 UsageVmDiskVO (com.cloud.usage.UsageVmDiskVO)1 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 BigDecimal (java.math.BigDecimal)1 PreparedStatement (java.sql.PreparedStatement)1 UsageRecordResponse (org.apache.cloudstack.api.response.UsageRecordResponse)1