]> arthur.barton.de Git - netdata.git/blob - charts.d/cpufreq.chart.sh
a new random number needed for demo2.html
[netdata.git] / charts.d / cpufreq.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 cpufreq_sys_dir="/sys/devices"
7 cpufreq_sys_depth=10
8 cpufreq_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 cpufreq_update_every=
13
14 cpufreq_find_all_files() {
15         find $1 -maxdepth $cpufreq_sys_depth -name scaling_cur_freq 2>/dev/null
16 }
17
18 # _check is called once, to find out if this chart should be enabled or not
19 cpufreq_check() {
20
21         # this should return:
22         #  - 0 to enable the chart
23         #  - 1 to disable the chart
24
25         [ -z "$( cpufreq_find_all_files $cpufreq_sys_dir )" ] && return 1
26         return 0
27 }
28
29 # _create is called once, to create the charts
30 cpufreq_create() {
31         local dir= file= id= i=
32
33         # we create a script with the source of the
34         # cpufreq_update() function
35         # - the highest speed we can achieve -
36         [ $cpufreq_source_update -eq 1 ] && echo >$TMP_DIR/cpufreq.sh "cpufreq_update() {"
37
38         echo "CHART sensors.cpufreq '' 'CPU Clock' 'MHz' 'cpufreq' '' line 7000 $cpufreq_update_every"
39         echo >>$TMP_DIR/cpufreq.sh "echo \"BEGIN sensors.cpufreq \$1\""
40
41         i=0
42         for file in $( cpufreq_find_all_files $cpufreq_sys_dir | sort -u )
43         do
44                 i=$(( i + 1 ))
45                 dir=$( dirname $file )
46                 cpu=
47
48                 [ -f $dir/affected_cpus ] && cpu=$( cat $dir/affected_cpus )
49                 [ -z "$cpu" ] && cpu="$i.a"
50
51                 id="$( fixid "cpu$cpu" )"
52
53                 echo >&2 "charts.d: cpufreq: on file='$file', dir='$dir', cpu='$cpu', id='$id'"
54
55                 echo "DIMENSION $id '$id' absolute 1 1000"
56                 echo >>$TMP_DIR/cpufreq.sh "printf \"SET $id = \"; cat $file "
57         done
58         echo >>$TMP_DIR/cpufreq.sh "echo END"
59
60         [ $cpufreq_source_update -eq 1 ] && echo >>$TMP_DIR/cpufreq.sh "}"
61         # cat >&2 $TMP_DIR/cpufreq.sh
62
63         # ok, load the function cpufreq_update() we created
64         [ $cpufreq_source_update -eq 1 ] && . $TMP_DIR/cpufreq.sh
65
66         return 0
67 }
68
69 # _update is called continiously, to collect the values
70 cpufreq_update() {
71         # the first argument to this function is the microseconds since last update
72         # pass this parameter to the BEGIN statement (see bellow).
73
74         # do all the work to collect / calculate the values
75         # for each dimension
76         # remember: KEEP IT SIMPLE AND SHORT
77
78         [ $cpufreq_source_update -eq 0 ] && . $TMP_DIR/cpufreq.sh $1
79
80         return 0
81 }
82