#! /usr/bin/perl -w

# Wireless LAN monitor
# Waider 29/03/2004
package Monitor::WLAN;

use Monitor;
@ISA = "Monitor";

sub newdata {
  my $mon = shift;
  my $text = $mon->{'text'};
  my $lasttime = $mon->{'lastrun'};
  my $ifsr = $mon->{'ifs'};

  if ( defined( $ifsr )) {
	%ifs = %{$ifsr};
  } else {
	%ifs = ();
  }

  if ( open( IFLIST, "</proc/net/wireless" )) {
	my @ifs = <IFLIST>;
	close( IFLIST );

	# discard first two lines
	shift @ifs;
	shift @ifs;

	for my $ifl (@ifs) {
	  $ifl =~ s/[: ]+/ /g;
	  $ifl =~ s/^\s+//;

	  # this is what we're parsing
      # eth1: 0000 53. 218. 164. 0 0 8 0 0 0
	  my ( $ifname, $status, $link, $signal, $noise, @rest ) =
        split(/\s+/, $ifl );

      if ( defined( $ifs{$ifname})) {
          my ( $olink, $osignal, $onoise ) =
            split( "\0", $ifs{$ifname});
          if ( $olink > $link ) {
              $link .= "(-)";
          } elsif ( $olink < $link ) {
              $link .= "(+)";
          } else {
              $link .= "(=)";
          }
      }
	  $ifs{$ifname} = "$link\0$signal\0$noise";
	}

	$text = "";
	for my $ifn ( sort keys %ifs ) {
	  $text .= sprintf( "%s: %d %d %d", $ifn, split( "\0", $ifs{$ifn}));
	}

	# stash the values
	$mon->{'ifs'} = \%ifs;

  } else {
	$text = $!;
  }

  $text;
}
