View Javadoc

1   /*
2    *  Copyright 2010 Felix Roethenbacher
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 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   * Process service check result external command.
29   *
30   * @author Felix Roethenbacher
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       * C'tor.
46       * @param hostName Host name.
47       * @param serviceDescription Service description.
48       * @param returnCode Return code.
49       * @param pluginOutput Plugin output.
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 }