RE: Printing for debugging
Thanks! Using the Apache2::RequestUtil and Apache2::Log, I can find out what my variables' values are to debug and check my code. Mitch Mitchell Raful MCSE CCNP MCB Quantico Personal and Family Readiness (MRI) 3044 Catlin Avenue Quantico, VA 22134 Com: 703-784-5991 DSN: 278-5991 Cell: 804-363-0731 -Original Message- From: Colin Wetherbee [mailto:[EMAIL PROTECTED] Sent: Monday, January 14, 2008 11:08 AM To: Raful Mr Mitchell H Cc: Modperl (E-mail) Subject: Re: Printing for debugging Raful Mr Mitchell H wrote: > I have my code pasted below. My module, loaded with > directive in Apache2, works well. However, sometimes switches don't > respond correctly to bulk queries of fdbPorts object. What I want to do > is to be able print the value of my variables either to the browser or a > log file. A regular print statement does not work for this. The > relevant variables are in a subroutine called from my sub handler. I'm not entirely sure where, in those lines of unindented HTML, you're trying to perform your logging. The only thing I could find quickly was the following code, but I can't be certain that's the subject of your question. if( $arp_session->{ErrorNum} ){ print "Got $arp_session->{ErrorStr} for $router\n"; } In order to print log messages to Apache's (or your virtual host's) error log, you can use the following. use Apache2::RequestUtil; use Apache2::Log; Apache2::RequestUtil->request->log->error('some_text'); Substituting other log levels (debug, info, warn, etc.) works, as well. See the following URL for more information. http://perl.apache.org/docs/2.0/api/Apache2/Log.html#LogLevel_Methods If this isn't what you're seeking, perhaps you could include a more concise test case and a more elaborate explanation in your next post? Colin
Printing for debugging
I have my code pasted below. My module, loaded with directive in Apache2, works well. However, sometimes switches don't respond correctly to bulk queries of fdbPorts object. What I want to do is to be able print the value of my variables either to the browser or a log file. A regular print statement does not work for this. The relevant variables are in a subroutine called from my sub handler. Thanks in advance. #!/usr/bin/perl -w package Handlers::PortMapper; use warnings; use strict; use HTML::Template; use DBI; use Socket qw(:DEFAULT :crlf); use dec_hex; use snmpSession; use SNMP; use Net::NBName; use Net::Ping; use Apache2::Request (); use Apache2::Const -compile=>'OK'; # %seen_vlans --tracks those vlans which are seen as each ports vlan membership is looked at # %device --all results for each device found on a switch are placed here for dispatching to a web page # @answer; my @switch_row; #--hold results of db query for switch my @router_row; #--hold results of db query for router whose arp cache you want my %devices_ref; my %hash; my $time; # --gets time query began my $chosen_switch; #--switch passed via http form data my $chosen_router; #--switch passed via http form data my $switch_ip; #--result one for initial query my $switch_model; #--result two for initial query my $devices_ref; my $router_ip; # --result for query of router address my $dbh; #--database handle my $sql; #--database query my @loop_data; #--hold hashes for my template # $i = 0 --counters # $j = 0 --counters sub handler { my $r = shift; my $req = Apache2::Request->new( $r ); $r->content_type('text/html'); my %mac_data; my @mac_array; #hold individual hex digits returned by ipNetToMediaPhysAddress my @loop_data = (); #array of anonymous hashes which is dispatched to web page for display my $template = HTML::Template->new( filename => '/home/mitch/www-dev/mod_perl/templates/portmapper.tmpl', debug => 1, die_on_bad_params => 0 ); my $time = localtime(); $chosen_switch = $req->param('chosen_switch'); $chosen_switch =~ s/(\s.*)//g; $chosen_router = $req->param( 'chosen_router' ); $dbh = DBI->connect( 'dbi:ODBC:MRIServer2k', 'mriinventory', '[EMAIL PROTECTED]', { PrintError => 1, RaiseError => 0, AutoCommit => 1 } ); $sql = "SELECT address, chassis_description FROM tblLiveCisco WHERE name='$chosen_switch'"; @switch_row = $dbh->selectrow_array( $sql ); $switch_ip = $switch_row[0]; $switch_model = $switch_row[1]; @router_row = $dbh->selectrow_array("SELECT address FROM tblLiveCisco WHERE name='$chosen_router'" ); $router_ip = $router_row[0]; $devices_ref = getDevices( $router_ip, $switch_ip, 'public'); foreach $_ (@{$devices_ref}) { %hash = %$_; push @loop_data, \%hash ; } $template->param(CHOSEN_SWITCH => $chosen_switch ); $template->param(MODEL => $switch_model ); $template->param(TIME => $time ); $template->param(LOOP_DATA => [EMAIL PROTECTED] ); print "Content-Type: text/html\n\n"; print $template->output; return Apache2::Const::OK; } sub getDevices { my $router = shift; my $switch = shift; my $community = shift; my @mac_array = (); my %seen_vlans = (); my %ifnames; #holds the ifIndex number and names of ports my %trunk; #holds all ports on a switch that are trunking my %arpCache; my @data; my @vlans; #all vlans found using vmVlan object my $arp_mib = 'ipNetToMediaPhysAddress'; my ( $mac_address,$ip, $dns_name, $device_type ); my $arp_session = snmpSession::openSession( $router , $community, 1 ); my $vb_arp = new SNMP::Varbind( [$arp_mib] ); for(my $var = $arp_session->getnext( $vb_arp ); ( $vb_arp->tag eq $arp_mib ) and not ( $arp_session->{ErrorNum} ); $var = $arp_session->getnext( $vb_arp ) ) { my $ip_addr = ( $vb_arp->tag . $vb_arp->iid ); $ip_addr =~ s/$arp_mib\d+\.//g; my @vars = split(/:/, $var ); for( my $x = 0; $x < scalar( @vars ); $x++ ) { if( length( $vars[$x] ) < 2 ) { $vars[$x] = "0"."$vars[$x]"; } push( @mac_array, $vars[$x] ); } my $arp_mac_address = join( "", @mac_array ); push( @{$arpCache{$arp_mac_address}}, $ip_addr ); @mac_array = (); } if( $arp_session->{ErrorNum} ){ print "Got $arp_session->{ErrorStr} for $router\n"; } my $vlan_session = snmpSession::openSession ( $switch, $community, 1 ); my $vb_vlan = new SNMP::VarList( ['vmVlan'], ['ifName'], ['vlanTrunkPortDynamicStatus'] ); my @answers1 = $vlan_session->bulkwalk(0, 100, $vb_vlan ); if( $vlan_session->{ErrorNum} ) { print "Got ", $vlan_session->{ErrorStr}, "on", $vlan_session->{DestHost}, " during Bulkwalk\n"; } my $vlan_answer = $answers1[0]; my $ifName_answer = $answers1[1]; my $trunk_ports = $answers1[2]; for( my $i = 0; $i < scalar(@$vlan_answer); $i++ ) { my $vlan = $vlan_answer->[$i]->[ 2]; if ( !exists ($seen_vlans{$vlan} ) ) { push( @vlans, $vlan ); $seen_vlans{$vlan} = 1; } } for( my $j = 0; $j < scalar(@$ifName_answer); $j++ ) { my $index = $ifName_answer->[$j]->[1]; my $interface_name = $ifName_answ
RE: segfault
Here is the complete script #!/usr/bin/perl package Handlers::Devices; use DBI; use HTML::Template; use Apache2::RequestRec (); use APR::Request::Param (); use Apache2::Const -compile => 'OK'; sub handler { my $r = shift; my $req = $r->param("chosen_base"); my $template = HTML::Template->new( filename => '/home/mitch/www-dev/mod_perl/templates/devices.tmpl' ); my $switch; my $base_abbreviation; my @routers; # Old way of doing things # # read( STDIN, my $buffer, $ENV{'CONTENT_LENGTH'} ); # @pairs = split /=/, $buffer; # $value = $pairs[1]; # if( $value =~ m/\+/ ){; #$value =~ s/\+/ /g; # } # my $chosen_base = $value; my @loop_data = (); $template->param(BASE => $chosen_base); $template->param(DATA => $req ); SWITCH: { if( $chosen_base =~ /Kan/ ) { $base_abbreviation = 'kba'; last SWITCH;} if( $chosen_base =~ /29/ ) { $base_abbreviation = 't'; last SWITCH;} if( $chosen_base =~ /New/ ) { $base_abbreviation = 'nri'; last SWITCH;} if( $chosen_base =~ /San/ ) { $base_abbreviation = 'sdi'; last SWITCH;} if( $chosen_base =~ /Che/ ) { $base_abbreviation = 'cpo'; last SWITCH;} if( $chosen_base =~ /Par/ ) { $base_abbreviation = 'pai'; last SWITCH;} $base_abbreviation = substr( $chosen_base, 0,3 ); } my $dbh = DBI->connect( 'dbi:ODBC:MRIServer2k', 'mriinventory', '[EMAIL PROTECTED]', { PrintError => 1, RaiseError => 0, AutoCommit => 1 } ); my $sql = "select name, location from tblLiveCisco where( name LIKE '$base_abbreviation%' )"; my $sth = $dbh->prepare( $sql ); $sth->execute(); my $rows; push @{$rows}, $_ while $_ = $sth->fetchrow_hashref(); $template->param( ROWS => $rows); foreach $row( @$rows ) { my %router; if ( ($row->{name} =~ /rtr/) || ($row->{name} =~ /dsw/) || ($row->{name} =~ /csw/) || ($row->{name} =~ /coresw/) ) { $router{ROUTER} = $row->{name}; push ( @routers, \%router ); } } $template->param( ROUTERS_LOOP => [EMAIL PROTECTED]); print "Content-Type: text/html\n\n"; print $template->output; return Apache2::Const::OK; } 1; Mitchell Raful MCSE CCNP MCB Quantico Personal and Family Readiness (MRI) 3044 Catlin Avenue Quantico, VA 22134 Com: 703-784-5991 DSN: 278-5991 Cell: 804-363-0731 -Original Message- From: Colin Wetherbee [mailto:[EMAIL PROTECTED] Sent: Wednesday, January 02, 2008 3:30 PM To: Raful Mr Mitchell H Cc: modperl@perl.apache.org Subject: Re: segfault Raful Mr Mitchell H wrote: > My script looks like this > > use Apache2::Request (); > > my $req = Apache2::Request-> new($r) > my $data = req->{/param-name}/ > // > It causes a segfault error. Any ideas? Hello. Please send us a bit more of your script. This snippet doesn't say much. Colin
segfault
My script looks like this use Apache2::Request (); my $req = Apache2::Request-> new($r) my $data = req->{param-name} It causes a segfault error. Any ideas? Mitch Mitchell Raful MCSE CCNP MCB Quantico Personal and Family Readiness (MRI) 3044 Catlin Avenue Quantico, VA 22134 Com: 703-784-5991 DSN: 278-5991 Cell: 804-363-0731
help with rewriting CGI
I have a site that is written with traditional CGI (no cgi.pm ). I use mod_perl2 but rather than run the scripts as CGI I want to convert them to mod_perl. I have read Beckman's latest on mod_perl 2 and have Practical Mod_Perl. Here's the question. From what I have read, it looks like I should re-write my perl scripts as perl modules and make the appropriate changes, such as using $r inside handlers, etc. Once complete, I can preload the modules at Apache startup. Is that correct? Also, I may want to use ajax, therefore, what modules will I need to run Apache2::Ajax? Thanks, Mitch Mitchell Raful MCSE CCNP MCB Quantico Personal and Family Readiness (MRI) 3044 Catlin Avenue Quantico, VA 22134 Com: 703-784-5991 DSN: 278-5991 Cell: 804-363-0731