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

  • A+
所属分类:Linux编程
摘要

Problem 如何使用C++编写自己的AgentSolution 一. VCS传递types.cf里面的参数给agent的方法,5.0是和4.0不一样的。下面例子在4

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/Sample

      vi Makefile, 增加 "-lrt" 选项到下面行:

      NEED_LIBS= -lvcsagfw -lnsl -lcrypt -lpthread -lrt

      编译Sample agent:

      ksuse:/opt/VRTSvcs/src/agent/Sample # make

      g++ -c -o agent.o agent.C

      g++ -o agent agent.o -lvcsagfw -lnsl -lcrypt -lpthread -lrt

开发过程:

      一。开发和编译自己的agent

      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

hares -add res_kft KFileTest sg_test ......

除非注明,否则均为@蒲公英网原创文章,转载必须以链接形式标明本文链接

本文链接:https://www.aipgy.com/53213.html

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: