Welcome 微信登录

首页 / 操作系统 / Linux / 如何使用C++编写自己的Agent?

Problem

如何使用C++编写自己的Agent

Solution
一. VCS传递types.cf里面的参数给agent的方法,5.0是和4.0不一样的。下面例子在4.1上开发。
二. 各种OS使用的C++编译器不一样,在Linux上使用gcc。
三. 本例编写一个FileOnOff agent:online entry point创建一个文件,monitor监控其存在,offline删除之。
搭建和检查开发环境:
      一。安装开发工具gcc以及相关组件: kSUSE:/mnt/suse/i586 # rpm -Uvh glibc-devel-2.4-31.2.i586.rpm ksuse:/mnt/suse/i586 # rpm -Uvh gcc-4.1.0-28.4.i586.rpm ksuse:/mnt/suse/i586 # rpm -Uvh libstdc++-devel-4.1.0-28.4.i586.rpm ksuse:/mnt/suse/i586 # rpm -Uvh gcc-c++-4.1.0-28.4.i586.rpm 二。编译VCS自带的Sample agent以检查开发环境:VCS默认提供的Sample agent是很好的检查开发环境的工具,若能顺利把Sample agent编译连接通过,说明开发环境已经搭建好。cd /opt/VRTSvcs/src/agent/Samplevi Makefile, 增加 "-lrt" 选项到下面行:NEED_LIBS= -lvcsagfw -lnsl -lcrypt -lpthread -lrt编译Sample agent:ksuse:/opt/VRTSvcs/src/agent/Sample # makeg++ -c -o agent.o agent.Cg++ -o agent agent.o -lvcsagfw -lnsl -lcrypt -lpthread -lrt
开发过程:
      一。开发和编译自己的agent ksuse:/opt/VRTSvcs/src/agent # cp -r Sample/ KFileTestksuse:/opt/VRTSvcs/src/agent # cd KFileTest/ksuse:/opt/VRTSvcs/src/agent/KFileTest # mv agent.C KFileTestAgent.Cvi MakefileAGENT= KFileTestCSOURCES= KFileTestAgent.Cvi KFileTestAgent.C增加一行:#include //for creat()等函数修改以下函数,实现ONLINE、OFFLINE、MONITOR三个entry pointVCSAGEXPORT 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里的PathNameint 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;}二。编译、连接和安装:makemake install ----这会把新的agent安装到/opt/VRTSvcs/binhaconf -makerwhatype -add KFileTesthaattr -add KFileTest PathName -stringhatype -modify KFileTest ArgList PathNamehaconf -dump -makero
hares -add res_kft KFileTest sg_test ......