smartctl 的返回值是由8个比特位组成的 , 具体的解释参考本为末尾部分。
使用smartctl -H可以根据磁盘已经收集的信息检测磁盘的监控状态。返回的是一个数值类型。
返回值具体代表什么意思需要翻译成8个比特位,并查询smartctl的RETURN VALUES。
如果smartctl的输出了failing的健康状态的话, 表示磁盘已经failed或者将要在24小时内failed了. 
1. 首先确保开启硬盘的几个选项 :

smartctl -S on -s on -o on /dev/sdd
[root@db-172-16-3-150 ~]# /opt/smartmontools-6.0/sbin/smartctl -S on -s on -o on /dev/sdd
smartctl 6.0 2012-10-10 r3643 [x86_64-linux-2.6.18-274.el5] (local build)
Copyright (C) 2002-12, Bruce Allen, Christian Franke, www.smartmontools.org=== START OF ENABLE/DISABLE COMMANDS SECTION ===
SMART Enabled.
SMART Attribute Autosave Enabled.
SMART Automatic Offline Testing Enabled every four hours.

2. smartctl健康检查选项 :

       -H, --healthCheck: Ask the device to report its SMART health status or pending TapeAlert messages.  SMART status  isbased  on  information  that  it  has  gathered from online and offline tests, which were used to deter-mine/update its SMART vendor-specific Attribute values. TapeAlert status  is  obtained  by  reading  theTapeAlert log page.If  the  device  reports failing health status, this means either that the device has already failed, orthat it is predicting its own failure within the next 24 hours.  If this happens, use the ′-a′ option toget more information, and get your data off the disk and to someplace safe as soon as you can.

健康检查示例 :

[root@db-172-16-3-150 ~]# /opt/smartmontools-6.0/sbin/smartctl -H /dev/sdd
smartctl 6.0 2012-10-10 r3643 [x86_64-linux-2.6.18-274.el5] (local build)
Copyright (C) 2002-12, Bruce Allen, Christian Franke, www.smartmontools.org=== START OF READ SMART DATA SECTION ===
SMART overall-health self-assessment test result: PASSED
返回值 :
[root@db-172-16-3-150 ~]# echo $?
0

或者仅输出错误 :

[root@db-172-16-3-150 ~]# /opt/smartmontools-6.0/sbin/smartctl -H -q errorsonly /dev/sdd
[root@db-172-16-3-150 ~]# 

3. 返回值解析脚本 :

[root@db-172-16-3-150 ~]# cat smart.sh
/opt/smartmontools-6.0/sbin/smartctl -H -d sat+megaraid,2 /dev/sdb
status=$?
for ((i=0; i<8; i++)); doecho "Bit $i: $((status & 2**i && 1))"
done

执行这个shell script :

[root@db-172-16-3-150 ~]# . ./smart.sh
smartctl 6.0 2012-10-10 r3643 [x86_64-linux-2.6.18-274.el5] (local build)
Copyright (C) 2002-12, Bruce Allen, Christian Franke, www.smartmontools.org=== START OF READ SMART DATA SECTION ===
SMART overall-health self-assessment test result: PASSED
Warning: This result is based on an Attribute check.Bit 0: 0
Bit 1: 0
Bit 2: 1
Bit 3: 0
Bit 4: 0
Bit 5: 0
Bit 6: 0
Bit 7: 0
以上这些比特位的含义见参考部分. 
这个结果是PASSWD的但是返回值不是0, 而是4. (bit2 :1).
从源码ataprint.cpp部分来看 : 
ataSmartStatus2(device) 这里的 ataSmartStatus2(device) 返回值并非0或-1.
所以到了如下代码段 :

    default:// Something went wrong with the SMART STATUS command.// The ATA SMART RETURN STATUS command provides the result in the ATA output// registers. Buggy ATA/SATA drivers and SAT Layers often do not properly// return the registers values.failuretest(OPTIONAL_CMD, returnval|=FAILSMART);if (!(smart_val_ok && smart_thres_ok)) {print_on();pout("SMART overall-health self-assessment test result: UNKNOWN!\n""SMART Status, Attributes and Thresholds cannot be read.\n\n");}else if (find_failed_attr(&smartval, &smartthres, attribute_defs, 1)) {print_on();pout("SMART overall-health self-assessment test result: FAILED!\n""Drive failure expected in less than 24 hours. SAVE ALL DATA.\n");print_off();returnval|=FAILATTR;returnval|=FAILSTATUS;if (options.smart_vendor_attrib)pout("See vendor-specific Attribute list for failed Attributes.\n\n");else {print_on();pout("Failed Attributes:\n");PrintSmartAttribWithThres(&smartval, &smartthres, attribute_defs, rpm, 1, options.output_format);}}else {pout("SMART overall-health self-assessment test result: PASSED\n");pout("Warning: This result is based on an Attribute check.\n");

【参考】

1. http://www.linuxjournal.com/article/6983

2. RETURN VALUES

       The  return  values of smartctl are defined by a bitmask.  If all is well with the disk, the return value (exitstatus) of smartctl is 0 (all bits turned off).  If a problem occurs, or an error, potential error, or fault isdetected,  then a non-zero status is returned.  In this case, the eight different bits in the return value havethe following meanings for ATA disks; some of these values may also be returned for SCSI disks.Bit 0: Command line did not parse.Bit 1: Device open failed, device did not return an IDENTIFY DEVICE structure, or device is in a low-power mode(see ′-n′ option above).Bit 2: Some SMART or other ATA command to the disk failed, or there was a checksum error in a SMART data struc-ture (see ′-b′ option above).Bit 3: SMART status check returned "DISK FAILING".Bit 4: We found prefail Attributes <= threshold.Bit 5: SMART status check returned "DISK OK" but we found that some (usage or prefail) Attributes have been  <=threshold at some time in the past.Bit 6: The device error log contains records of errors.Bit 7: The  device  self-test log contains records of errors.  [ATA only] Failed self-tests outdated by a newersuccessful extended self-test are ignored.To test within the shell for whether or not the different bits are turned on or off, you can use the  followingtype of construction (this is bash syntax):smartstat=$(($? & 8))This  looks  at  only  at  bit  3  of the exit status $?  (since 8=2^3).  The shell variable $smartstat will benonzero if SMART status check returned "disk failing" and zero otherwise.This bash script prints all status bits:status=$?for ((i=0; i<8; i++)); doecho "Bit $i: $((status & 2**i && 1))"done

smartctl command's RETURN VALUES相关推荐

  1. QMetaMethod::invoke: Unable to invoke methods with return values in queued connections

    报错信息为: QMetaMethod::invoke: Unable to invoke methods with return values in queued connections 解决方法是: ...

  2. 【QML】C++访问QML函数获取返回值时报:Unable to invoke methods with return values in queued connections

    QMetaMethod::invoke: Unable to invoke methods with return values in queued connections 相关资料可以参考:http ...

  3. mysql stored procedures with return values

    存储过程的功能非常强大,在某种程度上甚至可以替代业务逻辑层, 接下来就一个小例子来说明,用存储过程插入或更新语句. 1.数据库表结构 所用数据库为Sql Server2008. 2.创建存储过程 (1 ...

  4. WPF 使用Command,CommandParameter向 MVVM传递多个参数

    有的时候需要向ViewModel传递两个CommandParameter参数.需要实现一个Converter.然后就可以实现多个参数传递啦.本案例以MVVM为基础做的Demo.还有一个编写的按钮样式的 ...

  5. [UWP]了解模板化控件(7):支持Command

    原文:[UWP]了解模板化控件(7):支持Command 以我的经验来说,要让TemplatedControl支持Command的需求不会很多,大部分情况用附加属性解决这个需求会更便利些,譬如UWPC ...

  6. 【白话设计模式八】命令模式(Command)

    为什么80%的码农都做不了架构师?>>>    #0 系列目录# 白话设计模式 工厂模式 单例模式 [白话设计模式一]简单工厂模式(Simple Factory) [白话设计模式二] ...

  7. HashMap.values().addAll()异常

    问题:在使用一个Collection.addAll()方法时报告UnsupportedOperationException.追溯原因发现该collection是来自于hashMap.values(). ...

  8. 10+ commonly using find command switches with example Unix/Linux

    http://crybit.com/find-command-usage-with-example-unixlinux/ find command is one of the best search ...

  9. 设计模式--命令(Command)模式

    模式定义 将一个请求(行为)封装为一个对象,从而使你可用不同的请求对客户进行参数化,对请求排队或记录请求日志,以及支持可撤销的操作 类图 要点总结 Command模式的根本目的在于将"行为请 ...

最新文章

  1. 帧中继-实验(第三篇)
  2. java list详解_Java 中 list 用法案例详解
  3. mesh和wifi中继的区别_小米官方科普路由器Mesh功能,它和普通的中继有什么区别...
  4. ARM LDR与MOV的区别
  5. Java返回int型的空值_使用MyBatis查询int类型字段,返回NULL值时报异常的解决方法...
  6. GCC的内嵌汇编语法 ATT汇编语言语法
  7. 最长回文子串--动态规划
  8. C# HttpWebRequest 填充IP代理
  9. 洛谷1004方格取数
  10. MySQL int类型的显示
  11. NIVC-gt;IPR[IPADDR]
  12. java 集合教程_Java Collections
  13. 1. 制作软盘启动镜像.md
  14. CPU卡密钥管理系统
  15. d520笔记本linux无线网卡驱动下载,无线网卡万能驱动
  16. java8中的闭包Function/BiFunction
  17. 使用 Kind 在 5 分钟内快速部署一个 Kubernetes 高可用集群
  18. android 钉钉考勤日历,Flutter仿钉钉考勤日历
  19. 爬虫与数据分析(二)
  20. mfc程序退出时删除托盘图标

热门文章

  1. 响应式web设计之CSS3 Media Queries
  2. 100+诡异的数据集,20万Eclipse Bug、死囚遗言等
  3. 为什么st2 chrome无法显示api中的例子
  4. 统计字符串中个字符的个数
  5. mysql连接被拒绝 密码也对_解决Mysql数据库拒绝远程连接和忘记密码的问题
  6. Android中图像变换Matrix的原理应用
  7. Android Lint 检查规则的定制(基本篇)
  8. 大规模细粒度分类和特定领域的迁移学习
  9. 图说 WebAssembly(二):JIT 编译器
  10. 密码学研究-加密解密