// File: /workspace/github_cpp_makefile_star2_O3/spjmurray[P]nagios-plugin-check-scsi-smart/check_scsi_smart.cc
// Function: main
int main(int argc, char** argv) {

  const char* device = 0;
  const char* warning = "";
  const char* critical = "";

  static struct option long_options[] = {
    { "help",     no_argument,       0, 'h' },
    { "version",  no_argument,       0, 'V' },
    { "device",   required_argument, 0, 'd' },
    { "warning",  required_argument, 0, 'w' },
    { "critical", required_argument, 0, 'c' },
    { 0,          0,                 0, 0   }
  };

  int c;
  while((c = getopt_long(argc, argv, "hVd:w:c:", long_options, 0)) != -1) {
    switch(c) {
      case 'h':
        help();
        exit(0);
      case 'V':
        version();
        exit(0);
      case 'd':
        device = optarg;
        break;
      case 'w':
        warning = optarg;
        break;
      case 'c':
        critical = optarg;
        break;
      default:
        usage();
        exit(1);
        break;
    }
  }

  // Check for required arguments
  if(!device) {
    help();
    exit(NAGIOS_UNKNOWN);
  }

  // Parse optional arguments
  SmartThresholdMap warning_thresholds;
  if(!parse_thresholds(warning_thresholds, warning)) {
    help();
    exit(NAGIOS_UNKNOWN);
  }

  SmartThresholdMap critical_thresholds;
  if(!parse_thresholds(critical_thresholds, critical)) {
    help();
    exit(NAGIOS_UNKNOWN);
  }

  // Check the device is compatible with the check
  int fd = open(device, O_RDWR);
  if(fd == -1) {
    cerr << "UNKNOWN: unable to open device " << device << endl;
    exit(NAGIOS_UNKNOWN);
  }

  int sg_version;
  if((ioctl(fd, SG_GET_VERSION_NUM, &sg_version) == -1) || sg_version < 30000) {
    cerr << "UNKNOWN: " << device << " is either not an sg device, or the driver is old" << endl;
    exit(NAGIOS_UNKNOWN);
  }

  // Check the device can use SMART and that it is enabled
  uint16_t identify[SECTOR_SIZE / 2];
  if(!ata_identify(fd, reinterpret_cast<unsigned char*>(identify))) {
    cout << "OK: ATA command set unsupported" << endl;
    exit(NAGIOS_OK);
  }

  if(~StorageEndian::swap(identify[82]) & 0x01) {
    cout << "OK: SMART feature set unsupported" << endl;
    exit(NAGIOS_OK);
  }

  if(~StorageEndian::swap(identify[85]) & 0x01) {
    cout << "UNKNOWN: SMART feature set disabled" << endl;
    exit(NAGIOS_UNKNOWN);
  }

  int code = NAGIOS_OK;
  int prdfail = 0;
  int advisory = 0;
  int crit = 0;
  int warn = 0;
  int logs = 0;
  stringstream perfdata;

  // Perform the checks
  check_smart_attributes(fd, critical_thresholds, warning_thresholds, code, prdfail, advisory, crit, warn, perfdata);
  check_smart_log(fd, code, logs);

  // Print out the results and performance data
  const char* status[] = { "OK", "WARNING", "CRITICAL" };
  cout << status[code]
       << ": prdfail " << prdfail
       << ", advisory " << advisory
       << ", critical " << crit
       << ", warning " << warn
       << ", logs " << logs
       << " |" << perfdata.str()
       << endl;

  close(fd);

  return code;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x488, %rsp            # imm = 0x488
movq %rsi, %r12
movl %edi, %ebp
leaq 0x1139(%rip), %rax      # 0x624f
xorl %r14d, %r14d
leaq 0x10fd(%rip), %r13      # 0x621d
leaq 0x4059(%rip), %rbx      # 0x9180
movq 0x3e82(%rip), %r15      # 0x8fb0
movq %rax, 0x30(%rsp)
movq %rax, 0x28(%rsp)
movl %ebp, %edi
movq %r12, %rsi
movq %r13, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x4260
cmpl $0x64, %eax
jne 0x5155
movq (%r15), %r14
jmp 0x5138
cmpl $0x62, %eax
jle 0x5177
cmpl $0x63, %eax
je 0x5172
cmpl $0x77, %eax
jne 0x5542
movq (%r15), %rax
movq %rax, 0x30(%rsp)
jmp 0x5138
movq (%r15), %rax
jmp 0x5133
cmpl $-0x1, %eax
jne 0x554e
testq %r14, %r14
je 0x556e
leaq 0xd0(%rsp), %rax
movl $0x0, (%rax)
xorl %ecx, %ecx
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rcx, 0x20(%rax)
leaq 0x78(%rsp), %rdi
leaq 0x280(%rsp), %rdx
movq 0x30(%rsp), %rsi
callq 0x5874
leaq 0xc8(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x4df7
movl %eax, %ebx
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x51f5
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x41a0
testb %bl, %bl
je 0x5578
leaq 0xa0(%rsp), %rax
movl $0x0, (%rax)
xorl %ecx, %ecx
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rcx, 0x20(%rax)
leaq 0x58(%rsp), %rdi
leaq 0x280(%rsp), %rdx
movq 0x28(%rsp), %rsi
callq 0x5874
leaq 0x98(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x4df7
movl %eax, %ebx
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5263
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x41a0
testb %bl, %bl
je 0x5582
movq %r14, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x4120
movl %eax, %ebx
cmpl $-0x1, %eax
je 0x558c
leaq 0x24(%rsp), %rdx
movl $0x2282, %esi           # imm = 0x2282
movl %ebx, %edi
xorl %eax, %eax
callq 0x4200
cmpl $-0x1, %eax
je 0x55cf
cmpl $0x752f, 0x24(%rsp)     # imm = 0x752F
jle 0x55cf
xorl %eax, %eax
leaq 0xf8(%rsp), %rsi
movq %rax, 0x3(%rsi)
movq %rax, 0x8(%rsi)
movb $-0x7b, (%rsi)
movw $0xe08, 0x1(%rsi)       # imm = 0xE08
movb $0x1, 0x6(%rsi)
movb $-0x14, 0xe(%rsi)
leaq 0x280(%rsp), %rcx
movl %ebx, %edi
movl $0x10, %edx
movl $0x200, %r8d            # imm = 0x200
callq 0x48aa
testb %al, %al
je 0x5627
testb $0x1, 0x324(%rsp)
je 0x5650
testb $0x1, 0x32a(%rsp)
je 0x5679
xorl %eax, %eax
movl %eax, 0xc(%rsp)
movl %eax, 0x20(%rsp)
movl %eax, 0x1c(%rsp)
movl %eax, 0x18(%rsp)
movl %eax, 0x14(%rsp)
movl %eax, 0x10(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x4100
leaq 0x108(%rsp), %rax
subq $0x8, %rsp
leaq 0x1c(%rsp), %r10
leaq 0x20(%rsp), %r11
leaq 0xa0(%rsp), %rsi
leaq 0xd0(%rsp), %rdx
leaq 0x14(%rsp), %rcx
leaq 0x28(%rsp), %r8
leaq 0x24(%rsp), %r9
movl %ebx, %edi
pushq %rax
pushq %r10
pushq %r11
callq 0x4a9b
addq $0x20, %rsp
leaq 0xc(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movl %ebx, %edi
callq 0x4d18
movslq 0xc(%rsp), %r14
leaq 0x31d7(%rip), %rax      # 0x8570
movq (%rax,%r14,8), %r15
movq %r15, %rdi
callq 0x40a0
movq 0x3c1c(%rip), %rdi      # 0x8fc8
movq %r15, %rsi
movq %rax, %rdx
callq 0x41c0
movq 0x3c0a(%rip), %rdi      # 0x8fc8
leaq 0xf37(%rip), %rsi       # 0x62fc
movl $0xa, %edx
callq 0x41c0
movl 0x20(%rsp), %esi
movq 0x3bee(%rip), %rdi      # 0x8fc8
callq 0x4280
movq %rax, %r15
leaq 0xf1e(%rip), %rsi       # 0x6307
movl $0xb, %edx
movq %rax, %rdi
callq 0x41c0
movl 0x1c(%rsp), %esi
movq %r15, %rdi
callq 0x4280
movq %rax, %r15
leaq 0xf07(%rip), %rsi       # 0x6313
movl $0xb, %edx
movq %rax, %rdi
callq 0x41c0
movl 0x18(%rsp), %esi
movq %r15, %rdi
callq 0x4280
movq %rax, %r15
leaq 0xef0(%rip), %rsi       # 0x631f
movl $0xa, %edx
movq %rax, %rdi
callq 0x41c0
movl 0x14(%rsp), %esi
movq %r15, %rdi
callq 0x4280
movq %rax, %r15
leaq 0xed8(%rip), %rsi       # 0x632a
movl $0x7, %edx
movq %rax, %rdi
callq 0x41c0
movl 0x10(%rsp), %esi
movq %r15, %rdi
callq 0x4280
movq %rax, %r15
leaq 0xebd(%rip), %rsi       # 0x6332
movl $0x2, %edx
movq %rax, %rdi
callq 0x41c0
leaq 0x110(%rsp), %rsi
leaq 0x38(%rsp), %rdi
callq 0x4240
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq %r15, %rdi
callq 0x41c0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x4170
movsbl %al, %esi
movq %r15, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x40e0
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x54eb
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x41a0
movl %ebx, %edi
callq 0x42c0
movq 0x3aa7(%rip), %rsi      # 0x8fa0
leaq 0xf8(%rsp), %rdi
callq 0x4130
leaq 0x178(%rsp), %rdi
callq 0x4070
leaq 0x98(%rsp), %rdi
callq 0x58bc
leaq 0xc8(%rsp), %rdi
callq 0x58bc
movl %r14d, %eax
addq $0x488, %rsp            # imm = 0x488
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x68, %eax
jne 0x555f
callq 0x452e
jmp 0x5558
cmpl $0x56, %eax
jne 0x555f
callq 0x442c
xorl %edi, %edi
callq 0x41d0
callq 0x4498
movl $0x1, %edi
callq 0x41d0
callq 0x452e
jmp 0x569d
callq 0x452e
jmp 0x569d
callq 0x452e
jmp 0x569d
movq 0x3a55(%rip), %rdi      # 0x8fe8
leaq 0xc8c(%rip), %rsi       # 0x6226
movl $0x1f, %edx
callq 0x41c0
movq %r14, %rdi
callq 0x40a0
movq 0x3a35(%rip), %rdi      # 0x8fe8
movq %r14, %rsi
movq %rax, %rdx
callq 0x41c0
movq 0x3a23(%rip), %rdi      # 0x8fe8
callq 0x40b0
jmp 0x569d
movq 0x3a12(%rip), %rdi      # 0x8fe8
leaq 0xc69(%rip), %rsi       # 0x6246
movl $0x9, %edx
callq 0x41c0
movq %r14, %rdi
callq 0x40a0
movq 0x39f2(%rip), %rdi      # 0x8fe8
movq %r14, %rsi
movq %rax, %rdx
callq 0x41c0
movq 0x39e0(%rip), %rdi      # 0x8fe8
leaq 0xc41(%rip), %rsi       # 0x6250
movl $0x31, %edx
callq 0x41c0
movq 0x39c8(%rip), %rdi      # 0x8fe8
callq 0x40b0
jmp 0x569d
movq 0x399a(%rip), %rdi      # 0x8fc8
leaq 0xc4d(%rip), %rsi       # 0x6282
movl $0x1f, %edx
callq 0x41c0
movq 0x3982(%rip), %rdi      # 0x8fc8
callq 0x40b0
jmp 0x5558
movq 0x3971(%rip), %rdi      # 0x8fc8
leaq 0xc44(%rip), %rsi       # 0x62a2
movl $0x21, %edx
callq 0x41c0
movq 0x3959(%rip), %rdi      # 0x8fc8
callq 0x40b0
jmp 0x5558
movq 0x3948(%rip), %rdi      # 0x8fc8
leaq 0xc3d(%rip), %rsi       # 0x62c4
movl $0x23, %edx
callq 0x41c0
movq 0x3930(%rip), %rdi      # 0x8fc8
callq 0x40b0
movl $0x3, %edi
callq 0x41d0
jmp 0x5706
jmp 0x56fd
jmp 0x5706
jmp 0x572b
jmp 0x5706
movq %rax, %rbx
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x574f
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x41a0
jmp 0x574f
jmp 0x5706
movq %rax, %rbx
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x575c
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x41a0
jmp 0x575c
movq %rax, %rbx
jmp 0x575c
jmp 0x5706
jmp 0x572b
movq %rax, %rbx
jmp 0x574f
movq %rax, %rbx
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x572e
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x41a0
jmp 0x572e
movq %rax, %rbx
movq 0x386b(%rip), %rsi      # 0x8fa0
leaq 0xf8(%rsp), %rdi
callq 0x4130
leaq 0x178(%rsp), %rdi
callq 0x4070
leaq 0x98(%rsp), %rdi
callq 0x58bc
leaq 0xc8(%rsp), %rdi
callq 0x58bc
movq %rbx, %rdi
callq 0x4290
nop