2016-08-18 36 views
1

我在我的項目中使用Riemann來觸發電子郵件警報。只有特定的日子纔可以觸發電子郵件提醒(mon-fri @ 3am到8pm)。是否可以在黎曼的特定日期和特定時間配置電子郵件警報?

如果有的話,任何人都可以幫我參考。

我更新的代碼:

(tcp-server {:host "127.0.0.1" :port 5555}) 

(let [userindex1 (default :ttl 300 (update-index (index)))]) 
    (let [email (mailer {....email configuration})] 
      (streams 
     (where (service "log") 
      (smap 
       (fn [events] 
       (let [count-of-transaction (count (filter #(= "error" (:type %)) events))] 
        (event 
        { 
        :status "Failure" 
        :metric count-of-failures 
        :total-fail (< count-of-failures 2)}))) 

       (where (let [now (clj-time.core/now)] 
        (and (<= 3 (clj-time.core/hour now) 20) 
         (<= 1 (clj-time.core/day-of-week now) 5) 
         (= (:status event) "Failure") 
         (:total-fail event))) 
       (rollup 1 200 
       (email "[email protected]") 
       ))prn)))) 
+0

我假設這是一個發佈錯誤,但在你的例子中,最上面的let是在錯誤的地方。 –

+0

你的意思是這條線'(讓[userindex1(默認:ttl 300(update-index(index)))])' – Mangoski

回答

0

是的!你可以把你想要到這裏where條款的條件當前位置的任何代碼是一個示例配置取決於天和星期的時間來決定何時發送「警告」:

; -*- mode: clojure; -*- 
>; vim: filetype=clojure 

(riemann.repl/start-server! {:port 5557 :host "0.0.0.0"}) ;; this is only safe in a docker container 


; Listen on the local interface over TCP (5555), UDP (5555), and websockets 
; (5556) 
(let [host "0.0.0.0"] 
    (tcp-server {:host host}) 
    (ws-server {:host host})) 

; Expire old events from the index every 5 seconds. 
(periodically-expire 5) 

(require '[clj-time [core :refer [now hour day-of-week]]]) 

(let [index (tap :index (index))] 
    (streams 
    (where (service "log") 
      (fixed-event-window 3 
          (smap 
           (fn [events] 
           (let [count-of-failures (count (filter #(= "error" (:type %)) events)) 
             new-event (assoc (first events) 
                 :status "Failure" 
                 :metric count-of-failures 
                 :ttl 300 
                 :total-fail (> count-of-failures 2))]          
            new-event)) 
           (where (fn [event] 
             (let [now (clj-time.core/now)] 
             (and (<= 0 (clj-time.core/hour now) 17) 
               (<= 2 (clj-time.core/day-of-week now) 6) 
               (= (:status event) "Failure") 
               (:total-fail event)))) 
            (throttle 1 200 
               #(println "emailing about" %) 
               index))))))) 

(tests 
    (deftest index-test 
    (let [workday (clj-time.format/parse "2142-01-01T00:00:00.000Z") 
      weekend (clj-time.format/parse "2142-01-03T00:00:00.000Z")] 
     (let [index-after-events (with-redefs [clj-time.core/now (constantly workday)] 
           (inject! [{:service "log" 
              :type "error" 
              :time 42} 
              {:service "log" 
              :type "error" 
              :time 43} 
              {:service "log" 
              :type "error" 
              :time 44}]))] 
     (is (= index-after-events 
       {:index [{:service "log", :type "error", :time 42, :status "Failure", :metric 3, :ttl 300, :total-fail true}]}))) 
     (let [index-after-events (with-redefs [clj-time.core/now (constantly weekend)] 
           (inject! [{:service "log" 
              :type "error" 
              :time 42} 
              {:service "log" 
              :type "error" 
              :time 43} 
              {:service "log" 
              :type "error" 
              :time 44}]))] 
     (is (= index-after-events 
       {:index []})))))) 

並運行測試:

[email protected]:/test# riemann test sample.config 
INFO [2016-10-03 22:07:39,805] main - riemann.bin - Loading /test/sample.config 
INFO [2016-10-03 22:07:39,840] main - riemann.repl - REPL server {:port 5557, :host 0.0.0.0} online 

Testing riemann.config-test 
emailing about {:service log, :type error, :time 42, :status Failure, :metric 3, :ttl 300, :total-fail true} 

Ran 1 tests containing 2 assertions. 
0 failures, 0 errors. 

在這個例子中,我使用fixed-event-window,因爲我無法弄清楚如何使固定的時間窗口工作在黎曼單元測試。我認爲花費足夠的時間才能弄清楚,我認爲你會對迄今爲止看到的解決方案感興趣。如果您將其更改爲固定時間窗口,則其工作原理將相同,但測試不會運行。

+0

感謝您的迴應。我會嘗試你的建議。我們還有一個問題,我們是否可以將事件與以前的時間間隔事件進行比較。我提出了這個問題。 http://stackoverflow.com/q/39019835/3484667 – Mangoski

+0

我已經嘗試了上述建議的方法亞瑟。我收到一些錯誤。我用代碼更新了我的問題,並從編譯上述代碼中得到了錯誤。 – Mangoski

+0

我在這裏錯過了任何重要的功能。 – Mangoski

相關問題