// File: /workspace/github_cpp_makefile_star2_O2/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 0x15df(%rip), %rax      # 0x424f
xorl %r14d, %r14d
leaq 0x15a3(%rip), %r13      # 0x421d
leaq 0x44ff(%rip), %rbx      # 0x7180
movq 0x4338(%rip), %r15      # 0x6fc0
movq %rax, 0x30(%rsp)
movq %rax, 0x28(%rsp)
movl %ebp, %edi
movq %r12, %rsi
movq %r13, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x2260
cmpl $0x64, %eax
jne 0x2caf
movq (%r15), %r14
jmp 0x2c92
cmpl $-0x1, %eax
je 0x2cd1
cmpl $0x63, %eax
je 0x2ccc
cmpl $0x77, %eax
jne 0x2fb0
movq (%r15), %rax
movq %rax, 0x30(%rsp)
jmp 0x2c92
movq (%r15), %rax
jmp 0x2c8d
testq %r14, %r14
je 0x2fda
leaq 0xd0(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x78(%rsp), %rdi
leaq 0x280(%rsp), %rdx
movq 0x30(%rsp), %rsi
callq 0x31d2
leaq 0xc8(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x2a2a
movl %eax, %ebx
leaq 0x78(%rsp), %rdi
callq 0x20a0
testb %bl, %bl
je 0x2fe4
leaq 0xa0(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x58(%rsp), %rdi
leaq 0x280(%rsp), %rdx
movq 0x28(%rsp), %rsi
callq 0x31d2
leaq 0x98(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x2a2a
movl %eax, %ebx
leaq 0x58(%rsp), %rdi
callq 0x20a0
testb %bl, %bl
je 0x2fee
pushq $0x2
popq %rsi
movq %r14, %rdi
xorl %eax, %eax
callq 0x2120
movl %eax, %ebx
cmpl $-0x1, %eax
je 0x2ff8
leaq 0x24(%rsp), %rdx
movl $0x2282, %esi           # imm = 0x2282
movl %ebx, %edi
xorl %eax, %eax
callq 0x2210
cmpl $-0x1, %eax
je 0x3023
cmpl $0x752f, 0x24(%rsp)     # imm = 0x752F
jle 0x3023
leaq 0x280(%rsp), %rsi
movl %ebx, %edi
callq 0x2663
testb %al, %al
je 0x305a
testb $0x1, 0x324(%rsp)
je 0x307a
testb $0x1, 0x32a(%rsp)
je 0x309a
andl $0x0, 0xc(%rsp)
andl $0x0, 0x20(%rsp)
andl $0x0, 0x1c(%rsp)
andl $0x0, 0x18(%rsp)
andl $0x0, 0x14(%rsp)
andl $0x0, 0x10(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x2100
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 0x2792
addq $0x20, %rsp
leaq 0xc(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movl %ebx, %edi
callq 0x297d
movslq 0xc(%rsp), %r14
leaq 0x14ec(%rip), %rax      # 0x437c
movslq (%rax,%r14,4), %rsi
addq %rax, %rsi
movq 0x4132(%rip), %rdi      # 0x6fd0
callq 0x2180
leaq 0x1452(%rip), %rsi      # 0x42fc
movq %rax, %rdi
callq 0x2180
movl 0x20(%rsp), %esi
movq %rax, %rdi
callq 0x2280
leaq 0x1442(%rip), %rsi      # 0x4307
movq %rax, %rdi
callq 0x2180
movl 0x1c(%rsp), %esi
movq %rax, %rdi
callq 0x2280
leaq 0x1433(%rip), %rsi      # 0x4313
movq %rax, %rdi
callq 0x2180
movl 0x18(%rsp), %esi
movq %rax, %rdi
callq 0x2280
leaq 0x1424(%rip), %rsi      # 0x431f
movq %rax, %rdi
callq 0x2180
movl 0x14(%rsp), %esi
movq %rax, %rdi
callq 0x2280
leaq 0x1414(%rip), %rsi      # 0x432a
movq %rax, %rdi
callq 0x2180
movl 0x10(%rsp), %esi
movq %rax, %rdi
callq 0x2280
leaq 0x1401(%rip), %rsi      # 0x4332
movq %rax, %rdi
callq 0x2180
movq %rax, %r15
leaq 0x110(%rsp), %rsi
leaq 0x38(%rsp), %rdi
callq 0x2240
leaq 0x38(%rsp), %rsi
movq %r15, %rdi
callq 0x2170
movq %rax, %rdi
callq 0x20c0
leaq 0x38(%rsp), %rdi
callq 0x20a0
movl %ebx, %edi
callq 0x22c0
leaq 0xf8(%rsp), %rdi
callq 0x2130
leaq 0x98(%rsp), %rdi
callq 0x3218
leaq 0xc8(%rsp), %rdi
callq 0x3218
movl %r14d, %eax
addq $0x488, %rsp            # imm = 0x488
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x56, %eax
je 0x2fc1
cmpl $0x68, %eax
jne 0x2fcd
callq 0x249a
jmp 0x2fc6
callq 0x241c
xorl %edi, %edi
callq 0x21c0
callq 0x2457
pushq $0x1
popq %rdi
callq 0x21c0
callq 0x249a
jmp 0x30b5
callq 0x249a
jmp 0x30b5
callq 0x249a
jmp 0x30b5
movq 0x3ff1(%rip), %rdi      # 0x6ff0
leaq 0x1220(%rip), %rsi      # 0x4226
callq 0x2180
movq %rax, %rdi
movq %r14, %rsi
callq 0x2180
movq %rax, %rdi
callq 0x20c0
jmp 0x30b5
movq 0x3fc6(%rip), %rdi      # 0x6ff0
leaq 0x1215(%rip), %rsi      # 0x4246
callq 0x2180
movq %rax, %rdi
movq %r14, %rsi
callq 0x2180
leaq 0x1208(%rip), %rsi      # 0x4250
movq %rax, %rdi
callq 0x2180
movq %rax, %rdi
callq 0x20c0
jmp 0x30b5
movq 0x3f6f(%rip), %rdi      # 0x6fd0
leaq 0x121a(%rip), %rsi      # 0x4282
callq 0x2180
movq %rax, %rdi
callq 0x20c0
jmp 0x2fc6
movq 0x3f4f(%rip), %rdi      # 0x6fd0
leaq 0x121a(%rip), %rsi      # 0x42a2
callq 0x2180
movq %rax, %rdi
callq 0x20c0
jmp 0x2fc6
movq 0x3f2f(%rip), %rdi      # 0x6fd0
leaq 0x121c(%rip), %rsi      # 0x42c4
callq 0x2180
movq %rax, %rdi
callq 0x20c0
pushq $0x3
jmp 0x2fd4
jmp 0x30fe
jmp 0x30e6
jmp 0x30fe
jmp 0x3103
jmp 0x30fe
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x20a0
jmp 0x3113
jmp 0x30fe
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x20a0
jmp 0x3120
movq %rax, %rbx
jmp 0x3120
jmp 0x30fe
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x20a0
jmp 0x3106
jmp 0x3103
movq %rax, %rbx
jmp 0x3113
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x2130
leaq 0x98(%rsp), %rdi
callq 0x3218
leaq 0xc8(%rsp), %rdi
callq 0x3218
movq %rbx, %rdi
callq 0x2290
nop