Search code examples
clojurenullpointerexceptionriemann

Null pointer exception when trying to write to file using riemann


I am trying to set up multiple logging streams for riemann. I have decided to use the simple Clojure file write function to set up additional streams. However, the file is written to as soon as riemann is reloaded but results in a null pointer exception when the relevant event is called and the file needs to be written to.

(where (and (or (tagged "source1") (host "shubham"))
                    (not (= (:installation_id event) "default")))

               (smap (fn [event] (prepare-influx-event event {:time-unit :nanoseconds}))
                     influx
                     )

                (let [wrtr (io/writer "/var/log/riemann/test.txt" :append true)]
                  (.write wrtr "Listen please1\n")
                  (.close wrtr))
                ;;(spit "/var/log/riemann/test.txt" "Listen please2\n" :append true)
                )

Solution

  • the riemann config contains a Clojure expression that is run when riemann starts. The result of running this expsreassion, that is the thing that evaluating it returns, is then used to process all the events. Riemann's config file is a function, which returns a function that will do the actual work. (insert yo'dog memes here)

    In this case, when riemann loads that last expression it, while it's getting ready to run, it will open the file, write to it, close it. then it will take the result of closing it and treat that as the function to handle events.

    the result of closing a file is null, so it will later try to "run" that null as a function and a NPE results.

    you likely want an smap around the let, or just remove the close as you want riemann to keep this file open the whole time it runs. depending on what your going for you may consider making the call to smap above be the last expression inside the let.


    below this line is purely my opinion, and not part of the answer:

    you almost certainly want to use a proper logging-thing to log stuff to disk on the riemann host. These hosts tend to run for a long time uninterrupted (i tend to leave them a couple years) and if you have it writing to a single file, that file will eventually encounter physics (full disk, size limit etc.) and then if you ssh in and delete it, no space will be freed because riemann will still have the file open. This somehow seems to always happen during some event where you'd really rather monitoring was working. Using a thing with log-rotation is generally a good idea.