]> arthur.barton.de Git - netdata.git/blob - charts.d/sensors.chart.sh
fixed minor issues in sensors.chart.sh; added cpufreq.chart.sh; removed obsolete...
[netdata.git] / charts.d / sensors.chart.sh
1 #!/bin/sh
2
3 # sensors docs
4 # https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
5
6 # if this chart is called X.chart.sh, then all functions and global variables
7 # must start with X_
8
9 sensors_sys_dir="/sys/devices"
10 sensors_sys_depth=10
11 sensors_source_update=1
12
13 # _update_every is a special variable - it holds the number of seconds
14 # between the calls of the _update() function
15 sensors_update_every=
16
17 sensors_find_all_files() {
18         find $1 -maxdepth $sensors_sys_depth -name \*_input -o -name temp 2>/dev/null
19 }
20
21 sensors_find_all_dirs() {
22         sensors_find_all_files $1 | while read
23         do
24                 dirname $REPLY
25         done | sort -u
26 }
27
28 # _check is called once, to find out if this chart should be enabled or not
29 sensors_check() {
30
31         # this should return:
32         #  - 0 to enable the chart
33         #  - 1 to disable the chart
34
35         [ ! -z "$( sensors_find_all_files $sensors_sys_dir )" ] && return 0
36         return 1
37 }
38
39 sensors_check_files() {
40         # we only need sensors that report a non-zero value
41
42         local f= v=
43         for f in $*
44         do
45                 [ ! -f "$f" ] && continue
46
47                 v="$( cat $f )"
48                 v=$(( v + 1 - 1 ))
49                 [ $v -ne 0 ] && echo "$f" && continue
50                 
51                 echo >&2 "charts.d: sensors: $f gives zero values"
52         done
53 }
54
55 sensors_check_temp_type() {
56         # valid temp types are 1 to 6
57         # disabled sensors have the value 0
58
59         local f= t= v=
60         for f in $*
61         do
62                 t=$( echo $f | sed "s|_input$|_type|g" )
63                 [ "$f" = "$t" ] && echo "$f" && continue
64                 [ ! -f "$t" ] && echo "$f" && continue
65
66                 v="$( cat $t )"
67                 v=$(( v + 1 - 1 ))
68                 [ $v -ne 0 ] && echo "$f" && continue
69
70                 echo >&2 "charts.d: sensors: $f is disabled"
71         done
72 }
73
74 # _create is called once, to create the charts
75 sensors_create() {
76         local path= dir= name= x= file= lfile= labelname= labelid= device= subsystem= id= type= mode= files= multiplier= divisor=
77
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() {"
82
83         for path in $( sensors_find_all_dirs $sensors_sys_dir | sort -u )
84         do
85                 dir=$( basename $path )
86                 device=
87                 subsystem=
88                 id=
89                 type=
90                 name=
91
92                 [ -h $path/device ] && device=$( readlink -f $path/device )
93                 [ ! -z "$device" ] && device=$( basename $device )
94                 [ -z "$device" ] && device="$dir"
95
96                 [ -h $path/subsystem ] && subsystem=$( readlink -f $path/subsystem )
97                 [ ! -z "$subsystem" ] && subsystem=$( basename $subsystem )
98                 [ -z "$subsystem" ] && subsystem="$dir"
99
100                 [ -f $path/name ] && name=$( cat $path/name )
101                 [ -z "$name" ] && name="$dir"
102
103                 [ -f $path/type ] && type=$( cat $path/type )
104                 [ -z "$type" ] && type="$dir"
105
106                 id="$( fixid "$device.$subsystem.$dir" )"
107
108                 echo >&2 "charts.d: sensors: on path='$path', dir='$dir', device='$device', subsystem='$subsystem', id='$id', name='$name'"
109
110                 for mode in temperature voltage fans power current energy humidity
111                 do
112                         files=
113                         multiplier=1
114                         divisor=1
115                         algorithm="absolute"
116
117                         case $mode in
118                                 temperature)
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\""
125                                         divisor=1000
126                                         ;;
127
128                                 voltage)
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\""
134                                         divisor=1000
135                                         ;;
136
137                                 current)
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\""
143                                         divisor=1000
144                                         ;;
145
146                                 power)
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\""
152                                         divisor=1000000
153                                         ;;
154
155                                 fans)
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\""
161                                         ;;
162
163                                 emergy)
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"
170                                         divisor=1000000
171                                         ;;
172
173                                 humidity)
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\""
179                                         divisor=1000
180                                         ;;
181
182                                 *)
183                                         continue
184                                         ;;
185                         esac
186
187                         for x in $files
188                         do
189                                 file="$x"
190                                 fid="$( fixid "$file" )"
191                                 lfile="$( basename $file | sed "s|_input$|_label|g" )"
192                                 labelname="$( basename $file | sed "s|_input$||g" )"
193
194                                 if [ ! "$path/$lfile" = "$file" -a -f "$path/$lfile" ]
195                                         then
196                                         labelname="$( cat "$path/$lfile" )"
197                                 fi
198
199                                 echo "DIMENSION $fid '$labelname' $algorithm $multiplier $divisor"
200                                 echo >>$TMP_DIR/sensors.sh "printf \"SET $fid = \"; cat $file "
201                         done
202
203                         echo >>$TMP_DIR/sensors.sh "echo END"
204                 done
205         done
206
207         [ $cpufreq_source_update -eq 1 ] && echo >>$TMP_DIR/sensors.sh "}"
208         # cat >&2 $TMP_DIR/sensors.sh
209
210         # ok, load the function sensors_update() we created
211         [ $sensors_source_update -eq 1 ] && . $TMP_DIR/sensors.sh
212
213         return 0
214 }
215
216 # _update is called continiously, to collect the values
217 sensors_update() {
218         # the first argument to this function is the microseconds since last update
219         # pass this parameter to the BEGIN statement (see bellow).
220
221         # do all the work to collect / calculate the values
222         # for each dimension
223         # remember: KEEP IT SIMPLE AND SHORT
224
225         [ $sensors_source_update -eq 0 ] && . $TMP_DIR/sensors.sh $1
226
227         return 0
228 }
229