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  import java.util.Date;
21  
22  import org.apache.commons.lang.Validate;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import ch.syabru.nagios.broker.ExternalCommand;
27  
28  /**
29   * Schedule service check external command.
30   *
31   * @author Felix Roethenbacher
32   *
33   */
34  public class ScheduleServiceCheckCommand implements ExternalCommand {
35      private static final Logger logger = LoggerFactory.getLogger(
36              ScheduleServiceCheckCommand.class);
37  
38      private static final String COMMAND = "SCHEDULE_SVC_CHECK";
39  
40      private String hostName;
41      private String serviceDescription;
42      private Date checkTime;
43  
44      /**
45       * C'tor.
46       * @param hostName Host name.
47       * @param serviceDescription Service description.
48       * @param checkTime Check time.
49       */
50      public ScheduleServiceCheckCommand(String hostName,
51              String serviceDescription, Date checkTime)
52      {
53          Validate.notNull(hostName, "hostName must not be null");
54          Validate.notNull(serviceDescription,
55                  "serviceDescription must not be null");
56          Validate.notNull(checkTime, "checkTime must not be null");
57          this.hostName = hostName;
58          this.serviceDescription = serviceDescription;
59          this.checkTime = checkTime;
60      }
61  
62      @Override
63      public void writeCommand(Writer writer) throws IOException
64      {
65          StringBuilder sb = new StringBuilder();
66          sb.append("[");
67          sb.append(Long.toString(System.currentTimeMillis() / 1000));
68          sb.append("] ");
69          sb.append(COMMAND);
70          sb.append(";");
71          sb.append(hostName);
72          sb.append(";");
73          sb.append(serviceDescription);
74          sb.append(";");
75          sb.append(Long.toString(checkTime.getTime() / 1000));
76          sb.append("\n");
77          writer.write(sb.toString());
78          writer.flush();
79          logger.debug(sb.toString());
80      }
81  
82      public String getHostName() {
83          return hostName;
84      }
85  
86      public String getServiceDescription() {
87          return serviceDescription;
88      }
89  
90      public Date getCheckTime() {
91          return checkTime;
92      }
93  }