2015-10-19 58 views
1

我使用了黎曼默認的配置設置:添加自定義日期字段黎曼活動

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

(logging/init {:file "riemann.log"}) 

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

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

(let [index (index)] 
    ; Inbound events will be passed to these streams: 
    (streams 
    (default :ttl 60 
     ; Index all events immediately. 
     index 

     ; Log expired events. 
     (expired 
     (fn [event] (info "expired" event)))))) 

(streams 
     prn) 

它輸出事件本身(刪除主機名):

#riemann.codec.Event{:host "xxxxxxxx", :service "riemann server tcp 127.0.0.1:5555 in latency 0.99", :state "ok", :description nil, :metric nil, :tags nil, :time 722617016251/500, :ttl 20} 
#riemann.codec.Event{:host "xxxxxxxx", :service "riemann server tcp 127.0.0.1:5555 in latency 0.999", :state "ok", :description nil, :metric nil, :tags nil, :time 722617016251/500, :ttl 20} 
#riemann.codec.Event{:host "xxxxxxxx", :service "riemann server udp 127.0.0.1:5555 in rate", :state "ok", :description nil, :metric 0.0, :tags nil, :time 722617016251/500, :ttl 20} 
#riemann.codec.Event{:host "xxxxxxxx", :service "riemann server udp 127.0.0.1:5555 in latency 0.0", :state "ok", :description nil, :metric nil, :tags nil, :time 722617016251/500, :ttl 20} 
#riemann.codec.Event{:host "xxxxxxxx", :service "riemann server udp 127.0.0.1:5555 in latency 0.5", :state "ok", :description nil, :metric nil, :tags nil, :time 722617016251/500, :ttl 20} 
#riemann.codec.Event{:host "xxxxxxxx", :service "riemann server udp 127.0.0.1:5555 in latency 0.95", :state "ok", :description nil, :metric nil, :tags nil, :time 722617016251/500, :ttl 20} 
#riemann.codec.Event{:host "xxxxxxxx", :service "riemann server udp 127.0.0.1:5555 in latency 0.99", :state "ok", :description nil, :metric nil, :tags nil, :time 722617016251/500, :ttl 20} 
#riemann.codec.Event{:host "xxxxxxxx", :service "riemann server udp 127.0.0.1:5555 in latency 0.999", :state "ok", :description nil, :metric nil, :tags nil, :time 722617016251/500, :ttl 20} 

時間字段作爲UTC時間戳出現,使用上述配置我怎麼能如何添加一個額外的字段到這些名爲日期的事件,以dd-mm-yyyy hh:mm:ss格式顯示日期?例如:

19-10-2015 05:00:00

我有出現做時間轉換的一些功能,但我不知道如何實現它們在配置:

(defn logstash-v1-format 
    "Convert an event to a Logstash V1 format compatible document" 
    [event] 
    (merge (dissoc event :time :attributes) 
     (:attributes event) 
     {"@timestamp" (unix-to-iso8601 (:time event)) 
      "@version" "1" 
      })) 

(defn time-at 
    "Returns the Date of a unix epoch time." 
    [unix-time] 
    (java.util.Date. (long (* 1000 unix-time)))) 

(defn unix-to-iso8601 
    "Transforms unix time to iso8601 string" 
    [unix] 
    (clj-time.format/unparse (clj-time.format/formatters :date-time) 
          (clj-time.coerce/from-long (long (* 1000 unix))))) 

回答

1

答案是附上指數:

(adjust #(assoc % :timestamp (.format (java.text.SimpleDateFormat. "yyyy-MM-dd'T'hh:mm:ss'Z'") (java.util.Date.))) prn index) 

它最終被:

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

(logging/init {:file "riemann.log"}) 

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

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

(let [index (index)] 
    ; Inbound events will be passed to these streams: 
    (streams 
    (default :ttl 60 
     ; Index all events immediately. 
     (adjust #(assoc % :timestamp (.format (java.text.SimpleDateFormat. "yyyy-MM-dd'T'hh:mm:ss'Z'") (java.util.Date.))) prn index) 

     ; Log expired events. 
     (expired 
     (fn [event] (info "expired" event))))))