Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

monitoring_group_type 별 알림을 줄 수 있는 옵션 추가 #5

Merged
merged 1 commit into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<groupId>com.github.scouter-project</groupId>
<artifactId>scouter-plugin-server-alert-slack</artifactId>
<version>1.0.0</version>
<version>1.0.1-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -94,6 +94,15 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright 2016 Scouter Project.
*
* 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.
*
* @author Gookeun Lim
*/
package scouter.plugin.server.alert.slack;

import scouter.server.Configure;

/**
* <p>a class that helps to find a property value which matches to a specific monitoring_group_type key(aks obj_type).
* * You can defined a montioring_group_type values like below</p>
*
* {objType}.{remain value}ext_plugin_slack_...
*
* ex)
* ext_plugin_slack_channel=general_monitoring
* order_jvm.ext_plugin_slack_channel=order_monitoring
* stock_jvm.ext_plugin_slack_channel=stock_monitoring
*
* @author Gookeun Lim (passion.lim@gmail.com) on 2018.07.24
*
*/
public class MonitoringGroupConfigure {
private Configure conf;

public MonitoringGroupConfigure(Configure config) {
this.conf = config;
}

public String getValue(String key, String objType) {
return this.getValue(key, objType, null);
}

public String getGroupKey(String originalKey, String objType) {
if (originalKey == null) {
return originalKey;
}

return objType+"."+originalKey;
}

public String getValue(String key, String objType, String defaultValue) {
String groupKey = getGroupKey(key, objType);
String value = conf.getValue(groupKey);
if (value != null && value.trim().length() > 0) {
return value;
}
// default key value
value = conf.getValue(key);
return value != null? value : defaultValue;
}

public Boolean getBoolean(String key, final String objType, Boolean defaultValue) {
String groupKey = getGroupKey(key, objType);
Boolean value = toBoolean(conf.getValue(groupKey));
if (value != null) {
return value;
}
// default key value
value = toBoolean(conf.getValue(key));
return value != null? value : defaultValue;
}

public int getInt(String key, String objType, int defaultValue) {
String groupKey = getGroupKey(key, objType);
Integer value = toInteger(conf.getValue(groupKey));
if (value != null) {
return value;
}
// default key value
value = toInteger(conf.getValue(key));
return value != null ? value : defaultValue;
}

public long getLong(String key, String objType, long defaultValue) {
String groupKey = getGroupKey(key, objType);
Long value = toLong(conf.getValue(groupKey));
if (value != null) {
return value;
}
// default key value
value = toLong(conf.getValue(key));
return value != null ? value : defaultValue;
}


private Long toLong(String value) {
try {
if (value != null) {
return Long.parseLong(value);
}
} catch (Exception e) {
// ignore exception
}
return null;
}

private Integer toInteger(String value) {
try {
if (value != null) {
return Integer.parseInt(value);
}
} catch (Exception e) {
// ignore exception
}
return null;
}

private Boolean toBoolean(String value) {
try {
if (value != null) {
return Boolean.parseBoolean(value);
}
} catch (Exception e) {
// ignore exception
}
return null;
}
}
46 changes: 28 additions & 18 deletions src/main/java/scouter/plugin/server/alert/slack/SlackPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,17 @@
* @author Se-Wang Lee(ssamzie101@gmail.com) on 2016. 5. 2.
*/
public class SlackPlugin {

final Configure conf = Configure.getInstance();


private final MonitoringGroupConfigure groupConf;

private static AtomicInteger ai = new AtomicInteger(0);
private static List<Integer> javaeeObjHashList = new ArrayList<Integer>();

public SlackPlugin() {
groupConf = new MonitoringGroupConfigure(conf);

if (ai.incrementAndGet() == 1) {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);

Expand All @@ -84,7 +89,7 @@ public void run() {

mapPack = AgentCall.call(objectPack, RequestCmd.OBJECT_THREAD_LIST, mapPack);

int threadCountThreshold = conf.getInt("ext_plugin_thread_count_threshold", 0);
int threadCountThreshold = groupConf.getInt("ext_plugin_thread_count_threshold", objectPack.objType, 0);
int threadCount = mapPack.getList("name").size();

if (threadCountThreshold != 0 && threadCount > threadCountThreshold) {
Expand Down Expand Up @@ -112,19 +117,19 @@ public void run() {

@ServerPlugin(PluginConstants.PLUGIN_SERVER_ALERT)
public void alert(final AlertPack pack){
if (conf.getBoolean("ext_plugin_slack_send_alert", false)) {
if (groupConf.getBoolean("ext_plugin_slack_send_alert", pack.objType, false)) {

int level = conf.getInt("ext_plugin_slack_level", 0);
int level = groupConf.getInt("ext_plugin_slack_level", pack.objType, 0);
// Get log level (0 : INFO, 1 : WARN, 2 : ERROR, 3 : FATAL)
if(level <= pack.level){
new Thread(){
public void run(){
try{
String webhookURL = conf.getValue("ext_plugin_slack_webhook_url");
String channel = conf.getValue("ext_plugin_slack_channel");
String botName = conf.getValue("ext_plugin_slack_botName");
String iconURL = conf.getValue("ext_plugin_slack_icon_url");
String iconEmoji = conf.getValue("ext_plugin_slack_icon_emoji");
String webhookURL = groupConf.getValue("ext_plugin_slack_webhook_url", pack.objType);
String channel = groupConf.getValue("ext_plugin_slack_channel", pack.objType);
String botName = groupConf.getValue("ext_plugin_slack_botName", pack.objType);
String iconURL = groupConf.getValue("ext_plugin_slack_icon_url", pack.objType);
String iconEmoji = groupConf.getValue("ext_plugin_slack_icon_emoji", pack.objType);

assert webhookURL != null;

Expand Down Expand Up @@ -157,7 +162,7 @@ public void run(){
Message message = new Message(contents, channel, botName, iconURL, iconEmoji);
String payload = new Gson().toJson(message);

if(conf.getBoolean("ext_plugin_slack_debug", false)){
if(groupConf.getBoolean("ext_plugin_slack_debug", pack.objType, false)){
println("WebHookURL : "+webhookURL);
println("param : "+payload);
}
Expand Down Expand Up @@ -187,11 +192,12 @@ public void run(){
}
}
}

}.start();
}
}
}

@ServerPlugin(PluginConstants.PLUGIN_SERVER_OBJECT)
public void object(ObjectPack pack){
if (pack.version != null && pack.version.length() > 0) {
Expand Down Expand Up @@ -232,7 +238,10 @@ public void object(ObjectPack pack){

@ServerPlugin(PluginConstants.PLUGIN_SERVER_XLOG)
public void xlog(XLogPack pack) {
if (conf.getBoolean("ext_plugin_slack_xlog_enabled", true)) {

String objType = AgentManager.getAgent(pack.objHash).objType;

if ( groupConf.getBoolean("ext_plugin_slack_xlog_enabled", objType, true)) {
if (pack.error != 0) {
String date = DateUtil.yyyymmdd(pack.endTime);
String service = TextRD.getString(date, TextTypes.SERVICE, pack.service);
Expand All @@ -242,13 +251,13 @@ public void xlog(XLogPack pack) {
ap.title = "xlog Error";
ap.message = service + " - " + TextRD.getString(date, TextTypes.ERROR, pack.error);
ap.time = System.currentTimeMillis();
ap.objType = "scouter";
ap.objType = objType;
alert(ap);
}

try {
int elapsedThreshold = conf.getInt("ext_plugin_elapsed_time_threshold", 0);

int elapsedThreshold = groupConf.getInt("ext_plugin_elapsed_time_threshold", objType, 0);
if (elapsedThreshold != 0 && pack.elapsed > elapsedThreshold) {
String serviceName = TextRD.getString(DateUtil.yyyymmdd(pack.endTime), TextTypes.SERVICE, pack.service);

Expand All @@ -261,7 +270,7 @@ public void xlog(XLogPack pack) {
+ pack.service + "(" + serviceName + ") "
+ "elapsed time(" + pack.elapsed + " ms) exceed a threshold.";
ap.time = System.currentTimeMillis();
ap.objType = AgentManager.getAgent(pack.objHash).objType;
ap.objType = objType;

alert(ap);
}
Expand All @@ -272,7 +281,8 @@ public void xlog(XLogPack pack) {
}
}

@ServerPlugin(PluginConstants.PLUGIN_SERVER_COUNTER)

@ServerPlugin(PluginConstants.PLUGIN_SERVER_COUNTER)
public void counter(PerfCounterPack pack) {
String objName = pack.objName;
int objHash = HashUtil.hash(objName);
Expand All @@ -296,7 +306,7 @@ public void counter(PerfCounterPack pack) {
}

if (pack.timetype == TimeTypeEnum.REALTIME) {
long gcTimeThreshold = conf.getLong("ext_plugin_gc_time_threshold", 0);
long gcTimeThreshold = groupConf.getLong("ext_plugin_gc_time_threshold", objType, 0);
long gcTime = pack.data.getLong(CounterConstants.JAVA_GC_TIME);

if (gcTimeThreshold != 0 && gcTime > gcTimeThreshold) {
Expand Down
Loading