1、定义和初始化函数:

bool Init(std::string ip);

1.1、获取ip地址,这里的ip地址是通过makefile文件直接设定的。

bool XAgent::Init(string ip){if (ip.empty()){cerr << "Agent::init failed! ip is empty" << endl;return false;}}

1.2、调用动态链接库,这里的LXMysql是前面文章中讲过的

my = new LXMysql();

1.3、连接数据库,这里的Connect函数见 之前讲的这篇文章https://blog.csdn.net/wjl990316fddwjl/article/details/135379309

#ifdef _WIN32if (!my->Connect(ip.c_str(), "root", "990107Wjl@", "database_test")){cerr << "Agent::init failed! Connect DB failed!" <Connect(ip.c_str(), "fdd", "990107Wjl@", "test_fdd")){cerr << "Agent::init failed! Connect DB failed!" << endl;return false;}#endif //!_WIN32
bool LXMysql::Connect(const char* host, const char* user, const char* password, const char* database, unsigned short port, unsigned short flag){if (!Init())//防止内存泄露{cerr << "Connect faivertgled,mysql is not init!" << endl;return false;}if (!mysql_real_connect(mysql, host, user, password, database, port, 0, flag)){cerr << "mysql_real_connect failed!" << mysql_error(mysql) << endl;return false;}cout << "mysql connect success!" << endl;return true;}

1.4、日志文件地址

//日志的路径#define LOGPATH "/var/log/auth.log"

1.5、 测试日志是否存在

//读取日志文件fp = fopen(LOGPATH, "rb");if (!fp){cerr << "open" << LOGPATH << "failed" << endl;return false;}cout << "open" << LOGPATH << "success" << endl;

1.5、测试连接初始化

int main(int argc,char *argv[]){//agrc当前执行路径if (argc < 2){cout << "please set ip" << endl;cout << "./agent 192.168.122.128" <Init(argv[1])){//!XAgent::Get()->Init()通过::Get()访问静态函数,静态函数来调用Init(),cout << "agent Init failed!" << endl;return -1;}cout << "Agent Start!" << endl;std::cout << "Hello World!\n";}

完结,下一篇主要是读取日志文件,并读取最新日志文件