如何使用C++编写自己的Agent
Solution
一. VCS传递types.cf里面的参数给agent的方法,5.0是和4.0不一样的。下面例子在4.1上开发。
二. 各种OS使用的C++编译器不一样,在Linux上使用gcc。
三. 本例编写一个FileOnOff agent:online entry point创建一个文件,monitor监控其存在,offline删除之。
搭建和检查开发环境:
ksuse:/opt/VRTSvcs/src/agent # cp -r Sample/ KFileTest
ksuse:/opt/VRTSvcs/src/agent # cd KFileTest/
ksuse:/opt/VRTSvcs/src/agent/KFileTest # mv agent.C KFileTestAgent.C
vi Makefile
AGENT= KFileTest
CSOURCES= KFileTestAgent.C
vi KFileTestAgent.C
增加一行:
#include //for creat()等函数
修改以下函数,实现ONLINE、OFFLINE、MONITOR三个entry point
VCSAGEXPORT void VCSAgStartup ()
{
VCSAgV40EntryPointStruct ep;
ep.open = NULL;
ep.close = NULL; /* expecting script entry point */
ep.monitor = res_monitor;
ep.online = res_online; /* expecting script entry point */
ep.offline = res_offline;
ep.clean = NULL;
ep.attr_changed = NULL;
ep.shutdown = NULL;
ep.action = NULL;
ep.info = NULL;
VCSAgSetLogCategory (10031);
VCSAgRegisterEPStruct(V40, &ep);
}
extern "C"
unsigned int res_online(const char *res_name, void **attr_val) {
VCSAG_LOG_INIT("res_online");
const char* path_name = (const char*) attr_val[0]; //这就是types.cf里的PathName
int fd = creat(path_name, S_IRUSR | S_IWUSR);
VCSAG_LOG_MSG(VCS_ERROR,13001,VCS_DEFAULT_FLAGS, "CREATE:%s",path_name);
return 0;
}
unsigned int res_offline(const char *res_name, void **attr_val) {
char* path_name = (char*) attr_val[0];
remove(path_name);
return 0;
}
VCSAgResState res_monitor
(const char *res_name, void **attr_val, int *conf_level)
{
/* Intialize the OUT params. */
VCSAgResState state = VCSAgResUnknown;
*conf_level=0;
char* path_name = (char*) attr_val[0];
struct stat stat_buf;
if (stat(path_name,&stat_buf)==0) {
state = VCSAgResOnline;
*conf_level=100;
}
else {
state = VCSAgResOffline;
*conf_level=0;
}
/* Determine the state & confidence level of the resource. */
return state;
}
二。编译、连接和安装:
make
make install ----这会把新的agent安装到/opt/VRTSvcs/bin
haconf -makerw
hatype -add KFileTest
haattr -add KFileTest PathName -string
hatype -modify KFileTest ArgList PathName
haconf -dump -makero
hares -add res_kft KFileTest sg_test ......