1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package ch.syabru.nagios.broker.impl;
17
18 import java.io.IOException;
19 import java.io.Writer;
20
21 import org.apache.commons.lang.Validate;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import ch.syabru.nagios.broker.ExternalCommand;
26
27
28
29
30
31
32
33 public class ProcessServiceCheckResultCommand implements ExternalCommand {
34 private static final Logger logger = LoggerFactory.getLogger(
35 ProcessServiceCheckResultCommand.class);
36
37 private static final String COMMAND = "PROCESS_SERVICE_CHECK_RESULT";
38
39 private String hostName;
40 private String serviceDescription;
41 private int returnCode;
42 private String pluginOutput;
43
44
45
46
47
48
49
50
51 public ProcessServiceCheckResultCommand(String hostName,
52 String serviceDescription, int returnCode, String pluginOutput)
53 {
54 Validate.notNull(hostName, "hostName must not be null");
55 Validate.notNull(serviceDescription,
56 "serviceDescription must not be null");
57 Validate.isTrue(
58 returnCode == ExternalCommand.STATE_OK ||
59 returnCode == ExternalCommand.STATE_WARNING ||
60 returnCode == ExternalCommand.STATE_CRITICAL ||
61 returnCode == ExternalCommand.STATE_UNKNOWN);
62 Validate.notNull(pluginOutput, "pluginOutput must not be null");
63 this.hostName = hostName;
64 this.serviceDescription = serviceDescription;
65 this.returnCode = returnCode;
66 this.pluginOutput = pluginOutput;
67 }
68
69 @Override
70 public void writeCommand(Writer writer) throws IOException
71 {
72 StringBuilder sb = new StringBuilder();
73 sb.append("[");
74 sb.append(Long.toString(System.currentTimeMillis() / 1000));
75 sb.append("] ");
76 sb.append(COMMAND);
77 sb.append(";");
78 sb.append(hostName);
79 sb.append(";");
80 sb.append(serviceDescription);
81 sb.append(";");
82 sb.append(Integer.toString(returnCode));
83 sb.append(";");
84 sb.append(pluginOutput);
85 sb.append("\n");
86 writer.write(sb.toString());
87 writer.flush();
88 logger.debug(sb.toString());
89 }
90
91 public String getHostName() {
92 return hostName;
93 }
94
95 public String getServiceDescription() {
96 return serviceDescription;
97 }
98
99 public int getReturnCode() {
100 return returnCode;
101 }
102
103 public String getPluginOutput() {
104 return pluginOutput;
105 }
106 }