View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2007, 2014 Massimiliano Ziccardi
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *******************************************************************************/
16  package it.jnrpe.plugin.test;
17  
18  import it.jnrpe.ICommandLine;
19  import it.jnrpe.ReturnValue;
20  import it.jnrpe.Status;
21  import it.jnrpe.plugins.IPluginInterface;
22  import it.jnrpe.plugins.annotations.Option;
23  import it.jnrpe.plugins.annotations.Plugin;
24  import it.jnrpe.plugins.annotations.PluginOptions;
25  
26  /**
27   * A simple test plugin that returns the status as specified by the 'status'
28   * parameter and as text the value of the 'text' parameter.
29   * 
30   * @author Massimiliano Ziccardi
31   * 
32   */
33  @Plugin(name = "TEST", description = "This is just a test plugin. Invoke it passing the String you want to get back and,\n"
34  		+ "optionally, the status you want.\n"
35  		+ "Return:\n"
36  		+ "If you don't specify the status parameters, the return code is 'OK'.\n"
37  		+ "The returned message is always the text you pass as parameter (-t/--text)\n\n"
38  		+ "Example Command Definition (inside the server configuration, in the command section)\n\n"
39  		+ "check_test : TEST --text $ARG1$ --status $ARG2$\n\n"
40  		+ "or, using the XML:\n\n"
41  		+ "<command name=\"test\" plugin_name=\"TEST\">\n"
42  		+ "   <arg name=\"text\" value=\"$ARG1$\"/>\n"
43  		+ "   <arg name=\"status\" value=\"$ARG2$\"/>\n"
44  		+ "</command>\n\n"
45  		+ "Example invocation:\n\n"
46  		+ "./check_nrpe -n -H myjnrpeserver -c check_test -a 'Hello!critical'")
47  @PluginOptions({
48  		@Option(shortName = "t", longName = "text", description = "the message to print", required = true, hasArgs = true, argName = "text", optionalArgs = false, option = "text"),
49  		@Option(shortName = "s", longName = "status", description = "the status to return (ok, warning, critical, unknown) - defaults to \"OK\"", required = false, hasArgs = true, argName = "statcode", optionalArgs = false, option = "status"),
50  		@Option(shortName = "d", longName = "delay", description = "The number of seconds to wait before returnin (default is 0)", required = false, hasArgs = true, argName = "seconds", optionalArgs = false, option = "delay") })
51  public class CTestPlugin implements IPluginInterface {
52  
53  	/**
54  	 * Executes the plugin. The accepter params are:
55  	 * <UL>
56  	 * <LI>--text : the text to be returned
57  	 * <LI>--status : the status to be returned (ok, warning, critical. Any
58  	 * other status is interpreted as UNKNOWN). The default value is 'ok'.
59  	 * </UL>
60  	 * 
61  	 * @param cl
62  	 *            The command line
63  	 * @return The return value
64  	 */
65  	public final ReturnValue execute(final ICommandLine cl) {
66  		Status returnStatus;
67  
68  		String statusParam = cl.getOptionValue("status", "ok");
69  
70  		if (statusParam.equalsIgnoreCase("ok")) {
71  			returnStatus = Status.OK;
72  		} else if (statusParam.equalsIgnoreCase("critical")) {
73  			returnStatus = Status.CRITICAL;
74  		} else if (statusParam.equalsIgnoreCase("warning")) {
75  			returnStatus = Status.WARNING;
76  		} else {
77  			returnStatus = Status.UNKNOWN;
78  		}
79  
80  		if (cl.hasOption("delay")
81  				&& Integer.parseInt(cl.getOptionValue("delay")) > 0) {
82  			try {
83  				Thread.sleep(Integer.parseInt(cl.getOptionValue("delay")) * 1000);
84  			} catch (Exception e) {
85  			}
86  		}
87  
88  		return new ReturnValue(returnStatus, "TEST : "
89  				+ cl.getOptionValue("text"));
90  	}
91  }