Skip to content

Instantly share code, notes, and snippets.

@rantav
Last active September 3, 2016 15:48
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rantav/8296354 to your computer and use it in GitHub Desktop.
Save rantav/8296354 to your computer and use it in GitHub Desktop.
Riemann example how to check whether a host is in maintenance mode. And Ruby code to set/unset maintenance mode.
vagrant@precise64:~$ irb
1.9.3-p484 :002 > require "riemann"
=> true
# Activate maintenance-mode:
1.9.3-p484 :010 > Riemann::Client.new << {service: "maintenance-mode", host: "precise64", state: "active", ttl: Float::INFINITY}
=> nil
# Deactivate maintenance-mode:
1.9.3-p484 :011 > Riemann::Client.new << {service: "maintenance-mode", host: "precise64", state: nil, ttl: Float::INFINITY}
=> nil
; vim: filetype=clojure
(logging/init :file "/var/log/riemann.log")
; everything is available on the public interface
; probably not the best idea for a production system
(tcp-server :host "0.0.0.0")
(udp-server :host "0.0.0.0")
(ws-server :host "0.0.0.0")
(repl-server :host "0.0.0.0")
(periodically-expire 10)
; (maintenance-mode? "host-name") is true when the index has an entry
; for the service "maintenance-mode" with the state "active" for the host "host-name"
(defn maintenance-mode? [host]
"Is this host currently in maintenance mode?"
; Take an expression representing a query for maintenance mode
(->> (list 'and (list '= 'host host)
'(= service "maintenance-mode"))
; Search the current Riemann core's index for any matching events
(riemann.index/search (:index @core))
; Take the first match
first
; Find its state
:state
; Is it the string "active"?
(= "active")))
(let [client (tcp-client)
; Keep events for 5 minutes by default
index (default :ttl 300 (update-index (index)))]
(streams
; Print all streams when the host "precise64" is not in maintenance mode
; (this is just a demo)
(where (not (maintenance-mode? "precise64"))
#(info "the host precise64 is not in maintnance mode, OK?" %))
index
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment