随笔 - 89  文章 - 141  trackbacks - 0
<2008年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

常用链接

留言簿(9)

随笔分类

随笔档案

文章分类

文章档案

相册

Gis

OpenSource

搜索

  •  

最新评论

阅读排行榜

评论排行榜

  1 /**
  2 2005.10.13        scott 
  3     1. GetCommunity()完成对象分配回收
  4 
  5 
  6 */
  7 
  8 
  9 #ifdef WIN32
 10 #pragma  warning(disable:4786)
 11 #endif
 12 
 13 #include "flowvis.h"
 14 #include "util.h"
 15 #include "mrtg.h"
 16 #include <stdio.h>
 17 #include <stdlib.h>
 18 #include <math.h>
 19 #include "encrypt.h"
 20 
 21 
 22 
 23 MrtgInfo FlowVis::MRTG ;
 24 
 25 
 26 
 27 FlowVis::FlowVis(){
 28     
 29 }
 30 
 31 FlowVis::~FlowVis(){
 32 }
 33 
 34 bool FlowVis::ReloadConfig(){
 35     char  buff[1024];    
 36     _sconf.Lock();    
 37     SetMRTGInfo();
 38     //-- run time params set 
 39     if( get_conf_val("ENABLE",buff,sizeof(buff))){
 40         _sconf.rtParam.enable = (int) atoi(ValidString(buff).c_str());
 41     }
 42 
 43     if( get_conf_val("ALARM_VALUE",buff,sizeof(buff))){
 44         _sconf.rtParam.alarm_val = (int) atoi(ValidString(buff).c_str());
 45     }
 46 
 47     if( get_conf_val("LINK_SENSITIVE",buff,sizeof(buff))){
 48         _sconf.rtParam.link_sensitive = (int) atoi(ValidString(buff).c_str());
 49     }
 50     MainLog(NVLog::LOG_DEBUG,"灵敏度:%d",_sconf.rtParam.link_sensitive);
 51     if( get_conf_val("SHOW_TYPE",buff,sizeof(buff))){
 52         _sconf.rtParam.show_type = (int) atoi(ValidString(buff).c_str());
 53     }
 54     
 55     // -- 
 56     if( get_conf_val("DEBUG_HOST",buff,sizeof(buff))){
 57         _sconf.debug_host = ValidString(buff);
 58     }
 59     if( get_conf_val("DEBUG_PORT",buff,sizeof(buff))){
 60         _sconf.debug_port = (unsigned short)atoi(ValidString(buff).c_str());
 61     }
 62     if( get_conf_val("LOG_LEVEL",buff,sizeof(buff))){
 63         _sconf.log_level  = (unsigned int)atoi(ValidString(buff).c_str());
 64     }
 65     if( get_conf_val("USERLOG_PORT",buff,sizeof(buff))){
 66         _sconf.userlog_port  = (unsigned int)atoi(ValidString(buff).c_str());
 67     }    
 68     if( get_conf_val("JAVA_APP",buff,sizeof(buff))){
 69         MRTG.java  = ValidString(buff);
 70     }
 71     if( get_conf_val("PERL_APP",buff,sizeof(buff))){
 72         MRTG.perl  = ValidString(buff);
 73     }
 74     if( get_conf_val("SHOWLINK_INTERVAL",buff,sizeof(buff))){
 75         _sconf.showlink_interval  = (unsigned int)atoi(ValidString(buff).c_str());
 76     }
 77     if( get_conf_val("UNCHECK_LICENSE",buff,sizeof(buff))){
 78         _sconf.check_license  = false;
 79     }
 80     if( get_conf_val("LOGWIN_STARTUP_SHOW",buff,sizeof(buff))){
 81         _sconf.logwin_startup_show = true;
 82     }
 83     _sconf.Unlock();
 84     return true;
 85 }
 86 
 87 bool    FlowVis::Initial(const char * confile){
 88     int ret;
 89     chdir(GetExeFileDir().c_str());    
 90     #ifdef WIN32
 91         WORD wVersionRequested;
 92         WSADATA wsaData;
 93         int err; 
 94         wVersionRequested = MAKEWORD( 2,2 );
 95         err = WSAStartup( wVersionRequested, &wsaData );
 96         if ( err != 0 ) {
 97             return false;
 98         }
 99     #endif
100 
101     #ifdef FLOWVIS_SHOW
102         NVLog::RegFileWriter * fw = new NVLog::RegFileWriter("./show.log");
103     #else
104         NVLog::RegFileWriter * fw = new NVLog::RegFileWriter("./order.log");
105     #endif
106     fw->Open();
107     _log.SetWriter(fw);
108     
109 
110     //-- 加载配置文件
111     if!ReloadConfig() ){
112         MainLog(NVLog::LOG_ERROR,"reload config failed!");
113         return false;
114     }
115 
116 
117     if( _sconf.check_license ){
118         std::string msg="";
119         bool show;
120         
121         if(!GetLicensNumber(msg,show)){        
122     #ifdef FLOWVIS_ORDER
123         //    printf("\nget license failed ,%s",msg.c_str());
124             PopMessage(MSG_WARNING,msg.c_str());
125             MainLog(NVLog::LOG_ERROR,msg.c_str());
126             
127     #endif
128             return false;        
129         }else{
130             if( show ){
131     #ifdef FLOWVIS_ORDER
132             //    printf(msg.c_str());
133                 PopMessage(MSG_INFORMATION,msg.c_str());            
134                 MainLog(NVLog::LOG_MESSAGE,msg.c_str());
135     #endif
136             }
137             //printf("check license ok");
138         }
139     }
140 
141 
142 
143 
144     //-- license check end
145     if!CheckEnvironment() ){
146         MainLog(NVLog::LOG_ERROR,"检测系统运行环境条件失败");
147         return false;
148     }
149 #ifdef WIN32 
150     NVLog::WindowWriter * ww = new NVLog::WindowWriter("flowvis");
151     ww->Open();
152     _log.SetWriter(ww);
153     //-- for userlog
154     _userlog.SetFormat("<TIME>\t<MESSAGE>\n");
155     NVLog::WindowWriter * wwul = new NVLog::WindowWriter(USERLOG_WIN_TITLE,true);
156     wwul->Open();
157     _userlog.SetWriter(wwul);
158 #endif
159 
160 #ifdef _UNIX
161     //-- for userlog
162     _userlog.SetFormat("<TIME>\t<MESSAGE>\n");
163     
164     #ifdef FLOWVIS_SHOW
165         NVLog::UdpWriter *uw = new NVLog::UdpWriter(_sconf.debug_host.c_str(),_sconf.debug_port);
166     #else
167         NVLog::UdpWriter *uw = new NVLog::UdpWriter(_sconf.debug_host.c_str(),_sconf.debug_port+1);
168     #endif
169     uw->Open();
170     _log.SetWriter(uw);
171     
172 #ifdef FLOWVIS_ORDER
173     for(int n=0;n< 1000; n++){
174         if( Util_TryBindUpdAddr("127.0.0.1",n+_sconf.userlog_port) ){
175             _sconf.userlog_port+=n;
176             break;
177         }
178     }
179     MainLog(NVLog::LOG_DEBUG,"bind udp port :%d",    _sconf.userlog_port);
180     if( _sconf.logwin_startup_show ){
181         ShowUserLogWindow();    //add on 05.10.13 scott
182     }
183     uw = new NVLog::UdpWriter("127.0.0.1",_sconf.userlog_port);    
184     uw->Open();
185     _userlog.SetWriter(uw);
186 #endif    
187 #endif
188 
189     ret = OVwInit();
190     if( ret ){
191         MainLog(NVLog::LOG_ERROR,"system start error: OVwInit failed!");
192         return false;
193     }
194     LoadFlowVisObjectList();    
195 
196 #ifdef FLOWVIS_SHOW
197         //--清除进程号
198         FILE *fp ;
199         char buff[32];        
200         unsigned int pid ;
201         Util_DelFile("./fvshow.pid");
202         Util_DelFile("./del.f");
203         //--创建进程号,并写入进程文件
204         fp = fopen("./fvshow.pid","w");
205 
206 #ifdef WIN32
207         pid = GetCurrentProcessId();
208 #else
209         pid = (unsigned int)getpid();
210 #endif
211         sprintf(buff,"%u",pid);
212         fwrite(buff,1,strlen(buff),fp);
213         fclose(fp);
214 #endif
215 
216 #ifdef FLOWVIS_ORDER    // -- to create fields
217 /*
218     OVwFieldId fid = OVwDbFieldNameToFieldId(OVW_FIELD_ISFLOWVIS_SET);
219     OVwDbDeleteField(fid);
220     fid = OVwDbCreateField(OVW_FIELD_ISFLOWVIS_SET, ovwBooleanField, ovwLocateField|ovwGeneralField);
221     if( fid == ovwNullFieldId){
222         if( OVwError() != OVw_FIELD_EXISTS){
223             MainLog(NVLog::LOG_ERROR,"创建Field :%s 失败!",OVW_FIELD_ISFLOWVIS_SET);
224         }
225     }else{
226         MainLog(NVLog::LOG_ERROR,"创建Field :%s 成功!",OVW_FIELD_ISFLOWVIS_SET);
227     }*/
228 #endif
229     return true;
230 }
231 
232 
233 bool    FlowVis::LoadTaskObject(const char * base,const char * file){
234     FILE * fp;
235     char * ret;
236     char * sep = "#";
237     char line[1024];
238     unsigned int objid;
239     char *tok;
240     fvObject *obj;
241     FVSTRING ip,index,ifspeed;
242     FVSTRING path;
243     
244     path = base;
245     path = path+file;
246     MainLog(FVLOG::LOG_DEBUG," To LoadTaskObject %s/",path.c_str());
247     fp = fopen(path.c_str(),"r");
248     if(!fp){
249         return false;
250     }
251     objid = atoi(file);
252     if( objid == 0){
253         return false;
254     }
255 
256     ret = fgets(line,sizeof(line),fp);
257     if(!ret){
258         return false;
259     }
260     MainLog(FVLOG::LOG_DEBUG,"Read Line:%s",line);
261     tok = strtok(line,sep);
262         if( tok ){                        
263             ip = tok;
264             tok = strtok(NULL,sep);
265             if( tok ){
266                 index = tok;
267                 obj = new fvObject;
268                 obj->objid = objid;
269                 obj->ip = ip;
270                 obj->status = fvObject::CONFIRM;
271                 obj->index = atol(index.c_str());
272                 obj->subnet = CalcSubnet(obj->ip.c_str(),"");
273                 _speedneed.push_back(obj);
274                 MainLog(FVLOG::LOG_DEBUG,"加载任务对象:objid:%d,ip:%s,index:%u,subnet:%s",objid,ip.c_str(),obj->index,obj->subnet.c_str());
275             }                            
276         }    
277     fclose(fp);
278     return true;
279 }
280 /*
281     name:    LoadFlowVisObjectList
282     desc:    flowvis启动时,将设置过的对象从文件加载上来
283             <objectid#ip#index#speed>
284     param:
285     ret:    
286 */
287 void FlowVis::LoadFlowVisObjectList(){
288     std::vector<FVSTRING> filist;
289     FVSTRING path;
290 
291     std::vector<FVSTRING>::iterator itr;
292     
293     fvObject_List::iterator itrobj;
294     fvObject * obj;
295     for(itrobj= _obj_list.begin();itrobj!=_obj_list.end();itrobj++){
296         obj = *itrobj;
297         delete obj;
298     }
299     _speedneed.erase(_speedneed.begin(),_speedneed.end());
300     Util_EnumFile(DIR_INTERFACE,filist);
301     for(itr = filist.begin();itr!=filist.end();itr++){
302         path = DIR_INTERFACE;
303         path = path + "/";
304         LoadTaskObject(path.c_str(),(*itr).c_str());
305     }
306     
307 #ifdef FLOWVIS_ORDER
308     /*
309     OVwFieldId fid;
310     for(itrobj= _obj_list.begin();itrobj!=_obj_list.end();itrobj++){
311         obj = *itrobj;
312         fid= OVwDbFieldNameToFieldId(OVW_FIELD_ISFLOWVIS_SET);
313         OVwDbSetFieldBooleanValue(obj->objid,fid,TRUE);
314     }*/
315 #endif
316     
317 }
318 
319 void    FlowVis::Clearup(){
320 
321 }
322 
323 void FlowVis::OnMapClose(void *userData, OVwEventType event){
324     FlowVis * f = (FlowVis*)userData;    
325     MainLog(NVLog::LOG_MESSAGE,"OnMapClose");
326     
327     //-- restore sense    
328     f->_sample_lock.Lock();
329     fvObject_List::iterator itr;
330     fvObject * obj;
331 //    OVwFieldId fid;
332     for(itr = f->_sample_list.begin();itr!=f->_sample_list.end();itr++){
333         obj = *itr;
334         if( obj->status == fvObject::CONFIRM){
335             f->RestoreObjectBehavior(obj);    
336             /*
337             fid= OVwDbFieldNameToFieldId(OVW_FIELD_ISFLOWVIS_SET);
338             OVwDbSetFieldBooleanValue(obj->objid,fid,FALSE);*/
339         }
340     }
341     f->_sample_lock.Unlock();
342     MainLog(NVLog::LOG_MESSAGE,"准备恢复符号初始状态!");    
343     OVwMapInfo * map;
344     map = OVwGetMapInfo();
345     OVwAckMapClose(map,time(0));
346     //FlowVis::SystemExit(userData,event);
347 }
348 
349 void FlowVis::OnMapOpen(void *userData, OVwEventType event){
350     FlowVis * f = (FlowVis*)userData;    
351     MainLog(NVLog::LOG_MESSAGE,"OnMapOpen");
352     static int _do =0;
353     if!_do){
354         f->ShowLink();
355     }
356 }
357 
358 
359 void FlowVis::SystemExit(void *user, OVwEventType event,OVwBoolean normalEnd){
360     FlowVis * f = (FlowVis*)user;    
361     
362     MainLog(NVLog::LOG_MESSAGE,"On SystemExit,normalEnd:%d",normalEnd);
363 #ifdef FLOWVIS_ORDER
364     //-- to kill buddy : fvshow
365     //--清除进程号
366     /*
367     FILE *fp ;
368     char buff[32];                
369     unsigned int pid;
370     fp = fopen("./fvshow.pid","r");
371     if(fp){
372         fgets(buff,sizeof(buff),fp);
373         pid = (unsigned int)atol(buff);    
374         fclose(fp);            
375         if( pid ){
376             f->KillProcess(pid);
377         }
378     }*/        
379     Util_DelFile("./fvshow.pid");
380 #endif
381     
382     MainLog(NVLog::LOG_MESSAGE,"已经恢复符号初始状态! 系统退出");
383     //MainUserLog(NVLog::LOG_ERROR,"FLOWVIS::EXIT");
384 //    OVwDone();
385     exit(0);
386 }
387 
388 bool    FlowVis::MainLoop(){
389 #ifdef FLOWVIS_SHOW
390     MrtgSampling();
391 #endif
392 #ifdef FLOWVIS_ORDER
393     OVwAddActionCallback((char *)("SetObject"), (OVwActionCallbackProc)SetObject, (void*)this);
394     OVwAddActionCallback((char *)("CancelObject"), (OVwActionCallbackProc)CancelObject, (void*)this);
395     OVwAddActionCallback((char *)("ShowReport"), (OVwActionCallbackProc)ShowReport, (void*)this);
396     OVwAddCallback(ovwMapClose, NULL,(OVwCallbackProc) FlowVis::OnMapClose,(void*)this);
397     #ifdef WIN32
398         OVwAddCallback(ovwEndSession, NULL,(OVwCallbackProc) FlowVis::SystemExit,(void*)this);
399     #endif
400         OVwMainLoop();    
401     #ifdef _UNIX
402         FlowVis::SystemExit((void*)this,1,1);
403     #endif
404     MainLog(NVLog::LOG_DEBUG,"after OVwMainLoop()");
405 #endif
406     
407     return true;
408 }
409 
410 #define READ_SAMPLE_EXIT fclose(fp);return false;
411 
412 bool    FlowVis::ReadObjectInfo(fvObject* obj){
413     //j->    
414     char buff[1024];
415     FILE * fp;
416     FVSTRING str;
417     char * ret;
418     sprintf(buff,"%s/%s_%u",MRTG_DIR_OUTPUT,obj->ip.c_str(),obj->index);
419     unsigned int out,in;
420     float outr,inr;
421     MainLog(NVLog::LOG_DEBUG,"ReadObjectInfo::试图读取 %s",buff);
422     fp = fopen(buff,"r");
423     if!fp ){
424         MainLog(NVLog::LOG_DEBUG,"试图读取MRTG数据失败,文件打开失败%s",buff);
425         return false;
426     }
427     ret = fgets(buff,sizeof(buff),fp);    // -- out bytes;
428     if(!ret){
429         READ_SAMPLE_EXIT
430     }
431     str = ValidString(buff);
432     out = atol(str.c_str());
433     //--
434     ret = fgets(buff,sizeof(buff),fp);    // -- out bytes;
435     if(!ret){
436         MainLog(NVLog::LOG_DEBUG,"试图读取MRTG数据失败,数据可能未采集!");
437         READ_SAMPLE_EXIT
438     }
439     str = ValidString(buff);
440     in = atol(str.c_str());
441     //--
442     ret = fgets(buff,sizeof(buff),fp);    // -- out bytes;
443     if(!ret){
444         READ_SAMPLE_EXIT
445     }
446     str = ValidString(buff);
447     outr = atof(str.c_str());
448