Perl 板


LINE

※ 引述《wnick (浅龙勿用)》之铭言: : 我用PREL写了一支无限回圈的程式 : 用SNMP去取得一些SWITCH的资讯 : 但是程式会常常当掉 : 有什麽要注意的吗? : 不知道需不需要附上程式码? #! /usr/bin/perl -w eval '(exit $?0)' && eval 'exec /usr/bin/perl $0 ${1+"$@"}' && eval 'exec /usr/bin/perl $0 $argv:q' if 0; use strict; use DBI; use Net::SNMP qw(snmp_dispatcher oid_lex_sort); my $dbh = DBI->connect("dbi:mysql:database=dorm;host=*.*.*.*", "guest", "guest") or die $DBI::errstr; #my @HOST_OID = qw(1.3.6.1.2.1.2.2.1.10.4227626 1.3.6.1.2.1.2.2.1.10.4227634 1.3.6.1.2.1.2.2.1.10.4227642 1.3.6.1.2.1.2.2.1.10.4227650 1.3.6.1.2.1.2.2.1.10.4227658 1.3.6.1.2.1.2.2.1.10.4227666 1.3.6.1.2.1.2.2.1.10.4227674 1.3.6.1.2.1.2.2.1.10.4227682 1.3.6.1.2.1.2.2.1.10.4227690 1.3.6.1.2.1.2.2.1.10.4227698 1.3.6.1.2.1.2.2.1.10.4227706 1.3.6.1.2.1.2.2.1.10.4227714 1.3.6.1.2.1.2.2.1.10.4227722 1.3.6.1.2.1.2.2.1.10.4227730 1.3.6.1.2.1.2.2.1.10.4227738 1.3.6.1.2.1.2.2.1.10.4227746 1.3.6.1.2.1.2.2.1.10.4227754 1.3.6.1.2.1.2.2.1.10.4227762 1.3.6.1.2.1.2.2.1.10.4227770 1.3.6.1.2.1.2.2.1.10.4227778 1.3.6.1.2.1.2.2.1.10.4227786 1.3.6.1.2.1.2.2.1.10.4227794 1.3.6.1.2.1.2.2.1.10.4227802 1.3.6.1.2.1.2.2.1.10.4227810 1.3.6.1.2.1.2.2.1.10.4228041 1.3.6.1.2.1.2.2.1.10.4228049 1.3.6.1.2.1.2.2.1.10.4228057 1.3.6.1.2.1.2.2.1.10.4228065); my @OID = qw(4227626 4227634 4227642 4227650 4227658 4227666 4227674 4227682 4227690 4227698 4227706 4227714 4227722 4227730 4227738 4227746 4227754 4227762 4227770 4227778 4227786 4227794 4227802 4227810 4228041 4228049 4228057 4228065); my @HOSTS = qw(10.0.236.115 10.0.236.116 10.0.236.117 10.0.236.118 10.0.236.245 10.0.236.246 10.0.236.247 10.0.236.248 10.0.237.115 10.0.237.116 10.0.237.117 10.0.237.118 10.0.237.245 10.0.237.246 10.0.237.247 10.0.237.248 10.0.238.115 10.0.238.116 10.0.238.117 10.0.238.118 10.0.238.245 10.0.238.246 10.0.238.247 10.0.238.248 10.0.239.115 10.0.239.116 10.0.239.117 10.0.239.118 10.0.239.245 10.0.239.246 10.0.239.247 10.0.239.248 10.0.240.115 10.0.240.116 10.0.240.117 10.0.240.118 10.0.240.119 10.0.240.245 10.0.240.246 10.0.240.247 10.0.240.248 10.0.241.115 10.0.241.116 10.0.241.117 10.0.241.118 10.0.241.245 10.0.241.246 10.0.241.247 10.0.241.248 10.0.242.115 10.0.242.116 10.0.242.117 10.0.242.118 10.0.242.245 10.0.242.246 10.0.242.247 10.0.242.248 10.0.245.115 10.0.245.116 10.0.246.115 10.0.246.116 10.0.246.117 10.0.246.118 10.0.246.119 10.0.246.120 10.0.246.121 10.0.246.245 10.0.246.246 10.0.246.247 10.0.246.248 10.0.246.249 10.0.247.115 10.0.247.116 10.0.247.117 10.0.247.118 10.0.247.119 10.0.247.120 10.0.247.245 10.0.247.246 10.0.247.247 10.0.247.248 10.0.247.249 10.0.247.250 10.0.248.115 10.0.248.116 10.0.248.117 10.0.248.118 10.0.248.119 10.0.248.120 10.0.248.121 10.0.249.115 10.0.249.116 10.0.249.117 10.0.249.118 10.0.249.119 10.0.249.245 10.0.249.246 10.0.249.247 10.0.249.248); my $ifOutTable = '1.3.6.1.2.1.31.1.1.1.10'; my $ifInTable = '1.3.6.1.2.1.31.1.1.1.6'; my $result_in; my $result_out; my %host_time; my $sth_update; my $value_in; my $value_out; my $du_time; my $b_time; my $nowtime; my $error_time; my $host; my $conter_max = 18446744073709551615; my $b_value; my $n_value; my $dutime; my $avg; while(1){ $nowtime = time; foreach $host (@HOSTS){ #printf("======= %s =======", $host); # Create the SNMP session my ($session, $error) = Net::SNMP->session( -hostname => $host, -community => 'public', -port => 161, -version => 'snmpv2c' ); # Was the session created? if (!defined($session)) { $error_time = localtime; printf("ERROR: %s. TIME:%s\n", $error, $error_time); #exit 1; } if (defined($result_in = $session->get_table(-baseoid => $ifInTable)) && defined($result_out = $session->get_table(-baseoid => $ifOutTable))) { $nowtime = time; if (exists $host_time{$host}) { $b_time = $host_time{$host}; $du_time = ($nowtime - $b_time); } else{ $du_time = 0; } $host_time{$host} = $nowtime; my $sth = $dbh->prepare("SELECT s_port, in_octets, in_max, out_octets, out_max FROM dorm_flow WHERE s_ip = '$host'"); $sth->execute(); while (my @db_result = $sth->fetchrow_array) { my $get_in = '1.3.6.1.2.1.31.1.1.1.6.' . $db_result[0]; my $get_out = '1.3.6.1.2.1.31.1.1.1.10.' . $db_result[0]; $value_in = $result_in->{$get_in}; $value_out = $result_out->{$get_out}; if($du_time == 0){ $sth_update = $dbh->prepare("UPDATE dorm_flow SET in_octets = '$value_in', in_avg = 0, out_octets = '$value_out', out_avg = 0 WHERE s_ip = '$host' AND s_port = '$db_result[0]'"); $sth_update->execute(); } else{ my $in_avg = &avg($db_result[1], $value_in, $du_time); my $out_avg = &avg($db_result[3], $value_out, $du_time); #update to DB if ($in_avg > $db_result[2] && $out_avg > $db_result[4]){ $sth_update = $dbh->prepare("UPDATE dorm_flow SET in_octets = '$value_in', in_avg = '$in_avg', in_max = '$in_avg', out_octets = '$value_out', out_avg = '$out_avg', out_max = '$out_avg' WHERE s_ip = '$host' AND s_port = '$db_result[0]'"); $sth_update->execute(); } elsif ($in_avg > $db_result[2]){ $sth_update = $dbh->prepare("UPDATE dorm_flow SET in_octets = '$value_in', in_avg = '$in_avg', in_max = '$in_avg', out_octets = '$value_out', out_avg = '$out_avg' WHERE s_ip = '$host' AND s_port = '$db_result[0]'"); $sth_update->execute(); } elsif ($out_avg > $db_result[4]){ $sth_update = $dbh->prepare("UPDATE dorm_flow SET in_octets = '$value_in', in_avg = '$in_avg', out_octets = '$value_out', out_avg = '$out_avg', out_max = '$out_avg' WHERE s_ip = '$host' AND s_port = '$db_result[0]'"); $sth_update->execute(); } else{ $sth_update = $dbh->prepare("UPDATE dorm_flow SET in_octets = '$value_in', in_avg = '$in_avg', out_octets = '$value_out', out_avg = '$out_avg' WHERE s_ip = '$host' AND s_port = '$db_result[0]'"); $sth_update->execute(); } } } } else{ $error_time = localtime; printf("ERROR: %s. TIME:%s\n\n", $session->error(), $error_time); } $session->close; } sleep(264); } sub avg{ if ($_[1] < $_[0]){ $avg = (($_[1] + ($conter_max - $_[0]))/$_[2]); } else{ $avg = (($_[1] - $_[0])/$_[2]); } } __END__ 这样大家看得懂吗>\\\< --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.134.21.191
1F:推 appleboy46:你可以先贴到这里吗 http://src.wtgstudio.com/ 10/02 23:05
2F:→ appleboy46:然後把link给我们 10/02 23:05







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:BabyMother站内搜寻

TOP