blib/lib/Huawei/Healthchecker.pm

filter: Statementstmt 75.0% 6 / 8 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 81.8% 9 / 11 CC 1 SCAR 0.9
CC
1
Cov
75%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Huawei::Healthchecker;
2  
3 #版本信息
4 our $VERSION = "0.12";
5  
6 1 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 0 ▶use Net::Compare;
Subroutine BEGIN: called CC=1 SCAR=2.6
9  
10 #引用导出函数
11 use Exporter;
12  
13 #继承parent相关变量
14 use parent 'Exporter';
15  
16 #自动导出健康检查函数
17 our @EXPORT = qw(health_check);
18  
19 #设置华为设备通用健康检查哈希数据模型
20 my $attr = {
21 'hardware' => {
22 'device_version' => 'dis device',
23 'module_status' => 'dis device',
24 'power_status' => 'dis device power',
25 'disk_status' => 'dis health',
26 'fan_status' => 'dis device fan',
27 'alarm_status' => 'dis device alarm hardware',
28 'temperature_status' => 'dis device temperature all',
29 },
30 'status' => {
31 'cpu_status' => 'dis cpu',
32 'memory_status' => 'dis memory',
33 'ntp_status' => 'dis ntp status',
34 'vrrp_status' => 'dis vrrp',
35 'eth_trunk_status' => 'dis eth-trunk',
36 'err_down_status' => 'dis error-down recovery',
37 }
38 };
39  
40 #固化硬件巡检参数为数组(哈希无序)
41 my $hardware = $attr->{"hardware"};
42  
43 #固化关键指标巡检参数为数组(哈希无序)
44 my $status = $attr->{"status"};
45  
46 #-------------------------------------------------------------------------
47 # 网络设备- 关键指标巡检阈值
48 #-------------------------------------------------------------------------
49 #CPU告警阈值
50 my $cpu_threshold = 60;
51  
52 #内存告警阈值
53 my $memory_threshold = 80;
54  
55 #CPU历史MAX告警阈值
56 my $cpu_max_threshold = 85;
57  
58 #磁盘告警阈值
59 my $disk_threshold = 75;
60  
61 #-------------------------------------------------------------------------
62 # 网络设备-华为设备巡检入口函数
63 #-------------------------------------------------------------------------
64 sub health_check {
65  
66 #接收待巡检配置文件
67 my $config = shift;
68  
69 #解析巡检输出
70 my $rev = status_check($config);
71  
72 #初始化变量-设备巡检日志
73 my @comment;
74  
75 #获取设备版本信息
76 #my $verison = $rev->{"verison"};
77  
78 #初始化设备硬件状态巡检结果-数组
79 my @hardware_ret;
80 push @hardware_ret,
81 $rev->{"hardware"}{"device_module"}{"health"} || "unknow";
82 push @hardware_ret,
83 $rev->{"hardware"}{"device_power"}{"health"} || "unknow";
84 push @hardware_ret,
85 $rev->{"hardware"}{"device_disk"}{"health"} || "unknow";
86 push @hardware_ret,
87 $rev->{"hardware"}{"device_fan"}{"health"} || "unknow";
88 push @hardware_ret,
89 $rev->{"hardware"}{"device_temperature"}{"health"} || "unknow";
90 push @hardware_ret,
91 $rev->{"hardware"}{"device_alarm"}{"health"} || "unknow";
92  
93 #初始化设备关键指标巡检结果-数组
94 my @status_ret;
95 push @status_ret, $rev->{"status"}{"device_cpu"}{"health"} || "unknow";
96 push @status_ret, $rev->{"status"}{"device_memory"}{"health"} || "unknow";
97 push @status_ret, $rev->{"status"}{"device_ntp"}{"health"} || "unknow";
98 push @status_ret, $rev->{"status"}{"device_vrrp"}{"health"} || "unknow";
99 push @status_ret,
100 $rev->{"status"}{"device_eth_trunk"}{"health"} || "unknow";
101 push @status_ret,
102 $rev->{"status"}{"device_err_down"}{"health"} || "unknow";
103  
104 #遍历 $rev->{"hardware"} 各属性哈希值
105 foreach ( keys %{ $rev->{"hardware"} } ) {
106 my $ret = $rev->{"hardware"}->{$_};
107 if ( defined $ret->{"log"} ) {
108 push @comment, $ret->{"log"};
109 }
110 }
111  
112 #遍历 $rev->{"status"} 各属性哈希值
113 foreach ( keys %{ $rev->{"status"} } ) {
114 my $ret = $rev->{"status"}->{$_};
115 if ( defined $ret->{"log"} ) {
116 push @comment, $ret->{"log"};
117 }
118 }
119  
120 #将设备描述数组转换未字符串
121 my $comment = join( "\n", @comment ) if @comment;
122  
123 #计算设备巡检健康度
124 my $score = calculate_score($rev);
125  
126 #封装计算结果到匿名数组中,方便EXCEL读写
127 my $result = [ @hardware_ret, @status_ret, $score, $comment ];
128  
129 #返回健康检查结果
130 return ( $rev, $result );
131 }
132  
133 #-------------------------------------------------------------------------
134 # 网络设备-设备健康在线巡检函数入口
135 #-------------------------------------------------------------------------
136 sub status_check {
137  
138 #接收待巡检配置文件
139 my $config = shift;
140  
141 #设备巡检结果
142 my $result;
143  
144 #-------------------------------------------------------------------------
145 # 网络设备-硬件运行状态巡检命令行输出
146 #-------------------------------------------------------------------------
147 #my $device_version = catch_cmd( $hardware->{"device_version"}, $config );
148 my $device_module = catch_cmd( $hardware->{"module_status"}, $config );
149 my $device_power = catch_cmd( $hardware->{"power_status"}, $config );
150 my $device_disk = catch_cmd( $hardware->{"disk_status"}, $config );
151 my $device_fan = catch_cmd( $hardware->{"fan_status"}, $config );
152 my $device_alarm = catch_cmd( $hardware->{"alarm_status"}, $config );
153 my $device_temperature
154 = catch_cmd( $hardware->{"temperature_status"}, $config );
155  
156 #-------------------------------------------------------------------------
157 # 网络设备-关键运行指标巡检命令行输出
158 #-------------------------------------------------------------------------
159 my $device_cpu = catch_cmd( $status->{"cpu_status"}, $config );
160 my $device_memory = catch_cmd( $status->{"memory_status"}, $config );
161 my $device_ntp = catch_cmd( $status->{"ntp_status"}, $config );
162 my $device_vrrp = catch_cmd( $status->{"vrrp_status"}, $config );
163 my $device_eth_trunk
164 = catch_cmd( $status->{"eth_trunk_status"}, $config );
165 my $device_err_down = catch_cmd( $status->{"err_down_status"}, $config );
166  
167 #-------------------------------------------------------------------------
168 # 网络设备-硬件运行状态巡检判断
169 #-------------------------------------------------------------------------
170 #my $device_info = device_version($device_version);
171 my $module_status = module_status($device_module);
172 my $power_status = power_status($device_power);
173 my $disk_status = disk_status($device_disk);
174 my $fan_status = fan_status($device_fan);
175 my $alarm_status = alarm_status($device_alarm);
176 my $temperature_status = temperature_status($device_temperature);
177  
178 #-------------------------------------------------------------------------
179 # 网络设备-关键运行指标巡检判断
180 #-------------------------------------------------------------------------
181 my $cpu_status = cpu_status($device_cpu);
182 my $memory_status = memory_status($device_memory);
183 my $ntp_status = ntp_status($device_ntp);
184 my $vrrp_status = vrrp_status($device_vrrp);
185 my $eth_trunk_status = eth_trunk_status($device_eth_trunk);
186 my $err_down_status = err_down_status($device_err_down);
187  
188 #-------------------------------------------------------------------------
189 # 网络设备-设备版本型号信息 --检查项1 共计1项
190 #-------------------------------------------------------------------------
191 #$result->{"version"} = $device_info;
192  
193 #-------------------------------------------------------------------------
194 # 网络设备-硬件运行状态巡检结果 --检查项2 共计6项
195 #-------------------------------------------------------------------------
196 $result->{"hardware"}{"device_module"} = $module_status;
197 $result->{"hardware"}{"device_power"} = $power_status;
198 $result->{"hardware"}{"device_fan"} = $fan_status;
199 $result->{"hardware"}{"device_disk"} = $disk_status;
200 $result->{"hardware"}{"device_alarm"} = $alarm_status;
201 $result->{"hardware"}{"device_temperature"} = $temperature_status;
202  
203 #-------------------------------------------------------------------------
204 # 网络设备-关键运行指标巡检结果 --检查项3 共计6项
205 #-------------------------------------------------------------------------
206 $result->{"status"}{"device_cpu"} = $cpu_status;
207 $result->{"status"}{"device_memory"} = $memory_status;
208 $result->{"status"}{"device_ntp"} = $ntp_status;
209 $result->{"status"}{"device_vrrp"} = $vrrp_status;
210 $result->{"status"}{"device_eth_trunk"} = $eth_trunk_status;
211 $result->{"status"}{"device_err_down"} = $err_down_status;
212  
213 #-------------------------------------------------------------------------
214 # 输出巡检结果
215 #-------------------------------------------------------------------------
216 return $result;
217 }
218  
219 sub calculate_score {
220  
221 #接收health_check计算结果
222 my $rev = shift;
223  
224 #设备硬件巡检哈希
225 my $hardware = $rev->{"hardware"};
226  
227 #设备关键指标巡检哈希
228 my $status = $rev->{"status"};
229  
230 #设备巡检项总数
231 my $attr_count
232 = ( scalar( keys %{$hardware} ) ) + ( scalar( keys %{$status} ) );
233  
234 #硬件巡检通过项计数
235 my $hardware_attr = 0;
236 foreach ( keys %{$hardware} ) {
237 my $ret = $hardware->{$_};
238 my $health = $ret->{"health"};
239 if ( defined $health && $health eq "pass" ) {
240 ++$hardware_attr;
241 }
242 else {
243 my $alarm = $ret->{"alarm"};
244 if ( defined $alarm && $alarm eq "info" ) {
245 $hardware_attr -= 0.1;
246 }
247 elsif ( defined $alarm && $alarm eq "alert" ) {
248 $hardware_attr -= 0.2;
249 }
250 elsif ( defined $alarm && $alarm eq "critic" ) {
251 $hardware_attr -= 0.7;
252 }
253 }
254 }
255  
256 #关键指标巡检通过项计数
257 my $status_attr = 0;
258 foreach ( keys %{$status} ) {
259 my $ret = $status->{$_};
260 my $health = $ret->{"health"};
261 if ( defined $health && $health eq "pass" ) {
262 ++$status_attr;
263 }
264 else {
265 my $alarm = $ret->{"alarm"};
266 if ( defined $alarm && $alarm eq "info" ) {
267 $status_attr -= 0.1;
268 }
269 elsif ( defined $alarm && $alarm eq "alert" ) {
270 $status_attr -= 0.2;
271 }
272 elsif ( defined $alarm && $alarm eq "critic" ) {
273 $status_attr -= 0.7;
274 }
275 }
276 }
277  
278 #计算巡检得分情况
279 my $score = ( ( $hardware_attr + $status_attr ) / $attr_count ) * 100;
280  
281 #返回巡检得分项
282 return $score;
283 }
284  
285 #-------------------------------------------------------------------------
286 # 网络设备-硬件运行状态巡检 -- 设备版本信息校验
287 #-------------------------------------------------------------------------
288 sub device_version {
289  
290 #接收巡检命令行输出
291 my $rev = shift;
292  
293 #如果没匹配到巡检输出直接返回
294 return unless $rev;
295  
296 #设备版本标量
297 my $version;
298  
299 #遍历巡检结果以捕捉异常模块信息
300 foreach ( @{$rev} ) {
301 if (/Version\s(.*?)\s(.*)/) {
302  
303 #将巡检结果命令行分割为字符串数组
304 my $version = $2;
305  
306 #将非必要字符串()处理掉
307 $version =~ s/\(//;
308 $version =~ s/\)//;
309  
310 #捕捉到关键字即跳出
311 last;
312 }
313 }
314  
315 #返回巡检结果
316 return $version;
317 }
318  
319 #-------------------------------------------------------------------------
320 # 网络设备-硬件运行状态巡检 -- 设备模块检查
321 #-------------------------------------------------------------------------
322 sub module_status {
323  
324 #接收巡检命令行输出
325 my $rev = shift;
326  
327 #如果没匹配到巡检输出直接返回
328 return unless $rev;
329  
330 #模块巡检标量
331 my $result;
332  
333 #初始化巡检结果告警项
334 $result->{"alarm"} = undef;
335 $result->{"health"} = "pass";
336 $result->{"log"} = undef;
337  
338 #重组巡检结果拼接为字符串
339 my $origin = join( "\n", @{$rev} );
340 $result->{"origin"} = $origin;
341  
342 #快速检查是否有未注册的模块
343 my $ret = ( grep {/Unregistered/i} @{$rev} ) ? 1 : 0;
344  
345 #设备健康检查
346 return $result unless $ret;
347  
348 my @msg;
349  
350 #遍历巡检结果以捕捉异常模块信息
351 foreach ( @{$rev} ) {
352 if (/Unregistered/i) {
353  
354 #将巡检结果命令行写入数组中
355 push @msg, $_;
356 }
357 }
358  
359 if (@msg) {
360  
361 #将异常信息写入日志中
362 $result->{"log"} = "设备板卡告警:\n" . join( "\n", @msg );
363  
364 #设置告警等级
365 $result->{"alarm"} = "critic";
366  
367 #监控检查状态
368 $result->{"health"} = "fail";
369 }
370  
371 #返回巡检结果
372 return $result;
373 }
374  
375 #-------------------------------------------------------------------------
376 # 网络设备-硬件运行状态巡检 -- 电源模块检查
377 #-------------------------------------------------------------------------
378 sub power_status {
379  
380 #接收巡检命令行输出
381 my $rev = shift;
382  
383 #如果没匹配到巡检输出直接返回
384 return unless $rev;
385  
386 #模块巡检标量
387 my $result;
388  
389 #初始化巡检结果告警项
390 $result->{"alarm"} = undef;
391 $result->{"health"} = "pass";
392 $result->{"log"} = undef;
393  
394 #重组巡检结果拼接为字符串
395 my $origin = join( "\n", @{$rev} );
396 $result->{"origin"} = $origin;
397  
398 #快速检查是否有未注册的模块
399 my $ret = ( grep {/NotSupply/i} @{$rev} ) ? 1 : 0;
400  
401 #设备健康检查
402 return $result unless $ret;
403  
404 my @msg;
405  
406 #遍历巡检结果以捕捉异常模块信息
407 foreach ( @{$rev} ) {
408 if (/NotSupply/i) {
409  
410 #将巡检结果命令行写入数组中
411 push @msg, $_;
412 }
413 }
414  
415 if (@msg) {
416  
417 #将异常信息写入日志中
418 $result->{"log"} = "设备电源告警:\n" . join( "\n", @msg );
419  
420 #设置告警等级
421 $result->{"alarm"} = "critic";
422  
423 #监控检查状态
424 $result->{"health"} = "fail";
425 }
426  
427 #返回巡检结果
428 return $result;
429 }
430  
431 #-------------------------------------------------------------------------
432 # 网络设备-硬件运行状态巡检 -- 风扇模块检查
433 #-------------------------------------------------------------------------
434 sub fan_status {
435  
436 #接收巡检命令行输出
437 my $rev = shift;
438  
439 #如果没匹配到巡检输出直接返回
440 return unless $rev;
441  
442 #模块巡检标量
443 my $result;
444  
445 #初始化巡检结果告警项
446 $result->{"alarm"} = undef;
447 $result->{"health"} = "pass";
448 $result->{"log"} = undef;
449  
450 #重组巡检结果拼接为字符串
451 my $origin = join( "\n", @{$rev} );
452 $result->{"origin"} = $origin;
453  
454 #快速检查是否有未注册的模块
455 my $ret = ( grep {/Abnormal/i} @{$rev} ) ? 1 : 0;
456  
457 #设备健康检查
458 return $result unless $ret;
459  
460 my @msg;
461  
462 #遍历巡检结果以捕捉异常模块信息
463 foreach ( @{$rev} ) {
464 if (/Abnormal/i) {
465  
466 #将巡检结果命令行写入数组中
467 push @msg, $_;
468 }
469 }
470  
471 if (@msg) {
472  
473 #将异常信息写入日志中
474 $result->{"log"} = "设备风扇告警:\n" . join( "\n", @msg );
475  
476 #设置告警等级
477 $result->{"alarm"} = "critic";
478  
479 #监控检查状态
480 $result->{"health"} = "fail";
481 }
482  
483 #返回巡检结果
484 return $result;
485 }
486  
487 #-------------------------------------------------------------------------
488 # 网络设备-硬件运行状态巡检 -- 设备硬件告警检查
489 #-------------------------------------------------------------------------
490 sub alarm_status {
491  
492 #接收巡检命令行输出
493 my $rev = shift;
494  
495 #如果没匹配到巡检输出直接返回
496 return unless $rev;
497  
498 #模块巡检标量
499 my $result;
500  
501 #初始化巡检结果告警项
502 $result->{"alarm"} = undef;
503 $result->{"health"} = "pass";
504 $result->{"log"} = undef;
505  
506 #重组巡检结果拼接为字符串
507 my $origin = join( "\n", @{$rev} );
508 $result->{"origin"} = $origin;
509  
510 #快速检查是否有未注册的模块
511 my $ret = ( grep {/(critic|alert)/i} @{$rev} ) ? 1 : 0;
512  
513 #设备健康检查
514 return $result unless $ret;
515  
516 my @msg;
517  
518 #遍历巡检结果以捕捉异常模块信息
519 foreach ( @{$rev} ) {
520 if (/(?<alarm>(critic|alert))/i) {
521  
522 #将巡检结果命令行写入数组中
523 push @msg, $_;
524 }
525 }
526  
527 if (@msg) {
528  
529 #将异常信息写入日志中
530 $result->{"log"} = "设备硬件告警:\n" . join( "\n", @msg );
531  
532 #设置告警等级
533 $result->{"alarm"} = "critic";
534  
535 #监控检查状态
536 $result->{"health"} = "fail";
537 }
538  
539 #返回巡检结果
540 return $result;
541 }
542  
543 #-------------------------------------------------------------------------
544 # 网络设备-硬件运行状态巡检 -- 硬件温度告警检查
545 #-------------------------------------------------------------------------
546 sub temperature_status {
547  
548 #接收巡检命令行输出
549 my $rev = shift;
550  
551 #如果没匹配到巡检输出直接返回
552 return unless $rev;
553  
554 #模块巡检标量
555 my $result;
556  
557 #初始化巡检结果告警项
558 $result->{"alarm"} = undef;
559 $result->{"health"} = "pass";
560 $result->{"log"} = undef;
561  
562 #重组巡检结果拼接为字符串
563 my $origin = join( "\n", @{$rev} );
564 $result->{"origin"} = $origin;
565  
566 #快速检查是否有未注册的模块
567 my $ret = ( grep {/Abnormal/i} @{$rev} ) ? 1 : 0;
568  
569 #设备健康检查
570 return $result unless $ret;
571  
572 my @msg;
573  
574 #遍历巡检结果以捕捉异常模块信息
575 foreach ( @{$rev} ) {
576 if (/Abnormal/i) {
577  
578 #将巡检结果命令行写入数组中
579 push @msg, $_;
580 }
581 }
582  
583 if (@msg) {
584  
585 #将异常信息写入日志中
586 $result->{"log"} = "设备温度告警:\n" . join( "\n", @msg );
587  
588 #设置告警等级
589 $result->{"alarm"} = "critic";
590  
591 #监控检查状态
592 $result->{"health"} = "fail";
593 }
594  
595 #返回巡检结果
596 return $result;
597 }
598  
599 #-------------------------------------------------------------------------
600 # 网络设备-硬件运行状态巡检 -- 硬件存储告警检查
601 #-------------------------------------------------------------------------
602 sub disk_status {
603  
604 #接收巡检命令行输出
605 my $rev = shift;
606  
607 #如果没匹配到巡检输出直接返回
608 return unless $rev;
609  
610 #模块巡检标量
611 my $result;
612  
613 #初始化巡检结果告警项
614 $result->{"alarm"} = undef;
615 $result->{"health"} = "pass";
616 $result->{"log"} = undef;
617  
618 #重组巡检结果拼接为字符串
619 my $origin = join( "\n", @{$rev} );
620 $result->{"origin"} = $origin;
621  
622 #初始化硬盘巡检状态
623 my $status = 0;
624 my $storage;
625  
626 #遍历巡检结果以捕捉异常模块信息,仅关注最新的告警信息
627 foreach ( @{$rev} ) {
628  
629 #如果命中磁盘关键字将状态指为1
630 if (/System Disk Usage Information/i) {
631 $status = 1;
632 push @{$storage}, $_;
633 }
634  
635 #如果命中了存储关键字继续
636 elsif ($status) {
637  
638 #写入后续巡检结果
639 push @{$storage}, $_;
640 }
641  
642 #检查是否命中主机名
643 elsif (/^(\<.*\>|\[.*\])/i) {
644  
645 #将巡检输出置0,结束命令行捕捉
646 $status = 0;
647 last;
648 }
649 }
650  
651 foreach ( @{$storage} ) {
652 if (/\s+(\d+)\%\s+/i) {
653  
654 #写入异常状态
655 $result->{"log"} = "磁盘空间利用率告警:\n" . "$1\%"
656 if ( $1 > $disk_threshold );
657 $result->{"alarm"} = "info";
658  
659 #监控检查状态
660 $result->{"health"} = "fail" if ( $1 > $disk_threshold );
661 }
662 }
663  
664 #返回巡检结果
665 return $result;
666 }
667  
668 #-------------------------------------------------------------------------
669 # 网络设备-硬件运行状态巡检 -- CPU利用率检查
670 #-------------------------------------------------------------------------
671 sub cpu_status {
672  
673 #接收巡检命令行输出
674 my $rev = shift;
675  
676 #如果没匹配到巡检输出直接返回
677 return unless $rev;
678  
679 #模块巡检标量
680 my $result;
681  
682 #初始化巡检结果告警项
683 $result->{"alarm"} = undef;
684 $result->{"health"} = "pass";
685 $result->{"log"} = undef;
686  
687 #重组巡检结果拼接为字符串
688 my $origin = join( "\n", @{$rev} );
689 $result->{"origin"} = $origin;
690  
691 #遍历巡检结果以捕捉异常模块信息,仅关注最新的告警信息
692 foreach ( @{$rev} ) {
693 if (/CPU Using Percentage/i) {
694  
695 #将巡检结果命令行分割为字符串数组
696 my @info = map {s/(^\s+|\s+$)//r} split( /\:/, $_ );
697  
698 #将CPU运行利用率取出
699 my $usage = $info[1];
700  
701 #处理利用率指标前后空白字符串
702 $usage =~ s/(^\s+|\s+$)//;
703  
704 #去除%
705 $usage =~ s/\%//;
706  
707 #写入异常状态
708 $result->{"log"}
709 = "CPU利用率超出当前阈值: " . "$usage\%"
710 if $usage >= $cpu_threshold;
711  
712 #设置越线惩罚机制
713 if ( 60 <= $usage && $usage < 70 ) {
714 $result->{"alarm"} = "info";
715 }
716 elsif ( 70 <= $usage && $usage < 80 ) {
717 $result->{"alarm"} = "alert";
718 }
719 elsif ( $usage >= 80 ) {
720 $result->{"alarm"} = "critic";
721 }
722  
723 #监控检查状态
724 $result->{"health"} = "fail" if ( $usage >= $cpu_threshold );
725 }
726  
727 #------------------------------------------------------
728 # 检查逻辑待优化
729 #------------------------------------------------------
730 elsif (/Max CPU Usage \:/i) {
731  
732 #将巡检结果命令行分割为字符串数组
733 my @info = map {s/(^\s+|\s+$)//r} split( /\:/, $_ );
734  
735 #将历史CPU MAX 运行利用率取出
736 my $max_usage = $info[1];
737  
738 #处理利用率指标前后空白字符串
739 $max_usage =~ s/(^\s+|\s+$)//;
740  
741 #去除%
742 $max_usage =~ s/\%//;
743  
744 #写入异常状态
745 $result->{"log"}
746 = "CPU历史最大利用率超出阈值:" . "$max_usage\%"
747 if $max_usage > $cpu_max_threshold;
748  
749 $result->{"alarm"} = "critic" if $max_usage > $cpu_max_threshold;
750  
751 #监控检查状态
752 $result->{"health"} = "fail" if $max_usage > $cpu_max_threshold;
753 last;
754 }
755 }
756  
757 #返回巡检结果
758 return $result;
759 }
760  
761 #-------------------------------------------------------------------------
762 # 网络设备-硬件运行状态巡检 -- 内存利用率检查
763 #-------------------------------------------------------------------------
764 sub memory_status {
765  
766 #接收巡检命令行输出
767 my $rev = shift;
768  
769 #如果没匹配到巡检输出直接返回
770 return unless $rev;
771  
772 #模块巡检标量
773 my $result;
774  
775 #初始化巡检结果告警项
776 $result->{"alarm"} = undef;
777 $result->{"health"} = "pass";
778 $result->{"log"} = undef;
779  
780 #重组巡检结果拼接为字符串
781 my $origin = join( "\n", @{$rev} );
782 $result->{"origin"} = $origin;
783  
784 #设置缺省检查结果为PASS
785  
786 #遍历巡检结果以捕捉异常模块信息,仅关注最新的告警信息
787 foreach ( @{$rev} ) {
788 if (/Memory Using Percentage/i) {
789  
790 #将巡检结果命令行分割为字符串数组
791 my @info = map {s/(^\s+|\s+$)//r} split( /\:/, $_ );
792  
793 #取出内存利用率
794 my $usage = $info[1];
795  
796 #处理利用率指标前后空白字符串
797 $usage =~ s/(^\s+|\s+$)//;
798  
799 #去除%字符串
800 $usage =~ s/\%//;
801  
802 #写入异常状态
803 $result->{"log"}
804 = "内存利用率超出当前阈值:" . "$usage\%"
805 if ( $usage > $memory_threshold );
806  
807 #设置越线惩罚机制
808 if ( 60 <= $usage && $usage < 70 ) {
809 $result->{"alarm"} = "info";
810 }
811 elsif ( 70 <= $usage && $usage < 80 ) {
812 $result->{"alarm"} = "alert";
813 }
814 elsif ( $usage >= 80 ) {
815 $result->{"alarm"} = "critic";
816 }
817  
818 #监控检查状态
819 $result->{"health"} = "fail" if ( $usage >= $memory_threshold );
820 }
821 }
822  
823 #返回巡检结果
824 return $result;
825 }
826  
827 #-------------------------------------------------------------------------
828 # 网络设备-硬件运行状态巡检 -- NTP同步状态检查
829 #-------------------------------------------------------------------------
830 sub ntp_status {
831  
832 #接收巡检命令行输出
833 my $rev = shift;
834  
835 #如果没匹配到巡检输出直接返回
836 return unless $rev;
837  
838 #模块巡检标量
839 my $result;
840  
841 #初始化巡检结果告警项
842 $result->{"alarm"} = undef;
843 $result->{"health"} = "pass";
844 $result->{"log"} = undef;
845  
846 #重组巡检结果拼接为字符串
847 my $origin = join( "\n", @{$rev} );
848 $result->{"origin"} = $origin;
849  
850 #遍历巡检结果以捕捉异常模块信息,仅关注最新的告警信息
851 foreach ( @{$rev} ) {
852 if (/clock status/i) {
853  
854 #将巡检结果命令行分割为字符串数组
855 my @info = map {s/(^\s+|\s+$)//r} split( /\:/, $_ );
856  
857 #取出内存利用率
858 my $status = $info[1];
859  
860 #处理利用率指标前后空白字符串
861 $status =~ s/(^\s+|\s+$)//;
862  
863 #写入异常状态
864 $result->{"log"} = "NTP时钟源未同步\n"
865 if ( $status eq "unsynchronized" );
866 $result->{"alarm"} = "info";
867  
868 #监控检查状态
869 $result->{"health"} = "fail" if ( $status eq "unsynchronized" );
870 }
871 }
872  
873 #返回巡检结果
874 return $result;
875 }
876  
877 #-------------------------------------------------------------------------
878 # 网络设备-硬件运行状态巡检 -- VRRP状态检查
879 #-------------------------------------------------------------------------
880 sub vrrp_status {
881  
882 #接收巡检命令行输出
883 my $rev = shift;
884  
885 #如果没匹配到巡检输出直接返回
886 return unless $rev;
887  
888 #模块巡检标量
889 my $result;
890  
891 #初始化巡检结果告警项
892 $result->{"alarm"} = undef;
893 $result->{"health"} = "pass";
894 $result->{"log"} = undef;
895  
896 #重组巡检结果拼接为字符串
897 my $origin = join( "\n", @{$rev} );
898 $result->{"origin"} = $origin;
899  
900 #快速检查是否有未注册的模块
901 my $ret = ( grep {/Info: The VRRP does not exist/i} @{$rev} ) ? 1 : 0;
902  
903 #巡检结果命令行输出不存在则跳过
904 return $result if $ret;
905  
906 my @msg;
907  
908 #遍历巡检结果以捕捉异常模块信息,仅关注最新的告警信息
909 foreach ( @{$rev} ) {
910 if (/Initialize/i) {
911 push @msg, $_;
912 }
913 }
914  
915 #写入异常状态信息
916 if (@msg) {
917  
918 #添加异常信息到日志
919 $result->{"log"}
920 = "检测到VRRP工作异常:\n" . join( "\n", @msg );
921  
922 #设置告警等级
923 $result->{"alarm"} = "critic";
924  
925 #写入后续巡检结果
926 $result->{"health"} = "fail";
927 }
928  
929 #返回巡检结果
930 return $result;
931 }
932  
933 #-------------------------------------------------------------------------
934 # 网络设备-硬件运行状态巡检 -- ETH-TRUNK状态检查
935 #-------------------------------------------------------------------------
936 sub eth_trunk_status {
937  
938 #接收巡检命令行输出
939 my $rev = shift;
940  
941 #如果没匹配到巡检输出直接返回
942 return unless $rev;
943  
944 #模块巡检标量
945 my $result;
946  
947 #初始化巡检结果告警项
948 $result->{"alarm"} = undef;
949 $result->{"health"} = "pass";
950 $result->{"log"} = undef;
951  
952 #重组巡检结果拼接为字符串
953 my $origin = join( "\n", @{$rev} );
954 $result->{"origin"} = $origin;
955  
956 #快速检查是否有未注册的模块
957 my $ret = ( grep {/Unselect/i} @{$rev} ) ? 1 : 0;
958  
959 #巡检结果命令行输出不存在则跳过
960 return $result unless $ret;
961  
962 my @msg;
963  
964 #遍历巡检结果以捕捉异常模块信息,仅关注最新的告警信息
965 foreach ( @{$rev} ) {
966  
967 #匹配异常状态信息
968 if (/Unselect/i) {
969  
970 #将巡检结果命令行写入数组中
971 push @msg, $_;
972 }
973 }
974  
975 if (@msg) {
976  
977 #写入异常状态信息
978 $result->{"log"} = "ETH-TRUNK接口异常:\n" . join( "\n", @msg );
979  
980 #设置告警等级
981 $result->{"alarm"} = "critic";
982  
983 #写入后续巡检结果
984 $result->{"health"} = "fail";
985 }
986  
987 #返回巡检结果
988 return $result;
989 }
990  
991 #-------------------------------------------------------------------------
992 # 网络设备-硬件运行状态巡检 -- ERR-DOWN状态检查
993 #-------------------------------------------------------------------------
994 sub err_down_status {
995  
996 #接收巡检命令行输出
997 my $rev = shift;
998  
999 #如果没匹配到巡检输出直接返回
1000 return unless $rev;
1001  
1002 #模块巡检标量
1003 my $result;
1004  
1005 #初始化巡检结果告警项
1006 $result->{"alarm"} = undef;
1007 $result->{"health"} = "pass";
1008 $result->{"log"} = undef;
1009  
1010 #重组巡检结果拼接为字符串
1011 my $origin = join( "\n", @{$rev} );
1012 $result->{"origin"} = $origin;
1013  
1014 #快速检查是否有未注册的模块
1015 my $ret
1016 = ( grep {/Info: No error-down interface exists/i} @{$rev} ) ? 1 : 0;
1017  
1018 #巡检结果命令行输出不存在则跳过
1019 return $result if $ret;
1020  
1021 my @msg;
1022  
1023 #遍历巡检结果以捕捉异常模块信息,仅关注最新的告警信息
1024 foreach ( @{$rev} ) {
1025 if (/(efm-threshold-event|efm-remote-failure|bpdu-protection|link-flap|storm-control|mac-address-flapping)/i
1026 )
1027 {
1028 #将命中行写入数组中
1029 push @msg, $_;
1030 }
1031 }
1032 if (@msg) {
1033  
1034 #写入异常状态
1035 $result->{"log"} = "接口 error-down:\n" . join( "\n", @msg );
1036  
1037 #设置告警等级
1038 $result->{"alarm"} = "alert";
1039  
1040 #监控检查状态
1041 $result->{"health"} = "fail";
1042 }
1043  
1044 #返回巡检结果
1045 return $result;
1046 }
1047  
1048 1;
1049  
↑ Summary