32 |
|
//---CONSTRUCTORS--- |
33 |
|
|
34 |
|
public IRC__Alerter() { |
35 |
< |
// get the configuration for this alerter |
36 |
< |
Configuration config = ReferenceManager.getInstance().getCM().getConfiguration(_name); |
37 |
< |
|
38 |
< |
// an integer value |
39 |
< |
_level = Integer.parseInt(config.getProperty("Alerter.IRC.level")); |
40 |
< |
// the hostname of the IRC server |
41 |
< |
_IRCServer = config.getProperty("Alerter.IRC.IRCServer"); |
42 |
< |
// the port number of the IRC server |
43 |
< |
_IRCPort = Integer.parseInt(config.getProperty("Alerter.IRC.IRCPort")); |
44 |
< |
// the nickname to use |
45 |
< |
_nick = config.getProperty("Alerter.IRC.nick"); |
46 |
< |
// the channel to join |
47 |
< |
_channel = config.getProperty("Alerter.IRC.channel"); |
48 |
< |
// a message with the following: %level% and %message% |
49 |
< |
_message = config.getProperty("Alerter.IRC.message"); |
50 |
< |
|
35 |
> |
|
36 |
|
// connect to the IRC server |
37 |
|
_ircbot = null; |
38 |
|
try { |
39 |
|
_ircbot = new IRCBot(); |
40 |
|
_ircbot.connect(); |
41 |
< |
_ircbot.sendNotice("iscreamBot activated"); |
41 |
> |
_ircbot.sendNotice("i-scream alerting bot activated"); |
42 |
|
} catch(IOException e) { |
43 |
|
_logger.write(toString(), Logger.ERROR, "Error starting IRCBot: "+e); |
44 |
|
} |
45 |
+ |
|
46 |
+ |
_logger.write(toString(), Logger.SYSINIT, "IRC Alerter started"); |
47 |
|
} |
48 |
|
|
49 |
|
//---PUBLIC METHODS--- |
50 |
|
|
51 |
|
public void sendAlert(Alert alert) { |
52 |
+ |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
53 |
+ |
String levelName = cp.getProperty(_name, "Alerter.IRC.level"); |
54 |
+ |
int level = StringUtils.getStringPos(levelName, Alert.alertLevels); |
55 |
|
// only send if it's equal (or above) our level |
56 |
< |
if(alert.getLevel() >= _level) { |
56 |
> |
if(alert.getLevel() >= level) { |
57 |
> |
String alertType = Alert.alertLevels[alert.getLevel()]; |
58 |
> |
String thresholdType = Alert.thresholdLevels[alert.getThreshold()]; |
59 |
|
// sort out the message |
60 |
< |
String message = _message; |
61 |
< |
message = StringUtils.replaceText(message, "%level%", String.valueOf(alert.getLevel())); |
60 |
> |
String message = cp.getProperty(_name, "Alerter.IRC.message"); |
61 |
> |
message = StringUtils.replaceText(message, "%level%", alertType); |
62 |
> |
message = StringUtils.replaceText(message, "%threshold%", thresholdType); |
63 |
> |
message = StringUtils.replaceText(message, "%source%", alert.getSource()); |
64 |
|
message = StringUtils.replaceText(message, "%value%", alert.getValue()); |
65 |
|
message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue()); |
66 |
|
message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName()); |
67 |
+ |
message = StringUtils.replaceText(message, "%timeTillNextAlert%", getTimeString(Long.parseLong(alert.getTimeTillNextAlert()))); |
68 |
|
|
69 |
|
// send the message |
70 |
+ |
_logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ levelName + " level"); |
71 |
|
_ircbot.sendMsg(message); |
72 |
|
} |
73 |
|
} |
97 |
|
|
98 |
|
//---PRIVATE METHODS--- |
99 |
|
|
100 |
+ |
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 |
|
//---ACCESSOR/MUTATOR METHODS--- |
113 |
|
|
114 |
|
//---ATTRIBUTES--- |
107 |
– |
|
108 |
– |
// an integer value |
109 |
– |
private int _level; |
115 |
|
|
116 |
< |
// the hostname of the IRC server |
117 |
< |
private String _IRCServer; |
118 |
< |
|
114 |
< |
// the port number of the IRC server |
115 |
< |
private int _IRCPort; |
116 |
< |
|
117 |
< |
// the nickname to use |
118 |
< |
private String _nick; |
119 |
< |
|
120 |
< |
// the channel to join |
121 |
< |
private String _channel; |
122 |
< |
|
123 |
< |
// the ircbot |
116 |
> |
/** |
117 |
> |
* A reference to the IRCBot |
118 |
> |
*/ |
119 |
|
private IRCBot _ircbot; |
120 |
|
|
126 |
– |
// a message with the following: %level% and %message% |
127 |
– |
private String _message; |
128 |
– |
|
121 |
|
/** |
122 |
|
* This is the friendly identifier of the |
123 |
|
* component this class is running in. |
127 |
|
* can be placed here. This name could also |
128 |
|
* be changed to null for utility classes. |
129 |
|
*/ |
130 |
< |
private String _name = ClientMain.NAME; |
130 |
> |
private String _name = "IRC Alert"; |
131 |
|
|
132 |
|
/** |
133 |
|
* This holds a reference to the |
196 |
|
* @throws IOException if the connection fails |
197 |
|
*/ |
198 |
|
public void connect() throws IOException { |
199 |
+ |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
200 |
|
// setup the socket, reader and writer |
201 |
< |
_socket = new Socket(_IRCServer, _IRCPort); |
201 |
> |
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 |
|
_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 |
< |
_socketOut.println("NICK "+_nick); |
209 |
< |
_socketOut.println("USER iscream 8 * :i-scream Bot"); |
208 |
> |
_socketOut.println("NICK "+cp.getProperty(_name, "Alerter.IRC.nick")); |
209 |
> |
_socketOut.println("USER i-scream 8 * :i-scream alerter bot"); |
210 |
|
// join the channel |
211 |
+ |
_channel = cp.getProperty(_name, "Alerter.IRC.channel"); |
212 |
|
_socketOut.println("JOIN "+_channel); |
213 |
|
// start our listening thread |
214 |
|
this.start(); |
220 |
|
* @throws IOException if the disconnection fails |
221 |
|
*/ |
222 |
|
public void disconnect() throws IOException { |
223 |
< |
// send proper QUIET |
223 |
> |
// send proper QUIT |
224 |
|
_socketOut.println("QUIT : iscreamBot component shutting down..."); |
225 |
|
// close the socket |
226 |
|
_socketOut.close(); |
227 |
|
_socketIn.close(); |
228 |
|
_socket.close(); |
229 |
|
} |
230 |
< |
|
230 |
> |
|
231 |
|
/** |
232 |
|
* The socket connected to the server |
233 |
|
*/ |
243 |
|
*/ |
244 |
|
private BufferedReader _socketIn; |
245 |
|
|
246 |
+ |
/** |
247 |
+ |
* Just a reminder to what channel we're on... |
248 |
+ |
* this can't be dynamic :) |
249 |
+ |
*/ |
250 |
+ |
private String _channel; |
251 |
|
} |
252 |
|
|
253 |
|
} |