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.jmx;
17  
18  import it.jnrpe.ICommandLine;
19  import it.jnrpe.ReturnValue;
20  import it.jnrpe.Status;
21  import it.jnrpe.events.LogEvent;
22  import it.jnrpe.plugins.IPluginInterface;
23  
24  import java.io.ByteArrayOutputStream;
25  import java.io.IOException;
26  import java.io.PrintStream;
27  
28  /**
29   * The check JMX plugin.
30   *
31   * @author Massimiliano Ziccardi
32   */
33  public class CCheckJMX extends JMXQuery implements IPluginInterface {
34  
35      /**
36       * Executes the plugin.
37       *
38       * @param cl
39       *            The command line
40       * @return the result of the execution
41       */
42      public final ReturnValue execute(final ICommandLine cl) {
43          if (cl.hasOption('U')) {
44              setUrl(cl.getOptionValue('U'));
45          }
46          if (cl.hasOption('O')) {
47              setObject(cl.getOptionValue('O'));
48          }
49          if (cl.hasOption('A')) {
50              setAttribute(cl.getOptionValue('A'));
51          }
52          if (cl.hasOption('I')) {
53              setInfo_attribute(cl.getOptionValue('I'));
54          }
55          if (cl.hasOption('J')) {
56              setInfo_key(cl.getOptionValue('J'));
57          }
58          if (cl.hasOption('K')) {
59              setAttribute_key(cl.getOptionValue('K'));
60          }
61          if (cl.hasOption('w')) {
62              setWarning(cl.getOptionValue('w'));
63          }
64          if (cl.hasOption('c')) {
65              setCritical(cl.getOptionValue('c'));
66          }
67          if (cl.hasOption("username")) {
68              setUsername(cl.getOptionValue("username"));
69          }
70          if (cl.hasOption("password")) {
71              setPassword(cl.getOptionValue("password"));
72          }
73  
74          setVerbatim(2);
75          // setVerbatim(4);
76  
77          try {
78              connect();
79              execute();
80              ByteArrayOutputStream bout = new ByteArrayOutputStream();
81              PrintStream ps = new PrintStream(bout);
82              Status status = report(ps);
83              ps.flush();
84              ps.close();
85              return new ReturnValue(status, new String(bout.toByteArray()));
86          } catch (Exception ex) {
87              log.warn("An error has occurred during execution "
88                              + "of the CHECK_JMX plugin : "
89                              + ex.getMessage(), ex);
90              ByteArrayOutputStream bout = new ByteArrayOutputStream();
91              PrintStream ps = new PrintStream(bout);
92              Status status = report(ex, ps);
93              ps.flush();
94              ps.close();
95              return new ReturnValue(status, new String(bout.toByteArray()));
96          } finally {
97              try {
98                  disconnect();
99              } catch (IOException e) {
100                 log.warn("An error has occurred during execution"
101                           + " of the CHECK_JMX plugin : "
102                           + e.getMessage(), e);
103                 ByteArrayOutputStream bout = new ByteArrayOutputStream();
104                 PrintStream ps = new PrintStream(bout);
105 
106                 Status status = report(e, ps);
107                 ps.flush();
108                 ps.close();
109                 return new ReturnValue(status, new String(bout.toByteArray()));
110             }
111         }
112     }
113 
114     @Override
115     protected String getPluginName() {
116         return "CHECK_JMX";
117     }
118 }