dock_driver 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Ideas.txt DELETED
@@ -1,19 +0,0 @@
1
- = Ideas
2
-
3
- == Without Dependencies
4
-
5
- * Graphing
6
- * Automatic Coloring (with definable syntax)
7
-
8
- == With Dependencies
9
-
10
- * Maildir support
11
- * SNMP Poller
12
- * gateway / internet latency
13
-
14
- == dzen2 Ideas
15
-
16
- * buttons
17
- * events
18
- * text window
19
-
@@ -1,136 +0,0 @@
1
- # -*- ruby -*-
2
- # vim: set nosta noet ts=4 sw=4:
3
- # encoding: utf-8
4
-
5
- require 'ostruct'
6
-
7
- require 'dock_driver' unless defined? DockDriver
8
- require 'dock_driver/poll'
9
-
10
- # A class to provide periodic command execution, caching, and a
11
- # running average of changes in the output of the command.
12
- #
13
- class DockDriver::DataRatePoll < DockDriver::Poll
14
-
15
- # Default values to populate the OpenStruct with.
16
- #
17
- DEFAULTS = {
18
- :unit => :mb,
19
- :history_size => 2,
20
- :format => '%0.3f',
21
- }.freeze
22
-
23
- # Create a new DataRatePoll.
24
- #
25
- # === Arguments:
26
- #
27
- # +opts+ - A hash containing options. Merged with DEFAULTS.
28
- #
29
- # +block+ - If a block is given, replace :cmd with that block.
30
- #
31
- # === Options:
32
- #
33
- # Everything accepted by from Poll, plus:
34
- #
35
- # +:history_size+ - How many samples to keep
36
- #
37
- # +:unit+ - The unit to use. See the methods and aliases available.
38
- # (b, kb, mb, gb, tb, pb, eb, zb, yb, and full names: bits, kibibits, mebibits, ...)
39
- #
40
- def initialize( opts = {} )
41
- super DEFAULTS.merge( opts )
42
-
43
- @last_sample = nil
44
- @history = Array.new( self.history_size ) { 0 }
45
- end
46
-
47
- # Runs the command if enough time has elapsed since the last
48
- # poll. "Enough Time" is defined by the :delay option. Maintains the
49
- # history of output, average and formatting.
50
- #
51
- def poll
52
- return false unless super
53
-
54
- @sample = @sample.to_f
55
- @last_sample ||= @sample
56
- sample = 8 * ( @sample - @last_sample )
57
- @history.shift if @history.length >= self.history_size
58
- @history.push sample
59
- @last_sample = @sample
60
-
61
- true
62
- end
63
-
64
- # Returns the average value in bits.
65
- #
66
- def bits
67
- @history.inject( :+ ) / @history.count.to_f
68
- end
69
- alias :b :bits
70
-
71
- # Returns the average value in kibibits.
72
- #
73
- def kibibits
74
- bits / 1024.0
75
- end
76
- alias :kb :kibibits
77
-
78
- # Returns the average value in mebibits.
79
- #
80
- def mebibits
81
- kibibits / 1024.0
82
- end
83
- alias :mb :mebibits
84
-
85
- # Returns the average value in gibibits.
86
- #
87
- def gibibits
88
- mebibits / 1024.0
89
- end
90
- alias :gb :gibibits
91
-
92
- # Returns the average value in tebibits.
93
- #
94
- def tebibits
95
- gibibits / 1024.0
96
- end
97
- alias :tb :tebibits
98
-
99
- # Returns the average value in pebibits.
100
- #
101
- def pebibits
102
- tebibits / 1024.0
103
- end
104
- alias :pb :pebibits
105
-
106
- # Returns the average value in exbibits.
107
- #
108
- def exbibits
109
- pebibits / 1024.0
110
- end
111
- alias :eb :exbibits
112
-
113
- # Returns the average value in zebibits.
114
- #
115
- def zebibits
116
- exbibits / 1024.0
117
- end
118
- alias :zb :zebibits
119
-
120
- # Returns the average value in yobibits.
121
- #
122
- def yobibits
123
- zebibits / 1024.0
124
- end
125
- alias :yb :yobibits
126
-
127
- # Returns a string representing the data rate this poll tracks.
128
- #
129
- def to_s
130
- value = self.unit ? send( self.unit ) : bits
131
- return value.to_s if self.format.nil?
132
- self.format % value
133
- end
134
-
135
- end
136
-
@@ -1,79 +0,0 @@
1
- # -*- ruby -*-
2
- # vim: set nosta noet ts=4 sw=4:
3
- # encoding: utf-8
4
- #
5
- # Copyright (c) 2007-2012, Michael Granger and Mahlon E. Smith
6
- # All rights reserved.
7
- #
8
-
9
- ### A collection of utilities for working with Hashes.
10
- #
11
- module HashUtilities
12
-
13
- ###############
14
- module_function
15
- ###############
16
-
17
- ### Return a version of the given +hash+ with its keys transformed
18
- ### into Strings from whatever they were before.
19
- def stringify_keys( hash )
20
- newhash = {}
21
-
22
- hash.each do |key,val|
23
- if val.is_a?( Hash )
24
- newhash[ key.to_s ] = stringify_keys( val )
25
- else
26
- newhash[ key.to_s ] = val
27
- end
28
- end
29
-
30
- return newhash
31
- end
32
-
33
-
34
- ### Return a duplicate of the given +hash+ with its identifier-like keys
35
- ### transformed into symbols from whatever they were before.
36
- def symbolify_keys( hash )
37
- newhash = {}
38
-
39
- hash.each do |key,val|
40
- keysym = key.to_s.dup.untaint.to_sym
41
-
42
- if val.is_a?( Hash )
43
- newhash[ keysym ] = symbolify_keys( val )
44
- else
45
- newhash[ keysym ] = val
46
- end
47
- end
48
-
49
- return newhash
50
- end
51
- alias_method :internify_keys, :symbolify_keys
52
-
53
-
54
- # Recursive hash-merge function
55
- def merge_recursively( key, oldval, newval )
56
- case oldval
57
- when Hash
58
- case newval
59
- when Hash
60
- oldval.merge( newval, &method(:merge_recursively) )
61
- else
62
- newval
63
- end
64
-
65
- when Array
66
- case newval
67
- when Array
68
- oldval | newval
69
- else
70
- newval
71
- end
72
-
73
- else
74
- newval
75
- end
76
- end
77
-
78
- end # HashUtilities
79
-
@@ -1,90 +0,0 @@
1
- # -*- ruby -*-
2
- # vim: set nosta noet ts=4 sw=4:
3
- # encoding: utf-8
4
- #
5
-
6
- require 'ostruct'
7
-
8
- require 'dock_driver' unless defined? DockDriver
9
-
10
- # A class to provide periodic command execution and caching.
11
- #
12
- class DockDriver::Poll < OpenStruct
13
-
14
- # Default values to populate the OpenStruct with.
15
- #
16
- DEFAULTS = {
17
- :delay => 1,
18
- }.freeze
19
-
20
- # Create a new Poll.
21
- #
22
- # === Arguments:
23
- #
24
- # +opts+ - A hash containing options. Merged with DEFAULTS.
25
- #
26
- # +block+ - If a block is given, replace :cmd with that block.
27
- #
28
- # === Options:
29
- #
30
- # +:delay+ - Determines the minimum amount of time to let elapse between executions.
31
- #
32
- # +:cmd+ - A string or block to execute.
33
- #
34
- # +:scan_regex+ - A bare string to be interpreted as a regex for use with +scan+. Any
35
- # capture group matches will be joined with single spaces.
36
- #
37
- # +:format+ - A format string. See: Kernel#format
38
- #
39
- def initialize( opts = {}, &block )
40
- super DEFAULTS.merge( opts )
41
-
42
- @sample = nil
43
-
44
- if block_given?
45
- self.cmd = block
46
- end
47
- end
48
-
49
- # Runs the command if enough time has elapsed since the last
50
- # poll. "Enough Time" is defined by the :delay option. If the command
51
- # fails to run, it is disabled and the format and output string are set
52
- # to nil.
53
- #
54
- def poll
55
- return false if @sample and (Time.now - (self.last_poll || 0 )) < self.delay
56
-
57
- self.last_poll = Time.now
58
-
59
- case self.cmd
60
- when Proc
61
- @sample = self.cmd.call.to_s.chomp
62
- else
63
- output = `#{self.cmd.to_s} 2>&1`.chomp
64
- if $? == 0
65
- @sample = output
66
- else
67
- self.format = nil
68
- @sample = '[%s: Error!] ' % self.name
69
- raise output
70
- end
71
- end
72
-
73
- if self.scan_regex
74
- extract = @sample.scan( Regexp.new( self.scan_regex ) ).flatten
75
- @sample = extract.join( ' ' ) unless extract.empty?
76
- end
77
-
78
- true
79
- end
80
-
81
- # Returns the output of the command this Poll runs. It will return a formatted string if
82
- # the +:format+ option is correctly specified.
83
- #
84
- def to_s
85
- return @sample.to_s if self.format.nil?
86
- self.format % @sample
87
- end
88
-
89
- end
90
-
@@ -1,36 +0,0 @@
1
-
2
- require 'pathname'
3
- require 'fileutils'
4
- require 'dock_driver/dock'
5
-
6
- describe DockDriver::Dock do
7
-
8
- subject do
9
- @conf = Pathname( __FILE__ ).parent.parent.parent
10
- @conf += 'data/example.dock_driver.yml'
11
-
12
- # Use very common binaries that are all but guaranteed to be in the
13
- # user's path so that we can maintain a meaningful example config file.
14
- #
15
- dock = Dock.new( @conf.to_s, nil, 'cat' )
16
- dock.polls.values.each { |p| p.cmd = 'echo test' }
17
- dock
18
- end
19
-
20
- it 'should load a config file' do
21
- subject.dock_command.should_not be nil
22
- subject.template.should_not be nil
23
- subject.polls.should have_at_least( 2 ).things
24
- subject.polls.values.each { |p| p.should be_a_kind_of Poll }
25
- end
26
-
27
- it 'should only reload a config file with a more recent mtime' do
28
- subject.send( :load_config ).should be false
29
- sleep 0.1
30
- FileUtils.touch @conf
31
- subject.send( :load_config ).should be true
32
- subject.send( :load_config ).should be false
33
- end
34
-
35
- end
36
-
@@ -1,45 +0,0 @@
1
-
2
- require 'dock_driver/poll'
3
- require 'dock_driver/data_rate_poll'
4
-
5
- include DockDriver
6
-
7
- shared_examples 'a poll' do
8
-
9
- subject do
10
- described_class.new( :cmd => 'echo test' )
11
- end
12
-
13
- it 'should execute shell commands' do
14
- lambda do
15
- subject.poll.should == true
16
- end.should_not raise_error
17
- end
18
-
19
- it 'should execute blocks' do
20
- subject.cmd = lambda {}
21
- lambda do
22
- subject.poll.should == true
23
- end.should_not raise_error
24
- end
25
-
26
- it 'should cache command output' do
27
- subject.delay = 10
28
- subject.poll.should == true
29
- subject.poll.should == false
30
-
31
- subject.delay = 0
32
- subject.poll.should == true
33
- subject.poll.should == true
34
- end
35
-
36
- end
37
-
38
- describe Poll do
39
- it_behaves_like 'a poll'
40
- end
41
-
42
- describe DataRatePoll do
43
- it_behaves_like 'a poll'
44
- end
45
-
@@ -1,17 +0,0 @@
1
-
2
- require 'dock_driver'
3
-
4
- describe DockDriver do
5
-
6
- it 'should define a standard version number' do
7
- defined?( VERSION ).should == 'constant'
8
- VERSION.should =~ /^\d+\.\d+\.\d+$/
9
- end
10
-
11
- it 'should define a mercurial revision' do
12
- defined?( REVISION ).should == 'constant'
13
- REVISION.should =~ /^Revision: [[:xdigit:]]+ $/
14
- end
15
-
16
- end
17
-