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