diff --git a/api/src/com/cloud/agent/api/StartupTrafficMonitorCommand.java b/api/src/com/cloud/agent/api/StartupTrafficMonitorCommand.java new file mode 100644 index 00000000000..969ea343d15 --- /dev/null +++ b/api/src/com/cloud/agent/api/StartupTrafficMonitorCommand.java @@ -0,0 +1,28 @@ +/** + * Copyright (C) 2010 Cloud.com, Inc. All rights reserved. + * + * This software is licensed under the GNU General Public License v3 or later. + * + * It is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package com.cloud.agent.api; + +import com.cloud.host.Host; + +public class StartupTrafficMonitorCommand extends StartupCommand { + public StartupTrafficMonitorCommand() { + super(Host.Type.TrafficMonitor); + } + +} diff --git a/api/src/com/cloud/host/Host.java b/api/src/com/cloud/host/Host.java index 2ae9b501a95..69b3cccc8db 100755 --- a/api/src/com/cloud/host/Host.java +++ b/api/src/com/cloud/host/Host.java @@ -35,6 +35,7 @@ public interface Host { ExternalFirewall(false), ExternalLoadBalancer(false), PxeServer(false), + TrafficMonitor(false), ExternalDhcp(false); boolean _virtual; diff --git a/core/src/com/cloud/event/dao/UsageEventDao.java b/core/src/com/cloud/event/dao/UsageEventDao.java index acd6099cca1..520c760a017 100644 --- a/core/src/com/cloud/event/dao/UsageEventDao.java +++ b/core/src/com/cloud/event/dao/UsageEventDao.java @@ -35,4 +35,6 @@ public interface UsageEventDao extends GenericDao { List getRecentEvents(Date endDate) throws UsageServerException; + List listIpEvents(Date startDate, Date endDate); + } \ No newline at end of file diff --git a/core/src/com/cloud/event/dao/UsageEventDaoImpl.java b/core/src/com/cloud/event/dao/UsageEventDaoImpl.java index 241a1b80fb8..8292453116b 100644 --- a/core/src/com/cloud/event/dao/UsageEventDaoImpl.java +++ b/core/src/com/cloud/event/dao/UsageEventDaoImpl.java @@ -28,6 +28,7 @@ import javax.ejb.Local; import org.apache.log4j.Logger; +import com.cloud.event.EventTypes; import com.cloud.event.UsageEventVO; import com.cloud.exception.UsageServerException; import com.cloud.utils.DateUtil; @@ -43,6 +44,7 @@ public class UsageEventDaoImpl extends GenericDaoBase implem public static final Logger s_logger = Logger.getLogger(UsageEventDaoImpl.class.getName()); private final SearchBuilder latestEventsSearch; + private final SearchBuilder IpeventsSearch; private static final String COPY_EVENTS = "INSERT INTO cloud_usage.usage_event (id, type, account_id, created, zone_id, resource_id, resource_name, offering_id, template_id, size, resource_type) " + "SELECT id, type, account_id, created, zone_id, resource_id, resource_name, offering_id, template_id, size, resource_type FROM cloud.usage_event vmevt WHERE vmevt.id > ? and vmevt.id <= ? "; private static final String COPY_ALL_EVENTS = "INSERT INTO cloud_usage.usage_event (id, type, account_id, created, zone_id, resource_id, resource_name, offering_id, template_id, size, resource_type) " + @@ -55,7 +57,15 @@ public class UsageEventDaoImpl extends GenericDaoBase implem latestEventsSearch.and("processed", latestEventsSearch.entity().isProcessed(), SearchCriteria.Op.EQ); latestEventsSearch.and("enddate", latestEventsSearch.entity().getCreateDate(), SearchCriteria.Op.LTEQ); latestEventsSearch.done(); - } + + IpeventsSearch = createSearchBuilder(); + IpeventsSearch.and("startdate", IpeventsSearch.entity().getCreateDate(), SearchCriteria.Op.GTEQ); + IpeventsSearch.and("enddate", IpeventsSearch.entity().getCreateDate(), SearchCriteria.Op.LTEQ); + IpeventsSearch.and().op("assignEvent", IpeventsSearch.entity().getType(), SearchCriteria.Op.EQ); + IpeventsSearch.or("releaseEvent", IpeventsSearch.entity().getType(), SearchCriteria.Op.EQ); + IpeventsSearch.closeParen(); + IpeventsSearch.done(); + } @Override public List listLatestEvents(Date endDate) { @@ -159,4 +169,16 @@ public class UsageEventDaoImpl extends GenericDaoBase implem txn.close(); } } -} + + @Override + public List listIpEvents(Date startDate, Date endDate) { + Filter filter = new Filter(UsageEventVO.class, "createDate", Boolean.TRUE, null, null); + SearchCriteria sc = IpeventsSearch.create(); + sc.setParameters("startdate", startDate); + sc.setParameters("enddate", endDate); + sc.setParameters("assignEvent", EventTypes.EVENT_NET_IP_ASSIGN); + sc.setParameters("releaseEvent", EventTypes.EVENT_NET_IP_RELEASE); + return listBy(sc, filter); + } + +} diff --git a/server/src/com/cloud/agent/manager/AgentManagerImpl.java b/server/src/com/cloud/agent/manager/AgentManagerImpl.java index 49ae7a9bcdd..8a037b8f8dc 100755 --- a/server/src/com/cloud/agent/manager/AgentManagerImpl.java +++ b/server/src/com/cloud/agent/manager/AgentManagerImpl.java @@ -69,6 +69,7 @@ import com.cloud.agent.api.StartupProxyCommand; import com.cloud.agent.api.StartupPxeServerCommand; import com.cloud.agent.api.StartupRoutingCommand; import com.cloud.agent.api.StartupStorageCommand; +import com.cloud.agent.api.StartupTrafficMonitorCommand; import com.cloud.agent.api.UnsupportedAnswer; import com.cloud.agent.manager.allocator.HostAllocator; import com.cloud.agent.manager.allocator.PodAllocator; @@ -2386,6 +2387,8 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, ResourceS type = Host.Type.PxeServer; } else if (startup instanceof StartupExternalDhcpCommand) { type = Host.Type.ExternalDhcp; + } else if (startup instanceof StartupTrafficMonitorCommand) { + type = Host.Type.TrafficMonitor; } else { assert false : "Did someone add a new Startup command?"; } @@ -2645,7 +2648,7 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, ResourceS } Long podId = null; if (p == null) { - if (type != Host.Type.SecondaryStorage && type != Host.Type.ExternalFirewall && type != Host.Type.ExternalLoadBalancer) { + if (type != Host.Type.SecondaryStorage && type != Host.Type.ExternalFirewall && type != Host.Type.ExternalLoadBalancer && type != Host.Type.TrafficMonitor) { /* * s_logger.info("Unable to find the pod so we are creating one." ); p = createPod(pod, dcId, diff --git a/server/src/com/cloud/agent/manager/AgentMonitor.java b/server/src/com/cloud/agent/manager/AgentMonitor.java index 392c6c8469a..cafd8d685c3 100755 --- a/server/src/com/cloud/agent/manager/AgentMonitor.java +++ b/server/src/com/cloud/agent/manager/AgentMonitor.java @@ -106,7 +106,8 @@ public class AgentMonitor extends Thread implements Listener { for (HostVO host : hosts) { if (host.getType().equals(Host.Type.ExternalFirewall) || - host.getType().equals(Host.Type.ExternalLoadBalancer)) { + host.getType().equals(Host.Type.ExternalLoadBalancer) || + host.getType().equals(Host.Type.TrafficMonitor)) { continue; } diff --git a/server/src/com/cloud/host/dao/HostDao.java b/server/src/com/cloud/host/dao/HostDao.java index de103ba0e52..c008ada765d 100644 --- a/server/src/com/cloud/host/dao/HostDao.java +++ b/server/src/com/cloud/host/dao/HostDao.java @@ -169,4 +169,5 @@ public interface HostDao extends GenericDao { boolean directConnect(HostVO host, long msId, boolean secondConnect); + HostVO findTrafficMonitorHost(); } diff --git a/server/src/com/cloud/host/dao/HostDaoImpl.java b/server/src/com/cloud/host/dao/HostDaoImpl.java index d1aad602e31..af664f878d4 100644 --- a/server/src/com/cloud/host/dao/HostDaoImpl.java +++ b/server/src/com/cloud/host/dao/HostDaoImpl.java @@ -799,4 +799,17 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao return secondaryStorageHosts; } + + @Override + public HostVO findTrafficMonitorHost() { + SearchCriteria sc = TypeSearch.create(); + sc.setParameters("type", Host.Type.TrafficMonitor); + List trafficHosts = listBy(sc); + + if (trafficHosts == null || trafficHosts.size() < 1) { + return null; + } else { + return trafficHosts.get(0); + } + } } diff --git a/server/src/com/cloud/network/dao/IPAddressDao.java b/server/src/com/cloud/network/dao/IPAddressDao.java index deb301f3c9e..e6be50c964e 100644 --- a/server/src/com/cloud/network/dao/IPAddressDao.java +++ b/server/src/com/cloud/network/dao/IPAddressDao.java @@ -50,6 +50,6 @@ public interface IPAddressDao extends GenericDao { IPAddressVO findByAccountAndIp(long accountId, String ipAddress); - IPAddressVO findByIpAddress(String ipAddress); + List listAllocatedIps(); } diff --git a/server/src/com/cloud/network/dao/IPAddressDaoImpl.java b/server/src/com/cloud/network/dao/IPAddressDaoImpl.java index a339c054f32..2a9e94df368 100644 --- a/server/src/com/cloud/network/dao/IPAddressDaoImpl.java +++ b/server/src/com/cloud/network/dao/IPAddressDaoImpl.java @@ -52,6 +52,7 @@ public class IPAddressDaoImpl extends GenericDaoBase implemen protected final GenericSearchBuilder AllocatedIpCount; protected final GenericSearchBuilder AllIpCountForDashboard; protected final GenericSearchBuilder AllocatedIpCountForDashboard; + protected final SearchBuilder AllocatedIpSearch; // make it public for JUnit test @@ -96,6 +97,10 @@ public class IPAddressDaoImpl extends GenericDaoBase implemen AllocatedIpCountForDashboard.and("dc", AllocatedIpCountForDashboard.entity().getDataCenterId(), Op.EQ); AllocatedIpCountForDashboard.and("allocated", AllocatedIpCountForDashboard.entity().getAllocatedTime(), Op.NNULL); AllocatedIpCountForDashboard.done(); + + AllocatedIpSearch = createSearchBuilder(); + AllocatedIpSearch.and("allocated", AllocatedIpSearch.entity().getAllocatedTime(), Op.NNULL); + AllocatedIpSearch.done(); } @Override @@ -269,9 +274,8 @@ public class IPAddressDaoImpl extends GenericDaoBase implemen } @Override - public IPAddressVO findByIpAddress(String ipAddress) { - SearchCriteria sc = AllFieldsSearch.create(); - sc.setParameters("ipAddress", ipAddress); - return findOneBy(sc); + public List listAllocatedIps() { + SearchCriteria sc = AllocatedIpSearch.create(); + return listBy(sc); } } diff --git a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java index b06199783fe..ad67101b20a 100755 --- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java +++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java @@ -627,66 +627,6 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian @Override public void run() { - //Direct Network Usage - URL trafficSentinel; - try { - //Query traffic Sentinel - if(trafficSentinelHostname != null){ - trafficSentinel = new URL(trafficSentinelHostname+"/inmsf/Query?script=var+q+%3D+Query.topN(%22historytrmx%22,%0D%0A+++++++++++++++++%22ipsource,bytes%22,%0D%0A+++++++++++++++++%22sourcezone+!%3D+EXTERNAL" + - "+%26+destinationzone+%3D+EXTERNAL%22,%0D%0A+++++++++++++++++%22end+-+5+minutes,+end%22,%0D%0A+++++++++++++++++%22bytes%22,%0D%0A+++++++++++++++++100000);%0D%0A%0D%0Avar+totalsSent+%3D+" + - "{};%0D%0A%0D%0Avar+t+%3D+q.run(%0D%0A++function(row,table)+{%0D%0A++++if(row[0])+{++++%0D%0A++++++totalsSent[row[0]]+%3D+row[1];%0D%0A++++}%0D%0A++});%0D%0A%0D%0Avar+totalsRcvd+%3D+{};" + - "%0D%0A%0D%0Avar+q+%3D+Query.topN(%22historytrmx%22,%0D%0A+++++++++++++++++%22ipdestination,bytes%22,%0D%0A+++++++++++++++++%22destinationzone+!%3D+EXTERNAL+%26+sourcezone+%3D+EXTERNAL%22," + - "%0D%0A+++++++++++++++++%22end+-+5minutes,+end%22,%0D%0A+++++++++++++++++%22bytes%22,%0D%0A+++++++++++++++++100000);%0D%0A%0D%0Avar+t+%3D+q.run(%0D%0A++function(row,table)+{%0D%0A++++" + - "if(row[0])+{%0D%0A++++++totalsRcvd[row[0]]+%3D+row[1];%0D%0A++++}%0D%0A++});%0D%0A%0D%0Afor+(var+addr+in+totalsSent)+{%0D%0A++++var+TS+%3D+0;%0D%0A++++var+TR+%3D+0;%0D%0A++++if(totalsSent[addr])" + - "+TS+%3D+totalsSent[addr];%0D%0A++++if(totalsRcvd[addr])+TR+%3D+totalsRcvd[addr];%0D%0A++++println(addr+%2B+%22,%22+%2B+TS+%2B+%22,%22+%2B+TR);%0D%0A}&authenticate=basic&resultFormat=txt"); - - BufferedReader in = new BufferedReader( - new InputStreamReader(trafficSentinel.openStream())); - - String inputLine; - - while ((inputLine = in.readLine()) != null){ - //Parse the script output - StringTokenizer st = new StringTokenizer(inputLine, ","); - if(st.countTokens() == 3){ - String publicIp = st.nextToken(); - //Find the account owning the IP - IPAddressVO ipaddress = _ipAddressDao.findByIpAddress(publicIp); - if(ipaddress == null || ipaddress.getAccountId() == Account.ACCOUNT_ID_SYSTEM){ - continue; - } - Long bytesSent = new Long(st.nextToken()); - Long bytesRcvd = new Long(st.nextToken()); - if(bytesSent == null || bytesRcvd == null){ - s_logger.debug("Incorrect bytes for IP: "+publicIp); - } - Transaction txn = Transaction.open(Transaction.CLOUD_DB); - txn.start(); - //update user_statistics - UserStatisticsVO stats = _statsDao.lock(ipaddress.getAccountId(), ipaddress.getDataCenterId(), null, 0L, "DirectNetwork"); - if (stats == null) { - stats = new UserStatisticsVO(ipaddress.getAccountId(), ipaddress.getDataCenterId(), null, 0L, "DirectNetwork", null); - stats.setCurrentBytesSent(bytesSent); - stats.setCurrentBytesReceived(bytesRcvd); - _statsDao.persist(stats); - } else { - stats.setCurrentBytesSent(stats.getCurrentBytesSent() + bytesSent); - stats.setCurrentBytesReceived(stats.getCurrentBytesReceived() + bytesRcvd); - _statsDao.update(stats.getId(), stats); - } - txn.commit(); - txn.close(); - } - } - - in.close(); - } - } catch (MalformedURLException e1) { - s_logger.info("Invalid T raffic Sentinel URL",e1); - } catch (IOException e) { - s_logger.debug("Error in direct network usage accounting",e); - } - final List routers = _routerDao.listVirtualUpByHostId(null); s_logger.debug("Found " + routers.size() + " running routers. ");