1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
28
29
30
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
55
56
57
58
59
60
61
62
63
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 }