]> arthur.barton.de Git - netdata.git/blob - charts.d/sensors.chart.sh
updated to work on /sys/class/hwmon
[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/class/hwmon"
7 sensors_sys_depth=3
8
9 # _update_every is a special variable - it holds the number of seconds
10 # between the calls of the _update() function
11 sensors_update_every=
12
13 sensors_find_all_files() {
14         find -L $1 -maxdepth $sensors_sys_depth -name temp\?_input -o -name temp -o -name in\?_input -o -name fan\?_input 2>/dev/null
15 }
16
17 sensors_find_all_dirs() {
18         sensors_find_all_files $1 | while read
19         do
20                 dirname $REPLY
21         done | sort -u
22 }
23
24 # _check is called once, to find out if this chart should be enabled or not
25 sensors_check() {
26
27         # this should return:
28         #  - 0 to enable the chart
29         #  - 1 to disable the chart
30
31         [ ! -z "$( sensors_find_all_files $sensors_sys_dir )" ] && return 0
32         return 1
33 }
34
35 # _create is called once, to create the charts
36 sensors_create() {
37         local path= dir= name= x= file= lfile= labelname= labelid= device= subsystem= id= type= mode= files= multiplier= divisor=
38
39         echo >$TMP_DIR/temp.sh "sensors_update() {"
40
41         for path in $( sensors_find_all_dirs $sensors_sys_dir | sort -u )
42         do
43                 dir=$( basename $path )
44                 device=
45                 subsystem=
46                 id=
47                 type=
48                 name=
49
50                 [ -h $path/device ] && device=$( readlink -f $path/device )
51                 [ ! -z "$device" ] && device=$( basename $device )
52                 [ -z "$device" ] && device="$dir"
53
54                 [ -h $path/subsystem ] && subsystem=$( readlink -f $path/subsystem )
55                 [ ! -z "$subsystem" ] && subsystem=$( basename $subsystem )
56                 [ -z "$subsystem" ] && subsystem="$dir"
57
58                 [ -f $path/name ] && name=$( cat $path/name )
59                 [ -z "$name" ] && name="$dir"
60
61                 [ -f $path/type ] && type=$( cat $path/type )
62                 [ -z "$type" ] && type="$dir"
63
64                 id="$( fixid "$device.$subsystem.$dir" )"
65
66                 echo >&2 "charts.d: sensors on path='$path', dir='$dir', device='$device', subsystem='$subsystem', id='$id', name='$name'"
67
68                 for mode in temperature voltage fans
69                 do
70                         files=
71                         multiplier=1
72                         divisor=1
73                         case $mode in
74                                 temperature)
75                                         files="$( find -H $path -name temp\?_input -o -name temp | sort -u)"
76                                         [ -z "$files" ] && continue
77                                         echo "CHART sensors.temp_${id} '' '${name} Temperature' 'Temperature' 'Celcius Degrees' '' line 6000 $sensors_update_every"
78                                         echo >>$TMP_DIR/temp.sh "echo \"BEGIN sensors.temp_${id} \$1\""
79                                         divisor=1000
80                                         ;;
81
82                                 voltage)
83                                         files="$( find -H $path -name in\?_input )"
84                                         [ -z "$files" ] && continue
85                                         echo "CHART sensors.volt_${id} '' '${name} Voltage' 'Voltage' 'Volts' '' line 6001 $sensors_update_every"
86                                         echo >>$TMP_DIR/temp.sh "echo \"BEGIN sensors.volt_${id} \$1\""
87                                         divisor=1000
88                                         ;;
89
90                                 fans)
91                                         files="$( find -H $path -name fan\?_input )"
92                                         [ -z "$files" ] && continue
93                                         echo "CHART sensors.fan_${id} '' '${name} Fans Speed' 'Fans' 'Rotations Per Minute (RPM)' '' line 6002 $sensors_update_every"
94                                         echo >>$TMP_DIR/temp.sh "echo \"BEGIN sensors.fan_${id} \$1\""
95                                         ;;
96
97                                 *)
98                                         continue
99                                         ;;
100                         esac
101
102                         for x in $files
103                         do
104                                 file="$x"
105                                 fid="$( fixid "$file" )"
106                                 lfile="$( basename $file | sed "s|_input$|_label|g" )"
107                                 labelname="$( basename $file | sed "s|_input$||g" )"
108
109                                 if [ ! "$path/$lfile" = "$file" -a -f "$path/$lfile" ]
110                                         then
111                                         labelname="$( cat "$path/$lfile" )"
112                                 fi
113
114                                 echo "DIMENSION $fid '$labelname' absolute $multiplier $divisor"
115                                 echo >>$TMP_DIR/temp.sh "printf \"SET $fid = \"; cat $file "
116                         done
117
118                         echo >>$TMP_DIR/temp.sh "echo END"
119                 done
120         done
121
122         echo >>$TMP_DIR/temp.sh "}"
123         cat >&2 $TMP_DIR/temp.sh
124         . $TMP_DIR/temp.sh
125
126         return 0
127 }
128
129 # _update is called continiously, to collect the values
130 sensors_update() {
131         # the first argument to this function is the microseconds since last update
132         # pass this parameter to the BEGIN statement (see bellow).
133
134         # do all the work to collect / calculate the values
135         # for each dimension
136         # remember: KEEP IT SIMPLE AND SHORT
137
138 #       . $TMP_DIR/temp.sh $1
139
140         return 0
141 }
142