hpux/solaris/win32 的openview NNM开发(与nnm通信)Mrtg
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 // --
449 ret = fgets(buff,sizeof(buff),fp); // -- out bytes;
450 if ( ! ret){
451 READ_SAMPLE_EXIT
452 }
453 str = ValidString(buff);
454 inr = atof(str.c_str());
455 // -- end read
456 fclose(fp);
457 // --
458 MainLog(NVLog::LOG_DEBUG, " 读取MRTG数据成功! " );
459 obj -> flow_val = in + out;
460 obj -> using_rate = inr + outr;
461 MainLog(NVLog::LOG_DEBUG, " 读取MRTG数据成功 obj->flow_val:%d,obj->using_rate:%f " ,obj -> flow_val,obj -> using_rate);
462 return true ;
463 }
464
465 /*
466 MRTG采集数据的显示
467 */
468 void FlowVis::ShowLink(){
469 fvObject_List::iterator itr;
470 _sample_lock.Lock();
471 for (itr = _sample_list.begin();itr != _sample_list.end();itr ++ ){
472 fvObject * obj;
473 obj = * itr;
474 if ( obj -> status == fvObject::PENDING){
475 continue ;
476 }
477 #ifdef _UNIX
478 #ifdef FLOWVIS_SHOW
479 FILE * fp ;
480 fp = fopen( " ./del.f " , " r " );
481 if ( fp ){
482 fclose(fp);
483 Util_DelFile( " ./del.f " );
484 MainLog(NVLog::LOG_DEBUG, " 监测到对象被取消设置,停止显示对象信息 " );
485 break ;
486 }
487 #endif
488 #endif
489 if ( ! _sconf.rtParam.enable ){ // current flowvis is disabled
490 RestoreObjectBehavior(obj); // 还原符号原有的信息
491 continue ;
492 }
493 if ( ReadObjectInfo(obj)){
494 // -- format string
495 char buff[ 1024 ];
496 if ( _sconf.rtParam.show_type == 1 ){ // using_rate
497 if (obj -> flow_val < 1000 ){ // ^^2005.09.19 scott
498 sprintf(buff,REPRESENT_FORMAT_FLOW_RATE,obj -> flow_val);
499 } else {
500 sprintf(buff,REPRESENT_FORMAT_FLOW_RATE_KB,obj -> flow_val / 1000 );
501 }
502 } else {
503 sprintf(buff,REPRESENT_FORMAT_USING_RATE,obj -> using_rate);
504 }
505 obj -> text = buff;
506 MainLog(NVLog::LOG_MESSAGE, " 读取采样数据:%d,%f,Text:%s " ,obj -> flow_val,obj -> using_rate,buff);
507
508 if ( _sconf.rtParam.show_type == 1 ) { // 流速
509 CalcSymbolWidth(obj -> flow_val,obj -> symbol_type);
510 MainLog(NVLog::LOG_DEBUG, " after CalcSymbolWidth ,current:%d,symbol_type:%s " ,obj -> flow_val,obj -> symbol_type.c_str());
511 } else {
512 CalcSymbolWidthByPercent(obj -> using_rate,obj -> symbol_type);
513 MainLog(NVLog::LOG_DEBUG, " after CalcSymbolWidthByPercent ,percent:%f,symbol_type:%s " ,obj -> using_rate,obj -> symbol_type.c_str());
514 }
515 } else {
516 obj -> text = TEXT_MRTG_PREPARE;
517 obj -> symbol_type = " Connection:Generic " ;
518 }
519 RepresentObjectBehavior(obj);
520 }
521 _sample_lock.Unlock();
522 }
523
524 void FlowVis::MrtgSampling(){
525 while ( 1 ){
526 ShowLink();
527 for ( int n = 0 ;n < _sconf.showlink_interval ;n ++ ){
528 if ( n == 30 ){
529 ReloadConfig();
530 }
531 SLEEP_SEC( 1 );
532
533 OVwMapInfo * map = OVwGetMapInfo();
534 // MainLog(NVLog::LOG_DEBUG,"to OVwGetMapInfo()!");
535 if ( ! map){
536 MainLog(NVLog::LOG_DEBUG, " OVwGetMapInfo failed,fvshow will exit! " );
537 if ( OVwError() == OVw_CONNECTION_LOST){
538 exit( 1 );
539 }
540 } else {
541 OVwFreeMapInfo(map);
542 }
543
544 }
545 LoadFlowVisObjectList(); // -- n 时间之后重新加载链路信息
546 }
547 // MainLog(NVLog::LOG_MESSAGE,"ThreadMrtgSample exit");
548 }
549
550 // -- save object list to interface.list
551 // objectid#ip#index#subnet
552 bool FlowVis::SaveObjectList2File(){
553 char buff[ 1024 ];
554 FILE * fp;
555 fvObject_List::iterator itr;
556 fvObject * obj;
557 MainLog(FVLOG::LOG_MESSAGE, " 对象将被记录interface " );
558 return true ;
559 fp = fopen(FILE_INTERFACE, " w " );
560
561 _obj_lock.Lock();
562 for ( itr = _obj_list.begin();itr != _obj_list.end();itr ++ ){
563 obj = * itr;
564 sprintf(buff, " %u#%s#%d#%s\n " ,obj -> objid,obj -> ip.c_str(),obj -> index,obj -> subnet.c_str());
565 fputs(buff,fp);
566 MainLog(FVLOG::LOG_MESSAGE, " 对象已被记录到interface.list: %s " ,buff);
567 }
568 _obj_lock.Unlock();
569 fclose(fp);
570 return true ;
571 }
572
573 bool FlowVis::MrtgPrepare(fvObject * obj){
574 // - to mrtg processs
575 ShowUserLogWindow();
576 MainUserLog(FVLOG::LOG_DEBUG, " 链路[%s]正在设置 " ,Util_GetOVwSelectionName(obj -> objid).c_str());
577 if (SaveTask( "" ,obj)){
578 obj -> text = TEXT_MRTG_PREPARE;
579 MainLog(FVLOG::LOG_DEBUG, " to representobjectBehavior() after savetask " );
580 RepresentObjectBehavior(obj);
581 obj -> status = fvObject::CONFIRM; // switch status
582 MainLog(FVLOG::LOG_DEBUG, " 启动MRTG采集成功 ip:%s,index:%d " ,obj -> ip.c_str(),obj -> index);
583 MainUserLog(FVLOG::LOG_DEBUG, " 链路[%s]设置完毕 " ,Util_GetOVwSelectionName(obj -> objid).c_str());
584 } else {
585 MainLog(FVLOG::LOG_ERROR, " 启动MRTG采集失败 ip:%s,index:%d " ,obj -> ip.c_str(),obj -> index);
586 MainUserLog(FVLOG::LOG_ERROR, " 链路[%s]设置失败, "
587 " 原因可能是链路所在的设备Community不正确或设备无法访问 "
588 " ,及链路的属性不符合设置条件 " ,Util_GetOVwSelectionName(obj -> objid).c_str());
589 delete obj;
590 return false ;
591 } // -- end if
592 _obj_lock.Lock();
593 _obj_list.push_back(obj); // -- sucess task executed will be pushed into queue
594 _obj_lock.Unlock();
595 return true ;
596 // MainLog(NVLog::LOG_MESSAGE,"Thread Ifspeed exit");
597 }
598
599
600
601 bool FlowVis::CheckEnvironment(){
602 // 检测 if,mc,mrtg,flowvis.t,flowvis.conf
603 FILE * fp;
604 if ( (fp = fopen(CONFILE, " r " )) == NULL ){
605 MainLog(NVLog::LOG_ERROR, " flowvis.conf missed " );
606 return false ;
607 }
608 fclose(fp);
609 if ( NULL == (fp = fopen(MRTG_FILE_CFG_TEMPLATE, " r " ))){
610 MainLog(NVLog::LOG_ERROR, " flowvis.t missed " );
611 return false ;
612 }
613 fclose(fp);
614 Util_MkDir(DIR_INTERFACE);
615 Util_MkDir(MRTG_DIR_WORK);
616
617 return true ;
618 }
619
620
621 void FlowVis::SetMRTGInfo(){
622 MRTG.cfgmake = MRTG_FILE_CFGMAKER ;
623 MRTG.cfgT = MRTG_FILE_CFG_TEMPLATE;
624 MRTG.mrtg = MRTG_FILE_MAINAPP;
625 MRTG.perl = PERL_APP;
626 MRTG.workdir = MRTG_DIR_WORK;
627 MRTG.java = JAVA_APP;
628 }
629
630
631
632
633 bool FlowVis::CalcSymbolWidth(unsigned int v,std::string & connection){
634 double sensitive;
635 long tempUint;
636 char buff[ 128 ];
637
638 sensitive = _sconf.rtParam.link_sensitive;
639
640 #ifdef _HPUX
641 tempUint = ( long )((logf(v) / logf(sensitive)) * 2.0 - 3 );
642 # else
643 tempUint = ( long )((log(v) / log(sensitive)) * 2.0 - 3 );
644 #endif
645 if (tempUint == 0 || tempUint < 0 ){
646 connection = " SCarrier:S1Carrier " ;
647 } else if (tempUint > 40 ){
648 connection = " SCarrier:S40Carrier " ;
649 } else {
650 sprintf(buff, " SCarrier:S%dCarrier " ,tempUint);
651 connection = buff;
652 }
653 MainLog(NVLog::LOG_DEBUG, " CalcSymbolWidth() 流量:%d,灵敏度:%f,计算结果:%d " ,v,sensitive,tempUint);
654 return true ;
655 }
656
657
658 bool FlowVis::CalcSymbolWidthByPercent( float percent,std::string & connection){
659 unsigned int ret;
660 ret = (unsigned int )(percent * 0.4 );
661 ret = ret == 0 ? 1 : ret;
662 ret = ret > 40 ? 40 :ret;
663 char buff[ 128 ];
664 sprintf(buff, " SCarrier:S%dCarrier " ,ret);
665 connection = buff;
666 MainLog(NVLog::LOG_DEBUG, " CalcSymbolWidth() 流量percent:%f,计算结果:%d " ,percent,ret);
667 return true ;
668 }
669
670 void FlowVis::GetCommunity( const char * IP,FVSTRING & Community)
671 {
672 #ifdef FLOWVIS
673 // 根据IP地址取出Community
674 OVsnmpConfDest * Dest;
675 OVsnmpConfOpen(SNMP_CONF_OPEN_RDONLY);
676 Dest = OVsnmpConfResolveDest(IP,SNMP_CONF_FORCE_RESOLVE);
677 if (Dest){
678 Community = Dest -> confEntry -> community;
679 OVsnmpConfFreeDest( Dest);
680 }
681 #endif
682 }
683
684
685
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 // --
449 ret = fgets(buff,sizeof(buff),fp); // -- out bytes;
450 if ( ! ret){
451 READ_SAMPLE_EXIT
452 }
453 str = ValidString(buff);
454 inr = atof(str.c_str());
455 // -- end read
456 fclose(fp);
457 // --
458 MainLog(NVLog::LOG_DEBUG, " 读取MRTG数据成功! " );
459 obj -> flow_val = in + out;
460 obj -> using_rate = inr + outr;
461 MainLog(NVLog::LOG_DEBUG, " 读取MRTG数据成功 obj->flow_val:%d,obj->using_rate:%f " ,obj -> flow_val,obj -> using_rate);
462 return true ;
463 }
464
465 /*
466 MRTG采集数据的显示
467 */
468 void FlowVis::ShowLink(){
469 fvObject_List::iterator itr;
470 _sample_lock.Lock();
471 for (itr = _sample_list.begin();itr != _sample_list.end();itr ++ ){
472 fvObject * obj;
473 obj = * itr;
474 if ( obj -> status == fvObject::PENDING){
475 continue ;
476 }
477 #ifdef _UNIX
478 #ifdef FLOWVIS_SHOW
479 FILE * fp ;
480 fp = fopen( " ./del.f " , " r " );
481 if ( fp ){
482 fclose(fp);
483 Util_DelFile( " ./del.f " );
484 MainLog(NVLog::LOG_DEBUG, " 监测到对象被取消设置,停止显示对象信息 " );
485 break ;
486 }
487 #endif
488 #endif
489 if ( ! _sconf.rtParam.enable ){ // current flowvis is disabled
490 RestoreObjectBehavior(obj); // 还原符号原有的信息
491 continue ;
492 }
493 if ( ReadObjectInfo(obj)){
494 // -- format string
495 char buff[ 1024 ];
496 if ( _sconf.rtParam.show_type == 1 ){ // using_rate
497 if (obj -> flow_val < 1000 ){ // ^^2005.09.19 scott
498 sprintf(buff,REPRESENT_FORMAT_FLOW_RATE,obj -> flow_val);
499 } else {
500 sprintf(buff,REPRESENT_FORMAT_FLOW_RATE_KB,obj -> flow_val / 1000 );
501 }
502 } else {
503 sprintf(buff,REPRESENT_FORMAT_USING_RATE,obj -> using_rate);
504 }
505 obj -> text = buff;
506 MainLog(NVLog::LOG_MESSAGE, " 读取采样数据:%d,%f,Text:%s " ,obj -> flow_val,obj -> using_rate,buff);
507
508 if ( _sconf.rtParam.show_type == 1 ) { // 流速
509 CalcSymbolWidth(obj -> flow_val,obj -> symbol_type);
510 MainLog(NVLog::LOG_DEBUG, " after CalcSymbolWidth ,current:%d,symbol_type:%s " ,obj -> flow_val,obj -> symbol_type.c_str());
511 } else {
512 CalcSymbolWidthByPercent(obj -> using_rate,obj -> symbol_type);
513 MainLog(NVLog::LOG_DEBUG, " after CalcSymbolWidthByPercent ,percent:%f,symbol_type:%s " ,obj -> using_rate,obj -> symbol_type.c_str());
514 }
515 } else {
516 obj -> text = TEXT_MRTG_PREPARE;
517 obj -> symbol_type = " Connection:Generic " ;
518 }
519 RepresentObjectBehavior(obj);
520 }
521 _sample_lock.Unlock();
522 }
523
524 void FlowVis::MrtgSampling(){
525 while ( 1 ){
526 ShowLink();
527 for ( int n = 0 ;n < _sconf.showlink_interval ;n ++ ){
528 if ( n == 30 ){
529 ReloadConfig();
530 }
531 SLEEP_SEC( 1 );
532
533 OVwMapInfo * map = OVwGetMapInfo();
534 // MainLog(NVLog::LOG_DEBUG,"to OVwGetMapInfo()!");
535 if ( ! map){
536 MainLog(NVLog::LOG_DEBUG, " OVwGetMapInfo failed,fvshow will exit! " );
537 if ( OVwError() == OVw_CONNECTION_LOST){
538 exit( 1 );
539 }
540 } else {
541 OVwFreeMapInfo(map);
542 }
543
544 }
545 LoadFlowVisObjectList(); // -- n 时间之后重新加载链路信息
546 }
547 // MainLog(NVLog::LOG_MESSAGE,"ThreadMrtgSample exit");
548 }
549
550 // -- save object list to interface.list
551 // objectid#ip#index#subnet
552 bool FlowVis::SaveObjectList2File(){
553 char buff[ 1024 ];
554 FILE * fp;
555 fvObject_List::iterator itr;
556 fvObject * obj;
557 MainLog(FVLOG::LOG_MESSAGE, " 对象将被记录interface " );
558 return true ;
559 fp = fopen(FILE_INTERFACE, " w " );
560
561 _obj_lock.Lock();
562 for ( itr = _obj_list.begin();itr != _obj_list.end();itr ++ ){
563 obj = * itr;
564 sprintf(buff, " %u#%s#%d#%s\n " ,obj -> objid,obj -> ip.c_str(),obj -> index,obj -> subnet.c_str());
565 fputs(buff,fp);
566 MainLog(FVLOG::LOG_MESSAGE, " 对象已被记录到interface.list: %s " ,buff);
567 }
568 _obj_lock.Unlock();
569 fclose(fp);
570 return true ;
571 }
572
573 bool FlowVis::MrtgPrepare(fvObject * obj){
574 // - to mrtg processs
575 ShowUserLogWindow();
576 MainUserLog(FVLOG::LOG_DEBUG, " 链路[%s]正在设置 " ,Util_GetOVwSelectionName(obj -> objid).c_str());
577 if (SaveTask( "" ,obj)){
578 obj -> text = TEXT_MRTG_PREPARE;
579 MainLog(FVLOG::LOG_DEBUG, " to representobjectBehavior() after savetask " );
580 RepresentObjectBehavior(obj);
581 obj -> status = fvObject::CONFIRM; // switch status
582 MainLog(FVLOG::LOG_DEBUG, " 启动MRTG采集成功 ip:%s,index:%d " ,obj -> ip.c_str(),obj -> index);
583 MainUserLog(FVLOG::LOG_DEBUG, " 链路[%s]设置完毕 " ,Util_GetOVwSelectionName(obj -> objid).c_str());
584 } else {
585 MainLog(FVLOG::LOG_ERROR, " 启动MRTG采集失败 ip:%s,index:%d " ,obj -> ip.c_str(),obj -> index);
586 MainUserLog(FVLOG::LOG_ERROR, " 链路[%s]设置失败, "
587 " 原因可能是链路所在的设备Community不正确或设备无法访问 "
588 " ,及链路的属性不符合设置条件 " ,Util_GetOVwSelectionName(obj -> objid).c_str());
589 delete obj;
590 return false ;
591 } // -- end if
592 _obj_lock.Lock();
593 _obj_list.push_back(obj); // -- sucess task executed will be pushed into queue
594 _obj_lock.Unlock();
595 return true ;
596 // MainLog(NVLog::LOG_MESSAGE,"Thread Ifspeed exit");
597 }
598
599
600
601 bool FlowVis::CheckEnvironment(){
602 // 检测 if,mc,mrtg,flowvis.t,flowvis.conf
603 FILE * fp;
604 if ( (fp = fopen(CONFILE, " r " )) == NULL ){
605 MainLog(NVLog::LOG_ERROR, " flowvis.conf missed " );
606 return false ;
607 }
608 fclose(fp);
609 if ( NULL == (fp = fopen(MRTG_FILE_CFG_TEMPLATE, " r " ))){
610 MainLog(NVLog::LOG_ERROR, " flowvis.t missed " );
611 return false ;
612 }
613 fclose(fp);
614 Util_MkDir(DIR_INTERFACE);
615 Util_MkDir(MRTG_DIR_WORK);
616
617 return true ;
618 }
619
620
621 void FlowVis::SetMRTGInfo(){
622 MRTG.cfgmake = MRTG_FILE_CFGMAKER ;
623 MRTG.cfgT = MRTG_FILE_CFG_TEMPLATE;
624 MRTG.mrtg = MRTG_FILE_MAINAPP;
625 MRTG.perl = PERL_APP;
626 MRTG.workdir = MRTG_DIR_WORK;
627 MRTG.java = JAVA_APP;
628 }
629
630
631
632
633 bool FlowVis::CalcSymbolWidth(unsigned int v,std::string & connection){
634 double sensitive;
635 long tempUint;
636 char buff[ 128 ];
637
638 sensitive = _sconf.rtParam.link_sensitive;
639
640 #ifdef _HPUX
641 tempUint = ( long )((logf(v) / logf(sensitive)) * 2.0 - 3 );
642 # else
643 tempUint = ( long )((log(v) / log(sensitive)) * 2.0 - 3 );
644 #endif
645 if (tempUint == 0 || tempUint < 0 ){
646 connection = " SCarrier:S1Carrier " ;
647 } else if (tempUint > 40 ){
648 connection = " SCarrier:S40Carrier " ;
649 } else {
650 sprintf(buff, " SCarrier:S%dCarrier " ,tempUint);
651 connection = buff;
652 }
653 MainLog(NVLog::LOG_DEBUG, " CalcSymbolWidth() 流量:%d,灵敏度:%f,计算结果:%d " ,v,sensitive,tempUint);
654 return true ;
655 }
656
657
658 bool FlowVis::CalcSymbolWidthByPercent( float percent,std::string & connection){
659 unsigned int ret;
660 ret = (unsigned int )(percent * 0.4 );
661 ret = ret == 0 ? 1 : ret;
662 ret = ret > 40 ? 40 :ret;
663 char buff[ 128 ];
664 sprintf(buff, " SCarrier:S%dCarrier " ,ret);
665 connection = buff;
666 MainLog(NVLog::LOG_DEBUG, " CalcSymbolWidth() 流量percent:%f,计算结果:%d " ,percent,ret);
667 return true ;
668 }
669
670 void FlowVis::GetCommunity( const char * IP,FVSTRING & Community)
671 {
672 #ifdef FLOWVIS
673 // 根据IP地址取出Community
674 OVsnmpConfDest * Dest;
675 OVsnmpConfOpen(SNMP_CONF_OPEN_RDONLY);
676 Dest = OVsnmpConfResolveDest(IP,SNMP_CONF_FORCE_RESOLVE);
677 if (Dest){
678 Community = Dest -> confEntry -> community;
679 OVsnmpConfFreeDest( Dest);
680 }
681 #endif
682 }
683
684
685
1
2
3
4 #include < string.h >
5 #include < stdlib.h >
6 #include < sys / types.h >
7 #include < OV / ov_types.h >
8 #include < fcntl.h >
9 #include < stdio.h >
10 #include < signal.h >
11 #include < time.h >
12 #include < errno.h >
13
14
15
16 #ifdef _UNIX
17 #include < unistd.h >
18 #endif
19
20 #ifdef WIN32
21 #include < winsock.h >
22 #pragma comment(lib, " ws2_32.lib " )
23 typedef unsigned short mode_t;
24 #endif
25
26 #include < OV / OVsPMD.h >
27 #include < OVPath.h >
28
29
30 int ovspmdSocket = - 1 ;
31 fd_set rdmask; /* read mask for select */
32 OVsCodeType code; /* response code for OVsResponse */
33 OVsPMDCommand ovspmdCommand;
34
35 bool spmd_init(){
36 if (OVsInit( & ovspmdSocket) < 0 ){
37 return false ;
38 }
39 code = OVS_RSP_SUCCESS;
40 if (OVsInitComplete(code, " Initialization complete! " ) < 0 ){
41 return false ;
42 }
43 return true ;
44 }
45
46
47 void spmd_process(){
48 int selectRC;
49 for (;;){
50 FD_ZERO( & rdmask);
51 FD_SET(ovspmdSocket, & rdmask);
52 selectRC = select(ovspmdSocket + 1 , (fd_set * ) & rdmask, 0 , 0 , 0 );
53 if (selectRC <= 0 ){
54 break ;
55 }
56 if (FD_ISSET(ovspmdSocket, & rdmask)){
57 if (OVsReceive( & ovspmdCommand) < 0 ){
58 // 致命错误
59 break ;
60 }
61 switch (ovspmdCommand.code){
62 case OVS_CMD_EXIT:
63 goto __exit;
64 case OVS_CMD_NOOP:
65 case OVS_CMD_PAUSE:
66 case OVS_CMD_RESUME:
67 // send NACK back to ovspmd
68 // code = OVS_RSP_RESUME_NACK;
69 if (OVsResponse(code, " ignore message " ) < 0 ){
70 break ;
71 }
72 break ;
73 default :
74 break ;
75 }
76 }
77 }
78 __exit:
79 OVsDone( " flowvis loader exit " );
80 // rplog->Print(1,"PMD Command Exit!");
81 }
82
83
84
85
86
2
3
4 #include < string.h >
5 #include < stdlib.h >
6 #include < sys / types.h >
7 #include < OV / ov_types.h >
8 #include < fcntl.h >
9 #include < stdio.h >
10 #include < signal.h >
11 #include < time.h >
12 #include < errno.h >
13
14
15
16 #ifdef _UNIX
17 #include < unistd.h >
18 #endif
19
20 #ifdef WIN32
21 #include < winsock.h >
22 #pragma comment(lib, " ws2_32.lib " )
23 typedef unsigned short mode_t;
24 #endif
25
26 #include < OV / OVsPMD.h >
27 #include < OVPath.h >
28
29
30 int ovspmdSocket = - 1 ;
31 fd_set rdmask; /* read mask for select */
32 OVsCodeType code; /* response code for OVsResponse */
33 OVsPMDCommand ovspmdCommand;
34
35 bool spmd_init(){
36 if (OVsInit( & ovspmdSocket) < 0 ){
37 return false ;
38 }
39 code = OVS_RSP_SUCCESS;
40 if (OVsInitComplete(code, " Initialization complete! " ) < 0 ){
41 return false ;
42 }
43 return true ;
44 }
45
46
47 void spmd_process(){
48 int selectRC;
49 for (;;){
50 FD_ZERO( & rdmask);
51 FD_SET(ovspmdSocket, & rdmask);
52 selectRC = select(ovspmdSocket + 1 , (fd_set * ) & rdmask, 0 , 0 , 0 );
53 if (selectRC <= 0 ){
54 break ;
55 }
56 if (FD_ISSET(ovspmdSocket, & rdmask)){
57 if (OVsReceive( & ovspmdCommand) < 0 ){
58 // 致命错误
59 break ;
60 }
61 switch (ovspmdCommand.code){
62 case OVS_CMD_EXIT:
63 goto __exit;
64 case OVS_CMD_NOOP:
65 case OVS_CMD_PAUSE:
66 case OVS_CMD_RESUME:
67 // send NACK back to ovspmd
68 // code = OVS_RSP_RESUME_NACK;
69 if (OVsResponse(code, " ignore message " ) < 0 ){
70 break ;
71 }
72 break ;
73 default :
74 break ;
75 }
76 }
77 }
78 __exit:
79 OVsDone( " flowvis loader exit " );
80 // rplog->Print(1,"PMD Command Exit!");
81 }
82
83
84
85
86