#!/usr/bin/perl

# Simple script to set noop scheduler for all SSDs

use strict;

# All SSD models we're currently using
my @ssds = (
             qr/\s*SSD.+INTEL/,
             qr/\s*SAMSUNG.+MPP\-0VA/,
             qr/\s*MTRON MS(D|P)/,
             qr/\s*SAMSUNG\s470\sSeries\sSSD/,
             qr/\s*D2CSTK251M11-0240/
           );
my $tw = '/usr/local/sbin/tw_cli';
my $megacli = q{/usr/sbin/MegaCli ldpdinfo aall | grep -E 'Adapter|Inquiry Data|Virtual Drive'};
my $controller = {};

# Hide STDERR
open(STDERR,">/dev/null");

# Get 3ware information and unit/drive mappings for SSDs
if (open OUT, "$tw show |") {
  while (<OUT>) {
    if (/^c(\d+)\s+/) {
      my $c = $1;
      # Show details of each 3ware controller
      if (open OUT2,"$tw /c$c show |") {
        while (<OUT2>) {
          # We only care about ports assigned to units
          if (/^p(\d+)\s+\w+\s+u(\d+)\s+/) {
            my $p = $1;
            my $u = $2;
            # Get drive model information
            if (open OUT3, "$tw /c$c/p$p show model |") {
              while (<OUT3>) {
                if (/Model\s*\=\s*/) {
                  foreach my $e (@ssds) {
                    # Store information on controller and unit if qualifies for noop
                    $controller->{$c}{units}{$u} = 1 if ($_ =~ /$e/);
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  close(OUT);
}

# Get LSI information and unit/drive mappings for SSDs
if (open OUT, "$megacli |") {
  my $adapter = "";
  my $targetid = "";
  my $lsicontroller = {};
  while (<OUT>) {
    if (/^Adapter #(\d+)/) {
      $adapter = $1;
    } elsif (/^Virtual Drive:.+?\(Target Id: (\d+)\)/) {
      $targetid = $1;
    } elsif (/^Inquiry Data:/) {
      if (($adapter =~ /^\d+$/) && ($targetid =~ /^\d+$/)) {
        foreach my $e (@ssds) {
          # Store information on controller and unit if qualifies for noop
          $lsicontroller->{$adapter}{units}{$targetid} = 1 if ($_ =~ /$e/);
        }
      }
    }
  }
  close(OUT);
  if (keys %{$lsicontroller}) {
    # Generate scsi_host list
    if (open OUT, "/usr/bin/lsscsi | grep disk | grep LSI |") {
      my $scsi_host = {};
      my $cnt = 0;
      while (<OUT>) {
        if (/\[(\d+):\d+:\d+:\d+\]/) {
          next if (exists $scsi_host->{$1});
          $scsi_host->{$1} = $cnt;
          ++$cnt;
        }
      }
      close(OUT);
      # Associate scsi_host to lsi controller
      if (keys %{$scsi_host}) {
        foreach my $c (keys %{$lsicontroller}) {
          foreach my $hostid (keys %{$scsi_host}) {
            if ($c == $scsi_host->{$hostid}) {
              foreach my $u (keys %{$lsicontroller->{$c}{units}}) {
                $controller->{$hostid}{units}{$u} = 1;
              }
            }
          }
        }
      }
    }
  }
}

# Check all sd device models for onboard/3ware/LSI and set NOOP if qualifies
if (opendir(BLOCK,"/sys/block")) {
  while (my $dev = readdir(BLOCK)) {
    next unless ($dev =~ /^sd\D+$/);
    my $onboard = 0;
    my $setnoop = 0;
    # Only onboard drives will return models on hdparm
    if (open HDPARM, "/sbin/hdparm -i /dev/$dev |") {
      while (<HDPARM>) {
        my $line = $_;
        if (/Model\s*\=\s*/) {
          $onboard = 1;
          foreach my $e (@ssds) {
            $setnoop = 1 if ($line =~ /$e/);
          }
        }
      }
    }
    # Must be 3ware or LSI, check if the scsi device maps to qualified 3ware ports
    unless ($onboard) {
      if (opendir(DEVICE,"/sys/block/$dev/device/scsi_device")) {
        while (my $scsi_device = readdir(DEVICE)) {
          if ($scsi_device =~ /^(\d+)\:\d+\:(\d+)\:\d+$/) {
            $setnoop = 1 if (exists $controller->{$1}{units}{$2});
          }
        }
      }
    }
    # If device qualified, set NOOP unless already set
    if ($setnoop) {
      if (open SCHED,"/sys/block/$dev/queue/scheduler") {
        `echo noop > /sys/block/$dev/queue/scheduler` if (<SCHED> !~ /\[noop\]/);
      }
    }
  }
}

close(STDERR);
