1 # no need for shebang - this file is loaded from charts.d.plugin
4 # real-time performance and health monitoring, done right!
5 # (C) 2016 Costa Tsaousis <costa@tsaousis.gr>
10 # https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
12 # if this chart is called X.chart.sh, then all functions and global variables
15 # the directory the kernel keeps sensor data
16 sensors_sys_dir="${NETDATA_HOST_PREFIX}/sys/devices"
18 # how deep in the tree to check for sensor data
21 # if set to 1, the script will overwrite internal
22 # script functions with code generated ones
23 # leave to 1, is faster
24 sensors_source_update=1
26 # how frequently to collect sensor data
27 # the default is to collect it at every iteration of charts.d
30 sensors_priority=90000
32 declare -A sensors_excluded=()
34 sensors_find_all_files() {
35 find $1 -maxdepth $sensors_sys_depth -name \*_input -o -name temp 2>/dev/null
38 sensors_find_all_dirs() {
39 sensors_find_all_files $1 | while read
45 # _check is called once, to find out if this chart should be enabled or not
49 # - 0 to enable the chart
50 # - 1 to disable the chart
52 [ -z "$( sensors_find_all_files $sensors_sys_dir )" ] && error "no sensors found in '$sensors_sys_dir'." && return 1
56 sensors_check_files() {
57 # we only need sensors that report a non-zero value
58 # also remove not needed sensors
63 [ ! -f "$f" ] && continue
64 for ex in ${sensors_excluded[@]}; do
65 [[ $f =~ .*$ex$ ]] && excluded='1' && break
68 [ "$excluded" != "1" ] && v="$( cat $f )" || v=0
70 [ $v -ne 0 ] && echo "$f" && continue
73 error "$f gives zero values"
77 sensors_check_temp_type() {
78 # valid temp types are 1 to 6
79 # disabled sensors have the value 0
84 t=$( echo $f | sed "s|_input$|_type|g" )
85 [ "$f" = "$t" ] && echo "$f" && continue
86 [ ! -f "$t" ] && echo "$f" && continue
90 [ $v -ne 0 ] && echo "$f" && continue
92 error "$f is disabled"
96 # _create is called once, to create the charts
98 local path= dir= name= x= file= lfile= labelname= labelid= device= subsystem= id= type= mode= files= multiplier= divisor=
100 # we create a script with the source of the
101 # sensors_update() function
102 # - the highest speed we can achieve -
103 [ $sensors_source_update -eq 1 ] && echo >$TMP_DIR/sensors.sh "sensors_update() {"
105 for path in $( sensors_find_all_dirs $sensors_sys_dir | sort -u )
107 dir=$( basename $path )
114 [ -h $path/device ] && device=$( readlink -f $path/device )
115 [ ! -z "$device" ] && device=$( basename $device )
116 [ -z "$device" ] && device="$dir"
118 [ -h $path/subsystem ] && subsystem=$( readlink -f $path/subsystem )
119 [ ! -z "$subsystem" ] && subsystem=$( basename $subsystem )
120 [ -z "$subsystem" ] && subsystem="$dir"
122 [ -f $path/name ] && name=$( cat $path/name )
123 [ -z "$name" ] && name="$dir"
125 [ -f $path/type ] && type=$( cat $path/type )
126 [ -z "$type" ] && type="$dir"
128 id="$( fixid "$device.$subsystem.$dir" )"
130 debug "path='$path', dir='$dir', device='$device', subsystem='$subsystem', id='$id', name='$name'"
132 for mode in temperature voltage fans power current energy humidity
141 files="$( ls $path/temp*_input 2>/dev/null; ls $path/temp 2>/dev/null )"
142 files="$( sensors_check_files $files )"
143 files="$( sensors_check_temp_type $files )"
144 [ -z "$files" ] && continue
145 echo "CHART sensors.temp_$id '' '$name Temperature' 'Celsius' 'temperature' 'sensors.temp' line $((sensors_priority + 1)) $sensors_update_every"
146 echo >>$TMP_DIR/sensors.sh "echo \"BEGIN sensors.temp_$id \$1\""
151 files="$( ls $path/in*_input 2>/dev/null )"
152 files="$( sensors_check_files $files )"
153 [ -z "$files" ] && continue
154 echo "CHART sensors.volt_$id '' '$name Voltage' 'Volts' 'voltage' 'sensors.volt' line $((sensors_priority + 2)) $sensors_update_every"
155 echo >>$TMP_DIR/sensors.sh "echo \"BEGIN sensors.volt_$id \$1\""
160 files="$( ls $path/curr*_input 2>/dev/null )"
161 files="$( sensors_check_files $files )"
162 [ -z "$files" ] && continue
163 echo "CHART sensors.curr_$id '' '$name Current' 'Ampere' 'current' 'sensors.curr' line $((sensors_priority + 3)) $sensors_update_every"
164 echo >>$TMP_DIR/sensors.sh "echo \"BEGIN sensors.curr_$id \$1\""
169 files="$( ls $path/power*_input 2>/dev/null )"
170 files="$( sensors_check_files $files )"
171 [ -z "$files" ] && continue
172 echo "CHART sensors.power_$id '' '$name Power' 'Watt' 'power' 'sensors.power' line $((sensors_priority + 4)) $sensors_update_every"
173 echo >>$TMP_DIR/sensors.sh "echo \"BEGIN sensors.power_$id \$1\""
178 files="$( ls $path/fan*_input 2>/dev/null )"
179 files="$( sensors_check_files $files )"
180 [ -z "$files" ] && continue
181 echo "CHART sensors.fan_$id '' '$name Fans Speed' 'Rotations / Minute' 'fans' 'sensors.fans' line $((sensors_priority + 5)) $sensors_update_every"
182 echo >>$TMP_DIR/sensors.sh "echo \"BEGIN sensors.fan_$id \$1\""
186 files="$( ls $path/energy*_input 2>/dev/null )"
187 files="$( sensors_check_files $files )"
188 [ -z "$files" ] && continue
189 echo "CHART sensors.energy_$id '' '$name Energy' 'Joule' 'energy' 'sensors.energy' areastack $((sensors_priority + 6)) $sensors_update_every"
190 echo >>$TMP_DIR/sensors.sh "echo \"BEGIN sensors.energy_$id \$1\""
191 algorithm="incremental"
196 files="$( ls $path/humidity*_input 2>/dev/null )"
197 files="$( sensors_check_files $files )"
198 [ -z "$files" ] && continue
199 echo "CHART sensors.humidity_$id '' '$name Humidity' 'Percent' 'humidity' 'sensors.humidity' line $((sensors_priority + 7)) $sensors_update_every"
200 echo >>$TMP_DIR/sensors.sh "echo \"BEGIN sensors.humidity_$id \$1\""
212 fid="$( fixid "$file" )"
213 lfile="$( basename $file | sed "s|_input$|_label|g" )"
214 labelname="$( basename $file | sed "s|_input$||g" )"
216 if [ ! "$path/$lfile" = "$file" -a -f "$path/$lfile" ]
218 labelname="$( cat "$path/$lfile" )"
221 echo "DIMENSION $fid '$labelname' $algorithm $multiplier $divisor"
222 echo >>$TMP_DIR/sensors.sh "echo \"SET $fid = \"\$(< $file )"
225 echo >>$TMP_DIR/sensors.sh "echo END"
229 [ $sensors_source_update -eq 1 ] && echo >>$TMP_DIR/sensors.sh "}"
231 # ok, load the function sensors_update() we created
232 [ $sensors_source_update -eq 1 ] && . $TMP_DIR/sensors.sh
237 # _update is called continiously, to collect the values
239 # the first argument to this function is the microseconds since last update
240 # pass this parameter to the BEGIN statement (see bellow).
242 # do all the work to collect / calculate the values
244 # remember: KEEP IT SIMPLE AND SHORT
246 [ $sensors_source_update -eq 0 ] && . $TMP_DIR/sensors.sh $1