Skip to content

Instantly share code, notes, and snippets.

@haslinger
Last active February 8, 2017 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haslinger/9a1ee4caf15bed80e59f0e6a8f6f764b to your computer and use it in GitHub Desktop.
Save haslinger/9a1ee4caf15bed80e59f0e6a8f6f764b to your computer and use it in GitHub Desktop.
Temperature monitor
# m h dom mon dow command
# User environment wrapper provided by .rvm
*/5 * * * * /home/username/.rvm/wrappers/ruby-2.3.3/ruby /var/scripts/temperature.rb
# To check one of the files via monit and send email to an extra address:
check file vorzimmer with path /var/scripts/rrd-data/vorzimmer.txt
if match "Alarm!" then alert
if timestamp > 15 minutes then alert
alert email@example.com
require 'nokogiri'
require 'open-uri'
require "rrd"
# that's how the database has been created:
#rrd.create start: Time.now - 10.seconds, step: 5.minutes do
# datasource "abstellraum", type: :gauge, heartbeat: 10.minutes, min: 0, max: 50
# datasource "vorzimmer", type: :gauge, heartbeat: 10.minutes, min: 0, max: 50
# archive :average, every: 5.minutes, during: 1.day
# archive :average, every: 30.minutes, during: 1.week
# archive :average, every: 1.day, during: 1.year
#end
# read temperature sensor webserver page
doc = Nokogiri::HTML(open("http://1.2.3.4/ipwe.cgi", :http_basic_authentication => ["username", "super_secret_password"]))
# get data for one room
vorzimmer = doc.css("html body form table tbody tr:nth-child(2) td:nth-child(4)").text[/[0-9\.]+/]
# get data for another room
abstellraum = doc.css("html body form table tbody tr:nth-child(3) td:nth-child(4)").text[/[0-9\.]+/]
# open rrd database file
rrd_file = "/var/scripts/rrd-data/temperatures.rrd"
rrd = RRD::Base.new(rrd_file)
# store data in it
rrd.update Time.now, abstellraum.to_f, vorzimmer.to_f
# create small text files for monit escalation email
[[:vorzimmer, vorzimmer.to_f, 15] , [:abstellraum, abstellraum.to_f, 10]].each do |key, value, limit|
status = value < limit ? "#{Time.now()} - Alarm! temperature #{value}°C to low in #{key}\n" : "#{Time.now()} - ok\n"
File.open("/var/scripts/rrd-data/#{key}.txt", 'a') { |file| file.write(status) }
end
# create a nice graph from it
graph = RRD.graph "/var/www/html/today.png", title: "Temperatures today",
width: 800,
height: 250,
color: ["FONT#000000", "BACK#ffffff"],
start: Time.now - 1.day,
end: Time.now do
line rrd_file, abstellraum: :average,
color: "#0022ee",
label: "First room"
line rrd_file, vorzimmer: :average,
color: "#22ee00",
label: "Second room"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment