Accessing Event variables

109 views
Skip to first unread message

Varun S.K.

unread,
Jan 25, 2016, 2:23:36 AM1/25/16
to Riemann Users
I am new to Riemann. I want to do some computation by taking the values from event(dictionary) . Iam able to assign it some local variable and if i print the type of that variable it is printing it properly. But i want to call some function by passing that value it throws me an error. Even inside streams iam unable to make use of that variable to do some computation.Lets say for example i have :time in my event which is in epoch format . I want to convert it to normal time which is not working. If I define a variable inside streams and assign it some value and then convert the same value it works well and good. I need some explanation regarding how to  send the event variable as it is to some function 

Aphyr

unread,
Jan 25, 2016, 2:27:24 AM1/25/16
to rieman...@googlegroups.com
I'm not sure exactly what your question is, but I can point you to all kinds of
documentation! You might try the first few chapters of Clojure from the Ground
Up (https://aphyr.com/tags/Clojure-from-the-ground-up), these docs on the
Riemann stream model
(http://riemann.io/howto.html#understanding-the-riemann-stream-model) and the
smap stream, which applies a function to each event to transform it
(http://riemann.io/api/riemann.streams.html#var-smap).

--Kyle

Varun S.K.

unread,
Jan 25, 2016, 2:46:01 AM1/25/16
to rieman...@googlegroups.com
This is the code Im trying to implement:
(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)
;(streams prn)
(def w "")
(def z "")
(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))))))
(def email (mailer {:host "XXX"
                    :port 587
                    :user "XXX"
                    :pass "XXXX"
                    :tls "true"
                    :subject (fn [events](clojure.string/join ", "(map :time events)))   
                    :body (fn [eve](str x));;;this is where Im trying to access x, value output in the email is ":time"(without quotes)
                    :from "XXX"}))
(streams
            (where (service #"memory")
              (def x :time)
              #(info (x %))
              #(info (type (x %)))
              email "so...@some.com"

              
))
 Using this code, Inside the streams I'm assigning value of the time field of the event dictionary to a variable x. The event dictionary is nothing but the collection being passed in each stream which is filtered for the memory service. If I try accessing this variable x in the email function, the value thrown is the keyword itself , as in ":time" is the value being emailed to me. I also need to perform some computation on the variable x before passing it to the email function. I hope you've got an idea of my problem. Eagerly await your response.




--
You received this message because you are subscribed to a topic in the Google Groups "Riemann Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/riemann-users/jAyTTCugrBE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to riemann-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Aphyr

unread,
Jan 25, 2016, 3:05:47 AM1/25/16
to rieman...@googlegroups.com
On 01/24/2016 11:45 PM, Varun S.K. wrote:
> Inside the streams I'm assigning value of the time field of the event dictionary
> to a variable x. The event dictionary is nothing but the collection being passed
> in each stream which is filtered for the memory service. If I try accessing this
> variable x in the email function, the value thrown is the keyword itself , as in
> ":time" is the value being emailed to me.

No, you've defined `x` to be the keyword `:time`, which is why (str x) returns
":time". See https://aphyr.com/posts/306-clojure-from-the-ground-up-state.

--Kyle

Varun S.K.

unread,
Jan 25, 2016, 3:16:09 AM1/25/16
to rieman...@googlegroups.com
Ok. Thank you for the immediate response. How exactly do I access the events collections' :time and store it in a variable for further computation. I have been struggling with that for quite a while.



--Kyle

Aphyr

unread,
Jan 25, 2016, 3:16:59 AM1/25/16
to rieman...@googlegroups.com
On 01/25/2016 12:16 AM, Varun S.K. wrote:
> Ok. Thank you for the immediate response. How exactly do I access the events
> collections' :time and store it in a variable for further computation. I have
> been struggling with that for quite a while.

http://riemann.io/concepts.html
http://riemann.io/howto.html#custom-event-attributes

--Kyle

Varun S.K.

unread,
Jan 25, 2016, 4:14:49 AM1/25/16
to rieman...@googlegroups.com
Ok fine.I have improved on the code a bit.  
(def w "")
(def x "")
(def y "")
(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))))))
(def email (mailer {:host "XXX"
                    :port 587
                    :user "XXX"
                    :pass "YYY"
                    :tls "true"
                    :subject (fn [events](str x))   
                    :body (fn [eve](str y))
                    :from "ZZZ"}))
(streams
            (where (service #"memory")
              (def x (get event :time))
              (def y (+ x 1000))
              (email "so...@some.com")
         ))

This is my config file . I want to add 1000 to the timestamp and then email it . This is throwing me null pointer exception at x or y(not sure).



--Kyle

Nathan LeClaire

unread,
Jan 25, 2016, 6:46:16 PM1/25/16
to Riemann Users
Varun, how familiar are you with the functional principles behind Clojure? http://clojure.org/about/functional_programming

It looks like brushing up a bit on using immutable data structures could help with your configuration issues.  The code you have posted here strikes me as trying to write imperative programming-style code, which is not how Clojure is generally meant to be written.

Varun S.K.

unread,
Jan 25, 2016, 10:46:49 PM1/25/16
to rieman...@googlegroups.com

Hey Nathan , I am completely new to Functional programming. I have never done any kind of coding in this language. May be I should go through all basics of functional programming then I should start with clojure.
Thanks for your response

Diego Zamboni

unread,
Jan 26, 2016, 3:39:26 AM1/26/16
to rieman...@googlegroups.com
Varun,

I fully agree with Nathan - learning even a little bit of Clojure has a huge impact on how effectively you can use Riemann. Plus it’s a really nice language. At least read through http://riemann.io/clojure.html. I started with Kyle’s tutorial at https://aphyr.com/tags/Clojure-from-the-ground-up, and I’m now reading through “Clojure for the Brave and True” (http://www.braveclojure.com). I highly recommend both.

—Diego

On 26 Jan 2016, at 04:46, Varun S.K. <varun...@gmail.com> wrote:

Hey Nathan , I am completely new to Functional programming. I have never done any kind of coding in this language. May be I should go through all basics of functional programming then I should start with clojure.
Thanks for your response


--
You received this message because you are subscribed to the Google Groups "Riemann Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to riemann-user...@googlegroups.com.

Varun S.K.

unread,
Jan 27, 2016, 12:02:06 AM1/27/16
to rieman...@googlegroups.com
Sure Diego, I will go through the links which you provided . 
Thanks 

--
You received this message because you are subscribed to a topic in the Google Groups "Riemann Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/riemann-users/jAyTTCugrBE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to riemann-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages