1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
31
32
33
34 public class CNativePlugin extends PluginBase {
35
36
37
38
39
40
41
42
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 }