Merge pull request #3339 from jkohen/patch-1
[collectd.git] / contrib / exec-smartctl
1 #!/bin/bash
2
3 # Sample script for the exec plugin (collectd-exec(5))
4 #
5 # This script uses smartctl(8) to read HDD temperatures. The drives are
6 # attached to a 3ware RAID controller which hddtempd can't handle.
7 # Unfortunately the smartmontools don't have a library so we can't write a
8 # C-plugin, at least not easily.
9 # Please note that only root can read the SMART attributes from harddrives,
10 # because special ``capabilities'' are necessary. However, the exec plugin will
11 # refuse to run scripts as root, which is why `sudo' is used here for
12 # fine-grained root privileges for the user `smart'. This isn't as straigt
13 # forward as one might hope, but we think that the gained security is worth it.
14
15 # The sudo configuration looks something like this:
16 # -- 8< --
17 # Cmnd_Alias      SMARTCTL = /usr/sbin/smartctl -d 3ware\,0 -A /dev/twe0, /usr/sbin/smartctl -d 3ware\,1 -A /dev/twe0, /usr/sbin/smartctl -d ata -A /dev/sda
18 # smart   ALL = (root) NOPASSWD: SMARTCTL
19 # -- >8 --
20
21 HOSTNAME="${COLLECTD_HOSTNAME:-`hostname -f`}"
22 INTERVAL="${COLLECTD_INTERVAL:-60}"
23
24 while sleep "$INTERVAL"
25 do
26         TEMP=$((sudo smartctl -d 3ware,0 -A /dev/twe0 | grep Temperature_Celsius | awk '{ print $10; }') 2>/dev/null);
27         if [ $? -ne 0 ]
28         then
29                 TEMP="U"
30         fi
31         echo "PUTVAL $HOSTNAME/exec-smart/temperature-3ware_0 interval=$INTERVAL N:$TEMP"
32
33         TEMP=$((sudo smartctl -d 3ware,1 -A /dev/twe0 | grep Temperature_Celsius | awk '{ print $10; }') 2>/dev/null);
34         if [ $? -ne 0 ]
35         then
36                 TEMP="U"
37         fi
38         echo "PUTVAL $HOSTNAME/exec-smart/temperature-3ware_1 interval=$INTERVAL N:$TEMP"
39
40         TEMP=$((sudo smartctl -d ata -A /dev/sda | grep Temperature_Celsius | awk '{ print $10; }') 2>/dev/null);
41         if [ $? -ne 0 ]
42         then
43                 TEMP="U"
44         fi
45         echo "PUTVAL $HOSTNAME/exec-smart/temperature-sata_0 interval=$INTERVAL N:$TEMP"
46 done