iptables_acc_snmp
上传用户:shbosideng
上传日期:2013-05-04
资源大小:1555k
文件大小:3k
- #!/usr/bin/perl
- my ($req,$place,$returnOID,$mode, $IPTABLES,@counters);
- $IPTABLES = "iptables"; # cmdline to iptables
- $place=".1.3.6.1.4.1.2055";
- @counters = loadChains();
- main();
- sub main {
- my ($mode,$req);
- if (scalar(@ARGV) == 2) {
- ($mode,$req) = @ARGV;
- } else {
- ($req) = @ARGV;
- }
-
- SWITCH: for ($mode) {
- /-s/ && exit;
- /-n/ && do {
- SWITCH: for ($req) {
- /$place$/ && do {
- $returnOID = "$place.1";
- last SWITCH;
- };
- /$place.1$/ && do {
- $returnOID = "$place.2.1.1.1";
- last SWITCH;
- };
- /$place.2$/ && do {
- $returnOID = "$place.2.1.1.1";
- last SWITCH;
- };
- /$place.2.1$/ && do {
- $returnOID = "$place.2.1.1.1";
- last SWITCH;
- };
- /$place.2.1.1$/ && do {
- $returnOID = "$place.2.1.1.1";
- last SWITCH;
- };
- /$place.2.1.1.(.*)$/ && do {
- if ($1 < scalar(@counters)-1) {
- $returnOID = "$place.2.1.1." . ($1 + 1);
- } else {
- $returnOID = "$place.2.1.2.1";
- }
- last SWITCH;
- };
- /$place.2.1.2.(.*)$/ && do {
- if ($1 < scalar(@counters)-1) {
- $returnOID = "$place.2.1.2." . ($1 + 1);
- } else {
- exit;
- }
- last SWITCH;
- };
- exit;
- }
- last SWITCH;
- };
- SWITCH: for ($req) {
- /$place$/ && exit;
- /$place.2.1$/ && exit;
- /$place.2.1.1$/ && exit;
- /$place.2.1.2$/ && exit;
- $returnOID = $req;
- }
- }
- print "$returnOIDn";
- $_ = $returnOID;
- SWITCH: {
- /^$place.1$/ && do {
- print "stringn", getHostname() ;
- last SWITCH;
- };
- /^$place.2.1.(1|2).(.*)$/ && do {
- ($a, $b) = split ("=", @counters[$2]);
- if ($1 == 1) {
- print "stringn$an";
- } else {
- print "countern$bn";
- }
- last SWITCH;
- };
- /^$place.2.1.1.3.1.1.(1|2|3)$/ && do {
- print "integern$1n";
- last SWITCH;
- };
- /^$place.2.1.1.2.(1|2|3)$/ && do {
- SWITCH: for ($1) {
- /1/ && do {
- print "stringnfiltern";
- last SWITCH
- };
- /2/ && do {
- print "stringnnatn";
- last SWITCH
- };
- /3/ && do {
- print "stringnmanglen";
- last SWITCH
- };
- }
- last SWITCH;
- };
-
- print "stringnack... $returnOID $reqn";
- }
- }
- sub getHostname {
- return `/bin/hostname --fqdn`;
- }
- sub getChainCount {
- my ($table);
- $table = shift();
- return scalar(getChainNames($table));
- }
- sub getChainNames {
- my (@bufLines,$table,@chains);
- $table = shift();
- @bufLines = `$IPTABLES -t $table -nL`;
- foreach (@bufLines) {
- if (/^Chain (.*) ((.*)$/) {
- push (@chains, $1);
- }
- }
- return @chains;
- }
- sub getChainCounter {
- my (@rules, @count, $value);
- $table = shift();
- $chain = shift();
- @rules=`$IPTABLES -t $table -vnxL $chain`;
- splice @rules, 0, 2;
- $total=0;
- foreach $value (@rules) {
- @count=split(' ',$value);
- $total += $count[1];
- }
- return $total;
- }
- sub getChains {
- my ($table, @result, @names, $counter, $name,@buf);
- $table = shift();
- @chains = getChainNames($table);
- foreach $name (@chains) {
- $counter = getChainCounter ($table, $name);
- push (@result, "$table/$name=$counter");
- }
- return @result;
- }
- sub loadChains {
- my (@result, @buf, $table);
- foreach $table ("filter","nat","mangle") {
- @buf = getChains ($table);
- push (@result, @buf);
- }
- return @result;
- }