4 # https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
6 # if this chart is called X.chart.sh, then all functions and global variables
9 sensors_sys_dir="/sys/devices"
11 sensors_source_update=1
13 # _update_every is a special variable - it holds the number of seconds
14 # between the calls of the _update() function
17 sensors_find_all_files() {
18 find $1 -maxdepth $sensors_sys_depth -name \*_input -o -name temp 2>/dev/null
21 sensors_find_all_dirs() {
22 sensors_find_all_files $1 | while read
28 # _check is called once, to find out if this chart should be enabled or not
32 # - 0 to enable the chart
33 # - 1 to disable the chart
35 [ ! -z "$( sensors_find_all_files $sensors_sys_dir )" ] && return 0
39 sensors_check_files() {
40 # we only need sensors that report a non-zero value
45 [ ! -f "$f" ] && continue
49 [ $v -ne 0 ] && echo "$f" && continue
51 echo >&2 "charts.d: sensors: $f gives zero values"
55 sensors_check_temp_type() {
56 # valid temp types are 1 to 6
57 # disabled sensors have the value 0
62 t=$( echo $f | sed "s|_input$|_type|g" )
63 [ "$f" = "$t" ] && echo "$f" && continue
64 [ ! -f "$t" ] && echo "$f" && continue
68 [ $v -ne 0 ] && echo "$f" && continue
70 echo >&2 "charts.d: sensors: $f is disabled"
74 # _create is called once, to create the charts
76 local path= dir= name= x= file= lfile= labelname= labelid= device= subsystem= id= type= mode= files= multiplier= divisor=
78 # we create a script with the source of the
79 # sensors_update() function
80 # - the highest speed we can achieve -
81 [ $sensors_source_update -eq 1 ] && echo >$TMP_DIR/sensors.sh "sensors_update() {"
83 for path in $( sensors_find_all_dirs $sensors_sys_dir | sort -u )
85 dir=$( basename $path )
92 [ -h $path/device ] && device=$( readlink -f $path/device )
93 [ ! -z "$device" ] && device=$( basename $device )
94 [ -z "$device" ] && device="$dir"
96 [ -h $path/subsystem ] && subsystem=$( readlink -f $path/subsystem )
97 [ ! -z "$subsystem" ] && subsystem=$( basename $subsystem )
98 [ -z "$subsystem" ] && subsystem="$dir"
100 [ -f $path/name ] && name=$( cat $path/name )
101 [ -z "$name" ] && name="$dir"
103 [ -f $path/type ] && type=$( cat $path/type )
104 [ -z "$type" ] && type="$dir"
106 id="$( fixid "$device.$subsystem.$dir" )"
108 echo >&2 "charts.d: sensors: on path='$path', dir='$dir', device='$device', subsystem='$subsystem', id='$id', name='$name'"
110 for mode in temperature voltage fans power current energy humidity
119 files="$( ls $path/temp*_input 2>/dev/null; ls $path/temp 2>/dev/null )"
120 files="$( sensors_check_files $files )"
121 files="$( sensors_check_temp_type $files )"
122 [ -z "$files" ] && continue
123 echo "CHART sensors.temp_$id '' '$name Temperature' 'Celcius' '$device' '' line 6000 $sensors_update_every"
124 echo >>$TMP_DIR/sensors.sh "echo \"BEGIN sensors.temp_$id \$1\""
129 files="$( ls $path/in*_input 2>/dev/null )"
130 files="$( sensors_check_files $files )"
131 [ -z "$files" ] && continue
132 echo "CHART sensors.volt_$id '' '$name Voltage' 'Volts' '$device' '' line 6001 $sensors_update_every"
133 echo >>$TMP_DIR/sensors.sh "echo \"BEGIN sensors.volt_$id \$1\""
138 files="$( ls $path/curr*_input 2>/dev/null )"
139 files="$( sensors_check_files $files )"
140 [ -z "$files" ] && continue
141 echo "CHART sensors.curr_$id '' '$name Current' 'Ampere' '$device' '' line 6002 $sensors_update_every"
142 echo >>$TMP_DIR/sensors.sh "echo \"BEGIN sensors.curr_$id \$1\""
147 files="$( ls $path/power*_input 2>/dev/null )"
148 files="$( sensors_check_files $files )"
149 [ -z "$files" ] && continue
150 echo "CHART sensors.power_$id '' '$name Power' 'Watt' '$device' '' line 6003 $sensors_update_every"
151 echo >>$TMP_DIR/sensors.sh "echo \"BEGIN sensors.power_$id \$1\""
156 files="$( ls $path/fan*_input 2>/dev/null )"
157 files="$( sensors_check_files $files )"
158 [ -z "$files" ] && continue
159 echo "CHART sensors.fan_$id '' '$name Fans Speed' 'Rotations / Minute' '$device' '' line 6004 $sensors_update_every"
160 echo >>$TMP_DIR/sensors.sh "echo \"BEGIN sensors.fan_$id \$1\""
164 files="$( ls $path/energy*_input 2>/dev/null )"
165 files="$( sensors_check_files $files )"
166 [ -z "$files" ] && continue
167 echo "CHART sensors.energy_$id '' '$name Energy' 'Joule' '$device' '' areastack 6005 $sensors_update_every"
168 echo >>$TMP_DIR/sensors.sh "echo \"BEGIN sensors.energy_$id \$1\""
169 algorithm="incremental"
174 files="$( ls $path/humidity*_input 2>/dev/null )"
175 files="$( sensors_check_files $files )"
176 [ -z "$files" ] && continue
177 echo "CHART sensors.humidity_$id '' '$name Humidity' 'Percent' '$device' '' line 6006 $sensors_update_every"
178 echo >>$TMP_DIR/sensors.sh "echo \"BEGIN sensors.humidity_$id \$1\""
190 fid="$( fixid "$file" )"
191 lfile="$( basename $file | sed "s|_input$|_label|g" )"
192 labelname="$( basename $file | sed "s|_input$||g" )"
194 if [ ! "$path/$lfile" = "$file" -a -f "$path/$lfile" ]
196 labelname="$( cat "$path/$lfile" )"
199 echo "DIMENSION $fid '$labelname' $algorithm $multiplier $divisor"
200 echo >>$TMP_DIR/sensors.sh "printf \"SET $fid = \"; cat $file "
203 echo >>$TMP_DIR/sensors.sh "echo END"
207 [ $cpufreq_source_update -eq 1 ] && echo >>$TMP_DIR/sensors.sh "}"
208 # cat >&2 $TMP_DIR/sensors.sh
210 # ok, load the function sensors_update() we created
211 [ $sensors_source_update -eq 1 ] && . $TMP_DIR/sensors.sh
216 # _update is called continiously, to collect the values
218 # the first argument to this function is the microseconds since last update
219 # pass this parameter to the BEGIN statement (see bellow).
221 # do all the work to collect / calculate the values
223 # remember: KEEP IT SIMPLE AND SHORT
225 [ $sensors_source_update -eq 0 ] && . $TMP_DIR/sensors.sh $1