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

Hi fellow monks I am new to perl and i hope this question is too daft. I am try to monitor some cisco switches and want to create a simple html page for others to see the status of the switches. I am using snmp I can pull out the data. What I want to know is what is the best way to write the data from the array to a html page. I have posted the code just for the array, but I can post the rest if you think its necessary. Thanks in advance.
sub get_data { $hostkey = shift; for($if = 2; $if < 26; $if++) { $if_name{$hostkey}->{$if} = &get_snmp("$Cisco.$IfName.$if"); $hw_type{$hostkey}->{$if} = &get_snmp("$Cisco.$IfHwType.$if"); $if_desc{$hostkey}->{$if} = &get_snmp("$Generic.$ifDesc.$if"); $if_speed{$hostkey}->{$if} = &get_snmp("$Generic.$ifSpeed.$if"); $bw_in{$hostkey}->{$if} = &get_snmp("$Cisco.$IfInBitSec.$if"); $bw_out{$hostkey}->{$if} = &get_snmp("$Cisco.$IfOutBitSec.$if"); $if_lineprot{$hostkey}->{$if} = &get_snmp("$Cisco.$LineProto.$if") +; $vm_Vlan{$hostkey}->{$if} = &get_snmp("$Specific.$vmvlan.$if"); } return(0);

edited: Mon Jun 23 02:45:22 2003 by jeffa - title change (was: html question)

Replies are listed 'Best First'.
Re: Generating HTML from SNMP data
by submersible_toaster (Chaplain) on Jun 16, 2003 at 00:17 UTC

    Update: ++TomDLux , beat me to the punch! Signing up for the monastery is a great idea.
    HTML::Template will save your bacon. Specifically its looping feature. Design the layout of a table/cell for one switch status. Insert place holders into the relevant fields, and a template loop around the whole deal.

    <html> <body> <table border=3> <TMPL_LOOP switches> <tr> <td> <TMPL_VAR hostname> </td> <td> <TMPL_VAR status> </td> </tr> </TMPL_LOOP> </table> </body> </html>

    Then you can feed your data into that with HTML::Template

    #!/usr/bin/perl -w use strict; use Data::Dumper; use HTML::Template; my $template = HTML::Template->new( filename=>'./switch-status.tmpl' ) +; my @hosts = ( qw/ switch cisco1 cisco2 ninety nine cisco5 / ); my $info = []; foreach my $host ( @hosts ) { my $data = { hostname=> $host, status => Host2Status($host) , }; push @$info , $data; } $template->param(switches=>$info); print $template->output; sub Host2Status { my $host = shift; my $rand_status = int (rand 2)-1; my $status = ($rand_status) ? "Cool" : "Froody"; return $status; }

    This code works for me , no guarantees though. The cpan documentation for HTML::Template is good. If you are familiar with creating semi-complex datastructures in perl like an array of hashes, you will have this done in _NO_ time.


    I can't believe it's not psellchecked
Re: Generating HTML from SNMP data
by TomDLux (Vicar) on Jun 15, 2003 at 23:58 UTC

    You have to use HTML tags when posting messages on PerlMonks; in particular, wrap your code in <code> ..... </code>. If you had a membership and had logged in, you could go back and edit your message; now you have to hope some seniour brother takes pity on your message and corrects it.

    The simple solution is to use CGI and use the HTML tag routines there. But I would suggest using one of the template packages---I prefer HTML::Template, and separating the layout of the page from the dynamic content.

    Update: Maybe if is a supremely significant term or contraction with CISCO routers, but it's also a keyword in all programming languages. Next year, next month, next week, some unfortunate programmer ( perhaps you ) will be trying to decipher the code. Having an $if inside a for will probably require a second glance to determine the meaning. Is there a different word you can use for that variable name ... and similarly for $if_name, etc?

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Re: Generating HTML from SNMP data
by bobn (Chaplain) on Jun 16, 2003 at 01:51 UTC
    NOT TESTED.

    I haven't gotten into the template stuff others have mentioned earlier. But using those or rolling your own HTML, either way is easier if you choose a better data structure, which you can step throguh using keys or each.

    If it was me I'd change the structure to look like:
    $info{$hostkey}{$if}{if_name} = &get_snmp("$Cisco.$IfName.$if"); $info{$hostkey}{$if}{hw_type} = &get_snmp("$Cisco.$IfHwType.$if"); $info{$hostkey}{$if}{if_desc} = &get_snmp("$Generic.$ifDesc.$if"); $info{$hostkey}{$if}{if_speed} = &get_snmp("$Generic.$ifSpeed.$if"); $info{$hostkey}{$if}{bw_in} = &get_snmp("$Cisco.$IfInBitSec.$if"); $info{$hostkey}{$if}{bw_out} = &get_snmp("$Cisco.$IfOutBitSec.$if"); $info{$hostkey}{$if}{if_lineprot} = &get_snmp("$Cisco.$LineProto.$if") +; $info{$hostkey}{$if}{vm_Vlan} = &get_snmp("$Specific.$vmvlan.$if");
    Then you can say:
    use CGI; $q = new CGI; print $q->start_table(); for ( keys %{ $info{$hostkey}{$if} } ) { print $q->Tr([$q->td([$_, $info{$hostkey}{$if}{$_}])]) . "\n"; } print end_table();


    or the more understandable, but less approved:

    print "<table>\n"; for ( keys %{ $info{$hostkey}{$if} } ) { print "<tr><td>$_</td><td>$info{$hostkey}{$if}{$_}</td></tr>\n" } print "</table>"


    or use one of the templates mentioned above.

    --Bob Niederman, http://bob-n.com
Re: Generating HTML from SNMP data
by isotope (Deacon) on Jun 16, 2003 at 02:28 UTC
    Have you already dismissed MRTG?

    --isotope
Re: Generating HTML from SNMP data
by bobn (Chaplain) on Jun 16, 2003 at 01:44 UTC
    NOT TESTED.

    If it was me I'd change the structure to look like:
    $info{$hostkey}{$if}{if_name} = &get_snmp("$Cisco.$IfName.$if"); $info{$hostkey}{$if}{hw_type} = &get_snmp("$Cisco.$IfHwType.$if"); $info{$hostkey}{$if}{if_desc} = &get_snmp("$Generic.$ifDesc.$if"); $info{$hostkey}{$if}{if_speed} = &get_snmp("$Generic.$ifSpeed.$if"); $info{$hostkey}{$if}{bw_in} = &get_snmp("$Cisco.$IfInBitSec.$if"); $info{$hostkey}{$if}{bw_out} = &get_snmp("$Cisco.$IfOutBitSec.$if"); $info{$hostkey}{$if}{if_lineprot} = &get_snmp("$Cisco.$LineProto.$if") +; $info{$hostkey}{$if}{vm_Vlan} = &get_snmp("$Specific.$vmvlan.$if");
    Then you can say:
    use CGI; $q = new CGI; print $q->start_table(); for ( keys %{ $info{$hostkey}{$if} } ) { print $q->Tr([$q->td([$_, $info{$hostkey}{$if}{$_}])]) . "\n"; } print end_table();


    or the more understandable, but less approved:

    print "<table>\n"; for ( keys %{ $info{$hostkey}{$if} } ) { print "<tr><td>$_</td><td>$info{$hostkey}{$if}{$_}</td></tr>\n" } print "</table>"


    --Bob Niederman, http://bob-n.com