Skip to content

Commit

Permalink
HWKALERTS-45 Add a basic IRC Action Plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasponce committed Nov 16, 2015
1 parent dd2f3ef commit 57ec6ec
Show file tree
Hide file tree
Showing 12 changed files with 477 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2015 Red Hat, Inc. and/or its affiliates
and other contributors as indicated by the @author tags.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.hawkular.alerts</groupId>
<artifactId>hawkular-alerts-actions-plugins</artifactId>
<version>0.7.0-SNAPSHOT</version>
</parent>

<artifactId>hawkular-alerts-actions-irc</artifactId>
<packaging>war</packaging>

<name>Hawkular Alerts Action Irc Plugin</name>

<dependencies>

<dependency>
<groupId>org.schwering</groupId>
<artifactId>irclib</artifactId>
<version>${version.org.schwering}</version>
</dependency>

</dependencies>

<profiles>

<profile>
<id>standalone</id>
<build>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<executions>
<execution>
<id>bus-war-package</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
<configuration>
<warSourceDirectory>src/main/webapp-standalone</warSourceDirectory>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>
</profile>

</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.alerts.actions.irc;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.hawkular.alerts.actions.api.ActionMessage;
import org.hawkular.alerts.actions.api.ActionPluginListener;
import org.hawkular.alerts.actions.api.ActionPluginSender;
import org.hawkular.alerts.actions.api.ActionResponseMessage;
import org.hawkular.alerts.actions.api.MsgLogger;
import org.hawkular.alerts.actions.api.Plugin;
import org.hawkular.alerts.actions.api.Sender;
import org.hawkular.alerts.api.json.JsonUtil;
import org.hawkular.alerts.api.model.action.Action;
import org.hawkular.alerts.api.model.event.Event;
import org.schwering.irc.lib.IRCConfig;
import org.schwering.irc.lib.IRCConfigBuilder;
import org.schwering.irc.lib.IRCConnection;
import org.schwering.irc.lib.IRCConnectionFactory;

/**
* An example of listener for snmp processing.
*
* @author Jay Shaughnessy
* @author Lucas Ponce
*/
@Plugin(name = "irc")
public class IrcPlugin implements ActionPluginListener {
static final String IRC_HOST_DEFAULT = "irc.freenode.net";
static final String IRC_PORT_DEFAULT = "6667";
static final String IRC_NICK_DEFAULT = "hwk-alerts-bot";
static final String IRC_PASSWORD_DEFAULT = "H4wk0l43";
static final String IRC_CHANNEL_DEFAULT = "#hawkular-alerts";
static final String IRC_DETAIL_DEFAULT = "false";

private final MsgLogger msgLog = MsgLogger.LOGGER;
Map<String, String> defaultProperties = new HashMap<>();

private IRCConnection conn = null;
private String channel = null;
private String detail = null;

@Sender
ActionPluginSender sender;

private static final String MESSAGE_PROCESSED = "PROCESSED";
private static final String MESSAGE_FAILED = "FAILED";

public IrcPlugin() {
defaultProperties.put("host", IRC_HOST_DEFAULT);
defaultProperties.put("port", IRC_PORT_DEFAULT);
defaultProperties.put("nick", IRC_NICK_DEFAULT);
defaultProperties.put("password", IRC_PASSWORD_DEFAULT);
defaultProperties.put("detail", IRC_DETAIL_DEFAULT);
}

@Override
public Set<String> getProperties() {
return defaultProperties.keySet();
}

@Override
public Map<String, String> getDefaultProperties() {
return defaultProperties;
}

@Override
public void process(ActionMessage msg) throws Exception {
try {
if (!isConnected()) {
connect(msg.getAction().getProperties());
}
send(msg.getAction(), Boolean.parseBoolean(detail));
msgLog.infoActionReceived("irc", msg.toString());
Action successAction = msg.getAction();
successAction.setResult(MESSAGE_PROCESSED);
sendResult(successAction);
} catch (Exception e) {
Action failedAction = msg.getAction();
failedAction.setResult(MESSAGE_FAILED);
sendResult(failedAction);
}
}

private boolean isConnected() throws Exception {
if (conn == null) {
return false;
}
return conn.isConnected();
}

private void connect(Map<String, String> props) throws Exception {
String host = (props == null || !props.containsKey("host")) ? IRC_HOST_DEFAULT : props.get("host");
String port = (props == null || !props.containsKey("port")) ? IRC_PORT_DEFAULT : props.get("port");
String nick = (props == null || !props.containsKey("nick")) ? IRC_NICK_DEFAULT : props.get("nick");
String password = (props == null || !props.containsKey("password")) ? IRC_PASSWORD_DEFAULT
: props.get("password");
channel = (props == null || !props.containsKey("channel") ? IRC_CHANNEL_DEFAULT : props.get("channel"));
detail = (props == null || !props.containsKey("detail") ? IRC_DETAIL_DEFAULT : props.get("detail"));

IRCConfig config = IRCConfigBuilder.newBuilder()
.host(host)
.port(Integer.valueOf(port))
.nick(nick)
.username(nick)
.password(password)
.realname(nick)
.build();

conn = IRCConnectionFactory.newConnection(config);
conn.connect();
conn.doJoin(channel);
}

private void send(Action action, boolean detail) {
Event e = action.getEvent();
if (e != null) {
StringBuilder msg = new StringBuilder();
msg.append(e.getEventType());
msg.append(" ");
msg.append(e.getId());
msg.append(" at ");
msg.append(new Date(e.getCtime()).toString());
conn.doPrivmsg(channel, msg.toString());
if (detail) {
conn.doPrivmsg(channel, JsonUtil.toJson(e));
}
}
}

private void sendResult(Action action) {
if (sender == null) {
throw new IllegalStateException("ActionPluginSender is not present in the plugin");
}
if (action == null) {
throw new IllegalStateException("Action to update result must be not null");
}
ActionResponseMessage newMessage = sender.createMessage(ActionResponseMessage.Operation.RESULT);
newMessage.getPayload().put("action", JsonUtil.toJson(action));
try {
sender.send(newMessage);
} catch (Exception e) {
msgLog.error("Error sending ActionResponseMessage", e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0"?>
<!--
Copyright 2015 Red Hat, Inc. and/or its affiliates
and other contributors as indicated by the @author tags.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<jboss-deployment-structure>
<deployment>
<dependencies>
<module name="deployment.hawkular-alerts-rest.war" />
</dependencies>
</deployment>
</jboss-deployment-structure>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0"?>
<!--
Copyright 2015 Red Hat, Inc. and/or its affiliates
and other contributors as indicated by the @author tags.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<jboss-web>
<context-root>/hawkular/actions/irc</context-root>
</jboss-web>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!--
Copyright 2015 Red Hat, Inc. and/or its affiliates
and other contributors as indicated by the @author tags.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

</web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0"?>
<!--
Copyright 2015 Red Hat, Inc. and/or its affiliates
and other contributors as indicated by the @author tags.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<jboss-deployment-structure>
<deployment>
<dependencies>
<module name="org.hawkular.nest" />
</dependencies>
</deployment>
</jboss-deployment-structure>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0"?>
<!--
Copyright 2015 Red Hat, Inc. and/or its affiliates
and other contributors as indicated by the @author tags.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<jboss-web>
<context-root>/hawkular/actions/irc</context-root>
</jboss-web>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!--
Copyright 2015 Red Hat, Inc. and/or its affiliates
and other contributors as indicated by the @author tags.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

</web-app>

0 comments on commit 57ec6ec

Please sign in to comment.