# Subroutine to email custom inventory report as csv sub custom_inventory_report_csv { # Nominate a single mail address to receive the report my $mail_to = ''; # e.g. jbloggs@example.com # Nominate a group mailing list to receive the report my $profile = ''; # e.g. NetEng # Subject line of the emailed report my $subject = "Custom Inventory Report CSV"; # Name of the csv file my $CSVFILE = "$HOME_TMP/custom_inventory.csv"; # Header name of the first column (with field delimiter ",") my $csv_column_header = "Device".","; # Working variables my $OUT; my @body; my %data; # This structure defines all of the data to collect for your report. # Data is collected when an attribute is used to query the database, # resulting with a returned value. The returned value is recorded # and presented in your report. # # Each, key => value, pair represents: # "database attribute" => "column header name" # # Modify the attributes and column names to suit your report. # # NOTE: The last pair in the structure has been "commented out", # as an example of how to exclude data from your report, without # deleting the pair. This approach can be useful when testing. my @attr_column_names = ( {"ip4addr" => "IPv4 Address"}, {"model" => "Model"}, {"serial" => "Serial Number"}, {"sw_rev" => "Software (OS Version)"}, {"SNMPv2-MIB.sysLocation" => "Location (sysLocation)"}, {"SNMP.snmpState" => "Added"}, {"SNMPv2-MIB.sysUpTime" => "sysUpTime"}, {"SNMPv2-MIB.sysDescr" => "Description"}, #{"IF-MIB.ifInErrors" => "Error"}, ); # Get data for all the attributes defined above for my $i ( 0 .. $#attr_column_names ) { for my $attr ( keys %{ $attr_column_names[$i] } ) { # Build the list of column header names $csv_column_header .= $attr_column_names[$i]{$attr}. ","; # Database query to get attribute data for my $line (adb_result ("mget * * * $attr")) { my ($device, undef, undef, undef, $value) = split (" ", $line, 5); $data{$device}{$i} = $value || ""; # # Some attributes require additional work to get the data # into a presentable format. Two examples are shown below. # # Format returned sysUpTime data if ($attr eq "SNMPv2-MIB.sysUpTime" ) { my ($start_tt, $end_tt) = split (",", $value); $data{$device}{$i} = time_elapsed($start_tt) || ""; } # Format returned snmpState data if ($attr eq "SNMP.snmpState"){ my (undef, undef, $added_tt, undef) = split (",", $value); $data{$device}{$i} = date_fmt($added_tt, "yyyymmddhhmm") || ""; } } } } # Open a csv file open ($OUT, ">", $CSVFILE) or EXIT_FATAL ("Could not open $CSVFILE: $!"); # Print the column header to csv file print $OUT $csv_column_header; print $OUT "\n"; # Print data to csv file for my $device (sort keys %data) { my $row; $row = $device.","; for my $attr (0..$#attr_column_names) { if ($data{$device}{$attr}) { $row .= $data{$device}{$attr}.","; } else { $row .= ","; } } $row .= "\n"; print $OUT $row; } close ($OUT); # Send to a single email address if ($mail_to ne "") { mail ({ subject => $subject, to => $mail_to, body => \@body, attach => [$CSVFILE] }); } # Send to all email addresses in a profile if ($profile ne "") { for my $addr (config_get_emails ($profile)) { mail ({ subject => $subject, to => $addr, body => \@body, attach => [$CSVFILE] }); } } }