5 [DDL]: /mcollective/reference/plugins/ddl.html
8 Up to MCollective 2.0 the discovery system could only discover against
9 installed agents, configuration management classes or facts and the node
10 identities. We're extending this to support discovery against many
11 sources through a simple plugin system.
13 *NOTE:* This feature is available since version 2.1.0
15 The basic idea is that you could do discovery statements like the ones
18 {% highlight console %}
19 % mco find -S "fstat('/etc/rsyslog.conf').md5=/4edff591f6e38/"
22 {% highlight console %}
23 % mco find -S "sysctl('net.ipv4.conf.all.forwarding').value=1"
26 {% highlight console %}
27 % mco find -S "sysctl('net.ipv4.conf.all.forwarding').value=1 and % location=dc1"
30 You could also use these data sources in your own agents or other
35 reply[:value] = Data.sysctl(request[:sysctl_name]).value
39 These new data sources are plugins so you can provide via the plugin
40 system and they require DDL documents. The DDL will be used on both the
41 client and the server to provide strict validation and configuration.
43 The DDL for these plugins will affect the client libraries in the
46 * You will get errors if you try to discover using unknown functions
47 * Your input argument values will be validated against the DDL
48 * You will only be able to use output properties that are known in the DDL
49 * If a plugin DDL says it needs 5 seconds to run your discovery and maximum run times will increase by 5 seconds automatically
51 On the servers the DDL will:
53 * be used to validate known plugins
54 * be used to validate input arguments
55 * be used to validate requests for known output values
57 ## Viewing or retrieving results from a data plugin
59 You can view the output from a data plugin using the *rpcutil* agent:
61 {% highlight console %}
62 % mco rpc rpcutil get_data source=fstat query=/etc/hosts
66 atime: 2012-06-14 21:41:54
68 atime_seconds: 1339706514
69 ctime: 2012-01-18 20:28:34
71 ctime_seconds: 1326918514
73 md5: 54fb6627dbaa37721048e4549db3224d
75 mtime: 2010-01-12 13:28:22
77 mtime_seconds: 1263302902
86 The same action can be used to retrieve data programatically.
88 ## Writing a data plugin
90 ### The Ruby logic for the plugin
91 The data plugins should not change the system in anyway, you should take
92 care to create plugins that only reads the state of the system. If you
93 want to affect the status of the system you should write Agents.
95 These plugins are kept simple as they will be typed on the command line
96 so the following restrictions are present:
98 * They can only take 1 input argument
99 * They can only return simple String, Numeric or Booleans no Hashes or complex data types
100 * They should be fast as these will impact discovery times and agent run times.
102 Writing data plugins is easy and mimic the basics of writing agents,
103 below we have a simple *sysctl* plugin that was used in the examples
106 {% highlight ruby linenos %}
109 class Sysctl_data<Base
110 activate_when { File.executable?("/sbin/sysctl") && Facter["kernel"] == "Linux" }
113 shell = Shell.new("/sbin/sysctl %s" % sysctl)
116 if shell.status.exitstatus == 0
117 value = shell.stdout.chomp.split(/\s*=\s*/)[1]
120 value = Integer(value) if value =~ /^\d+$/
121 value = Float(value) if value =~ /^\d+\.\d+$/
124 result[:value] = value
132 The class names have to be *Something_data* and they must inherit from
133 *Base* as in the example here. The file would be saved in the *libdir*
134 as *data/sysctl_data.rb* and *data/sysctl_data.ddl*.
136 This plugin will only be activated if the file */sbin/sysctl* exist, is
137 executable and if the system is a Linux server. This allow us to install
138 it on a Windows machine where it will just be disabled and those
139 machines will never be discovered using this function.
141 We then create a block that would be the main body of the query. We use
142 the *MCollective::Shell* class to run sysctl, parse the output and save
143 it into the *result* hash.
145 The result hash is the only way to return values from these plugins. You
146 can only save simple strings, numbers or booleans in the result.
148 ### The DDL for the plugin
149 As mentioned every data plugin requires a DDL. These DDL files mimic
150 those of the [SimpleRPC Agents][DDL].
152 Below you'll find a DDL for the above sysctl data plugin:
154 {% highlight ruby linenos %}
155 metadata :name => "Sysctl values",
156 :description => "Retrieve values for a given sysctl",
157 :author => "R.I.Pienaar <rip@devco.net>",
158 :license => "ASL 2.0",
160 :url => "http://marionette-collective.org/",
163 dataquery :description => "Sysctl values" do
165 :prompt => "Variable Name",
166 :description => "Valid Variable Name",
168 :validation => /^[\w\-\.]+$/,
172 :description => "Kernel Parameter Value",
173 :display_as => "Value"
177 The *timeout* must be set correctly, if your data source is slow you
178 need to reflect that in the timeout here. The timeout will be used on
179 the clients to decide how long to wait for discovery responses from the
180 network so getting this wrong will result in nodes not being discovered.
182 Each data plugin can only have one *dataquery* block with exactly 1
183 *input* block but could have multiple *output* blocks.
185 It's important to get the validation correct, here we only accept the
186 characters we know are legal in sysctl variables on Linux. We will
187 specifically never allow backticks to be used in arguments to avoid
188 accidental shell exploits.
190 Note the correlation between output names and the use in discovery and
191 agents here we create an output called *value* this means we would use
194 {% highlight console %}
195 % mco find -S "sysctl('net.ipv4.conf.all.forwarding').value=1"
198 And we would output the result from our plugin code as:
200 {% highlight ruby linenos %}
201 result[:value] = value
204 And in any agent where we might use the data source:
206 {% highlight ruby linenos %}
207 something = Data.sysctl('net.ipv4.conf.all.forwarding').value
210 These have to match everywhere, you cannot reference undeclared data and
211 you cannot use input that does not validate against the DDL declared
214 Refer to the full [DDL] documentation for details on all possible values
215 of the *metadata*, *input* and *output* blocks.
217 ## Auto generated documentation
218 As with agents the DDL can be used to generate documentation, if you
219 wanted to know what the input and output values are for a specific
220 plugin you can use *mco plugin doc* to see generated documentation.
222 {% highlight console %}
223 % mco plugin doc sysctl
227 Retrieve values for a given sysctl
229 Author: R.I.Pienaar <rip@devco.net>
233 Home Page: http://marionette-collective.org/
235 QUERY FUNCTION INPUT:
237 Description: Valid Variable Name
238 Prompt: Variable Name
240 Validation: (?-mix:^[\w\-\.]+$)
243 QUERY FUNCTION OUTPUT:
246 Description: Kernel Parameter Value
251 ## Available plugins for a node You can use the *mco inventory*
252 application to see remotely what plugins a node has available:
254 {% highlight console %}
255 % mco inventory your.node
256 Inventory for your.node: