Started a few months ago to do some cacti related work, using cacti to monitor some important network devices and servers. Cacti is based on the SNMP protocol, which is used to poll for information about the monitored device (Traffic server resource usage, etc.). But sometimes there are devices that cause a false positive as SNMP down because the polling response time is long or is not polled at the specified time, and this is not actually the case. In addition to adjusting some of the parameter settings of the cacti itself, you can use Perl scripts to monitor the network connectivity of these devices in real time
The code is as follows:
#!usr/bin/perl -wuse net::P ing; #因为是一直检测所以条件一直为真while (1) {$result = '; # Used to record the results of the test #iphost.txt file saves the device IP that needs to be detected, reads the device's IP line-by-row, calls the test function to test, logs the returned results down to open (F, "Iphost.txt");while ($temp =<f >) { $result = $result. &ping_test ($temp);} Close (F); #完成测试, the output test results and the completion time print $result. " ";p rint &get_time." test finish "." \ n "; #将每次测试结果覆盖写入state. txt file open (FP," +<state.txt ");p rint fp $result; close (FP); Sleep (30);} #连通性测试函数, using Net::p ing module sub ping_test{my ($host) [email protected]_; $count =0; #用来记录ping不通的次数 $pingtool = Net::P ing->new ("ICMP"), #每个设备的IP测试五次, if the Ping does not work, $count self-increment for ($j =0; $j <5; $j + +) { if (! $pingtool->ping ($host)) { $count + +;} } #根据 the value of the $count to determine whether the connectivity test, 1 for passing the test, 0 for the failed, 5 ping tests are not considered as the device connectivity test does not pass if ($count ==5) { $state = 0;} else { $state =1; ,} $pingtool->close ();return $state; # $stateThe test results are recorded and returned} #获取当前时间的函数sub get_time{my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $ISDST) = LocalTime ();my $date = ($mon +1 . ' /' . $mday);my $time = ($hour. ":". $min);return $date. " ". $time;}
The device IP to be tested and the test results are saved in the Iphost.txt file and the State.txt file respectively, so that when the new device needs to be monitored, modify the iphost.txt file, and then through PHP to display the test results on the web side, and periodically refresh, PHP code as follows
In fact, the test code is very simple, the results of this test can be and cacti a piece to determine whether the device is working properly, greatly improve the accuracy of monitoring. In addition, in the actual use of the process, you can also include in the perl script mail alarm and SMS platform alarm. Having problems in your work should be positive to improve your thinking.
This article is from the "never-say-nothing" blog, please be sure to keep this source http://yahuinm.blog.51cto.com/9686320/1689780
PELR Scripts monitor the connectivity of critical network devices and servers in real time