标题:C++&python实现动态更新nagios配置[原创] 出处:运维进行时 时间:Sun, 03 May 2009 23:21:05 +0000 作者:root 地址:https://blog.liuts.com/post/157/ 内容: 功能:根据获取到的服务器信息(数据来源格式为XML)动态去更新nagios配置。 C++部分 *************************************************************************** * Copyright (C) 2009 by liuts * * liutiansi@gmail.com* * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include #include #include #include "tinyxml.h" #include "tinyxml.cpp" #include "tinystr.h" #include "tinystr.cpp" #include "tinyxmlparser.cpp" #include "tinyxmlerror.cpp" using namespace std; class nagios_rsync { public: nagios_rsync(char *sys_par); ~nagios_rsync (); string hostgroups (string hostgroup_name,string members); string hosts (string members,string sysos); string services (string members); string writefile (string filename,string addstring); string (*displayXmlDocument_class (string filename))[2]; string (*displayXmlDocument_info (string filename))[3]; void produfile(); private: char *sysargc; string (*p_class)[2]; string (*p_info)[3]; bool servernum; string service_str; string hostgroups_str; string server_str; string serveros_str; string host_str; string ClassInfo[50][2]; string ServerInfo[800][3]; }; //构造func,传入更新标志; nagios_rsync::nagios_rsync (char *sys_par) { servernum=false; service_str="Switch,"; hostgroups_str=""; server_str=""; serveros_str=""; host_str="define host{\n"; host_str+=" name windows-server ; The name of this host template\n"; host_str+=" use generic-host ; This template inherits other values from the generic-host template\n"; host_str+=" check_period 24x7 ; By default, Linux hosts are checked round the clock\n"; host_str+=" max_check_attempts 10 ; Check each Linux host 10 times (max)\n"; host_str+=" check_command check-host-alive ; Default command to check Linux hosts\n"; host_str+=" notification_period workhours ; Linux admins hate to be woken up, so we only notify during the day\n"; host_str+=" ; Note that the notification_period variable is being overridden from\n"; host_str+=" ; the value that is inherited from the generic-host template!\n"; host_str+=" #notification_interval 120 ; Resend notification every 2 hours\n"; host_str+=" notification_interval 780\n"; host_str+=" notification_options d,u,r ; Only send notifications for specific host states\n"; host_str+=" contact_groups admins ; Notifications get sent to the admins by default\n"; host_str+=" register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!\n"; host_str+=" notification_period 24x7\n"; host_str+="}\n"; sysargc=sys_par; displayXmlDocument_class("./NagiosServerClass.xml"); displayXmlDocument_info("./NagiosServerInfo.xml"); } //类虚构func,销毁创建的指针; nagios_rsync::~nagios_rsync () { free(p_class); free(p_info); delete ClassInfo; delete ServerInfo; } //生成hostgroups.cfg串方法; string nagios_rsync::hostgroups(string hostgroup_name,string members) { string hostgroupsstr=""; hostgroupsstr+="define hostgroup{\n"; hostgroupsstr+=" hostgroup_name "+hostgroup_name+"\n"; hostgroupsstr+=" alias\n"; hostgroupsstr+=" members "+members+"\n"; hostgroupsstr+="}\n"; return hostgroupsstr; } //生成hosts.cfg串方法; string nagios_rsync::hosts(string members,string sysos) { string hosts=""; hosts+="define host{\n"; hosts+=" use "+sysos+"\n"; hosts+=" host_name "+members+"\n"; hosts+=" alias "+members+"\n"; hosts+=" address "+members+"\n"; hosts+="}\n"; return hosts; } //生成service.cfg串方法; string nagios_rsync::services(string members) { string services=""; services+="define service{\n"; services+=" use local-service\n"; services+=" hostgroup_name "+members+"\n"; services+=" service_description PING\n"; services+=" check_command check_ping!200.0,20%!600.0,60%\n"; services+=" normal_check_interval 5\n"; services+=" retry_check_interval 1\n"; services+="}\n"; return services; } //写配置文件方法,形参为文件名及串; string nagios_rsync::writefile(string filename,string addstring) { string mainpath="/nagios/etc/"+filename; ofstream FileObject; FileObject.open(mainpath.c_str(),ios::app); FileObject< FileObject.close(); return "1"; } //获取XML分类文件到指针; string (*nagios_rsync::displayXmlDocument_class(string filename))[2] { TiXmlDocument doc(filename.c_str()); doc.LoadFile(); TiXmlElement *root_r = doc.RootElement(); int i=0; for(TiXmlNode *node = root_r->FirstChild(); node; node = node->NextSibling()) { //遍历输出子节点名称及值; TiXmlNode *child = node->FirstChild(); int j=0; while(child) { ClassInfo[i][j]=child->Value(); int type = child->Type(); if (type == TiXmlNode::ELEMENT) { ClassInfo[i][j]=child->ToElement()->GetText(); } child = node->IterateChildren(child); j+=1; } i+=1; } p_class=ClassInfo; } //获取XML文件服务器信息数据到指针; string (*nagios_rsync::displayXmlDocument_info(string filename))[3] { TiXmlDocument doc(filename.c_str()); doc.LoadFile(); TiXmlElement *root_r = doc.RootElement(); //static vector > ClassInfo(m,vector(n)); int i=0; for(TiXmlNode *node = root_r->FirstChild(); node; node = node->NextSibling()) { //遍历输出节点属性名称及值; if (node->Type() == TiXmlNode::ELEMENT) { for(TiXmlAttribute *attr = node->ToElement()->FirstAttribute(); attr; attr = attr->Next()) { cout Type(); if (type == TiXmlNode::ELEMENT) { ServerInfo[i][j]=child->ToElement()->GetText(); } child = node->IterateChildren(child); j+=1; } i+=1; } p_info=ServerInfo; } //遍历处理两个指针; void nagios_rsync::produfile() { int i=0; int j=0; //遍历分类指针; for (i=0;i Generated by Bo-blog 2.1.1 Release