]> arthur.barton.de Git - netdata.git/blob - charts.d/sensors.chart.sh
support disabled sensors
[netdata.git] / charts.d / sensors.chart.sh
1 #!/bin/sh
2
3 # if this chart is called X.chart.sh, then all functions and global variables
4 # must start with X_
5
6 sensors_sys_dir="/sys/devices"
7 sensors_sys_depth=10
8 sensors_source_update=1
9
10 # _update_every is a special variable - it holds the number of seconds
11 # between the calls of the _update() function
12 sensors_update_every=
13
14 sensors_find_all_files() {
15         find $1 -maxdepth $sensors_sys_depth -name \*_input -o -name temp 2>/dev/null
16 }
17
18 sensors_find_all_dirs() {
19         sensors_find_all_files $1 | while read
20         do
21                 dirname $REPLY
22         done | sort -u
23 }
24
25 # _check is called once, to find out if this chart should be enabled or not
26 sensors_check() {
27
28         # this should return:
29         #  - 0 to enable the chart
30         #  - 1 to disable the chart
31
32         [ ! -z "$( sensors_find_all_files $sensors_sys_dir )" ] && return 0
33         return 1
34 }
35
36 sensors_check_files() {
37         # we only need sensors that report a non-zero value
38
39         local f= v=
40         for f in $*
41         do
42                 [ ! -f "$f" ] && continue
43
44                 v="$( cat $f )"
45                 v=$(( v + 1 - 1 ))
46                 [ $v -ne 0 ] && echo "$f" && continue
47                 
48                 echo >&2 "charts.d: sensors: $f gives zero values"
49         done
50 }
51
52 sensors_check_temp_type() {
53         # valid temp types are 1 to 6
54         # disabled sensors have the value 0
55
56         local f= t= v=
57         for f in $*
58         do
59                 t=$( echo $f | sed "s|_input$|_type|g" )
60                 [ "$f" = "$t" ] && echo "$f" && continue
61                 [ ! -f "$t" ] && echo "$f" && continue
62
63                 v="$( cat $t )"
64                 v=$(( v + 1 - 1 ))
65                 [ $v -ne 0 ] && echo "$f" && continue
66
67                 echo >&2 "charts.d: sensors: $f is disabled"
68         done
69 }
70
71 # _create is called once, to create the charts
72 sensors_create() {
73         local path= dir= name= x= file= lfile= labelname= labelid= device= subsystem= id= type= mode= files= multiplier= divisor=
74
75         # we create a script with the source of the
76         # sensors_update() function
77         # - the highest speed we can achieve -
78         [ $sensors_source_update -eq 1 ] && echo >$TMP_DIR/temp.sh "sensors_update() {"
79
80         for path in $( sensors_find_all_dirs $sensors_sys_dir | sort -u )
81         do
82                 dir=$( basename $path )
83                 device=
84                 subsystem=
85                 id=
86                 type=
87                 name=
88
89                 [ -h $path/device ] && device=$( readlink -f $path/device )
90                 [ ! -z "$device" ] && device=$( basename $device )
91                 [ -z "$device" ] && device="$dir"
92
93                 [ -h $path/subsystem ] && subsystem=$( readlink -f $path/subsystem )
94                 [ ! -z "$subsystem" ] && subsystem=$( basename $subsystem )
95                 [ -z "$subsystem" ] && subsystem="$dir"
96
97                 [ -f $path/name ] && name=$( cat $path/name )
98                 [ -z "$name" ] && name="$dir"
99
100                 [ -f $path/type ] && type=$( cat $path/type )
101                 [ -z "$type" ] && type="$dir"
102
103                 id="$( fixid "$device.$subsystem.$dir" )"
104
105                 echo >&2 "charts.d: sensors: on path='$path', dir='$dir', device='$device', subsystem='$subsystem', id='$id', name='$name'"
106
107                 for mode in temperature voltage fans power current energy humidity
108                 do
109                         files=
110                         multiplier=1
111                         divisor=1
112                         algorithm="absolute"
113
114                         case $mode in
115                                 temperature)
116                                         files="$( ls $path/temp*_input 2>/dev/null; ls $path/temp 2>/dev/null )"
117                                         files="$( sensors_check_files $files )"
118                                         files="$( sensors_check_temp_type $files )"
119                                         [ -z "$files" ] && continue
120                                         echo "CHART sensors.temp_${id} '' '${name} Temperature' 'Celcius' '${device}' '' line 6000 $sensors_update_every"
121                                         echo >>$TMP_DIR/temp.sh "echo \"BEGIN sensors.temp_${id} \$1\""
122                                         divisor=1000
123                                         ;;
124
125                                 voltage)
126                                         files="$( ls $path/in*_input 2>/dev/null )"
127                                         files="$( sensors_check_files $files )"
128                                         [ -z "$files" ] && continue
129                                         echo "CHART sensors.volt_${id} '' '${name} Voltage' 'Volts' '${device}' '' line 6001 $sensors_update_every"
130                                         echo >>$TMP_DIR/temp.sh "echo \"BEGIN sensors.volt_${id} \$1\""
131                                         divisor=1000
132                                         ;;
133
134                                 current)
135                                         files="$( ls $path/curr*_input 2>/dev/null )"
136                                         files="$( sensors_check_files $files )"
137                                         [ -z "$files" ] && continue
138                                         echo "CHART sensors.curr_${id} '' '${name} Current' 'Ampere' '${device}' '' line 6002 $sensors_update_every"
139                                         echo >>$TMP_DIR/temp.sh "echo \"BEGIN sensors.curr_${id} \$1\""
140                                         divisor=1000
141                                         ;;
142
143                                 power)
144                                         files="$( ls $path/power*_input 2>/dev/null )"
145                                         files="$( sensors_check_files $files )"
146                                         [ -z "$files" ] && continue
147                                         echo "CHART sensors.power_${id} '' '${name} Power' 'Watt' '${device}' '' line 6003 $sensors_update_every"
148                                         echo >>$TMP_DIR/temp.sh "echo \"BEGIN sensors.power_${id} \$1\""
149                                         divisor=1000000
150                                         ;;
151
152                                 fans)
153                                         files="$( ls $path/fan*_input 2>/dev/null )"
154                                         files="$( sensors_check_files $files )"
155                                         [ -z "$files" ] && continue
156                                         echo "CHART sensors.fan_${id} '' '${name} Fans Speed' 'Rotations / Minute' '${device}' '' line 6004 $sensors_update_every"
157                                         echo >>$TMP_DIR/temp.sh "echo \"BEGIN sensors.fan_${id} \$1\""
158                                         ;;
159
160                                 emergy)
161                                         files="$( ls $path/energy*_input 2>/dev/null )"
162                                         files="$( sensors_check_files $files )"
163                                         [ -z "$files" ] && continue
164                                         echo "CHART sensors.energy_${id} '' '${name} Energy' 'Joule' '${device}' '' areastack 6005 $sensors_update_every"
165                                         echo >>$TMP_DIR/temp.sh "echo \"BEGIN sensors.energy_${id} \$1\""
166                                         algorithm="incremental"
167                                         divisor=1000000
168                                         ;;
169
170                                 humidity)
171                                         files="$( ls $path/humidity*_input 2>/dev/null )"
172                                         files="$( sensors_check_files $files )"
173                                         [ -z "$files" ] && continue
174                                         echo "CHART sensors.humidity_${id} '' '${name} Humidity' 'Percent' '${device}' '' line 6006 $sensors_update_every"
175                                         echo >>$TMP_DIR/temp.sh "echo \"BEGIN sensors.humidity_${id} \$1\""
176                                         divisor=1000
177                                         ;;
178
179                                 *)
180                                         continue
181                                         ;;
182                         esac
183
184                         for x in $files
185                         do
186                                 file="$x"
187                                 fid="$( fixid "$file" )"
188                                 lfile="$( basename $file | sed "s|_input$|_label|g" )"
189                                 labelname="$( basename $file | sed "s|_input$||g" )"
190
191                                 if [ ! "$path/$lfile" = "$file" -a -f "$path/$lfile" ]
192                                         then
193                                         labelname="$( cat "$path/$lfile" )"
194                                 fi
195
196                                 echo "DIMENSION $fid '$labelname' $algorithm $multiplier $divisor"
197                                 echo >>$TMP_DIR/temp.sh "printf \"SET $fid = \"; cat $file "
198                         done
199
200                         echo >>$TMP_DIR/temp.sh "echo END"
201                 done
202         done
203
204         echo >>$TMP_DIR/temp.sh "}"
205         # cat >&2 $TMP_DIR/temp.sh
206
207         # ok, load the function sensors_update() we created
208         [ $sensors_source_update -eq 1 ] && . $TMP_DIR/temp.sh
209
210         return 0
211 }
212
213 # _update is called continiously, to collect the values
214 sensors_update() {
215         # the first argument to this function is the microseconds since last update
216         # pass this parameter to the BEGIN statement (see bellow).
217
218         # do all the work to collect / calculate the values
219         # for each dimension
220         # remember: KEEP IT SIMPLE AND SHORT
221
222         [ $sensors_source_update -eq 0 ] && . $TMP_DIR/temp.sh $1
223
224         return 0
225 }
226