Difference between revisions of "Net-SNMP subagent"

From OpenVZ Virtuozzo Containers Wiki
Jump to: navigation, search
(+source tag, License)
(categorized)
 
(One intermediate revision by the same user not shown)
Line 7: Line 7:
  
 
<source lang="perl">
 
<source lang="perl">
#!/usr/bin/perl  
+
#!/usr/bin/perl
  
 
$program = $0;
 
$program = $0;
Line 22: Line 22:
 
@catIndex{@catName} = (0..$#catName);
 
@catIndex{@catName} = (0..$#catName);
  
sub handler {  
+
sub handler {
     my ($handler, $reg_info, $request_info, $requests) = @_;  
+
     my ($handler, $reg_info, $request_info, $requests) = @_;
 
     open(UB,"<$source");
 
     open(UB,"<$source");
 
     while(<UB>){
 
     while(<UB>){
        my %vmachine;
+
my %vmachine;
        if ( /\D*(\d+):[\s\t]+([a-z]+)[\t\s]+(\d+)[\t\s]+(\d+)[\t\s]+(\d+)[\t\s]+(\d+)[\t\s]+(\d+).*/ ){  
+
if ( /\D*(\d+):[\s\t]+([a-z]+)[\t\s]+(\d+)[\t\s]+(\d+)[\t\s]+(\d+)[\t\s]+(\d+)[\t\s]+(\d+).*/ ){
    $vid=$1;  
+
    $vid=$1;
    $beancounters{$vid}=\%vmachine ;  
+
    $beancounters{$vid}=\%vmachine ;
            ${beancounters{$vid}{0}}="www";
+
    ${beancounters{$vid}{0}}="www";
 
    open(CONF,"</etc/vz/conf/$vid.conf");
 
    open(CONF,"</etc/vz/conf/$vid.conf");
 
    my $name=$vid;
 
    my $name=$vid;
Line 41: Line 41:
 
    }
 
    }
 
    ${beancounters{$vid}{0}}=$name;
 
    ${beancounters{$vid}{0}}=$name;
            $resource=$2 ;
+
    $resource=$2 ;
            $held=$3 ;
+
    $held=$3 ;
            $maxheld=$4 ;
+
    $maxheld=$4 ;
            $barrier=$5 ;
+
    $barrier=$5 ;
            $limit=$6 ;
+
    $limit=$6 ;
            $failcnt=$7 ;
+
    $failcnt=$7 ;
            ${beancounters{$vid}}{${resIndex{$resource}}}=[$held , $maxheld , $barrier , $limit ,$failcnt ];
+
    ${beancounters{$vid}}{${resIndex{$resource}}}=[$held , $maxheld , $barrier , $limit ,$failcnt ];
  
 
}
 
}
        if ( /^[\W\d]+([a-z]+)[\t\s]+(\d+)[\t\s]+(\d+)[\t\s]+(\d+)[\t\s]+(\d+)[\t\s]+(\d+).*/ ) {
+
if ( /^[\W\d]+([a-z]+)[\t\s]+(\d+)[\t\s]+(\d+)[\t\s]+(\d+)[\t\s]+(\d+)[\t\s]+(\d+).*/ ) {
                $resource=$1 ;
+
$resource=$1 ;
                $held=$2 ;
+
$held=$2 ;
                $maxheld=$3 ;
+
$maxheld=$3 ;
                $barrier=$4 ;
+
$barrier=$4 ;
                $limit=$5 ;
+
$limit=$5 ;
                $failcnt=$6 ;
+
$failcnt=$6 ;
                ${beancounters{$vid}}{${resIndex{$resource}}}=[$held , $maxheld , $barrier , $limit ,$failcnt ];
+
${beancounters{$vid}}{${resIndex{$resource}}}=[$held , $maxheld , $barrier , $limit ,$failcnt ];
        }
+
}
 
     }
 
     }
 
     close(UB);
 
     close(UB);
  
 
     for ( my $request = $requests; $request; $request = $request->next() ) {
 
     for ( my $request = $requests; $request; $request = $request->next() ) {
   
+
my $reqOID = $request->getOID();
        my $reqOID = $request->getOID();
+
my $reqMode = $request_info->getMode();
        my $reqMode = $request_info->getMode();
 
 
my $myOID = new NetSNMP::OID($OID);
 
my $myOID = new NetSNMP::OID($OID);
 
#1.3.6.1.4.1.2021.51.ctcount = 5
 
#1.3.6.1.4.1.2021.51.ctcount = 5
Line 75: Line 74:
 
# $request->setValue(ASN_INTEGER, scalar keys (%beancounters));
 
# $request->setValue(ASN_INTEGER, scalar keys (%beancounters));
  
        if ($reqMode == MODE_GET) {
+
if ($reqMode == MODE_GET) {
 
    print STDERR $reqOID." MODE_GET\n" if($debug);;
 
    print STDERR $reqOID." MODE_GET\n" if($debug);;
        if($reqOID == $myOID+".0"){
+
    if($reqOID == $myOID+".0"){
 
$request->setValue(ASN_INTEGER, scalar keys (%beancounters));
 
$request->setValue(ASN_INTEGER, scalar keys (%beancounters));
 
    } elsif($reqOID =~ /$myOID\.([0-9]+)[\.]?([0-9]+)?[\.]?([0-9]+)?[\.]?([0-9]+)?/) {
 
    } elsif($reqOID =~ /$myOID\.([0-9]+)[\.]?([0-9]+)?[\.]?([0-9]+)?[\.]?([0-9]+)?/) {
Line 86: Line 85:
 
my $catReq=$4;
 
my $catReq=$4;
 
my %vz;
 
my %vz;
 
  
 
print STDERR "Type:$typeReq, VzID: $vzidReq, Res: $resReq/$resName[$resReq], Cat: $catReq/$catName[$catReq]\n" if($debug);;
 
print STDERR "Type:$typeReq, VzID: $vzidReq, Res: $resReq/$resName[$resReq], Cat: $catReq/$catName[$catReq]\n" if($debug);;
Line 134: Line 132:
 
    }
 
    }
 
}
 
}
   
 
 
 
    }
 
    }
  
 
} elsif ($reqMode == MODE_GETNEXT) {
 
} elsif ($reqMode == MODE_GETNEXT) {
 
    print STDERR $reqOID." MODE_GETNEXT\n" if($debug);
 
    print STDERR $reqOID." MODE_GETNEXT\n" if($debug);
        if($reqOID < $myOID+".0"){
+
    if($reqOID < $myOID+".0"){
    $request->setOID($OID.".0");
+
$request->setOID($OID.".0");
 
$request->setValue(ASN_INTEGER, scalar keys (%beancounters));
 
$request->setValue(ASN_INTEGER, scalar keys (%beancounters));
 
    } elsif($reqOID =~ /$myOID\.([0-9]+)[\.]?([0-9]+)?[\.]?([0-9]+)?[\.]?([0-9]+)?$/) {
 
    } elsif($reqOID =~ /$myOID\.([0-9]+)[\.]?([0-9]+)?[\.]?([0-9]+)?[\.]?([0-9]+)?$/) {
Line 149: Line 145:
 
my $catReq=$4;
 
my $catReq=$4;
 
my %vz;
 
my %vz;
+
 
 
print STDERR "Type:$typeReq, VzID: $vzidReq, Res: $resReq/$resName[$resReq], Cat: $catReq/$catName[$catReq]\n" if($debug);;
 
print STDERR "Type:$typeReq, VzID: $vzidReq, Res: $resReq/$resName[$resReq], Cat: $catReq/$catName[$catReq]\n" if($debug);;
  
Line 167: Line 163:
  
 
if(!defined($resReq) && defined(${vz{0}})) {
 
if(!defined($resReq) && defined(${vz{0}})) {
        $request->setOID($myOID.".1.".$vzidReq.".0");
+
    $request->setOID($myOID.".1.".$vzidReq.".0");
 
    $request->setValue(ASN_OCTET_STR, "".${vz{0}});
 
    $request->setValue(ASN_OCTET_STR, "".${vz{0}});
 
} elsif(!defined($catReq) && defined(${vz{$resReq}[0]})) {
 
} elsif(!defined($catReq) && defined(${vz{$resReq}[0]})) {
        $request->setOID($myOID.".1.".$vzidReq.".".$resReq.".0");
+
    $request->setOID($myOID.".1.".$vzidReq.".".$resReq.".0");
 
    $request->setValue(ASN_INTEGER, ${vz{$resReq}[0]});
 
    $request->setValue(ASN_INTEGER, ${vz{$resReq}[0]});
 
} elsif($catReq< $#catName && defined(${vz{$resReq}[$catReq]})) {
 
} elsif($catReq< $#catName && defined(${vz{$resReq}[$catReq]})) {
Line 202: Line 198:
 
}
 
}
 
    }
 
    }
        }
+
}
 
     }
 
     }
}  
+
}
  
 
{
 
{
        my $regoid = new NetSNMP::OID($OID);  
+
my $regoid = new NetSNMP::OID($OID);
        $agent->register($program, $regoid, \&handler);
+
$agent->register($program, $regoid, \&handler);
 
}
 
}
 
 
  
 
#eof // nick
 
#eof // nick
Line 224: Line 218:
 
   perl $source = '/proc/user_beancounters';  
 
   perl $source = '/proc/user_beancounters';  
 
   perl do "/etc/snmp/snmpagent.pl";
 
   perl do "/etc/snmp/snmpagent.pl";
 +
 +
[[Category: Monitoring]]
 +
[[Category: HOWTO]]
 +
[[Category: UBC]]

Latest revision as of 18:07, 6 March 2009

Here is snmp subagent to export UBC via SNMP protocol.

Author - n1ckname. License: GNU GPL v2.


/etc/snmp/snmpagent.pl[edit]

#!/usr/bin/perl

$program = $0;

use NetSNMP::OID (':all');
use NetSNMP::agent (':all');
use NetSNMP::ASN (':all');
use Data::Dumper;
my $debug = 0;
my @resName=qw(name vmguarpages tcpsndbuf othersockbuf physpages numfile numtcpsock numpty numsiginfo tcprcvbuf dcachesize numiptent numothersock privvmpages kmemsize numflock shmpages lockedpages dummy numproc oomguarpages dgramrcvbuf);
my @catName=qw(held maxheld barrier limit failcnt);
my %resIndex,%catIndex;
@resIndex{@resName} = (0..$#resName);
@catIndex{@catName} = (0..$#catName);

sub handler {
    my ($handler, $reg_info, $request_info, $requests) = @_;
    open(UB,"<$source");
    while(<UB>){
	my %vmachine;
	if ( /\D*(\d+):[\s\t]+([a-z]+)[\t\s]+(\d+)[\t\s]+(\d+)[\t\s]+(\d+)[\t\s]+(\d+)[\t\s]+(\d+).*/ ){
	    $vid=$1;
	    $beancounters{$vid}=\%vmachine ;
	    ${beancounters{$vid}{0}}="www";
	    open(CONF,"</etc/vz/conf/$vid.conf");
	    my $name=$vid;
	    while(<CONF>) {
		if(/NAME[\s\t]?=[\s\t]?["]?([^"]+)["]?/)  {
		    $name=$1;
		    last;
		    #"
		}
	    }
	    ${beancounters{$vid}{0}}=$name;
	    $resource=$2 ;
	    $held=$3 ;
	    $maxheld=$4 ;
	    $barrier=$5 ;
	    $limit=$6 ;
	    $failcnt=$7 ;
	    ${beancounters{$vid}}{${resIndex{$resource}}}=[$held , $maxheld , $barrier , $limit ,$failcnt ];

	}
	if ( /^[\W\d]+([a-z]+)[\t\s]+(\d+)[\t\s]+(\d+)[\t\s]+(\d+)[\t\s]+(\d+)[\t\s]+(\d+).*/ ) {
		$resource=$1 ;
		$held=$2 ;
		$maxheld=$3 ;
		$barrier=$4 ;
		$limit=$5 ;
		$failcnt=$6 ;
		${beancounters{$vid}}{${resIndex{$resource}}}=[$held , $maxheld , $barrier , $limit ,$failcnt ];
	}
    }
    close(UB);

    for ( my $request = $requests; $request; $request = $request->next() ) {
	my $reqOID = $request->getOID();
	my $reqMode = $request_info->getMode();
	my $myOID = new NetSNMP::OID($OID);
#1.3.6.1.4.1.2021.51.ctcount = 5
#1.3.6.1.4.1.2021.51.ct.101.name = www
#1.3.6.1.4.1.2021.51.ct.101.kmemsize.held = 458584
#1.3.6.1.4.1.2021.51.ct.101.kmemsize.maxheld = 1490364
#1.3.6.1.4.1.2021.51.ct.101.kmemsize.barrier = 11055923

#		$request->setValue(ASN_INTEGER, scalar keys (%beancounters));

	if ($reqMode == MODE_GET) {
	    print STDERR $reqOID." MODE_GET\n" if($debug);;
	    if($reqOID == $myOID+".0"){
		$request->setValue(ASN_INTEGER, scalar keys (%beancounters));
	    } elsif($reqOID =~ /$myOID\.([0-9]+)[\.]?([0-9]+)?[\.]?([0-9]+)?[\.]?([0-9]+)?/) {

		my $typeReq=$1;
		my $vzidReq=$2;
		my $resReq=$3;
		my $catReq=$4;
		my %vz;

		print STDERR "Type:$typeReq, VzID: $vzidReq, Res: $resReq/$resName[$resReq], Cat: $catReq/$catName[$catReq]\n" if($debug);;

		if(!$vzidReq) {
		    foreach $vzid (sort keys %beancounters) {
			$vzidReq=$vzid;
			last;
		    }
		}
		if(!$vzidReq || !exists(${beancounters{$vzidReq}})) {
		    $request->setError($request_info,SNMP_ERR_NOSUCHNAME);
		    last;
		}

		%vz=%{${beancounters{$vzidReq}}};

		if((!defined($resReq) or $resReq == 0) && defined(${vz{0}})) {
		    $request->setValue(ASN_OCTET_STR, "".${vz{0}});
		} elsif(!defined($catReq) && defined(${vz{$resReq}[0]})) {
		    $request->setValue(ASN_INTEGER, ${vz{$resReq}[0]});

		} elsif($catReq< $#catName && defined(${vz{$resReq}[$catReq]})) {
		    if(9223372036854775807 == ${vz{$resReq}[$catReq+1]}) {
			$request->setValue(ASN_INTEGER,-1);
		    } else {
			$request->setValue(ASN_INTEGER,${vz{$resReq}[$catReq+1]});
		    }
		} elsif($resReq<scalar keys %vz && defined(${vz{$resReq+1}[0]})) {
		    if(9223372036854775807 == ${vz{$resReq}[$catReq+1]}) {
			$request->setValue(ASN_INTEGER,-1);
		    } else {
			$request->setValue(ASN_INTEGER, ${vz{$resReq+1}[0]});
		    }
		} else  {
		    my $finish=1;
		    foreach $vzid (sort keys %beancounters) {
			if($vzid > $vzidReq) {
			    %vz=%{${beancounters{$vzid}}};
			    $vzidReq=$vzid;
			    $finish=0;
			    last;
			}
		    }
		    if(!$finish)  {
			$request->setValue(ASN_INTEGER, "".${vz{0}});
		    }
		}
	    }

	} elsif ($reqMode == MODE_GETNEXT) {
	    print STDERR $reqOID." MODE_GETNEXT\n" if($debug);
	    if($reqOID < $myOID+".0"){
		$request->setOID($OID.".0");
		$request->setValue(ASN_INTEGER, scalar keys (%beancounters));
	    } elsif($reqOID =~ /$myOID\.([0-9]+)[\.]?([0-9]+)?[\.]?([0-9]+)?[\.]?([0-9]+)?$/) {
		my $typeReq=$1;
		my $vzidReq=$2;
		my $resReq=$3;
		my $catReq=$4;
		my %vz;

		print STDERR "Type:$typeReq, VzID: $vzidReq, Res: $resReq/$resName[$resReq], Cat: $catReq/$catName[$catReq]\n" if($debug);;

		if(!$vzidReq) {
		    foreach $vzid (sort keys %beancounters) {
			$vzidReq=$vzid;
			last;
		    }
		}
		if(!$vzidReq || !exists(${beancounters{$vzidReq}})) {
		    $request->setError($request_info,SNMP_ERR_NOSUCHNAME);
		    last;
		}


		%vz=%{${beancounters{$vzidReq}}};

		if(!defined($resReq) && defined(${vz{0}})) {
		    $request->setOID($myOID.".1.".$vzidReq.".0");
		    $request->setValue(ASN_OCTET_STR, "".${vz{0}});
		} elsif(!defined($catReq) && defined(${vz{$resReq}[0]})) {
		    $request->setOID($myOID.".1.".$vzidReq.".".$resReq.".0");
		    $request->setValue(ASN_INTEGER, ${vz{$resReq}[0]});
		} elsif($catReq< $#catName && defined(${vz{$resReq}[$catReq]})) {
		    $request->setOID($myOID.".1.".$vzidReq.".".$resReq.".".($catReq+1));
		    if(9223372036854775807 == ${vz{$resReq}[$catReq+1]}) {
			$request->setValue(ASN_INTEGER,-1);
		    } else {
			$request->setValue(ASN_INTEGER,${vz{$resReq}[$catReq+1]});
		    }
		} elsif($resReq<scalar keys %vz && defined(${vz{$resReq+1}[0]})) {
		    $request->setOID($myOID.".1.".$vzidReq.".".($resReq+1).".0");
		    if(9223372036854775807 == ${vz{$resReq}[$catReq+1]}) {
			$request->setValue(ASN_INTEGER,-1);
		    } else {
			$request->setValue(ASN_INTEGER, ${vz{$resReq+1}[0]});
		    }
		} else  {
		    my $finish=1;
		    foreach $vzid (sort keys %beancounters) {
			if($vzid > $vzidReq) {
			    %vz=%{${beancounters{$vzid}}};
			    $vzidReq=$vzid;
			    $finish=0;
			    last;
			}
		    }
		    if(!$finish)  {
			$request->setOID($myOID.".1.".$vzidReq.".0.0");
			$request->setValue(ASN_OCTET_STR, ${vz{0}});
		    }
		}
	    }
	}
    }
}

{
	my $regoid = new NetSNMP::OID($OID);
	$agent->register($program, $regoid, \&handler);
}

#eof // nick

/etc/snmp/snmpd.conf[edit]

 rouser my_username priv
 createUser my_username MD5 my_password DES
 perl $OID = '.1.3.6.1.4.1.2021.51'; 
 perl $debugging = '1';
 perl $verbose = '1';
 perl $source = '/proc/user_beancounters'; 
 perl do "/etc/snmp/snmpagent.pl";