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.EventObject;
020
021 import com.googlecode.jsendnsca.core.Level;
022 import com.googlecode.jsendnsca.core.MessagePayload;
023 import com.googlecode.jsendnsca.core.NagiosPassiveCheckSender;
024 import com.googlecode.jsendnsca.core.NagiosSettings;
025 import org.apache.camel.management.EventNotifierSupport;
026 import org.apache.camel.management.event.CamelContextStartupFailureEvent;
027 import org.apache.camel.management.event.CamelContextStopFailureEvent;
028 import org.apache.camel.management.event.ExchangeFailedEvent;
029 import org.apache.camel.management.event.ExchangeFailureHandledEvent;
030 import org.apache.camel.management.event.ExchangeRedeliveryEvent;
031 import org.apache.camel.management.event.ServiceStartupFailureEvent;
032 import org.apache.camel.management.event.ServiceStopFailureEvent;
033
034 /**
035 * An {@link org.apache.camel.spi.EventNotifier} which sends alters to Nagios.
036 *
037 * @version
038 */
039 public class NagiosEventNotifier extends EventNotifierSupport {
040
041 private NagiosSettings nagiosSettings;
042 private NagiosConfiguration configuration;
043 private NagiosPassiveCheckSender sender;
044 private String serviceName = "Camel";
045 private String hostName = "localhost";
046
047 public void notify(EventObject eventObject) throws Exception {
048 // create message payload to send
049 String message = eventObject.toString();
050 Level level = determineLevel(eventObject);
051 MessagePayload payload = new MessagePayload(getHostName(), level.ordinal(), getServiceName(), message);
052
053 if (log.isInfoEnabled()) {
054 log.info("Sending notification to Nagios: {}", payload.getMessage());
055 }
056 sender.send(payload);
057 log.trace("Sending notification done");
058 }
059
060 public boolean isEnabled(EventObject eventObject) {
061 return true;
062 }
063
064 protected Level determineLevel(EventObject eventObject) {
065 // failures is considered critical
066 if (eventObject instanceof ExchangeFailedEvent
067 || eventObject instanceof CamelContextStartupFailureEvent
068 || eventObject instanceof CamelContextStopFailureEvent
069 || eventObject instanceof ServiceStartupFailureEvent
070 || eventObject instanceof ServiceStopFailureEvent) {
071 return Level.CRITICAL;
072 }
073
074 // the failure was handled so its just a warning
075 // and warn when a redelivery attempt is done
076 if (eventObject instanceof ExchangeFailureHandledEvent
077 || eventObject instanceof ExchangeRedeliveryEvent) {
078 return Level.WARNING;
079 }
080
081 // default to OK
082 return Level.OK;
083 }
084
085 public NagiosConfiguration getConfiguration() {
086 if (configuration == null) {
087 configuration = new NagiosConfiguration();
088 }
089 return configuration;
090 }
091
092 public void setConfiguration(NagiosConfiguration configuration) {
093 this.configuration = configuration;
094 }
095
096 public NagiosSettings getNagiosSettings() {
097 return nagiosSettings;
098 }
099
100 public void setNagiosSettings(NagiosSettings nagiosSettings) {
101 this.nagiosSettings = nagiosSettings;
102 }
103
104 public String getServiceName() {
105 return serviceName;
106 }
107
108 public void setServiceName(String serviceName) {
109 this.serviceName = serviceName;
110 }
111
112 public String getHostName() {
113 return hostName;
114 }
115
116 public void setHostName(String hostName) {
117 this.hostName = hostName;
118 }
119
120 @Override
121 protected void doStart() throws Exception {
122 if (nagiosSettings == null) {
123 nagiosSettings = configuration.getNagiosSettings();
124 }
125 sender = new NagiosPassiveCheckSender(nagiosSettings);
126
127 log.info("Using " + configuration);
128 }
129
130 @Override
131 protected void doStop() throws Exception {
132 sender = null;
133 }
134
135 }