Skip to content

Commit

Permalink
(WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrocarrico committed Aug 6, 2015
1 parent e2aecfb commit facd257
Show file tree
Hide file tree
Showing 24 changed files with 831 additions and 293 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
pimon (0.1.11)
pimon (0.2.0.alpha.beta)
faye-websocket (= 0.10.0)
haml (= 4.0.6)
sinatra (= 1.4.6)
Expand Down
20 changes: 7 additions & 13 deletions config/default.yml
@@ -1,14 +1,8 @@
chart:
cpu:
color: '#D2691E'
disk:
color: '#CDC673'
mem:
color: '#87CEFA'
temp:
color: '#FF9B04'
swap:
color: '#3CB371'
colors:
cpu: '#D2691E'
disk: '#CDC673'
mem: '#87CEFA'
temp: '#FF9B04'
swap: '#3CB371'
stats_collector:
number_of_stats: 6
time_period_in_secs: 30
time_period_in_secs: 10
18 changes: 6 additions & 12 deletions config/test.yml
@@ -1,15 +1,9 @@
chart:
cpu:
color: '#D2691E'
disk:
color: '#CDC673'
mem:
color: '#87CEFA'
temp:
color: '#FF9B04'
swap:
color: '#3CB371'
colors:
cpu: '#D2691E'
disk: '#CDC673'
mem: '#87CEFA'
temp: '#FF9B04'
swap: '#3CB371'
hostname: 'test_hostname'
stats_collector:
number_of_stats: 6
time_period_in_min: 10
1 change: 0 additions & 1 deletion config/test_broken.yml
Expand Up @@ -8,5 +8,4 @@ chart:
swap:
color: '#3CB371'
stats_collector:
number_of_stats: 6
time_period_in_min: 10
61 changes: 36 additions & 25 deletions lib/pimon.rb
Expand Up @@ -12,55 +12,66 @@
class Pimon < Sinatra::Base
set :public_folder, "#{File.dirname(__FILE__)}/pimon/public"
set :views, "#{File.dirname(__FILE__)}/pimon/views"
set :sockets, []
set :connected_websockets, []

configure :development, :production do
filename = "#{File.dirname(__FILE__)}/../config/default.yml"
config = PimonConfig.create_new(ENV['PIMON_CONFIG'] || filename)

EventMachine::next_tick do
settings.timer = EventMachine::add_periodic_timer(config.stats[:time_period_in_secs]) do
settings.stats_checker.collect_stats
@stats = settings.stats_checker.show_stats
settings.sockets.each{ |s| s.send(@stats) }
end
end
config = PimonConfig.load(ENV['PIMON_CONFIG'] || filename)

set :config, config
set :stats_checker, StatsCollector.new(config)
set :stats_collector, StatsCollector.new(config)
set :timer, nil

settings.stats_checker.collect_stats
end

configure :test do
config = PimonConfig.create_new("#{File.dirname(__FILE__)}/../config/test.yml")

set :config, config
set :stats_checker, StatsCollector.new(config)
set :stats_collector, StatsCollector.new(config)
set :timer, nil
end

def initialize(*args, &bk)
super
self.start_collecting_stats
self
end

get '/' do
last_update = settings.stats_collector.last_update
last_modified(last_update) if ENV['RACK_ENV'] != 'development' && last_update

haml :index, :locals => { :charts => settings.stats_collector.charts, :hostname => settings.config.hostname }
end

get '/stats' do
if Faye::WebSocket.websocket?(request.env)
ws = Faye::WebSocket.new(request.env)
websocket = Faye::WebSocket.new(request.env)

ws.on(:open) do
settings.sockets << ws
@stats ||= settings.stats_checker.show_stats
ws.send(@stats)
websocket.on(:open) do
settings.connected_websockets << websocket
websocket.send(settings.stats_collector.stats.to_json)
end

ws.on(:close) do
settings.sockets.delete(ws)
websocket.on(:close) do
settings.connected_websockets.delete(websocket)
end

ws.rack_response
websocket.rack_response
else
last_update = settings.stats_checker.last_update
last_modified(last_update) if ENV['RACK_ENV'] != 'development' && last_update
halt 404
end
end

haml :index
def start_collecting_stats
unless settings.test?
settings.stats_collector.collect_stats
EventMachine::next_tick do
settings.timer = EventMachine::add_periodic_timer(settings.config.stats[:time_period_in_secs]) do
settings.stats_collector.collect_stats
settings.connected_websockets.each{ |s| s.send(settings.stats_collector.stats.to_json) }
end
end
end
end
end
13 changes: 8 additions & 5 deletions lib/pimon/pimon_config.rb
Expand Up @@ -3,20 +3,23 @@
require_relative 'hash_extensions'

class PimonConfig
def self.create_new(filename)
attr_reader :config
attr_reader :hostname

def self.load(filename)
return self.new(filename)
end

def chart
@config[:chart]
def colors
config[:colors]
end

def stats
@config[:stats_collector]
config[:stats_collector]
end

def hostname
@hostname ||= @config[:hostname].nil? ? `hostname` : @config[:hostname]
hostname ||= config[:hostname].nil? ? `hostname` : config[:hostname]
end

private
Expand Down
10 changes: 4 additions & 6 deletions lib/pimon/probe/cpu_usage.rb
@@ -1,11 +1,9 @@
require_relative 'probe'
require_relative 'sample'

class Probe::CpuUsage < Probe
def self.check
100 - `vmstat 1 2`.split(/\n/)[3].split(" ")[14].to_i
end

def self.symbol
:cpu
def self.check(date=Time.now)
value = 100 - `vmstat 1 2`.split(/\n/)[3].split(" ")[14].to_i
Probe::Sample.new(:date => date, :probe_name => 'cpu', :value => value, :unit => unit)
end
end
10 changes: 4 additions & 6 deletions lib/pimon/probe/disk_usage.rb
@@ -1,11 +1,9 @@
require_relative 'probe'
require_relative 'sample'

class Probe::DiskUsage < Probe
def self.check
`df`.split(/\n/)[1].split(" ")[4].to_i
end

def self.symbol
:disk
def self.check(date=Time.now)
value = `df`.split(/\n/)[1].split(" ")[4].to_i
Probe::Sample.new(:date => date, :probe_name => "disk", :value => value, :unit => unit)
end
end
9 changes: 3 additions & 6 deletions lib/pimon/probe/memory_usage.rb
@@ -1,12 +1,9 @@
require_relative 'probe'
require_relative 'system_memory'
require_relative 'sample'

class Probe::MemoryUsage < Probe
def self.check
SystemMemory.check(:mem)
end

def self.symbol
:mem
def self.check(date=Time.now)
Probe::Sample.new(:date => date, :probe_name => 'mem', :value => SystemMemory.check(:mem), :unit => unit)
end
end
6 changes: 1 addition & 5 deletions lib/pimon/probe/probe.rb
@@ -1,9 +1,5 @@
class Probe
def self.check
raise "Not implemented"
end

def self.symbol
def self.check(date)
raise "Not implemented"
end

Expand Down
22 changes: 22 additions & 0 deletions lib/pimon/probe/sample.rb
@@ -0,0 +1,22 @@
class Probe::Sample
attr_accessor :date
attr_accessor :probe_name
attr_accessor :value
attr_accessor :unit

def initialize(options)
@date = options[:date]
@probe_name = options[:probe_name]
@value = options[:value]
@unit = options[:unit]
end

def to_json(options)
{
:date => date.strftime("%Y-%m-%d %H:%M:%S"),
:probe_name => probe_name,
:value => value,
:unit => unit
}.to_json(options)
end
end
9 changes: 3 additions & 6 deletions lib/pimon/probe/swap_usage.rb
@@ -1,12 +1,9 @@
require_relative 'probe'
require_relative 'system_memory'
require_relative 'sample'

class Probe::SwapUsage < Probe
def self.check
SystemMemory.check(:swap)
end

def self.symbol
:swap
def self.check(date=Time.now)
Probe::Sample.new(:date => date, :probe_name => 'swap', :value => SystemMemory.check(:swap), :unit => unit)
end
end
13 changes: 5 additions & 8 deletions lib/pimon/probe/temperature.rb
@@ -1,15 +1,12 @@
# encoding: UTF-8
require_relative 'probe'
require_relative 'sample'

class Probe::Temperature < Probe
def self.check
`cat /sys/class/thermal/thermal_zone0/temp`[0..1].to_i
def self.check(date=Time.now)
value = `cat /sys/class/thermal/thermal_zone0/temp`[0..1].to_i
Probe::Sample.new(:date => date, :probe_name => 'temp', :value => value, :unit => unit)
end

def self.symbol
:temp
end


def self.unit
'ºC'
end
Expand Down
11 changes: 0 additions & 11 deletions lib/pimon/probe/time_check.rb

This file was deleted.

9 changes: 3 additions & 6 deletions lib/pimon/probe/uptime.rb
@@ -1,13 +1,10 @@
require 'sys/uptime'

require_relative 'probe'
require_relative 'sample'

class Probe::Uptime < Probe
def self.check
Sys::Uptime.dhms
end

def self.symbol
:uptime
def self.check(date=Time.now)
Probe::Sample.new(:date => date, :probe_name => 'uptime', :value => Sys::Uptime.dhms)
end
end

0 comments on commit facd257

Please sign in to comment.