/**
* JASMINe
* Copyright (C) 2009 Bull S.A.S.
* Contact: jasmine@ow2.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
* USA
*/
package org.ow2.jasmine.rules.packages.cpu

import java.io.Serializable;
import java.util.Date;
import java.lang.StringBuffer;

import org.objectweb.jasmine.rules.dispatcher.NotificationDispatcher;
import org.ow2.jasmine.event.beans.JasmineEventEB;
import org.ow2.jasmine.event.beans.JasmineEventNotification;

rule "CPU > 3%"
    no-loop false
    when
        $event : JasmineEventEB( probe ==  "java.lang:type=OperatingSystem:currentCpuLoad", value >= 3)
    then
        // Create a new notification
        // notification id is auto-generated
        JasmineEventNotification notification = new JasmineEventNotification();
        
        // Set the notification message
        notification.setMessage($event.getProbe() + " is too high : " + $event.getValue());
        // Set the notification date
        notification.setDate(new Date());
        // Set the notification level
        notification.setLevel("Warning");
        // set the notification status
        notification.setStatus(false);
        // Set the rule
        // The rules are configured in notification-config.xml file
        notification.setRule("CPU > 3%");
        
        // Add some details to the notification
        notification.addDetail("domain", $event.getDomain());
        notification.addDetail("server", $event.getServer());
        
        // Send the notification to the dispatcher
        NotificationDispatcher.dispatchEvent(NotificationDispatcher.EventAction.SAVE_IN_DATABASE, notification);

end

rule "CPU > 5%"
    no-loop false
    when
        $event : JasmineEventEB( probe ==  "java.lang:type=OperatingSystem:currentCpuLoad", value >= 5 )
    then
        // Create a new notification
        // notification id is auto-generated
        JasmineEventNotification notification = new JasmineEventNotification();
        
        // Set the notification message
        StringBuffer message = new StringBuffer();
        message.append("Date : " + $event.getTimestamp().toString());
        message.append("\n");
        message.append("Domain : " + $event.getDomain());
        message.append("\n");
        message.append("Server : " + $event.getServer());
        message.append("\n");
        message.append($event.getProbe() + " > 5% : " + $event.getValue());
        message.append("\n");
        notification.setMessage(message.toString());
        // Set the notification date
        notification.setDate(new Date());
        // Set the notification level
        notification.setLevel("Error");
        // Set the notification status
        notification.setStatus(false);
        // Set the rule
        // The rules are configured in notification-config.xml file
        notification.setRule("CPU > 5%");
        
        // Add some details to the notification
        notification.addDetail("domain", $event.getDomain());
        notification.addDetail("server", $event.getServer());
        
        // Send the notification to the dispatcher
        NotificationDispatcher.dispatchEvent(NotificationDispatcher.EventAction.SEND_MAIL, notification,"jasmine@ow2.org","jonas@localhost");

end