]> arthur.barton.de Git - netdata.git/blobdiff - charts.d/hddtemp.chart.sh
Merge pull request #1146 from ktsaou/master
[netdata.git] / charts.d / hddtemp.chart.sh
index e42db092b99ade3c802dcbecd3a2bb3811bd9038..15895c5e160a71a845a586ab1f602492b26b3061 100755 (executable)
@@ -1,4 +1,11 @@
-#!/bin/bash
+# no need for shebang - this file is loaded from charts.d.plugin
+
+# netdata
+# real-time performance and health monitoring, done right!
+# (C) 2016 Costa Tsaousis <costa@tsaousis.gr>
+# GPL v3+
+#
+# contributed by @paulfantom with PR #511
 
 # if this chart is called X.chart.sh, then all functions and global variables
 # must start with X_
@@ -13,7 +20,8 @@ hddtemp_priority=90000
 
 # _check is called once, to find out if this chart should be enabled or not
 hddtemp_check() {
-       nc $hddtemp_host $hddtemp_port &>/dev/null && return 0 || return 1
+    require_cmd nc || return 1
+       run nc $hddtemp_host $hddtemp_port && return 0 || return 1
 }
 
 # _create is called once, to create the charts
@@ -24,12 +32,13 @@ hddtemp_create() {
                unset hddtemp_disks
                hddtemp_disks=( `grep -Po '/dev/[^|]+' <<< "$all" | cut -c 6-` )
        fi
-       local disk_names
-       disk_names=(`sed -e 's/||/\n/g;s/^|//' <<< "$all" | cut -d '|' -f2 | tr ' ' '_'`)
+#      local disk_names
+#      disk_names=(`sed -e 's/||/\n/g;s/^|//' <<< "$all" | cut -d '|' -f2 | tr ' ' '_'`)
 
        echo "CHART hddtemp.temperature 'disks_temp' 'temperature' 'Celsius' 'Disks temperature' 'hddtemp.temp' line $((hddtemp_priority)) $hddtemp_update_every"
        for i in `seq 0 $((${#hddtemp_disks[@]}-1))`; do
-               echo "DIMENSION ${hddtemp_disks[i]} ${disk_names[i]} absolute 1 1"
+#              echo "DIMENSION ${hddtemp_disks[i]} ${disk_names[i]} absolute 1 1"
+               echo "DIMENSION ${hddtemp_disks[$i]} '' absolute 1 1"
        done
        return 0
 }
@@ -38,14 +47,26 @@ hddtemp_create() {
 hddtemp_last=0
 hddtemp_count=0
 hddtemp_update() {
-       #local all
+#        local all=( `nc $hddtemp_host $hddtemp_port | sed -e 's/||/\n/g;s/^|//' | cut -d '|' -f3` )
+#      local all=( `nc $hddtemp_host $hddtemp_port | awk 'BEGIN { FS="|" };{i=4; while (i <= NF) {print $i+0;i+=5;};}'` )
+       OLD_IFS=$IFS
+       set -f
+       IFS="|" all=( $(nc $hddtemp_host $hddtemp_port 2>/dev/null) )
+       set +f
+       IFS=$OLD_IFS
 
-        local all=( `nc $hddtemp_host $hddtemp_port | sed -e 's/||/\n/g;s/^|//' | cut -d '|' -f3` )
+       # check if there is some data
+       if [ -z "${all[3]}" ]; then 
+               return 1
+       fi
 
        # write the result of the work.
        echo "BEGIN hddtemp.temperature $1"
-       for i in `seq 0 $((${#hddtemp_disks[@]}-1))`; do
-               echo "SET ${hddtemp_disks[i]} = ${all[i]}"
+       end=${#hddtemp_disks[@]}
+       for ((i=0; i<end; i++)); do
+               # temperature - this will turn SLP to zero
+                t=$(( ${all[ $((i * 5 + 3)) ]} ))
+               echo "SET ${hddtemp_disks[$i]} = $t"
        done
        echo "END"