forked from choria-legacy/marionette-collective
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mc-irb
executable file
·158 lines (129 loc) · 3.96 KB
/
mc-irb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env ruby
# Simple IRB shell for mcollective
#
# mc-irb nrpe
# Determining the amount of hosts matching filter for 2 seconds .... 47
# >> rpc :runcommand, :command => "check_disks"
#
# * [ ============================================================> ] 47 / 47
#
#
# dev1.your.net Request Aborted
# CRITICAL
# Output: DISK CRITICAL - free space: / 176 MB (4% inode=86%);
# Exit Code: 2
# Performance Data: /=3959MB;3706;3924;0;4361 /boot=26MB;83;88;0;98 /dev/shm=0MB;217;230;0;256
#
# => true
# >> mchelp
# <shows the DDL based help for the chosen agent>
# => true
# >> rpc(:runcommand, :command => "check_disks") do |resp|
# ?> puts resp[:sender] + ": " + resp[:data][:output]
# >> end
#
# * [ ============================================================> ] 47 / 47
#
# dev1.your.net: DISK OK
# <snip>
# => true
# >>
#
# You can access the agent variable via @agent from where you can do the usual manipulation of filters etc,
# if you wish to switch to a different agent mid run just do newagent("some_other_agent")
#
# If you install the Bond gem you'll get some DDL assisted completion in the rpc method
require 'rubygems'
require 'irb'
def consolize &block
yield
IRB.setup(nil)
irb = IRB::Irb.new
IRB.conf[:MAIN_CONTEXT] = irb.context
irb.context.evaluate("require 'irb/completion'", 0)
begin
require 'bond'
Bond.start
Bond.complete(:method => "rpc") do |e|
begin
if e.argument == 1
if e.arguments.last == "?"
puts "\n\nActions for #{@agent_name}:\n"
@agent.ddl.actions.each do |action|
puts "%20s - %s" % [ ":#{action}", @agent.ddl.action_interface(action)[:description] ]
end
print "\n" + e.line
end
@agent.ddl.actions
elsif e.argument > 1
action = eval(e.arguments[0]).to_s
ddl = @agent.ddl.action_interface(action)
if e.arguments.last == "?"
puts "\n\nArguments for #{action}:\n"
ddl[:input].keys.each do |input|
puts "%20s - %s" % [ ":#{input}", ddl[:input][input][:description] ]
end
print "\n" + e.line
end
ddl[:input].keys
end
rescue Exception
[]
end
end
rescue Exception
end
trap("SIGINT") do
irb.signal_handle
end
catch(:IRB_EXIT) do
irb.eval_input
end
end
def mchelp
system("mc-rpc --agent-help #{@agent_name}|less")
true
end
def rpc(method_name, *args, &block)
unless block_given?
if args.size > 0
args = args.first
else
args = {}
end
if args[:verbose]
args.delete(:verbose)
printrpc(@agent.send(method_name, args), :verbose => true)
printrpcstats
else
printrpc @agent.send(method_name, args)
printrpcstats
end
else
@agent.send(method_name, args.first).each do |resp|
yield resp
end
printrpcstats
end
true
rescue MCollective::DDLValidationError => e
puts "Request did not pass DDL validation: #{e}"
end
def newagent(agent)
@agent_name = agent
@options[:filter]["agent"] = []
@agent = rpcclient(@agent_name, :options => @options)
@agent.discover(:verbose => true)
@agent.progress = true
true
end
consolize do
require 'mcollective'
include MCollective::RPC
@options = rpcoptions{|p,o| }
unless ARGV.size == 1
puts "Please specify an agent name on the command line"
exit 1
end
newagent(ARGV[0])
end