sensors.chart.sh 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. # shellcheck shell=bash
  2. # no need for shebang - this file is loaded from charts.d.plugin
  3. # SPDX-License-Identifier: GPL-3.0-or-later
  4. # netdata
  5. # real-time performance and health monitoring, done right!
  6. # (C) 2016 Costa Tsaousis <costa@tsaousis.gr>
  7. #
  8. # sensors docs
  9. # https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
  10. # if this chart is called X.chart.sh, then all functions and global variables
  11. # must start with X_
  12. # the directory the kernel keeps sensor data
  13. sensors_sys_dir="${NETDATA_HOST_PREFIX}/sys/devices"
  14. # how deep in the tree to check for sensor data
  15. sensors_sys_depth=10
  16. # if set to 1, the script will overwrite internal
  17. # script functions with code generated ones
  18. # leave to 1, is faster
  19. sensors_source_update=1
  20. # how frequently to collect sensor data
  21. # the default is to collect it at every iteration of charts.d
  22. sensors_update_every=
  23. sensors_priority=90000
  24. declare -A sensors_excluded=()
  25. sensors_find_all_files() {
  26. find "$1" -maxdepth $sensors_sys_depth -name \*_input -o -name temp 2>/dev/null
  27. }
  28. sensors_find_all_dirs() {
  29. # shellcheck disable=SC2162
  30. sensors_find_all_files "$1" | while read; do
  31. dirname "$REPLY"
  32. done | sort -u
  33. }
  34. # _check is called once, to find out if this chart should be enabled or not
  35. sensors_check() {
  36. # this should return:
  37. # - 0 to enable the chart
  38. # - 1 to disable the chart
  39. [ -z "$(sensors_find_all_files "$sensors_sys_dir")" ] && error "no sensors found in '$sensors_sys_dir'." && return 1
  40. return 0
  41. }
  42. sensors_check_files() {
  43. # we only need sensors that report a non-zero value
  44. # also remove not needed sensors
  45. local f v excluded
  46. for f in "$@"; do
  47. [ ! -f "$f" ] && continue
  48. for ex in "${sensors_excluded[@]}"; do
  49. [[ $f =~ .*$ex$ ]] && excluded='1' && break
  50. done
  51. [ "$excluded" != "1" ] && v="$(cat "$f")" || v=0
  52. v=$((v + 1 - 1))
  53. [ $v -ne 0 ] && echo "$f" && continue
  54. excluded=
  55. error "$f gives zero values"
  56. done
  57. }
  58. sensors_check_temp_type() {
  59. # valid temp types are 1 to 6
  60. # disabled sensors have the value 0
  61. local f t v
  62. for f in "$@"; do
  63. # shellcheck disable=SC2001
  64. t=$(echo "$f" | sed "s|_input$|_type|g")
  65. [ "$f" = "$t" ] && echo "$f" && continue
  66. [ ! -f "$t" ] && echo "$f" && continue
  67. v="$(cat "$t")"
  68. v=$((v + 1 - 1))
  69. [ $v -ne 0 ] && echo "$f" && continue
  70. error "$f is disabled"
  71. done
  72. }
  73. # _create is called once, to create the charts
  74. sensors_create() {
  75. local path dir name x file lfile labelname device subsystem id type mode files multiplier divisor
  76. # we create a script with the source of the
  77. # sensors_update() function
  78. # - the highest speed we can achieve -
  79. [ $sensors_source_update -eq 1 ] && echo >"$TMP_DIR/sensors.sh" "sensors_update() {"
  80. for path in $(sensors_find_all_dirs "$sensors_sys_dir" | sort -u); do
  81. dir=$(basename "$path")
  82. device=
  83. subsystem=
  84. id=
  85. type=
  86. name=
  87. [ -h "$path/device" ] && device=$(readlink -f "$path/device")
  88. [ ! -z "$device" ] && device=$(basename "$device")
  89. [ -z "$device" ] && device="$dir"
  90. [ -h "$path/subsystem" ] && subsystem=$(readlink -f "$path/subsystem")
  91. [ ! -z "$subsystem" ] && subsystem=$(basename "$subsystem")
  92. [ -z "$subsystem" ] && subsystem="$dir"
  93. [ -f "$path/name" ] && name=$(cat "$path/name")
  94. [ -z "$name" ] && name="$dir"
  95. [ -f "$path/type" ] && type=$(cat "$path/type")
  96. [ -z "$type" ] && type="$dir"
  97. id="$(fixid "$device.$subsystem.$dir")"
  98. debug "path='$path', dir='$dir', device='$device', subsystem='$subsystem', id='$id', name='$name'"
  99. for mode in temperature voltage fans power current energy humidity; do
  100. files=
  101. multiplier=1
  102. divisor=1
  103. algorithm="absolute"
  104. case $mode in
  105. temperature)
  106. files="$(
  107. ls "$path"/temp*_input 2>/dev/null
  108. ls "$path/temp" 2>/dev/null
  109. )"
  110. files="$(sensors_check_files "$files")"
  111. files="$(sensors_check_temp_type "$files")"
  112. [ -z "$files" ] && continue
  113. echo "CHART 'sensors.temp_${id}_${name}' '' 'Temperature' 'Celsius' 'temperature' 'sensors.temp' line $((sensors_priority + 1)) $sensors_update_every '' '' 'sensors'"
  114. echo >>"$TMP_DIR/sensors.sh" "echo \"BEGIN 'sensors.temp_${id}_${name}' \$1\""
  115. divisor=1000
  116. ;;
  117. voltage)
  118. files="$(ls "$path"/in*_input 2>/dev/null)"
  119. files="$(sensors_check_files "$files")"
  120. [ -z "$files" ] && continue
  121. echo "CHART 'sensors.volt_${id}_${name}' '' 'Voltage' 'Volts' 'voltage' 'sensors.volt' line $((sensors_priority + 2)) $sensors_update_every '' '' 'sensors'"
  122. echo >>"$TMP_DIR/sensors.sh" "echo \"BEGIN 'sensors.volt_${id}_${name}' \$1\""
  123. divisor=1000
  124. ;;
  125. current)
  126. files="$(ls "$path"/curr*_input 2>/dev/null)"
  127. files="$(sensors_check_files "$files")"
  128. [ -z "$files" ] && continue
  129. echo "CHART 'sensors.curr_${id}_${name}' '' 'Current' 'Ampere' 'current' 'sensors.curr' line $((sensors_priority + 3)) $sensors_update_every '' '' 'sensors'"
  130. echo >>"$TMP_DIR/sensors.sh" "echo \"BEGIN 'sensors.curr_${id}_${name}' \$1\""
  131. divisor=1000
  132. ;;
  133. power)
  134. files="$(ls "$path"/power*_input 2>/dev/null)"
  135. files="$(sensors_check_files "$files")"
  136. [ -z "$files" ] && continue
  137. echo "CHART 'sensors.power_${id}_${name}' '' 'Power' 'Watt' 'power' 'sensors.power' line $((sensors_priority + 4)) $sensors_update_every '' '' 'sensors'"
  138. echo >>"$TMP_DIR/sensors.sh" "echo \"BEGIN 'sensors.power_${id}_${name}' \$1\""
  139. divisor=1000000
  140. ;;
  141. fans)
  142. files="$(ls "$path"/fan*_input 2>/dev/null)"
  143. files="$(sensors_check_files "$files")"
  144. [ -z "$files" ] && continue
  145. echo "CHART 'sensors.fan_${id}_${name}' '' 'Fans Speed' 'Rotations / Minute' 'fans' 'sensors.fans' line $((sensors_priority + 5)) $sensors_update_every '' '' 'sensors'"
  146. echo >>"$TMP_DIR/sensors.sh" "echo \"BEGIN 'sensors.fan_${id}_${name}' \$1\""
  147. ;;
  148. energy)
  149. files="$(ls "$path"/energy*_input 2>/dev/null)"
  150. files="$(sensors_check_files "$files")"
  151. [ -z "$files" ] && continue
  152. echo "CHART 'sensors.energy_${id}_${name}' '' 'Energy' 'Joule' 'energy' 'sensors.energy' areastack $((sensors_priority + 6)) $sensors_update_every '' '' 'sensors'"
  153. echo >>"$TMP_DIR/sensors.sh" "echo \"BEGIN 'sensors.energy_${id}_${name}' \$1\""
  154. algorithm="incremental"
  155. divisor=1000000
  156. ;;
  157. humidity)
  158. files="$(ls "$path"/humidity*_input 2>/dev/null)"
  159. files="$(sensors_check_files "$files")"
  160. [ -z "$files" ] && continue
  161. echo "CHART 'sensors.humidity_${id}_${name}' '' 'Humidity' 'Percent' 'humidity' 'sensors.humidity' line $((sensors_priority + 7)) $sensors_update_every '' '' 'sensors'"
  162. echo >>"$TMP_DIR/sensors.sh" "echo \"BEGIN 'sensors.humidity_${id}_${name}' \$1\""
  163. divisor=1000
  164. ;;
  165. *)
  166. continue
  167. ;;
  168. esac
  169. for x in $files; do
  170. file="$x"
  171. fid="$(fixid "$file")"
  172. lfile="$(basename "$file" | sed "s|_input$|_label|g")"
  173. labelname="$(basename "$file" | sed "s|_input$||g")"
  174. if [ ! "$path/$lfile" = "$file" ] && [ -f "$path/$lfile" ]; then
  175. labelname="$(cat "$path/$lfile")"
  176. fi
  177. echo "DIMENSION $fid '$labelname' $algorithm $multiplier $divisor"
  178. echo >>"$TMP_DIR/sensors.sh" "echo \"SET $fid = \"\$(< $file )"
  179. done
  180. echo >>"$TMP_DIR/sensors.sh" "echo END"
  181. done
  182. done
  183. [ $sensors_source_update -eq 1 ] && echo >>"$TMP_DIR/sensors.sh" "}"
  184. # ok, load the function sensors_update() we created
  185. # shellcheck source=/dev/null
  186. [ $sensors_source_update -eq 1 ] && . "$TMP_DIR/sensors.sh"
  187. return 0
  188. }
  189. # _update is called continuously, to collect the values
  190. sensors_update() {
  191. # the first argument to this function is the microseconds since last update
  192. # pass this parameter to the BEGIN statement (see below).
  193. # do all the work to collect / calculate the values
  194. # for each dimension
  195. # remember: KEEP IT SIMPLE AND SHORT
  196. # shellcheck source=/dev/null
  197. [ $sensors_source_update -eq 0 ] && . "$TMP_DIR/sensors.sh" "$1"
  198. return 0
  199. }