Created
October 14, 2012 02:39
-
-
Save bleonard/3887058 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyPlugin | |
def self.calculates(name) | |
self.calculations << name | |
end | |
def self.calculations | |
@calculations ||= [] | |
end | |
def initialize(debug=false) | |
@debug = debug | |
end | |
def debug? | |
!!@debug | |
end | |
def report | |
out = {} | |
self.class.calculations.each do |name| | |
# e.g. calculate_disk | |
results = send("calculate_#{name}") | |
out[name] = results | |
end | |
{:plugins => out } | |
end | |
end | |
class DiskPlugin < MyPlugin | |
calculates :disk | |
def calculate_disk | |
parse_stats_to_hash(command) | |
end | |
private | |
def command | |
if debug? | |
return 'zones/eca56d73-3ae9-4264-b8ba-eed1cc90cbb7 61G 3.7G 57G 7% / | |
/lib 269M 256M 13M 96% /lib | |
/lib/svc/manifest 3.1T 504K 3.1T 1% /lib/svc/manifest | |
/lib/svc/manifest/site 61G 3.7G 57G 7% /lib/svc/manifest/site | |
/sbin 269M 256M 13M 96% /sbin | |
/usr 377M 352M 26M 94% /usr | |
/usr/ccs 61G 3.7G 57G 7% /usr/ccs | |
/usr/local 61G 3.7G 57G 7% /usr/local | |
/usr/lib/libc/libc_hwcap1.so.1 377M 352M 26M 94% /lib/libc.so.1' | |
else | |
return %x[df -h | sed 1d | awk '{print $1, $5}' | sed 's/%//'] | |
end | |
end | |
def parse_stats_to_hash(stats) | |
stats.split("\n").inject({}) do |hash, line| | |
key, value = line.split(' ') | |
hash[key.strip.to_sym] = value.to_i | |
hash | |
end | |
end | |
end | |
class SomethingElse < MyPlugin | |
calculates :memory | |
calculates :io | |
def calculate_io | |
{:whatver => "here"} | |
end | |
def calculate_memory | |
{:stats_for_memory => 12, :other => "test"} | |
end | |
end | |
def disk(debug=false) | |
DiskPlugin.new(debug).report | |
end | |
def other(debug=false) | |
SomethingElse.new(debug).report | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment