17
17
*/
18
18
package scouter .plugin .server .alert .email ;
19
19
20
+ import java .util .ArrayList ;
21
+ import java .util .List ;
22
+ import java .util .concurrent .Executors ;
23
+ import java .util .concurrent .ScheduledExecutorService ;
24
+ import java .util .concurrent .TimeUnit ;
25
+ import java .util .concurrent .atomic .AtomicInteger ;
26
+
20
27
import org .apache .commons .mail .DefaultAuthenticator ;
21
28
import org .apache .commons .mail .Email ;
22
29
import org .apache .commons .mail .SimpleEmail ;
23
30
24
31
import scouter .lang .AlertLevel ;
32
+ import scouter .lang .TextTypes ;
33
+ import scouter .lang .TimeTypeEnum ;
34
+ import scouter .lang .counters .CounterConstants ;
25
35
import scouter .lang .pack .AlertPack ;
36
+ import scouter .lang .pack .MapPack ;
26
37
import scouter .lang .pack .ObjectPack ;
38
+ import scouter .lang .pack .PerfCounterPack ;
39
+ import scouter .lang .pack .XLogPack ;
27
40
import scouter .lang .plugin .PluginConstants ;
28
41
import scouter .lang .plugin .annotation .ServerPlugin ;
42
+ import scouter .net .RequestCmd ;
29
43
import scouter .server .Configure ;
44
+ import scouter .server .CounterManager ;
30
45
import scouter .server .Logger ;
31
46
import scouter .server .core .AgentManager ;
47
+ import scouter .server .db .TextRD ;
48
+ import scouter .server .netio .AgentCall ;
49
+ import scouter .util .DateUtil ;
50
+ import scouter .util .HashUtil ;
32
51
33
52
/**
34
53
* Scouter server plugin to send alert via email
@@ -39,6 +58,48 @@ public class EmailPlugin {
39
58
40
59
// Get singleton Configure instance from server
41
60
final Configure conf = Configure .getInstance ();
61
+
62
+ private static AtomicInteger ai = new AtomicInteger (0 );
63
+ private static List <Integer > javaeeObjHashList = new ArrayList <Integer >();
64
+
65
+ public EmailPlugin () {
66
+ if (ai .incrementAndGet () == 1 ) {
67
+ ScheduledExecutorService executor = Executors .newScheduledThreadPool (1 );
68
+
69
+ // thread count check
70
+ executor .scheduleAtFixedRate (new Runnable () {
71
+ @ Override
72
+ public void run () {
73
+ for (int objHash : javaeeObjHashList ) {
74
+ if (AgentManager .isActive (objHash )) {
75
+ ObjectPack objectPack = AgentManager .getAgent (objHash );
76
+ MapPack mapPack = new MapPack ();
77
+ mapPack .put ("objHash" , objHash );
78
+
79
+ mapPack = AgentCall .call (objectPack , RequestCmd .OBJECT_THREAD_LIST , mapPack );
80
+
81
+ int threadCountThreshold = conf .getInt ("ext_plugin_thread_count_threshold" , 0 );
82
+ int threadCount = mapPack .getList ("name" ).size ();
83
+
84
+ if (threadCountThreshold != 0 && threadCount > threadCountThreshold ) {
85
+ AlertPack ap = new AlertPack ();
86
+
87
+ ap .level = AlertLevel .WARN ;
88
+ ap .objHash = objHash ;
89
+ ap .title = "Thread count exceed a threahold." ;
90
+ ap .message = objectPack .objName + "'s Thread count(" + threadCount + ") exceed a threshold." ;
91
+ ap .time = System .currentTimeMillis ();
92
+ ap .objType = objectPack .objType ;
93
+
94
+ alert (ap );
95
+ }
96
+ }
97
+ }
98
+ }
99
+ },
100
+ 0 , 5 , TimeUnit .SECONDS );
101
+ }
102
+ }
42
103
43
104
@ ServerPlugin (PluginConstants .PLUGIN_SERVER_ALERT )
44
105
public void alert (final AlertPack pack ) {
@@ -150,7 +211,12 @@ public void object(ObjectPack pack) {
150
211
ap .title = "An object has been activated." ;
151
212
ap .message = pack .objName + " is connected." ;
152
213
ap .time = System .currentTimeMillis ();
153
- ap .objType = "scouter" ;
214
+
215
+ if (AgentManager .getAgent (pack .objHash ) != null ) {
216
+ ap .objType = AgentManager .getAgent (pack .objHash ).objType ;
217
+ } else {
218
+ ap .objType = "scouter" ;
219
+ }
154
220
155
221
alert (ap );
156
222
} else if (op .alive == false ) {
@@ -161,13 +227,86 @@ public void object(ObjectPack pack) {
161
227
ap .title = "An object has been activated." ;
162
228
ap .message = pack .objName + " is reconnected." ;
163
229
ap .time = System .currentTimeMillis ();
164
- ap .objType = "scouter" ;
230
+ ap .objType = AgentManager . getAgent ( pack . objHash ). objType ;
165
231
166
232
alert (ap );
167
233
}
168
234
// inactive state can be handled in alert() method.
169
235
}
170
236
}
237
+
238
+ @ ServerPlugin (PluginConstants .PLUGIN_SERVER_XLOG )
239
+ public void xlog (XLogPack pack ) {
240
+ try {
241
+ int elapsedThreshold = conf .getInt ("ext_plugin_elapsed_time_threshold" , 0 );
242
+
243
+ if (elapsedThreshold != 0 && pack .elapsed > elapsedThreshold ) {
244
+ String serviceName = TextRD .getString (DateUtil .yyyymmdd (pack .endTime ), TextTypes .SERVICE , pack .service );
245
+
246
+ AlertPack ap = new AlertPack ();
247
+
248
+ ap .level = AlertLevel .WARN ;
249
+ ap .objHash = pack .objHash ;
250
+ ap .title = "Elapsed time exceed a threahold." ;
251
+ ap .message = "[" + AgentManager .getAgentName (pack .objHash ) + "] "
252
+ + pack .service + "(" + serviceName + ") "
253
+ + "elapsed time(" + pack .elapsed + " ms) exceed a threshold." ;
254
+ ap .time = System .currentTimeMillis ();
255
+ ap .objType = AgentManager .getAgent (pack .objHash ).objType ;
256
+
257
+ alert (ap );
258
+ }
259
+
260
+ } catch (Exception e ) {
261
+ Logger .printStackTrace (e );
262
+ }
263
+ }
264
+
265
+ @ ServerPlugin (PluginConstants .PLUGIN_SERVER_COUNTER )
266
+ public void counter (PerfCounterPack pack ) {
267
+ String objName = pack .objName ;
268
+ int objHash = HashUtil .hash (objName );
269
+ String objType = null ;
270
+ String objFamily = null ;
271
+
272
+ if (AgentManager .getAgent (objHash ) != null ) {
273
+ objType = AgentManager .getAgent (objHash ).objType ;
274
+ }
275
+
276
+ if (objType != null ) {
277
+ objFamily = CounterManager .getInstance ().getCounterEngine ().getObjectType (objType ).getFamily ().getName ();
278
+ }
279
+
280
+ try {
281
+ // in case of objFamily is javaee
282
+ if (CounterConstants .FAMILY_JAVAEE .equals (objFamily )) {
283
+ // save javaee type's objHash
284
+ if (!javaeeObjHashList .contains (objHash )) {
285
+ javaeeObjHashList .add (objHash );
286
+ }
287
+
288
+ if (pack .timetype == TimeTypeEnum .REALTIME ) {
289
+ long gcTimeThreshold = conf .getLong ("ext_plugin_gc_time_threshold" , 0 );
290
+ long gcTime = pack .data .getLong (CounterConstants .JAVA_GC_TIME );
291
+
292
+ if (gcTimeThreshold != 0 && gcTime > gcTimeThreshold ) {
293
+ AlertPack ap = new AlertPack ();
294
+
295
+ ap .level = AlertLevel .WARN ;
296
+ ap .objHash = objHash ;
297
+ ap .title = "Elapsed time exceed a threahold." ;
298
+ ap .message = objName + "'s GC time(" + gcTime + " ms) exceed a threshold." ;
299
+ ap .time = System .currentTimeMillis ();
300
+ ap .objType = objType ;
301
+
302
+ alert (ap );
303
+ }
304
+ }
305
+ }
306
+ } catch (Exception e ) {
307
+ Logger .printStackTrace (e );
308
+ }
309
+ }
171
310
172
311
private void println (Object o ) {
173
312
if (conf .getBoolean ("ext_plugin_email_debug" , false )) {
0 commit comments