Difference between revisions of "Net-SNMP subagent"
AlexKuklin (talk | contribs) (New page: Here is snmp subagent to export UBC via SNMP protocol. Author - n1ckname === /etc/snmp/snmpagent.pl === #!/usr/bin/perl $program = $0; use NetSNMP::OID (':all'); use Net...) |
(+source tag, License) |
||
Line 1: | Line 1: | ||
Here is snmp subagent to export UBC via SNMP protocol. | Here is snmp subagent to export UBC via SNMP protocol. | ||
− | Author - n1ckname | + | Author - n1ckname. License: GNU GPL v2. |
=== /etc/snmp/snmpagent.pl === | === /etc/snmp/snmpagent.pl === | ||
− | + | <source lang="perl"> | |
− | + | #!/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 | |
+ | </source> | ||
=== /etc/snmp/snmpd.conf === | === /etc/snmp/snmpd.conf === | ||
Revision as of 17:33, 6 March 2009
Here is snmp subagent to export UBC via SNMP protocol.
Author - n1ckname. License: GNU GPL v2.
/etc/snmp/snmpagent.pl
#!/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
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";