标题:实现真实的机柜模拟图[原创] 出处:运维进行时 时间:Fri, 09 Jul 2010 10:42:04 +0000 作者:刘天斯 地址:https://blog.liuts.com/post/206/ 内容: 一般能反映机房设备位置、结构我们都喜欢通过网络拓扑图来展现,但个人感觉还不够直观、明了的表现出自己想要的结果(自己太挑剔了,呵呵)。因此写一个生成真实机柜模拟图平台,实现与真实服务器外观、服务状态、空闲位置等信息。 在线效果图 http://blog.liuts.com/idc/ 系统截图 1、平台显示某一排截图 点击在新窗口中浏览此图片 https://blog.liuts.com/attachment.php?fid=136 2、平台显示某台服务器详细信息截图 点击在新窗口中浏览此图片 https://blog.liuts.com/attachment.php?fid=135 3、状态说明 点击在新窗口中浏览此图片 https://blog.liuts.com/attachment.php?fid=134 2U服务器正常状态 点击在新窗口中浏览此图片 https://blog.liuts.com/attachment.php?fid=133 2U服务器当机状态 系统原理 通过获取运维平台的服务器信息(包括位置、操作系统、机型等),格式为XML,通过c++的tinyxml来解析并渲染成比较美观的HTML格式。当机的信息通过Nagios来获取。这样就可以生成非常人性化的展现平台了:) 系统代码Servermap.cpp /*************************************************************************** * Copyright (C) 2010 by Liu Tiansi * * 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, CN. * ***************************************************************************/ #include #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 servermap { public: servermap( string *serverrow,string _idctype); ~servermap(); string int2str( int num); void Getdownserver (); string writefile (string filename); string GetServerCondition (string ip,string servertype); string (*displayXmlDocument_info (string filename))[5]; void ProduRow(); void ProduCurrServer(); private: string idctype; string (*p_info)[5]; // 所有的服务器信息指针(从XML文件中遍历); string (*pserver_info)[5]; // 当前机房的服务器信息指针(从XML文件中遍历); string ServerInfo[800][5]; // 所有的服务器信息数组(从XML文件中遍历); string ServerInfo_CurrServer[300][5]; //当前机房数组,从ServerInfo中过滤出来; string ServerDownIP[50]; //当服务器清单; int ServerInfoNumber; //获取所有信息的有效行; string *CurrServer_row; //指向当前机房数组的指针; int CurrServerInfoNumber; //获取当前机房信息的有效行; string HTMLstr; //存储HTML串; }; //构造func,传入排数及机房类型; servermap::servermap( string *Serverrow,string _idctype) { idctype=_idctype; //初始化HTML头; HTMLstr="\n\n\n服务器模拟状态图\n"; HTMLstr+="\n"; //机房排数组; CurrServer_row=Serverrow; ServerInfoNumber=0; CurrServerInfoNumber=0; //获取当前服务器清单; Getdownserver(); //遍历所有服务器信息; displayXmlDocument_info("ServerInfoAll.xml"); //简化当前机房服务器清单; ProduCurrServer(); } //类虚构func,销毁创建的指针; servermap::~servermap() { //clear mem; } //整形转字符串方法; string servermap::int2str( int num) { if (num == 0 ) return " 0 "; string str = "" ; int num_ = num > 0 ? num : - 1 * num; while (num_) { str = ( char )(num_ % 10 + 48 ) + str; num_ /= 10 ; } if (num < 0 ) str = " - " + str; return str; } //返回服务器状态图片; string servermap::GetServerCondition (string ip,string servertype) { bool Obtaining=false; for (int i=0;iNext()) { cout ToElement()->GetText(); } child = node->IterateChildren(child); j+=1; } i+=1; } ServerInfoNumber=i; p_info=ServerInfo; //free(ClassInfo); } //生成当前机房数组; void servermap::ProduCurrServer() { const char * strtmp; string strswap,stradd,Position0,Position1,Position2,Position3; for (int i=0;i Generated by Bo-blog 2.1.1 Release