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;
17  
18  import it.jnrpe.ICommandLine;
19  import it.jnrpe.ReturnValue;
20  import it.jnrpe.Status;
21  import it.jnrpe.plugins.PluginBase;
22  import it.jnrpe.utils.StreamManager;
23  import it.jnrpe.utils.StringUtils;
24  
25  import java.io.BufferedReader;
26  import java.io.File;
27  import java.io.InputStreamReader;
28  
29  /**
30   * Executes external (executable) plugins.
31   *
32   * @author Massimiliano Ziccardi
33   */
34  public class CNativePlugin extends PluginBase {
35  
36      /**
37       * The first parameter must be the full
38       * path to the executable.
39       *
40       * The rest of the array is sent to the executable as commands parameters
41       * @param cl The parsed command line
42       * @return The return value of the plugin
43       */
44      public final ReturnValue execute(final ICommandLine cl) {
45          File fProcessFile = new File(cl.getOptionValue("executable"));
46          StreamManager streamMgr = new StreamManager();
47  
48          if (!fProcessFile.exists()) {
49              return new ReturnValue(Status.UNKNOWN,
50                      "Could not exec executable : "
51                              + fProcessFile.getAbsolutePath());
52          }
53  
54          try {
55              String[] vsParams =
56                      StringUtils.split(cl.getOptionValue("args", ""), false);
57              String[] vCommand = new String[vsParams.length + 1];
58              vCommand[0] = cl.getOptionValue("executable");
59              System.arraycopy(vsParams, 0, vCommand, 1, vsParams.length);
60              Process p = Runtime.getRuntime().exec(vCommand);
61              BufferedReader br =
62                      (BufferedReader) streamMgr.handle(new BufferedReader(
63                              new InputStreamReader(p.getInputStream())));
64              String sMessage = br.readLine();
65              int iReturnCode = p.waitFor();
66  
67              return new ReturnValue(Status.fromIntValue(iReturnCode), sMessage);
68          } catch (Exception e) {
69              log.warn("Error executing the native plugin : "
70                      + e.getMessage(), e);
71              return new ReturnValue(Status.UNKNOWN,
72                      "Could not exec executable : " + fProcessFile.getName()
73                              + " - ERROR : " + e.getMessage());
74          } finally {
75              streamMgr.closeAll();
76          }
77      }
78  
79      @Override
80      protected String getPluginName() {
81          return "NATIVE";
82      }
83  }