Projet

Général

Profil

Révision 2129ffc2

ID2129ffc2d59ca640c37ddc3c93df95775f1baefb
Parent 5dd3e258
Enfant 92e747e9

Ajouté par Mark Edwards il y a presque 14 ans

Initial version

Voir les différences:

plugins/other/cpufreq-avg
1
#!/bin/sh
2
#
3
# Plugin to measure average CPU frequency for each CPU/core.
4
#
5
# Contributed by Mark Edwards
6
#
7
# Usage: Place in /etc/munin/plugins (or link it there  using ln -s)
8
#
9
# Parameters understood:
10
#
11
#       config   (required)
12
#       autoconf (optional - used by munin-config)
13
#
14
# $Log$
15
#
16
# Revision 0.2  2010/01/04 16:37:00  medwards
17
# Minor bugfixes in config section
18
#
19
# Revision 0.1  2010/01/04 16:13:00  medwards
20
# First version
21
#
22
#
23
#
24
# Magic markers - optional - used by installation scripts and
25
# munin-config:
26
#
27
#%# family=auto
28
#%# capabilities=autoconf
29

  
30
if [ "$1" = "autoconf" ]; then
31
		if [ -r /sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state ]; then
32
			echo yes
33
			exit 0
34
		else
35
			echo no
36
			exit 1
37
		fi
38
fi
39

  
40
cpu_count=`grep -c "^processor" /proc/cpuinfo`
41

  
42
if [ "$1" = "config" ]; then
43

  
44
		echo 'graph_title Average CPU Frequency'
45
		up_lim=`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq`
46
		low_lim=`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq`
47
		up_lim=`expr $up_lim \* 1000` # Convert to Hz
48
		low_lim=`expr $low_lim \* 1000` # Convert to Hz
49
		echo "graph_args -u $up_lim -l $low_lim -r --base 1000"
50
		echo 'graph_vlabel Hz'
51
		echo 'graph_category system'
52
		cpu=0
53
		while [ $cpu -lt $cpu_count ]
54
		do
55
			echo "cpu_$cpu.label CPU $cpu"
56
			echo "cpu_$cpu.type GAUGE"
57
			echo "cpu_$cpu.info Hz"
58
			cpu=`expr $cpu + 1`
59
		done
60
		exit 0
61
fi
62

  
63
# Run measurements
64
cpu=0
65
while [ $cpu -lt $cpu_count ]
66
do
67

  
68
	time_in_state=`cat /sys/devices/system/cpu/cpu$cpu/cpufreq/stats/time_in_state`
69

  
70
	# Check/create statefile(s)
71
	statefile="/var/lib/munin/plugin-state/cpufreq-avg_cpu$cpu.state"
72
	if [ ! -r "$statefile" ]
73
	then
74
		echo "$time_in_state" > $statefile
75
		if [ "$?" -ne "0" ]
76
		then
77
			exit ${1}
78
		else
79
			cpu=`expr $cpu + 1`
80
			continue
81
		fi
82
	fi
83
	state=`cat $statefile`
84
	
85
	# Calculated total time since last state
86
	total_time=0
87
	total_time=$(
88
		echo "$time_in_state" | { 
89
			i=0
90
			while read line
91
			do
92
				this_freq=`echo $line | awk '{ print $1; }'`
93
				this_time=`echo $line | awk '{ print $2; }'`
94
				this_time_state=`echo "$state" | grep $this_freq | awk '{ print $2; }'`
95
				if [ $this_time -ge $this_time_state ] # Only measure if state is valid
96
				then
97
					time_diff=`expr $this_time - $this_time_state` # Calculate time since last state
98
					total_time=`expr $total_time + $time_diff`
99
				fi
100
				i=`expr $i + 1`
101
			done
102
			echo $total_time
103
		}
104
	)
105
	
106
	# Measure average CPU frequency if total time calculation was successful
107

  
108
	frequency=0
109
	frequency=$(
110
		echo "$time_in_state" | {
111
			i=0
112
			while read line
113
			do
114
				this_freq=`echo $line | awk '{ print $1; }'`
115
				this_time=`echo $line | awk '{ print $2; }'`
116
				this_time_state=`echo "$state" | grep $this_freq | awk '{ print $2; }'`
117
				this_freq=`expr $this_freq \* 1000` # Convert to Hz
118
				this_time=`expr $this_time - $this_time_state` # Calculate time since last state
119
				if [ $total_time -gt 0 ]
120
				then
121
					calc=`echo "($this_time / $total_time) * $this_freq" | bc -l`
122
					frequency=`echo "$frequency + $calc" | bc -l`
123
				fi
124
				i=`expr $i + 1`
125
			done
126
			echo $frequency
127
		}
128
	)
129

  
130
	# Round result to an integer and return it
131
	frequency=`echo "scale=0 ; ($frequency+0.5)/1" | bc -l`
132
	if [ $frequency -gt 0 ]
133
	then
134
		echo "cpu_$cpu.value $frequency"
135
	fi
136
	
137
	# Update statefile
138
	echo "$time_in_state" > $statefile
139
	
140
	cpu=`expr $cpu + 1`
141

  
142
done
143

  
144
exit 0

Formats disponibles : Unified diff