seren / freenas-temperature-graphing

Scripts to graph FreeNAS CPU and drive temperatures
Other
77 stars 14 forks source link

Re: https://github.com/seren/freenas-temperature-graphing/issues/10 #11

Closed hexadecagram closed 7 years ago

hexadecagram commented 7 years ago

Modified data collection, graph generation, and format scripts to better accommodate non-S.M.A.R.T. drives, min and max safe temperature limits for mechanical drives, and a wider overall temperature range, and to produce accurate verbose output, and create graphs with more readable curves.

seren commented 7 years ago

Cool. Could you explain the awk code?

BEGIN         { attr=0; temp=0 }
$1 ~ /19[04]/ { if (attr != "194") { attr=$1; temp=$10 } }
END           { print temp }

It looks like it operates on lines beginning with 190 or 194, but I'm not sure what the conditional is doing.

hexadecagram commented 7 years ago

It's a tricky bit of code and yeah it's not completely obvious as to how it works. You could massage the following into a comment before you commit if you like.

You're correct. The second line of the AWK program matches the first field of the smartctl output if it is 190 or 194. We have to consider 5 cases with the program:

  1. The device neither reports attribute 190 nor 194, which results in the whole AWK script matching nothing with the result of DevTemp being set to '' (sanity check failed, so nothing is graphed for this device). Actually, the END block may end up setting DevTemp to 0, which we do not want. Would be safer to modify the following lines in the script (the one with the conditional is fine): BEGIN { attr=0; temp=-273.15; } END { if (temp != "-273.15") print temp; } We should use absolute zero (-273.15 degrees Celsius) because it would be impossible for a drive to be that cold. But in Antarctica? It could be really cold.
  2. The device only reports attribute 190. The temp variable is initialized to 0 in the BEGIN block so it gets set and attr is set to 190. The END block then prints temp, setting DevTemp to the temperature reported by attribute 190.
  3. The device only reports attribute 194. The temp variable is initialized to 0 so it gets set and attr is set to 194.
  4. The device reports attribute 190 before 194. The temp variable is initialized to 0 so on the first match it gets set and attr is set to 190. On the second match, $1 is 194 so temp is not overwritten and attr remains set to 190, so the temperature reported by attribute 190 is given preference.
  5. The device reports attribute 194 before 190. The temp variable is initialized to 0 so on the first match it gets set and attr is set to 194. On the second match, $1 is 190 so temp gets overwritten and attr is set to 190, so once again the temperature reported by attribute 190 is given preference.
seren commented 7 years ago

Ahh, ok. So it's setting the temperature from matching lines, giving precedence to attribute 190. When I tried it, it output a temperature of zero for my usb non-SMART drive. I think that if it doesn't find a match, the output is the default value of temp which is 0. I tried setting it -99 (which should never be a valid temperature) and catching that in the loop which worked, but I'm not sure if you can think of a better method:

BEGIN         { attr=0; temp=-99 }
$1 ~ /19[04]/ { if (attr != "194") { attr=$1; temp=$10 } }
END           { print temp }
    if ! [ "$DevTemp" == "-99" ]; then
      drivedevs="${drivedevs} ${i}"
      [ -n "$verbose" ] && echo "drivedevs: ${drivedevs}"
    fi
hexadecagram commented 7 years ago

I edited my comment with a solution because I thought of exactly that after the fact. ;-)

Note that I also changed the END block rather than modify the outer bash script, as you did. Would work either way, but IMO having it in the AWK program makes it cleaner and more obvious without having to explain it in a comment.

seren commented 7 years ago

Cool, I agree that having it in the awk script is cleaner. I'll merge this and add the change. I'll probably stick with the -99 rather than -273.15 since it's a bit easier to read/understand and I can't imagine any FreeNAS server drive being either temperature while being turned on. :)

hexadecagram commented 7 years ago

That's probably me just letting my FreeBSD side show through. Very scientifically-minded bunch. But you're right, it's not likely that they use FreeNAS on the space shuttle. And even if they did, hey, it's open source.