scouter.agent.counter.task.AgentHeartBeat Maven / Gradle / Ivy
/*
* Copyright 2015 the original author or authors.
* @https://github.com/scouter-project/scouter
*
* 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 scouter.agent.counter.task;
import scouter.Version;
import scouter.agent.Configure;
import scouter.agent.counter.CounterBasket;
import scouter.agent.counter.anotation.Counter;
import scouter.agent.netio.data.HostAgentDataProxy;
import scouter.agent.netio.data.net.TcpWorker;
import scouter.lang.pack.ObjectPack;
import scouter.util.StringKeyLinkedMap;
import scouter.util.StringUtil;
import java.util.Enumeration;
import static scouter.lang.constants.ScouterConstants.TAG_OBJ_DETECTED_TYPE;
public class AgentHeartBeat {
public AgentHeartBeat() {
}
private static StringKeyLinkedMap objects = new StringKeyLinkedMap();
public static void addObject(String objType, int objHash, String objName) {
ObjectPack old = objects.get(objName);
if (old != null && objType.equals(old.objType)) {
return;
}
ObjectPack p = new ObjectPack();
p.objType = objType;
p.objHash = objHash;
p.objName = objName;
objects.put(objName, p);
}
@Counter
public void alive(CounterBasket pw) {
HostAgentDataProxy.sendHeartBeat(getMainObject());
Enumeration en = objects.values();
while (en.hasMoreElements()) {
HostAgentDataProxy.sendHeartBeat(en.nextElement());
}
}
private ObjectPack getMainObject() {
Configure conf = Configure.getInstance();
ObjectPack p = new ObjectPack();
p.objType = conf.obj_type;
p.objHash = conf.getObjHash();
p.objName = conf.getObjName();
p.version = Version.getAgentFullVersion();
p.address = TcpWorker.localAddr;
if(StringUtil.isNotEmpty(conf.getObjDetectedType())){
p.tags.put(TAG_OBJ_DETECTED_TYPE, conf.getObjDetectedType());
}
return p;
}
public static void clearSubObjects() {
objects.clear();
}
}