001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.component.nagios;
018
019 import java.util.concurrent.ExecutorService;
020
021 import com.googlecode.jsendnsca.core.INagiosPassiveCheckSender;
022 import com.googlecode.jsendnsca.core.Level;
023 import com.googlecode.jsendnsca.core.MessagePayload;
024 import com.googlecode.jsendnsca.core.NonBlockingNagiosPassiveCheckSender;
025 import org.apache.camel.Exchange;
026 import org.apache.camel.impl.DefaultProducer;
027
028 import static org.apache.camel.component.nagios.NagiosConstants.HOST_NAME;
029 import static org.apache.camel.component.nagios.NagiosConstants.LEVEL;
030 import static org.apache.camel.component.nagios.NagiosConstants.SERVICE_NAME;
031
032 /**
033 * @version $Revision: 18295 $
034 */
035 public class NagiosProducer extends DefaultProducer {
036
037 private final INagiosPassiveCheckSender sender;
038
039 public NagiosProducer(NagiosEndpoint endpoint, INagiosPassiveCheckSender sender) {
040 super(endpoint);
041 this.sender = sender;
042 }
043
044 public void process(Exchange exchange) throws Exception {
045 String message = exchange.getIn().getBody(String.class);
046
047 Level level = exchange.getIn().getHeader(LEVEL, Level.class);
048 if (level == null) {
049 String name = exchange.getIn().getHeader(LEVEL, Level.OK.name(), String.class);
050 level = Level.valueOf(name);
051 }
052 String serviceName = exchange.getIn().getHeader(SERVICE_NAME, exchange.getContext().getName(), String.class);
053 String hostName = exchange.getIn().getHeader(HOST_NAME, "localhost", String.class);
054
055 MessagePayload payload = new MessagePayload(hostName, level.ordinal(), serviceName, message);
056
057 if (log.isDebugEnabled()) {
058 log.debug("Sending notification to Nagios: " + payload.getMessage());
059 }
060 sender.send(payload);
061 if (log.isTraceEnabled()) {
062 log.trace("Sending notification done");
063 }
064 }
065
066 @Override
067 protected void doStart() throws Exception {
068 // if non blocking then set a executor service on it
069 if (sender instanceof NonBlockingNagiosPassiveCheckSender) {
070 NonBlockingNagiosPassiveCheckSender nonBlocking = (NonBlockingNagiosPassiveCheckSender) sender;
071 ExecutorService executor = getEndpoint().getCamelContext().getExecutorServiceStrategy()
072 .newSingleThreadExecutor(this, getEndpoint().getEndpointUri());
073 nonBlocking.setExecutor(executor);
074 }
075 super.doStart();
076 }
077
078 @Override
079 protected void doStop() throws Exception {
080 super.doStop();
081 // if non blocking then shutdown executor
082 if (sender instanceof NonBlockingNagiosPassiveCheckSender) {
083 ((NonBlockingNagiosPassiveCheckSender) sender).shutdown();
084 }
085 }
086 }