Site Scripts

To use any of the Site Scripts:

  1. Download the script.
  2. Go to the Admin -> API -> Site Scripting menu.
  3. Paste the script into the text box and click Save.
  4. Reload the Site Scripting page so the new Perl function is displayed in the drop down list.
  5. Many script functions can be manually run by selecting the function from the drop down list and clicking Run

Available Site Scripts

  1. Cisco Error-Disable
  2. Create WeatherMap Data File
  3. Delete Unreachable Devices
  4. Discover API Examples
  5. Discover Dell iDRAC6 Devices
  6. Discover From CSV File
  7. Discover Ping-Only Devices
  8. Email Status Alert
  9. Export 5 minute interface statistics
  10. Export Device Chassis report
  11. Export Device Extended Details
  12. Export Device Summary report
  13. Import Ping-Only Devices
  14. Interface Speeds by Group
  15. Interface Speeds by Name
  16. List IP Addresses with Multiple Devices
  17. Send Status Alert via Syslog
  18. Send Threshold Alert via Syslog
  19. Sync Device Name with sysName

1. Cisco Error-Disable

Error-disable is a feature in Cisco switches that automatically disables a switch port when an error is detected. The reasons a switch port can go into error-disable mode include:

The MIB objects for error-disable are dynamically created in a conceptual MIB table when the port is disabled, therefore the error-disable state can NOT be collected using the normal polling process. The only method of retrieving a list of ports in the error-disabled state is an SNMP Walk. This script performs an SNMP walk of CISCO-ERR-DISABLE-MIB.cErrDisableIfStatusCause at the specified time (default 8am) and emails a list of the errors to the address defined by the "address" variable.

Download script
sub sched_1h_cisco_error_disabled
{
   my $ip;
   my $cmd;
   my $line;
   my $IN;
   my $OUT;
   my $ipaddr;
   my $name;
   my $ifindex;
   my $interface;
   my $reason;
   my $filename = "$HOME_TMP/cisco-error-disabled";
   my $snmp_cfg_ref = config_load_snmp_cfg ();
   my $ip2name_ref = config_load_ip2name ();
   my $tm_ref = get_localtime ();
   my @body;
   my %results;
   my $address = '';   # XXX Set me

   return if ($tm_ref->{hour} != 8); # Run daily at 8am

   open ($OUT, "|-", "$SNMP_PROG > $filename") or EXIT_FATAL ("Can't run $SNMP_PROG");
   for my $dev (keys %{ $snmp_cfg_ref }) {
      next if ($snmp_cfg_ref->{$dev}{state} eq "down");
      $ip = $snmp_cfg_ref->{$dev}{ipaddr} || next;
      $cmd = $snmp_cfg_ref->{$dev}{cmd}   || next;
      printf $OUT "walk %s %s CISCO-ERR-DISABLE-MIB.cErrDisableIfStatusCause\n", $ip, $cmd;
   }
   close $OUT;
 
   open ($IN, "<", $filename) || EXIT_FATAL ("Can't open $filename");
   while ($line = <$IN>) {
      chomp $line;
      next if ($line =~ /^#/m);
      ( $ipaddr, undef, undef, $ifindex, undef, $reason ) = split( / /, $line );
      $ifindex = sprintf( "%d", $ifindex );
      $name = $ip2name_ref->{$ipaddr};
      ( undef, $interface, undef ) = split( / /, adb_result( "mget * $name * /ifindex/ value $ifindex" ) );
      push @{ $results{ $name } }, ( sprintf( "%s - %s", $interface, $reason ) );
   }
   close $IN;
 
   for my $dev (keys %results) {
      push @body, sprintf( "\n%s:", $dev );
      foreach my $entry ( @{ $results{ $dev } } ) {
         push @body, sprintf( "   %s", $entry );
      }
   }
 
   if ($address ne "" and scalar @body > 0) {
      mail ({ to => $address, subject => 'Cisco Error Disabled', body => \@body });
   }

   return;
}

2. Create WeatherMap Data File

This script creates a tab delimited output file that can be sent to, or retrieved by, your weathermap server. The output format is:

{DeviceName}:{InterfaceName} {In BPS} {Out BPS}

You need to use the auto or manual grouping to add all interfaces to the "weathermap-links" interface group. For example, in the auto grouping:

add interface group weathermap-links
assign interface router1 Fa0/2 = weathermap-links
assign interface router2 Fa0/5 = weathermap-links

If the $remote_user, $remote_host, and $remote_path parameters are filled in, the script will scp the output file to your weathermap server. You will need to install a public SSH key on your weathermap server to enable authentication. SSH to the AKIPS server as akips user and run the following command, replacing {user} and {weathermap host} with your weathermap server username and IP address.

ssh-copy-id -i /home/akips/.ssh/id_rsa.pub {user}@{weathermap host}

Refer to the Network WeatherMap documentation.

Download script
sub sched_1m_weathermap_links
{
   my $remote_user = ""; # e.g. 'www'
   my $remote_host = ""; # e.g. 10.1.2.3
   my $remote_path = ""; # e.g. /var/www/html/plugins/weathermap/configs
   my $remote_file = "akips.txt";
   my $data_file   = "/tmp/weathermap.txt";
   my %data;
   my $OUT;

   for my $line (adb_result ("mcalc avg time last1m ifrate * * /^IF-MIB.*BitRate/ any group weathermap-links")) {
      my ($device, $interface, $attr, undef, $val) = split (" ", $line, 5);
      my $link = sprintf ("%s:%s", $device, $interface);
      given ($attr) {
         when ("IF-MIB.ifInBitRate")  { $data{$link}{inbps}  = $val; }
         when ("IF-MIB.ifOutBitRate") { $data{$link}{outbps} = $val; }
      }
   }

   open ($OUT, ">", $data_file) || EXIT_FATAL ("Could not create %s", $data_file);
   for my $link (keys %data) {
      printf $OUT "%s\t%s\t%s\n", $link, $data{$link}{inbps}, $data{$link}{outbps};
   }
   close $OUT;

   if ($remote_user ne "" and $remote_host ne "" and $remote_path ne "" and $remote_file ne "") {
      system (sprintf ("/usr/bin/scp -q %s %s@%s:%s/%s", $data_file, $remote_user, $remote_host, $remote_path, $remote_file));
   }

   return;
}

3. Delete Unreachable Devices

This script automatically deletes all devices which have been down for over 7 days. The script runs at the end of a Discover or Rewalk.

Download script
sub config_delete_unreachable_devices
{
   my $prune_days = 7;
   my $prune_time = 60 * 60 * 24 * $prune_days;
   my @arr = adb_result ("mget enum * ping4 PING.icmpState");
   my $cur_tt = time ();

   for my $line (@arr) {
      my ($dev, undef, undef, undef, $val) = split (" ", $line, 5);
      my (undef, $state, undef, $mtime, undef) = split (",", $val, 5);
      next if ($state ne "down");
      if ($cur_tt - $mtime > $prune_time) {
         printf " Removing %s\n", $dev;
         errlog ($ERR_DEBUG, "Deleting unreachable device %s", $dev);
         adb_send (sprintf ("delete device %s", $dev));
      }
   }
   adb_flush ();
   return;
}


4. Discover API Examples

This example shows some ways a Discover can be initiated from a Site Script.

Download script
sub custom_discover
{
   # Open discover log file
   discover_log ("discover-script.log");


   # Full discovery using Ping Ranges and SNMP Parameters
   # under Admin > Discover > Discover/Rewalk

   #if (discover_scan () > 0) {
   #   discover_config ();
   #}


   # Single device discovery using specified SNMP parameters

   #if (discover_scan ("version 2 community public", "10.1.2.3") > 0) {
   #   discover_config ();
   #}


   # Scan IP address ranges using SNMP Parameters
   # under Admin > Discover > Discover/Rewalk

   #if (discover_scan (undef, "10.1.2.0/24", "10.1.3.0/24") > 0) {
   #   discover_config ();
   #}


   # Close discover log file
   stdout_log_close ();
}

5. Discover Dell iDRAC6 Devices

This script runs at the end of a successful Discover (not a Rewalk) and adds any iDRAC6 devices as ping-only devices. It will also add the relevant objects for the Dell iDRAC System Status report.

NOTES:

Download script
sub config_discover_idrac6
{
   my $IN;
   my %walk;

   if (open ($IN, "<", $DISCOVER_WALKS)) {
      while (my $line = <$IN>) {
         chomp $line;
         my ($ip, $mib, $obj, $idx, undef, $val) = split (" ", $line, 6);
         next if ($mib ne "DELL-RAC-MIB");
         if (defined $cfg_oids{$mib}{$obj}) {
            $walk{$ip}{$mib}{$obj}{$idx} = $val;
         }
      }
      close $IN;
   }

   if (open ($IN, "<", $DISCOVER_DEVICES)) {
      while (my $line = <$IN>) {
         chomp $line;
         my ($ip4, $ip6, $device, $snmp_opt, $sysObjectID, $sysDescr) = split (",", $line, 6);

         if ($sysObjectID eq "DELL-RAC-MIB.drsOutofBandGroup") {
            my %dev = ();
            $dev{device}  = $device;
            $dev{ip4addr} = $ip4;
            $dev{ip6addr} = $ip6;
            $dev{descr}   = $sysDescr;
            if (config_add_ping_device (\%dev) == 1) {
               printf "Added %s\n", $device;

               # Add IDRAC6 objects
               my $mib = "DELL-RAC-MIB";
               adb_send (sprintf ("add child %s idrac", $device));
               for my $obj (keys %{$walk{$ip4}{$mib}}) {
                  my $cfg_ref = $cfg_oids{$mib}{$obj};
                  my $type    = $cfg_ref->{type};
                  my $value   = $walk{$ip4}{$mib}{$obj}{"0"};

                  if ($type eq "text") {
                     adb_send (sprintf ("add %s %s idrac %s.%s = \"%s\"", $type, $device, $mib, $obj, $value));
                  }
               }
            }
         }
      }
      close $IN;
      adb_flush ();
   }

   return;
}

6. Discover From CSV File

This script shows how to initiate a Discover/Rewalk from a CSV file. The CSV file could be generated from an external IPAM system, for example.

The format of the CSV input file is:

Place your CSV file in /tmp/devices.csv on the AKIPS server and run this script.

NOTE: This script will run a Discover/Rewalk for each unique set of SNMP credentials in your CSV file.

Download script
# File format:
#   ipaddr,SNMP parameters
# e.g.
#   10.1.8.250,version 2 community foobar
#
sub custom_discover_import_csv
{
   my $DEVICES_CSV = "/tmp/devices.csv";
   my $IN;
   my $line;
   my %cfg;

   return if (not -e $DEVICES_CSV);

   # Open discover log file
   discover_log ("discover-script.log");

   open ($IN, "<", $DEVICES_CSV);
   while ($line = <$IN>) {
      chomp $line;
      my ($ipaddr, $snmp_param) = split (",", $line, 2);
      trim $snmp_param;
      $cfg{$snmp_param}{$ipaddr} = 1;
   }

   for my $snmp_param (sort keys %cfg) {
      if (discover_scan ($snmp_param, keys %{ $cfg{$snmp_param} }) > 0) {
         discover_config ();
      }
   }

   # Close discover log file
   stdout_log_close ();

   unlink $DEVICES_CSV;

   return;
}

7. Discover Ping-Only Devices

This script performs a ping scan of the specified ranges and creates the appropriate configuration for ping-only devices.

Edit the @ranges array definition at the top of the function to define your own IP ranges.

Download script
use Socket;

sub custom_scan_ping_devices
{
   my @ranges = (
      "10.1.1.0/24",
   );
   my @domains = config_load_domains ();
   my $IN;
   my $OUT;
   my %result;

   open ($OUT, "|-", "$PING_SCAN -o $HOME_TMP/ping-scan.out");
   for my $r (@ranges) {
      printf $OUT "%s\n", $r;
   }
   close $OUT;  # Block on the close until nm-ping-scan completes

   open ($IN, "<", "$HOME_TMP/ping-scan.out");
   while (my $ipaddr = <$IN>) {
      chomp $ipaddr;

      my $name = gethostbyaddr (inet_aton ($ipaddr), AF_INET);

      if (defined $name) {
         $name = config_strip_sysname ($name, @domains);
      }
      else {
         $name = $ipaddr;
      }

      if ($ipaddr =~ /$REGEX_IPV4_ADDR/m) {
         $result{$name}{ip4addr} = $ipaddr;
         $result{$name}{device}  = $name;
      }
      elsif ($ipaddr =~ /$REGEX_IPV6_ADDR/m) {
         $result{$name}{ip6addr} = $ipaddr;
         $result{$name}{device}  = $name;
      }
   }
   close $IN;

   for my $name (keys %result) {
      config_add_ping_device ($result{$name});
   }

   return;
}

8. Email Status Alert

This is a basic example which shows:

NOTE: This example script:

Download script
sub alert_mail_status
{
   my ($arg_ref) = @_;
   my $mail_to = ''; # e.g. jbloggs@example.com
   my $profile = ''; # e.g. NetEng
   my $subject = sprintf ("Status: %s %s %s %s",
                          $arg_ref->{device},
                          $arg_ref->{child},
                          $arg_ref->{descr},
                          $arg_ref->{state});
   my $ipaddr = adb_result (sprintf ("get %s sys SNMP.ipaddr", $arg_ref->{device}));
   my $time_str = strftime ("%H:%M", localtime ($arg_ref->{tt}));
   my @body;

   push @body, sprintf ("%s %s %s %s %s %s %s %s",
                        $time_str,
                        $arg_ref->{device},
                        $ipaddr,
                        $arg_ref->{child},
                        $arg_ref->{descr},
                        $arg_ref->{attr},
                        $arg_ref->{alias} || "",
                        $arg_ref->{state});

   # Send to a single email address
   if ($mail_to ne "") {
      mail ({ subject => $subject, to => $mail_to, body => \@body });
   }

   # 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 });
      }
   }

   return;
}


9. Export 5 minute interface statistics

This script creates a 5 minute CSV file for all interface statistics. The format of the file is:

  1. Device Name
  2. Interface Name
  3. In Octets
  4. Out Octets
  5. In BPS
  6. Out BPS
  7. In Percent Utilisation
  8. Out Percent Utilisation
  9. In Packets
  10. Out Packets
  11. In Unicast Packets
  12. Out Unicast Packets
  13. In Broadcast Packets
  14. Output Broadcast Packets
  15. In Multicast Packets
  16. Out Multicast Packets
  17. In Errors
  18. Out Errors
  19. In Discards
  20. Out Discards
  21. In Error Percentage
  22. Out Error Percentage
  23. In Discard Percentage
  24. Out Discard Percentage
The output file is located in: /home/akips/tmp/ifstats-last5m.csv

Download script
sub sched_5m_ifstats
{
   my $filename = "$HOME_TMP/ifstats-last5m.csv";
   my %stat;
   my $OUT;

   for my $line (adb_result ("mcalc avg time last5m ifutil * * /^IF-MIB.if.*Util/")) {
      my ($device, $interface, $attr, undef, $val) = split (" ", $line, 5);
      $attr =~ s/IF-MIB\.if//m;
      next if ($val eq "n/a");
      $stat{$device}{$interface}{$attr} = $val;
   }

   for my $line (adb_result ("mcalc avg time last5m ifrate * * /^IF-MIB/")) {
      my ($device, $interface, $attr, undef, $val) = split (" ", $line, 5);
      $attr =~ s/IF-MIB\.if//m;
      next if ($val eq "n/a");
      $stat{$device}{$interface}{$attr} = $val;
   }

   for my $line (adb_result ("mcalc total time last5m counter * * /^IF-MIB/")) {
      my ($device, $interface, $attr, undef, $val) = split (" ", $line, 5);
      $attr =~ s/IF-MIB\.if//m;
      $attr =~ s/^HC//m;
      $val  =~ s/n\/a//m;
      $stat{$device}{$interface}{$attr} = $val;
   }

   open ($OUT, ">", "$filename.tmp") or EXIT_FATAL ("Can't open $filename.tmp: $!");

   for my $device (sort keys %stat) {
      for my $interface (sort keys %{ $stat{$device} }) {
         my $InPkts;
         my $OutPkts;
         my $InUtil = "";
         my $OutUtil = "";
         my $InErrorsPct = 0;
         my $OutErrorsPct = 0;
         my $InDiscardsPct = 0;
         my $OutDiscardsPct = 0;
         my $ref = $stat{$device}{$interface};

         next if (not defined $ref->{InUtil});
         next if (not defined $ref->{OutUtil});

         if ($ref->{InUtil} ne "") {
            $InUtil = sprintf ("%.2f", $ref->{InUtil} / 100);
         }

         if ($ref->{OutUtil} ne "") {
            $OutUtil = sprintf ("%.2f", $ref->{OutUtil} / 100);
         }

         $InPkts = ($ref->{InUcastPkts}     || 0)
                 + ($ref->{InBroadcastPkts} || 0)
                 + ($ref->{InMulticastPkts} || 0)
                 + ($ref->{InErrors}        || 0)
                 + ($ref->{InDiscards}      || 0);

         $OutPkts = ($ref->{OutUcastPkts}     || 0)
                  + ($ref->{OutBroadcastPkts} || 0)
                  + ($ref->{OutMulticastPkts} || 0);

         if ($InPkts > 0) {
            $InErrorsPct   = ($ref->{InErrors}   || 0) * 100 / $InPkts;
            $InDiscardsPct = ($ref->{InDiscards} || 0) * 100 / $InPkts;
         }

         if ($OutPkts > 0) {
            $OutErrorsPct   = ($ref->{OutErrors}   || 0) * 100 / $OutPkts;
            $OutDiscardsPct = ($ref->{OutDiscards} || 0) * 100 / $OutPkts;
         }

         printf {$OUT} "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%.2f,%.2f,%.2f,%.2f\n",
                       $device,
                       $interface,
                       $ref->{InOctets}   || 0,
                       $ref->{OutOctets}  || 0,
                       $ref->{InBitRate}  || 0,
                       $ref->{OutBitRate} || 0,
                       $InUtil,
                       $OutUtil,
                       $InPkts,
                       $OutPkts,
                       $ref->{InUcastPkts}      || 0,
                       $ref->{OutUcastPkts}     || 0,
                       $ref->{InBroadcastPkts}  || 0,
                       $ref->{OutBroadcastPkts} || 0,
                       $ref->{InMulticastPkts}  || 0,
                       $ref->{OutMulticastPkts} || 0,
                       $ref->{InErrors}         || 0,
                       $ref->{OutErrors}        || 0,
                       $ref->{InDiscards}       || 0,
                       $ref->{OutDiscards}      || 0,
                       $InErrorsPct,
                       $OutErrorsPct,
                       $InDiscardsPct,
                       $OutDiscardsPct;
      }
   }

   close $OUT;
   rename ("$filename.tmp", $filename);

   return;
}

10. Export Device Chassis report

This script creates a CSV file containing the same information as ReportsDeviceChassis.

The format of the output file is:

  1. Device Name
  2. Model
  3. Serial
  4. Software Version
  5. Hardware Version
  6. Firmware Version
  7. Chassis Description

The output file is located in: /home/akips/tmp/device-chassis.csv

Download script
sub custom_export_device_chassis
{
   my $filename = "$HOME_TMP/device-chassis.csv";
   my @body;
   my %data;
   my $OUT;

   for my $line (adb_result ('mget text * /^chassis\./ *')) {
      my ($dev, $child, $attr, undef, $val) = split (" ", $line, 5);
      $data{$dev}{$child}{$attr} = $val;
   }

   for my $line (adb_result ('mget child * /^chassis\./')) {
      my ($dev, $child, undef, $val) = split (" ", $line, 4);
      my ($index, $descr) = split (",", $val);
      $data{$dev}{$child}{descr} = $descr;
   }

   open ($OUT, ">", "$filename.tmp") or EXIT_FATAL ("Can't open $filename.tmp: $!");
   for my $dev (nat_sort keys %data) {
      for my $child (sort { nat_cmp ($data{$dev}{$a}{descr}, $data{$dev}{$b}{descr}) }
                     keys %{ $data{$dev} }) {
         printf $OUT "%s,%s,%s,%s,%s,%s,\"%s\"\n",
                     $dev,
                     $data{$dev}{$child}{model}  || "",
                     $data{$dev}{$child}{serial} || "",
                     $data{$dev}{$child}{sw_rev} || "",
                     $data{$dev}{$child}{hw_rev} || "",
                     $data{$dev}{$child}{fw_rev} || "",
                     $data{$dev}{$child}{descr}  || "";
      }
   }
   close $OUT;
   rename ("$filename.tmp", $filename);

   return;
}

11. Export Device Extended Details

This script creates a CSV file containing the same information as ReportsDeviceSummary, plus SysContact, SNMP parameters, and group membership.

The format of the output file is:

  1. Device Name
  2. IPv4 Address
  3. IPv6 Address
  4. SysUpTime (Unix epoch time)
  5. SysUpTime (in seconds)
  6. SysUpTime (days/hours/minutes/seconds)
  7. Added (Unix epoch time)
  8. Added (yyyy-mm-dd HH:MM:SS)
  9. Location (sysLocation)
  10. Identifier (sysObjectID)
  11. Description (sysDescr)
  12. Contact (sysContact)
  13. SNMP Parameters
  14. Group Membership (space separated list)

The output file is located in: /home/akips/tmp/device-summary-ext.csv

Download script
sub custom_export_device_extended
{
   my $filename = "$HOME_TMP/device-summary-ext.csv";
   my $snmp_cfg_ref = config_load_snmp_cfg ();
   my @groups;
   my %data;
   my $OUT;

   for my $line (adb_result ("mget text * sys *")) {
      my ($dev, $child, $attr, undef, $val) = split (" ", $line, 5);
      $attr =~ s/^SNMPv2-MIB\.//img;
      $data{$dev}{$attr} = $val;
   }

   for my $line (adb_result ("mget uptime * sys *")) {
      my ($dev, $child, $attr, undef, $val) = split (" ", $line, 5);
      my ($uptime, $utime) = split (",", $val);
      $attr =~ s/^SNMPv2-MIB\.//img;
      $data{$dev}{$attr} = $uptime;
   }

   for my $line (adb_result ("mtime device *")) {
      my ($dev, undef, $val) = split (" ", $line, 3);
      my ($ctime, $mtime, $utime) = split (",", $val);
      $data{$dev}{ctime} = $ctime;
   }

   for my $line (adb_result ("mgroup device *")) {
      my ($dev, undef, $val) = split (" ", $line, 3);
      if ($val eq "none") {
         $data{$dev}{groups} = "";
      }
      else {
         $val =~ s/,/ /mg;
         $data{$dev}{groups} = $val;
      }
   }

   open ($OUT, ">", "$filename.tmp") or EXIT_FATAL ("Can't open $filename.tmp: $!");
   for my $dev (nat_sort keys %data) {
      my $snmp_cmd = $snmp_cfg_ref->{$dev}{cmd} || "";
      $snmp_cmd =~ s/ maxrep \d+$//m;  # strip maxrep parameter

      my $uptime_sec = "";
      if (defined $data{$dev}{sysUpTime}) {
         $uptime_sec = time () - $data{$dev}{sysUpTime};
      }

      printf $OUT "%s,%s,%s,%s,%s,%s,%d,%s,\"%s\",%s,\"%s\",\"%s\",%s,%s\n",
                  $dev,
                  $data{$dev}{ip4addr}     || "",
                  $data{$dev}{ip6addr}     || "",
                  $data{$dev}{sysUpTime}   || "",
                  $uptime_sec,
                  time_elapsed ($data{$dev}{sysUpTime} || ""),
                  $data{$dev}{ctime},
                  date_fmt ($data{$dev}{ctime}, "yyyymmddhhmmss"),
                  $data{$dev}{sysLocation} || "",
                  $data{$dev}{sysObjectID} || "",
                  $data{$dev}{sysDescr}    || "",
                  $data{$dev}{sysContact}  || "",
                  $snmp_cmd,
                  $data{$dev}{groups}      || "";
   }
   close $OUT;
   rename ("$filename.tmp", $filename);

   return;
}

12. Export Device Summary report

This script creates a CSV file containing the same information as ReportsDeviceSummary.

The format of the output file is:

  1. Device Name
  2. IPv4 Address
  3. IPv6 Address
  4. SysUpTime (Unix epoch time)
  5. SysUpTime (in seconds)
  6. SysUpTime (days/hours/minutes/seconds)
  7. Added (Unix epoch time)
  8. Added (yyyy-mm-dd HH:MM:SS)
  9. Location (sysLocation)
  10. Identifier (sysObjectID)
  11. Description (sysDescr)

The output file is located in: /home/akips/tmp/device-summary.csv

Download script
sub custom_export_device_summary
{
   my $filename = "$HOME_TMP/device-summary.csv";
   my @body;
   my %data;
   my $OUT;

   for my $line (adb_result ("mget text * sys *")) {
      my ($dev, $child, $attr, undef, $val) = split (" ", $line, 5);
      $attr =~ s/^SNMPv2-MIB\.//img;
      $data{$dev}{$attr} = $val;
   }

   for my $line (adb_result ("mget uptime * sys *")) {
      my ($dev, $child, $attr, undef, $val) = split (" ", $line, 5);
      my ($uptime, $utime) = split (",", $val);
      $attr =~ s/^SNMPv2-MIB\.//img;
      $data{$dev}{$attr} = $uptime;
   }

   for my $line (adb_result ("mtime device *")) {
      my ($dev, undef, $val) = split (" ", $line, 5);
      my ($ctime, $mtime, $utime) = split (",", $val);
      $data{$dev}{ctime} = $ctime;
   }

   open ($OUT, ">", "$filename.tmp") or EXIT_FATAL ("Can't open $filename.tmp: $!");
   for my $dev (nat_sort keys %data) {
      my $uptime_sec = "";
      if (defined $data{$dev}{sysUpTime}) {
         $uptime_sec = time () - $data{$dev}{sysUpTime};
      }

      printf $OUT "%s,%s,%s,%s,%s,%s,%d,%s,\"%s\",%s,\"%s\"\n",
                  $dev,
                  $data{$dev}{ip4addr}     || "",
                  $data{$dev}{ip6addr}     || "",
                  $data{$dev}{sysUpTime}   || "",
                  $uptime_sec,
                  time_elapsed ($data{$dev}{sysUpTime} || ""),
                  $data{$dev}{ctime},
                  date_fmt ($data{$dev}{ctime}, "yyyymmddhhmmss"),
                  $data{$dev}{sysLocation} || "",
                  $data{$dev}{sysObjectID} || "",
                  $data{$dev}{sysDescr}    || "";
   }
   close $OUT;
   rename ("$filename.tmp", $filename);

   return;
}

13. Import Ping-Only Devices

This script imports a CSV formatted file and creates the appropriate configuration for ping-only devices.

Place your CSV file in /tmp/import.csv on the AKIPS server and run this script.

The format of the CSV input file is:

Download script
#
# CSV file format:
# devicename,ip4addr,ip6addr,descr,location,contact
#
sub custom_import_ping_devices
{
   my $FILENAME = "/tmp/import.csv";
   my $IN;
   my $line;
   my %dev;
   
   open ($IN, "<", $FILENAME) or EXIT_FATAL ("Could not open $FILENAME: $!");

   while ($line = <$IN>) {
      chomp $line;
      %dev = ();
      ($dev{device}, $dev{ip4addr}, $dev{ip6addr},
       $dev{descr}, $dev{location}, $dev{contact}) = split (",", $line);
       
      if (config_add_ping_device (\%dev) == 1) {
         printf "Added %s\n", $dev{device};
         # add some grouping commands here...
      }
      else {
         printf "Failed to add %s\n", $dev{device};
      }
   }
   close $IN;
   return;
}

14. Interface Speeds by Group

This script manually sets interface speeds in a specified group.

Site Scripting uses function names starting with ifspeed_ to run commands to manually set interface speeds.

You can test your ifspeed_ code by selecting "All ifspeed_ functions" from the dropdown, then clicking Run. Make sure there are no error messages in the output on the right side of the screen. You can then check an Interface report to confirm that the speed has been set correctly.

If the interfaces are collected in an interface group, you can use the mset command together with a group filter. For example, if you have an interface group named wan-links, this will set the speed to 2Gbps for all interfaces in that group.

Download script
sub ifspeed_interface_group
{
   # Command syntax:
   # mset integer {device regex} {interface regex} IF-MIB.ifSpeed [any|all|not group {group name}] = {speed in bits per sec}

   adb_send ("mset integer * * IF-MIB.ifSpeed any group wan-links = 2000000000");
   adb_flush ();
   return;
}

15. Interface Speeds by Name

This script manually sets interface speeds by specifying device name and interface name.

Site Scripting uses function names starting with ifspeed_ to run commands to manually set interface speeds.

You can test your ifspeed_ code by selecting "All ifspeed_ functions" from the dropdown, then clicking Run. Make sure there are no error messages in the output on the right side of the screen. You can then check an Interface report to confirm that the speed has been set correctly.

If you only need to manually set speeds on a handful of interfaces, you can issue individual set commands specifying the device and interface names. The following example sets the speed to 2Gbps for 3 interfaces on the device atlanta-ro.

Download script
sub ifspeed_interfaces
{
   # Command syntax:
   # set {device} {interface} IF-MIB.ifSpeed = {speed in bits per sec}

   adb_send ("set atlanta-ro Te3/1 IF-MIB.ifSpeed = 2000000000");
   adb_send ("set atlanta-ro Te3/2 IF-MIB.ifSpeed = 2000000000");
   adb_send ("set atlanta-ro Te3/3 IF-MIB.ifSpeed = 2000000000");
   adb_flush ();
   return;
}

16. List IP Addresses with Multiple Devices

If multiple devices have the same IPv4 address configured in AKIPS, this script will list them.

Download script
sub custom_duplicate_ipaddr
{
   my %ip2dev;
   my %dev_ctime;
   my $cnt = 0;

   for my $line (adb_result ("mget * * sys ip4addr")) {
      my ($dev, undef, undef, undef, $ip) = split (" ", $line, 5);
      push @{ $ip2dev{$ip} }, $dev;
   }

   for my $line (adb_result ("mtime device *")) {
      my ($dev, undef, $val) = split (" ", $line, 3);
      my ($ctime, $mtime, $utime) = split (",", $val);
      $dev_ctime{$dev} = $ctime;
   }

   for my $ip (nat_sort keys %ip2dev) {
      if (scalar @{ $ip2dev{$ip} } > 1) {
         printf "%s\n", $ip;
         for my $dev (sort { $dev_ctime{$a} <=> $dev_ctime{$b} } @{ $ip2dev{$ip} }) {
            my $ctime = $dev_ctime{$dev};
            printf " %-20s <a href=\"/device-editor?mode=display;device_list=%s\" target=_blank>%s</a>\n",
                   time_simple ($ctime), $dev, $dev;
            $cnt++;
         }
         printf "\n";
      }
   }

   if ($cnt == 0) {
      printf "No duplicates found.\n";
   }

   return;
}

17. Send Status Alert via Syslog

This is a basic example which shows:

The priority parameter must be one of the following:

The facility parameter must be one of the following:

Download script
sub alert_status_send_syslog
{
   my ($arg_ref) = @_;
   my $msg;

   # Modify the following parameters. You must set $dest_ip to enable this script.
   # Refer to the Site Script documentation at www.akips.com for details
   my $dest_ip  = ""; # IP address of remote syslog collector
   my $priority = "error";
   my $facility = "local3";

   # Get the IP address of the device which triggered the alert
   my $dev_ip   = adb_result (sprintf ("get %s sys SNMP.ipaddr", $arg_ref->{device}));
   my $time_str = strftime ("%Y-%m-%d %H:%M:%S%z", localtime ($arg_ref->{tt}));
   #my $time_utc = strftime ("%Y-%m-%d %H:%M:%SZ", gmtime ($arg_ref->{tt}));

   # Parse the MIB and object which triggered the alert
   my ($mib, $obj) = split (/\./m, $arg_ref->{attr}, 2);

   # The following formats the message similar to Status Alerting email messages.
   if ($mib eq "IF-MIB") {
      $msg = sprintf ("%s %s %s %s %s %s %s",
                      $time_str,
                      $arg_ref->{alias} || "",
                      $arg_ref->{device},
                      $dev_ip,
                      $arg_ref->{child},  # interface name
                      $arg_ref->{descr},
                      $arg_ref->{state});
   }
   else {
      $msg = sprintf ("%s %s %s %s %s %s",
                      $time_str,
                      $arg_ref->{alias} || "",
                      $arg_ref->{device},
                      $dev_ip,
                      $arg_ref->{descr},
                      $arg_ref->{state});
   }

   # Send the syslog message
   if ($dest_ip ne "") {
      syslog ({
         ipaddr   => $dest_ip,
         priority => $priority,
         facility => $facility,
         message  => $msg,
      });
   }

   return;
}


18. Send Threshold Alert via Syslog

This is a basic example which shows:

The priority parameter must be one of the following:

The facility parameter must be one of the following:

Download script
sub alert_threshold_send_syslog
{
   my ($arg_ref) = @_;
   my $msg;
   my $thr = $arg_ref->{thr};
   my $val = $arg_ref->{val};

   # Modify the following parameters. You must set $dest_ip to enable this script.
   # Refer to the Site Script documentation at www.akips.com for details
   my $dest_ip  = ""; # IP address of remote syslog collector
   my $priority = "error";
   my $facility = "local3";

   # Get the IP address of the device which triggered the alert
   my $dev_ip   = adb_result (sprintf ("get %s sys SNMP.ipaddr", $arg_ref->{device}));
   my $time_str = strftime ("%Y-%m-%d %H:%M:%S%z", localtime ($arg_ref->{tt}));
   #my $time_utc = strftime ("%Y-%m-%d %H:%M:%SZ", gmtime ($arg_ref->{tt}));

   # Parse the MIB and object which triggered the alert
   my ($mib, $obj) = split (/\./m, $arg_ref->{attr}, 2);

   # The following formats the message similar to Threshold Alerting email messages.
   if ($mib eq "IF-MIB") {
      given ($obj) {
         when (/if.*BitRate/m) {
            $thr = int_ifspeed ($thr);
            $val = int_ifspeed ($val);
         }
         when (/if.*Util/m) {
            $thr = int_util ($thr);
            $val = int_util ($val);
         }
         default {
            $thr = int_metric ($thr);
            $val = int_metric ($val);
         }
      }
      trim $val;
      $msg = sprintf ("%s %s %s %s %s %s %s %s %s %s %s",
                      $time_str,
                      $arg_ref->{alias} || "",
                      $arg_ref->{device},
                      $dev_ip,
                      $arg_ref->{child},  # interface name
                      $arg_ref->{descr},
                      $arg_ref->{lastn},
                      $arg_ref->{stat},   # avg or total
                      $val,               # calculated value
                      $arg_ref->{state},  # above or below
                      $thr);              # threshold value
   }
   else {
      $msg = sprintf ("%s %s %s %s %s %s %s %s %s %s",
                      $time_str,
                      $arg_ref->{alias} || "",
                      $arg_ref->{device},
                      $dev_ip,
                      $arg_ref->{descr},
                      $arg_ref->{lastn},
                      $arg_ref->{stat},   # avg or total
                      $val,               # calculated value
                      $arg_ref->{state},  # above or below
                      $thr);              # threshold value
   }

   # Send the syslog message
   if ($dest_ip ne "") {
      syslog ({
         ipaddr   => $dest_ip,
         priority => $priority,
         facility => $facility,
         message  => $msg,
      });
   }

   return;
}


19. Sync Device Name with sysName

This script renames all devices using the value from SNMPv2-MIB.sysName as returned by each device. It is run at the end of the discover/rewalk because its function name starts with "config_".

Download script
sub config_update_sysname
{
   my @domains = config_load_domains ();
   for my $line (adb_result ("mget text * sys SNMPv2-MIB.sysName")) {
      my @arr = split (" ", $line, 5);
      my $devname = $arr[0];
      my $sysname = $arr[4] || "";
      $sysname = config_strip_sysname ($sysname, @domains);
      if ($devname ne $sysname) {
         errlog ($ERR_DEBUG, "rename %s -> %s", $devname, $sysname);
         adb_send (sprintf ("rename device %s %s", $devname, $sysname));
      }
   }
   adb_flush ();

   return;
}