1

使用 Drools fusion,我想为计算机的 cpu_idle 做一些警报。条件是:

  1. 我会每 10 秒从我监控的机器上收到一条记录;

  2. 如果 cpu_idle<10 ,drools 应该打开一个可能 10mim 的时间窗口,并开始计算条件 cpu_idle<10 发生的次数,变量是发生时间。

  3. 10分钟后,流口水检查发生时间。如果发生时间> 5,然后做一些警报,否则什么也不做。

我不知道该怎么做。我用的是drools 6.1。下面是主要代码:</p>

public static void main(String[] args) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add( ResourceFactory.newByteArrayResource(str.getBytes()),
                  ResourceType.DRL );
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

    EntryPoint atmStream = ksession.getEntryPoint("ATM Stream" );

     ksession.fireAllRules();
}

我有一个像这样的唱片豆:

public class Record {

    private double cpu_idle;
    private Timestamp collecte_time;
    public double getCpu_idle() {
        return cpu_idle;
    }
    public void setCpu_idle(double cpu_idle) {
        this.cpu_idle = cpu_idle;
    }
    public Timestamp getCollecte_time() {
        return collecte_time;
    }
    public void setCollecte_time(Timestamp collecte_time) {
        this.collecte_time = collecte_time;
    }
}
4

1 回答 1

0

你需要一个

class Window {
    int count = 1;
    long time;
    Window( long time ){...}
    // getters and setters
}

以及以下规则(均未经测试):

rule "nothing to do"
when
    $r: Record( cpu_idle >= 10 )
    not Window()
then
    retract( $r );
end

rule "open new window"
when
    $r: Record( cpu_idle < 10, $ts: collecte_time )
    not Window()
then
    insert( new Window( $ts.getTime() ) );
    retract( $r );
end

rule "keep window"
when
    $r: Record( cpu_idle >= 10, $ts: collecte_time )
    $w: Window( $ns: time )
    eval( ($ts.getTime() - $ns)/1000 <= 10*60)
then
    retract( $r );
    modify( $w ){ setCount( $w.getCount() + 1 ) }
end

rule "drop window"
when
    $r: Record( cpu_idle >= 10, $ts: collecte_time )
    $w: Window( $ns: time )
    eval( ($ts.getTime() - $ns)/1000 > 10*60)
then
    retract( $r );
    retract( $w );
end

rule "alarm"
when
    $w: Window( count >= 5 )
then
    // raise alarm
    retract( $w );
end

我会在 Record 中使用 Date 而不是 lava.sql.Timestamp。

于 2015-03-02T06:58:30.573 回答