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 $Revision: 20316 $
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 if (log.isTraceEnabled()) {
058 log.trace("Sending notification done");
059 }
060 }
061
062 public boolean isEnabled(EventObject eventObject) {
063 return true;
064 }
065
066 protected Level determineLevel(EventObject eventObject) {
067 // failures is considered critical
068 if (eventObject instanceof ExchangeFailedEvent
069 || eventObject instanceof CamelContextStartupFailureEvent
070 || eventObject instanceof CamelContextStopFailureEvent
071 || eventObject instanceof ServiceStartupFailureEvent
072 || eventObject instanceof ServiceStopFailureEvent) {
073 return Level.CRITICAL;
074 }
075
076 // the failure was handled so its just a warning
077 // and warn when a redelivery attempt is done
078 if (eventObject instanceof ExchangeFailureHandledEvent
079 || eventObject instanceof ExchangeRedeliveryEvent) {
080 return Level.WARNING;
081 }
082
083 // default to OK
084 return Level.OK;
085 }
086
087 public NagiosConfiguration getConfiguration() {
088 if (configuration == null) {
089 configuration = new NagiosConfiguration();
090 }
091 return configuration;
092 }
093
094 public void setConfiguration(NagiosConfiguration configuration) {
095 this.configuration = configuration;
096 }
097
098 public NagiosSettings getNagiosSettings() {
099 return nagiosSettings;
100 }
101
102 public void setNagiosSettings(NagiosSettings nagiosSettings) {
103 this.nagiosSettings = nagiosSettings;
104 }
105
106 public String getServiceName() {
107 return serviceName;
108 }
109
110 public void setServiceName(String serviceName) {
111 this.serviceName = serviceName;
112 }
113
114 public String getHostName() {
115 return hostName;
116 }
117
118 public void setHostName(String hostName) {
119 this.hostName = hostName;
120 }
121
122 @Override
123 protected void doStart() throws Exception {
124 if (nagiosSettings == null) {
125 nagiosSettings = configuration.getNagiosSettings();
126 }
127 sender = new NagiosPassiveCheckSender(nagiosSettings);
128
129 log.info("Using " + configuration);
130 }
131
132 @Override
133 protected void doStop() throws Exception {
134 sender = null;
135 }
136
137 }