简体   繁体   English

是否可以在黎曼的特定日期和特定时间配置电子邮件警报?

[英]Is it possible to configure email alert at specific days and specific time in Riemann?

I am using Riemann in my project to trigger an email alert. 我在我的项目中使用Riemann来触发电子邮件警报。 Is it possible to trigger email alert only for specific days lets say(mon-fri @ 3am to 8pm) only. 是否有可能仅在特定日期触发电子邮件警报(仅限于周一至周五下午3点至晚上8点)。

If so can anyone help me with the reference. 如果是这样,任何人都可以帮助我参考。

My updated code: 我的更新代码:

(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 "xxx@xx.com")
                 ))prn))))

Yes! 是! you can put any code you want into the conditional positon of a where clause here is an example config that depends on the time of day and day of week to decide when to send "alerts": 你可以把你想要的任何代码放到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 []}))))))

and run the tests: 并运行测试:

root@176ee53d0c62:/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.

In this example I used fixed-event-window because I couldn't figure out how to make fixed-time-window work in a unit test in riemann. 在这个例子中,我使用了fixed-event-window因为我无法弄清楚如何在riemann的单元测试中使固定时间窗口工作。 I think it's taken long enough to figure that out that I thought you would be interested in seeing the solution thus far. 我认为已经花了很长时间才弄清楚我认为你有兴趣看到迄今为止的解决方案。 If you change it to a fixed-time-window it will work the same, except the tests won't run. 如果将其更改为固定时间窗口,它将工作相同,但测试将不会运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM