May 4, 2017

A tour of Riemann : A simple check

How to create a simple check with Riemann ?

A basic Riemann configuration

This is a basic riemann.config file (it should be in /etc/riemann/riemann.config if you have installed Riemann using rpm/deb).

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

(let [host "0.0.0.0"]
  (tcp-server {:host host})
  (udp-server {:host host}))

(periodically-expire 10)

(streams)

Here, i initialize logging, start a tcp and a udp server, and configure index expiration every 10 seconds.

(streams) do nothing actually.

Check if metric > to a threshold

Create a file named mycorp/system/ram.clj. We want a stream which:

  • Filter all events excepts events about RAM.

  • Write the event in the log file if the RAM is > to 90 %.

We will simulate these events using Riemann test API:

ram graph

Let’s define a basic mycorp/system/ram.clj (doing nothing):

(ns mycorp.system.ram
  (:require [riemann.config :refer :all]
            [riemann.streams :refer :all]
            [riemann.test :refer :all]
            [clojure.tools.logging :refer :all]))

(def threshold 90)

(def ram-stream)

(tests
  (deftest ram-stream-test))

We have imported Riemann essentials namespaces, defined a threshold variable, a ram-stream variable (which will contain our stream), and a test. Let’s complete the test part:

(tests
 (deftest ram-stream-test
   (let [result (inject! [mycorp.system.ram/ram-stream]
                         [{:host "foo"
                           :service "memory/percent-used"
                           :metric 60
                           :time 1}
                          {:host "foo"
                           :service "ramdom-event"
                           :metric 4000
                           :time 3}
                          {:host "foo"
                           :service "memory/percent-used"
                           :metric 95
                           :time 11}
                          {:host "foo"
                           :service "memory/percent-used"
                           :metric 80
                           :time 21}
                           {:host "foo"
                           :service "foobar"
                           :metric 3100
                           :time 24}
                          {:host "foo"
                           :service "memory/percent-used"
                           :metric 92
                           :time 31}])]
     (is (= (:ram-stream-tap result)
            [{:host "foo"
              :service "memory/percent-used"
              :metric 95
              :time 11}
             {:host "foo"
              :service "memory/percent-used"
              :metric 92
              :time 31}])))))

As you can see, i will inject into mycorp.system.ram/ram-stream a serie of events. Some are RAM events (and are represented by the previous graph), and some random events. I store the inject! result into result.

A tap is a point where you can observe events. Take a look at the writing tests section in the Riemann howto to learn more about tap.

In my test, i get the value of the tap named :ram-stream-tap.

Obviously, my test will fail because ram-stream is not defined. Let’s define it:

(def ram-stream
  (where (service "memory/percent-used")
    (where (> (:metric event) threshold)
      (io #(info %))
      (tap :ram-stream-tap))))

I use where to filter events whose name is not "ram-used" and where again to filter events with a :metric < to threshold. I print remaining events in the log file using info and push them into a tap for testing.

I could also use only one where stream:

(def ram-stream
  (where (and (service "memory/percent-used")
              (> (:metric event) threshold))
    (io #(info %))
    (tap :ram-stream-tap)))

Now, import your ram.clj file in riemann.config:

(include "mycorp/system/ram.clj")

(require '[riemann.test :refer :all]
         '[mycorp.system.ram :as ram])

;; rest of the file

Launch your tests:

riemann test /etc/riemann/riemann.config

Ran 1 tests containing 1 assertions.
0 failures, 0 errors.

Good job ! You can now use this stream in your configuration. Add it in riemann.config:

(streams
 ram/ram-stream)

And that’s it. You have a stream checking the ram. Not very useful (just a threshold), but it’s something :)

In the next article, things will be more interesting.

Code here.

Tags: devops english

Add a comment








If you have a bug/issue with the commenting system, please send me an email (my email is in the "About" section).

Top of page