summaryrefslogtreecommitdiff
path: root/munin/munin-cpufreq
blob: 7ac69517c75799f1b9ee7b863fa5ef7d1a1d9507 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/ruby

# Copyright (c) 2006 Peter Palfrader
#
#%# family=auto
#%# capabilities=autoconf

def bail_out(m)
	STDERR.puts "#{$0}: #{m}"
	exit 1
end

def autoconf
	if FileTest.directory? "/sys/devices/system/cpu/cpu0/cpufreq/"
		puts "yes"
	else
		puts "no (no /sys/devices/system/cpu/cpu0/cpufreq/)"
	end
end

def findmasters
	cpudirs = Dir.glob("/sys/devices/system/cpu/cpu*/").map{ |b| File.basename b }
	cpus = cpudirs.map{ |d| /^cpu([0-9]+)$/.match(d)[1].to_i }.sort{|a,b| a<=>b }
	masters = []
	cpus.each do |cpunum|
		affected = IO.read("/sys/devices/system/cpu/cpu#{cpunum}/cpufreq/affected_cpus").split.map{|s| s.to_i}.sort{|a,b| a<=>b }
		bail_out "huh?  /sys/devices/system/cpu/cpu#{cpunum}/cpufreq/affected_cpus says it does not affect #{cpunum}!?" unless affected.include? cpunum
		h = {}
		h['cpunum'] = affected[0]
		h['fieldname'] = "cpu#{affected[0]}"
		h['label'] = affected.map{|c| "cpu#{c}"}.join(", ")
		h['max'] = IO.read("/sys/devices/system/cpu/cpu#{cpunum}/cpufreq/cpuinfo_max_freq").to_i
		masters.push(h)
		affected.each{ |a| cpus.delete(a) }
	end
	masters
end

def config
	m = findmasters
	max = m.map{ |i| i['max'] }.max
	puts 'graph_title CPU frequency'
	puts "graph_args -l 0 -u #{max * 1000} --base 1000"
	puts 'graph_vlabel Hz'
	puts 'graph_category system'
	puts 'graph_period second'
	puts 'graph_info Shows the CPU frequency of all installed CPUs'
	m.each do |m|
		puts "#{m['fieldname']}.label #{m['label']}"
		puts "#{m['fieldname']}.cdef #{m['fieldname']},1000,*"
		puts "#{m['fieldname']}.info Hz"
		puts "#{m['fieldname']}.type GAUGE"
	end
end

def report
	m = findmasters
	sleep(5) # we are interested how it does without us poking it,
	         # so maybe sleeping a bit will help
	m.each do |m|
		# value = IO.read("/sys/devices/system/cpu/cpu#{m['cpunum']}/cpufreq/cpuinfo_cur_freq").to_i
		# cpuinfo_cur_freq is not readable
		value = IO.read("/sys/devices/system/cpu/cpu#{m['cpunum']}/cpufreq/scaling_cur_freq").to_i
		puts "#{m['fieldname']}.value #{value}"
	end
end

case ARGV[0]
	when "autoconf"
		autoconf
	when "config"
		config
	else
		report
end