1

I am using Riemann in my project to trigger an email alert. Is it possible to trigger email alert only for specific days lets say(mon-fri @ 3am to 8pm) only.

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))))
Mangoski
  • 2,058
  • 5
  • 25
  • 43

1 Answers1

0

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":

; -*- 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. 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.

Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
  • Thanks for your kind response. I will try your suggestion. I have one more question whether we can compare the events with previous time interval events. I have raised the question. http://stackoverflow.com/q/39019835/3484667 – Mangoski Aug 19 '16 at 06:03
  • I have tried the above suggested method Arthur. I am getting some error. I have updated my question with code and the error which I get from compiling the above code. – Mangoski Aug 29 '16 at 11:50
  • Am I missing any important functionality here. – Mangoski Aug 31 '16 at 06:15
  • Your missing the symbol now between 'hour' and ') 20' I'll fix my example when I'm back at my computer – Arthur Ulfeldt Aug 31 '16 at 23:09
  • I have updated my code with `now` now it didn't throw any error but my `rollup` function is not working after adding the above code. Whenver the condition is satisfied it throwing an email. – Mangoski Sep 02 '16 at 12:40
  • The above code looks doesn't works for me . It's triggering the alert all the time my scenario is to trigger between `3AM` to `8PM`. My `rollup` functionality is also not working after including the code. Can you please correct me where I am going wrong. – Mangoski Sep 06 '16 at 06:05
  • Did you get any chance to lookup the issue – Mangoski Sep 13 '16 at 09:22
  • Thanks for the reminder. I'm traveling this weekend and will get back to this on Tuesday. Thanks for being patient (but not so patient that you let me forget ;) – Arthur Ulfeldt Sep 17 '16 at 01:42
  • I'm trying to write an example with tests, but my tests don't work on time-based windows. Which has me confused. – Arthur Ulfeldt Sep 24 '16 at 00:58
  • Hi @Arthur did you get a chance to look on this – Mangoski Oct 03 '16 at 06:55
  • I have not figured out how to use time based windows in unit tests in riemann, so I posted my anser using event based windows. You can switch these to time based ones after you make sure it works in your environment using event based ones. – Arthur Ulfeldt Oct 03 '16 at 22:13