2015-08-25 135 views
1

我是clojure的新手,需要設置riemann配置,以便編輯/添加新條件。我們現在有什麼:需要幫助優化clojure語句

(defn tell-ops 
([to] 
    (by [:service] 
     (throttle 3 360 
      (rollup 2 360 
       slackerDefault 
       (email to))))) 
([to channel] 
    (by [:service] 
     (throttle 3 360 
      (rollup 2 360 
       (slacker channel) 
       (email to)))))) 

    ............ 

(where (state "FATAL") 
    (where (service #"^Serv1") 
      (tell-ops "[email protected]" "#dev-ops1")) 

    (where (service #"^Serv2") 
      (tell-ops "[email protected]")) 
    .... 

) 

而且,它缺乏default語句,像 如果沒有匹配,告訴-OPS「[email protected]

我想我需要的東西就像一個頂級struct

(def services 
[{:regex #"^serv1" :mail "[email protected]" :channel "#serv1"} 
    {:regex #"serv2$" :mail "[email protected]"} ]) 

因此很容易添加新的。但我不知道如何循環穿透式該數組考慮缺席的:在第二種情況下通道,使「默認調用」如果沒有正則表達式的提前

回答

1

匹配

謝謝,我不知道多少關於Riemann,但我認爲你可以使用標準的Clojure數據處理工具來解決你的問題。我喜歡您對警報策略的頂級結構的想法。我會在最後添加一個全面的策略來處理默認情況。修改你的代碼位,使之黎曼特定少:

(defn tell-ops! 
    [{:keys [mail channel]}] 
    (when mail (println (str "mail = " mail))) 
    (when channel (println (str "channel = " channel)))) 

(def policies 
    [{:regex #"^serv1.*" :mail "[email protected]" :channel "#serv1"} 
    {:regex #".*serv2$" :mail "[email protected]"} 
    {:regex #".*" :mail "[email protected]"}]) 

(defn alert! 
    [policies service-in-alert] 
    (-> (drop-while (fn 
        [{:keys [regex]}] 
        (nil? (re-matches regex service-in-alert))) 
     policies) 
    first 
    tell-ops!)) 

一些注意事項:

  • 這是地道的功能名稱使用!對於產生副作用(如發佈一條消息功能通道或發送電子郵件)
  • ,如果你想爲tell-ops!功能的硬盤默認情況下,你可以解構地圖時使用Clojure的默認值支持:{:keys [mail channel] :or {mail "[email protected]" channel "#default-chan"}}
+0

謝謝,我會試一試並在此發佈結果! –