ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/client/alerters/IRC__Alerter.java
Revision: 1.8
Committed: Sun Mar 4 02:41:16 2001 UTC (23 years, 2 months ago) by ajm
Branch: MAIN
Changes since 1.7: +22 -6 lines
Log Message:
Revamped monitoring and alert fireing mechanism.
Now takes account of timing of last alerts.
Now uses threshold value a guide to how quickly alerts should be escalated.

File Contents

# User Rev Content
1 tdb 1.1 //---PACKAGE DECLARATION---
2     package uk.ac.ukc.iscream.client.alerters;
3    
4     //---IMPORTS---
5     import uk.ac.ukc.iscream.client.*;
6     import uk.ac.ukc.iscream.core.*;
7     import uk.ac.ukc.iscream.util.*;
8     import uk.ac.ukc.iscream.componentmanager.*;
9    
10     import java.io.*;
11     import java.net.*;
12    
13     /**
14     * This Alert sends an IRC message.
15     *
16 tdb 1.2 * @author $Author: tdb1 $
17 ajm 1.8 * @version $Id: IRC__Alerter.java,v 1.7 2001/03/03 01:09:53 tdb1 Exp $
18 tdb 1.1 */
19     public class IRC__Alerter implements PluginAlerter {
20    
21     //---FINAL ATTRIBUTES---
22    
23     /**
24     * The current CVS revision of this class
25     */
26 ajm 1.8 public final String REVISION = "$Revision: 1.7 $";
27 tdb 1.1
28     public final String DESC = "Sends alerts on an IRC channel";
29    
30     //---STATIC METHODS---
31    
32     //---CONSTRUCTORS---
33    
34     public IRC__Alerter() {
35 tdb 1.7
36 tdb 1.1 // connect to the IRC server
37     _ircbot = null;
38     try {
39     _ircbot = new IRCBot();
40     _ircbot.connect();
41 tdb 1.7 _ircbot.sendNotice("i-scream alerting bot activated");
42 tdb 1.1 } catch(IOException e) {
43     _logger.write(toString(), Logger.ERROR, "Error starting IRCBot: "+e);
44     }
45 tdb 1.4
46 tdb 1.5 _logger.write(toString(), Logger.SYSINIT, "IRC Alerter started");
47 tdb 1.1 }
48    
49     //---PUBLIC METHODS---
50    
51     public void sendAlert(Alert alert) {
52 tdb 1.7 ConfigurationProxy cp = ConfigurationProxy.getInstance();
53 ajm 1.8 String levelName = cp.getProperty(_name, "Alerter.IRC.level");
54     int level = StringUtils.getStringPos(levelName, Alert.alertLevels);
55 tdb 1.1 // only send if it's equal (or above) our level
56 tdb 1.7 if(alert.getLevel() >= level) {
57 ajm 1.8 String alertType = Alert.alertLevels[alert.getLevel()];
58     String thresholdType = Alert.thresholdLevels[alert.getThreshold()];
59 tdb 1.1 // sort out the message
60 tdb 1.7 String message = cp.getProperty(_name, "Alerter.IRC.message");
61 tdb 1.6 message = StringUtils.replaceText(message, "%level%", alertType);
62 ajm 1.8 message = StringUtils.replaceText(message, "%threshold%", thresholdType);
63 tdb 1.3 message = StringUtils.replaceText(message, "%source%", alert.getSource());
64 tdb 1.2 message = StringUtils.replaceText(message, "%value%", alert.getValue());
65     message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue());
66     message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName());
67 ajm 1.8 message = StringUtils.replaceText(message, "%timeTillNextAlert%", getTimeString(Long.parseLong(alert.getTimeTillNextAlert())));
68 tdb 1.1
69     // send the message
70 ajm 1.8 _logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ levelName + " level");
71 tdb 1.1 _ircbot.sendMsg(message);
72     }
73     }
74    
75     /**
76     * Overrides the {@link java.lang.Object#toString() Object.toString()}
77     * method to provide clean logging (every class should have this).
78     *
79     * This uses the uk.ac.ukc.iscream.util.NameFormat class
80     * to format the toString()
81     *
82     * @return the name of this class and its CVS revision
83     */
84     public String toString() {
85     return FormatName.getName(
86     _name,
87     getClass().getName(),
88     REVISION);
89     }
90    
91     /**
92     * return the String representation of what the filter does
93     */
94     public String getDescription(){
95     return DESC;
96     }
97    
98     //---PRIVATE METHODS---
99    
100 ajm 1.8 private String getTimeString(long time) {
101     String timeString = null;
102     if (time >= 60) {
103     timeString = (time / 60) + " minute(s)";
104     } else if (time >= 3600) {
105     timeString = ((time/60) / 60) + " hour(s)";
106     } else {
107     timeString = time + " second(s)";
108     }
109     return timeString;
110     }
111    
112 tdb 1.1 //---ACCESSOR/MUTATOR METHODS---
113    
114     //---ATTRIBUTES---
115    
116 tdb 1.7 /**
117     * A reference to the IRCBot
118     */
119 tdb 1.1 private IRCBot _ircbot;
120    
121     /**
122     * This is the friendly identifier of the
123     * component this class is running in.
124     * eg, a Filter may be called "filter1",
125     * If this class does not have an owning
126     * component, a name from the configuration
127     * can be placed here. This name could also
128     * be changed to null for utility classes.
129     */
130 ajm 1.8 private String _name = "IRC Alert";
131 tdb 1.1
132     /**
133     * This holds a reference to the
134     * system logger that is being used.
135     */
136     private Logger _logger = ReferenceManager.getInstance().getLogger();
137    
138     //---STATIC ATTRIBUTES---
139    
140     //---INNER CLASSES---
141    
142     /**
143     * This class provides some basic IRCBot functionality. It connects
144     * to a specified server, and will remain there until told to
145     * leave. Whilst connected it can send a message or a notice to
146     * the server.
147     */
148     class IRCBot extends Thread {
149    
150     /**
151     * Main thread loop, this part of the class listens for
152     * messages from the server, and acts accordingly. At the
153     * present moment it only responds to pings.
154     */
155     public void run() {
156     // flag so we can stop the loop
157     boolean run = true;
158     while(run) {
159     try {
160     // read a command
161     String cmd = _socketIn.readLine();
162     // if it's a PING...
163     if(cmd.startsWith("PING")) {
164     // ...send a PONG
165     _socketOut.println("PONG" + cmd.substring(4));
166     }
167     } catch (IOException e) {
168     // comms failure, lets stop
169     _logger.write(this.toString(), Logger.ERROR, "Comms error: "+e);
170     //run = false;
171     }
172     }
173     }
174    
175     /**
176     * Sends a message to the channel.
177     *
178     * @param msg The message to send
179     */
180     public void sendMsg(String msg) {
181     _socketOut.println("PRIVMSG "+_channel+" :"+msg);
182     }
183    
184     /**
185     * Sends a notice to the channel.
186     *
187     * @param msg The notice to send
188     */
189     public void sendNotice(String msg) {
190     _socketOut.println("NOTICE "+_channel+" :"+msg);
191     }
192    
193     /**
194     * Connect to the IRC server, log in, and join the channel.
195     *
196     * @throws IOException if the connection fails
197     */
198     public void connect() throws IOException {
199 tdb 1.7 ConfigurationProxy cp = ConfigurationProxy.getInstance();
200 tdb 1.1 // setup the socket, reader and writer
201 tdb 1.7 String server = cp.getProperty(_name, "Alerter.IRC.IRCServer");
202     int port = Integer.parseInt(cp.getProperty(_name, "Alerter.IRC.IRCPort"));
203     _socket = new Socket(server, port);
204 tdb 1.1 _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
205     _socketOut = new PrintWriter(_socket.getOutputStream(), true);
206     // send the various log in messages
207     _socketOut.println("PASS");
208 tdb 1.7 _socketOut.println("NICK "+cp.getProperty(_name, "Alerter.IRC.nick"));
209     _socketOut.println("USER i-scream 8 * :i-scream alerter bot");
210 tdb 1.1 // join the channel
211 tdb 1.7 _channel = cp.getProperty(_name, "Alerter.IRC.channel");
212 tdb 1.1 _socketOut.println("JOIN "+_channel);
213     // start our listening thread
214     this.start();
215     }
216    
217     /**
218     * Disconnect "nicely" from the IRC server.
219     *
220     * @throws IOException if the disconnection fails
221     */
222     public void disconnect() throws IOException {
223 tdb 1.6 // send proper QUIT
224 tdb 1.1 _socketOut.println("QUIT : iscreamBot component shutting down...");
225     // close the socket
226     _socketOut.close();
227     _socketIn.close();
228     _socket.close();
229     }
230 tdb 1.7
231 tdb 1.1 /**
232     * The socket connected to the server
233     */
234     private Socket _socket;
235    
236     /**
237     * The writer
238     */
239     private PrintWriter _socketOut;
240    
241     /**
242     * The reader
243     */
244     private BufferedReader _socketIn;
245    
246 tdb 1.7 /**
247     * Just a reminder to what channel we're on...
248     * this can't be dynamic :)
249     */
250     private String _channel;
251 tdb 1.1 }
252    
253     }