/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.stratos.cartridge.agent.statistics.publisher;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.stratos.cartridge.agent.config.CartridgeAgentConfiguration;
import org.apache.stratos.cartridge.agent.event.publisher.CartridgeAgentEventPublisher;
import org.apache.stratos.cartridge.agent.util.CartridgeAgentConstants;
import org.apache.stratos.cartridge.agent.util.CartridgeAgentUtils;

/**
 * Health statistics notifier thread for publishing statistics periodically to CEP.
 */
public class HealthStatisticsNotifier implements Runnable {
    private static final Log log = LogFactory.getLog(HealthStatisticsNotifier.class);

    private final HealthStatisticsPublisher statsPublisher;
    private long statsPublisherInterval = 15000;
    private boolean terminated;

    public HealthStatisticsNotifier() {
        this.statsPublisher = new HealthStatisticsPublisher();

        String interval = System.getProperty("stats.notifier.interval");
        if (interval != null) {
            statsPublisherInterval = Long.getLong(interval);
        }
    }

    @Override
    public void run() {
        while (!terminated) {
            try {
                try {
                    Thread.sleep(statsPublisherInterval);
                } catch (InterruptedException ignore) {
                }

                if (statsPublisher.isEnabled()) {
                    if(!CartridgeAgentUtils.checkPortsActive("localhost", CartridgeAgentConfiguration.getInstance().getPorts())
                            && ( !CartridgeAgentEventPublisher.isMaintenance() || !CartridgeAgentEventPublisher.isReadyToShutdown() ) ) {
                        if(log.isInfoEnabled()) {
                            log.info("Publishing ports not open event");
                        }
                        statsPublisher.publish(
                                CartridgeAgentConfiguration.getInstance().getClusterId(),
                                CartridgeAgentConfiguration.getInstance().getNetworkPartitionId(),
                                CartridgeAgentConfiguration.getInstance().getMemberId(),
                                CartridgeAgentConfiguration.getInstance().getPartitionId(),
                                CartridgeAgentConstants.PORTS_NOT_OPEN,
                                1
                                );
                    }

                    double memoryConsumption = HealthStatisticsReader.getMemoryConsumption();
                    if(log.isInfoEnabled()) {
                        log.info(String.format("Publishing memory consumption: %f", memoryConsumption));
                    }
                    statsPublisher.publish(
                            CartridgeAgentConfiguration.getInstance().getClusterId(),
                            CartridgeAgentConfiguration.getInstance().getNetworkPartitionId(),
                            CartridgeAgentConfiguration.getInstance().getMemberId(),
                            CartridgeAgentConfiguration.getInstance().getPartitionId(),
                            CartridgeAgentConstants.MEMORY_CONSUMPTION,
                            memoryConsumption
                    );

                    double loadAverage = HealthStatisticsReader.getLoadAverage();
                    if(log.isInfoEnabled()) {
                        log.info(String.format("Publishing load average: %f", loadAverage));
                    }
                    statsPublisher.publish(
                            CartridgeAgentConfiguration.getInstance().getClusterId(),
                            CartridgeAgentConfiguration.getInstance().getNetworkPartitionId(),
                            CartridgeAgentConfiguration.getInstance().getMemberId(),
                            CartridgeAgentConfiguration.getInstance().getPartitionId(),
                            CartridgeAgentConstants.LOAD_AVERAGE,
                            loadAverage
                    );
                } else if (log.isWarnEnabled()) {
                    log.warn("Statistics publisher is disabled");
                }
            } catch (Exception e) {
                if (log.isErrorEnabled()) {
                    log.error("Could not publish health statistics", e);
                }
            }
        }
    }

    /**
     * Terminate load balancer statistics notifier thread.
     */
    public void terminate() {
        terminated = true;
    }
}

@@ -2224,1004 +2224,8 @@
 )) %7B
-%0A                    if(!CartridgeAgentUtils.checkPortsActive(%22localhost%22, CartridgeAgentConfiguration.getInstance().getPorts())%0A                            && ( !CartridgeAgentEventPublisher.isMaintenance() %7C%7C !CartridgeAgentEventPublisher.isReadyToShutdown() ) ) %7B%0A                        if(log.isInfoEnabled()) %7B%0A                            log.info(%22Publishing ports not open event%22);%0A                        %7D%0A                        statsPublisher.publish(%0A                                CartridgeAgentConfiguration.getInstance().getClusterId(),%0A                                CartridgeAgentConfiguration.getInstance().getNetworkPartitionId(),%0A                                CartridgeAgentConfiguration.getInstance().getMemberId(),%0A                                CartridgeAgentConfiguration.getInstance().getPartitionId(),%0A                                CartridgeAgentConstants.PORTS_NOT_OPEN,%0A                                1%0A                                );%0A                    %7D
 %0A%0A  

Remove port checking after instance activation