s3b0 has asked for the wisdom of the Perl Monks concerning the following question:

Welcome great Perl Monks,

I've got scrip (below) and I don't know how write querry (hashed part) for these vriables:
$ifType, $ifSpeed, $sysUpTime, $ifOperStatus , $ifLastChange
which must be printed in the HTML part (in the end) under picture and not necessarily be saved in file.

This is all I can see under the picure:
IP: 83.11.32.57 SysUpTime: .1.3.6.1.2.1.1.3.0 Status: .1.3.6.1.2.1.2.2.1.8.131076 Type: .1.3.6.1.2.1.2.2.1.3.131076 Speed: .1.3.6.1.2.1.2.2.1.5.131076 Last Change: .1.3.6.1.2.1.2.2.1.9.131076
#!/usr/bin/env perl use strict; use CGI qw(:standard); use Net::SNMP; use Data::Dumper; use GD::Graph::lines; use POSIX qw(strftime); my $community = 'public'; # Spolecznosc my $port = 161; # Port, na ktorym dzia&#322;a my $version = 2; # Wersja SNMP my $max_probes = '20'; # Ile maksymalnie probek my $prefix = "."; #aktualny kata +log w ktorym jest skrypt open (HOST, "< $prefix/hosts.txt") || die 'Cannot open file hosts.txt' +; # Otwarcie pliku hosts.txt z listą hostow do odczytu my @HOSTS = <HOST>; # Wciagnij plik do zmiennej HOS +TS close (HOST); # Zamknij plik my $sysOutPckt = '.1.3.6.1.2.1.2.2.1.16.131076'; my $sysInPckt = '.1.3.6.1.2.1.2.2.1.10.131076'; #my $sysOutPckt = '.1.3.6.1.2.1.2.2.1.16.1'; #my $sysInPckt = '.1.3.6.1.2.1.2.2.1.10.1'; my $ifType ='.1.3.6.1.2.1.2.2.1.3.131076'; my $ifSpeed ='.1.3.6.1.2.1.2.2.1.5.131076'; my $sysUpTime ='.1.3.6.1.2.1.1.3.0'; my $ifOperStatus ='.1.3.6.1.2.1.2.2.1.8.131076'; my $ifLastChange = '.1.3.6.1.2.1.2.2.1.9.131076'; my $IP = remote_host(); my $now = time(); # czas w unixtime foreach my $host (@HOSTS) { chomp $host; # obetnij koniec linii my (@captions, $to_write, @data, @data1, @data2, @data_for_graph); + # zmienne dla obrazka open DANE, ">> $prefix/$host.txt" || die 'Cannot open host file'; + # Otwarcie pliku X.X.X.X z danymi do wykresu dla ka&#380;dego adr +esu z pliku hosts.txt my ($session, $error) = Net::SNMP->session( -hostname => $host, -community => $community, -port => $port, ); if (!defined($session)) { printf("ERROR1: %s", $error); exit 1; } my $response = $session->get_request( -varbindlist => [$sysInPckt] ); if (!defined($response)) { print("ERROR2: ", $session->error()); $session->close(); exit 1; } $to_write = $response->{$sysInPckt}; my $response = $session->get_request( -varbindlist => [$sysOutPckt] ); if (!defined($response)) { print("ERROR2: ", $session->error()); $session->close(); exit 1; } $to_write = $to_write." ".$response->{$sysOutPckt}; #----------------------Other info------------------------------------- +---- # # my $response = $session->get_request(-varbindlist => [$sysUpTi +me]); # # if (!defined($response)) { # print("ERROR2: ", $session->error()); # $session->close(); # exit 1; # } # $to_write = $to_write." ".$response->{$sysUpTime}; #-------- # my $response = $session->get_request(-varbindlist => [$ifOperStat +us]); # # print("ERROR2: ", $session->error()); # $session->close(); # exit 1; # } # $to_write = $to_write." ".$response->{$ifOperStatus}; #--------- # my $response = $session->get_request(-varbindlist => [$ifLastChan +ge]); # # if (!defined($response)) { # $session->close(); # exit 1; # } # $to_write = $to_write." ".$response->{$ifLastChange}; #--------------------------------------------------------------- $session->close(); print DANE "$now $to_write\n"; close DANE; open DANE, "< $prefix/$host.txt"; open DANETMP, "> $prefix/$host.tmp"; my @dane = <DANE>; close DANE; my $count = 1; my @danetmp = (); for ($count=1; $count <= $max_probes; $count++) { push (@danetmp, pop(@dane)); if ((scalar @dane) == 0) { last } ; } @danetmp = sort {$a <=> $b} @danetmp; foreach my $danetmp (@danetmp) { print DANETMP $danetmp; } close DANETMP; rename "$prefix/$host.tmp", "$prefix/$host.txt"; foreach my $dana (@danetmp) { chomp $dana; my ($date, $what1, $what2) = split / /, $dana; push(@captions, strftime "%m-%d %H:%M", localtime($date)); push(@data1, $what1); push(@data2, $what2); } push @data, \@data1; push @data, \@data2; push @data_for_graph, \@captions; push @data_for_graph, @data; my $graph = GD::Graph::lines->new(1000, 300); $graph->set( dclrs => [ qw(black lblue blue ) ], x_label_skip => 2 +); my $gd = $graph->plot(\@data_for_graph) or die $graph->error; my $filename = "$prefix/$host.png"; open(IMG, ">$filename") or die $!; binmode IMG; print IMG $gd->png; close IMG; } #------------------HTML Part-------------------- print "Content-type: text/html\n\n"; print"<html> <head> <title>Traffic Monitor</title> </head> <body>"; open (HOST, "< hosts.txt") || die "Cannot open file"; my @HOSTS = <HOST>; close (HOST); foreach my $host (@HOSTS) { chomp $host; print "<br /><br /><br />\n"; print "<img src='C:/EasyPHP2/cgi-bin/$host.png' alt='$host.png'></ +img>"; print "<br /><br /><br />\n"; print "IP: $IP<br />\n";; print "SysUpTime: $sysUpTime<br />\n"; print "Status: $ifOperStatus<br />\n"; print "Type: $ifType<br />\n"; print "Speed: $ifSpeed<br />\n"; print "Last Change: $ifLastChange<br />\n"; } print"</body></html>";

Replies are listed 'Best First'.
Re: !!!Help need!!! Howto write qerry (SNMP session)
by Anonymous Monk on Sep 26, 2007 at 00:10 UTC
    #3