summaryrefslogtreecommitdiffstats
path: root/bin/hwmon
blob: cc583c409d1b8ab78c7992910a2ab6f14e452185 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/bin/sh

set -eu

debug=0

err()
{
	local fmt="${1}"
	shift 1

	printf "Error: ${fmt}\n" "${@}" 1>&2
	return 0
}

dbg()
{
	local fmt="${1}"
	shift 1

	case "${debug}" in '1')
		printf "DEBUG: ${fmt}\n" "${@}" 1>&2
	esac

	return 0
}

lookup_chips()
{
	local hwmon_dir=
	local hwmon=
	local name=

	for hwmon_dir in /sys/class/hwmon/hwmon*; do
		hwmon="${hwmon_dir#/sys/class/hwmon/}"
		if [ -f "${hwmon_dir}/name" ]; then
			read -r name <"${hwmon_dir}/name"
		elif [ -f "${hwmon_dir}/device/name" ]; then
			read -r name <"${hwmon_dir}/device/name"
		else
			return 1
		fi
		printf '%s %s\n' "${hwmon}" "${name}"
		dbg 'Chip %s = %s' "${name}" "${hwmon}"
	done

	return 0
}

lookup_chip()
{
	local chip="${1}"
	local chips="${2}"
	shift 2
	local hwmon=
	local name=

	while read -r hwmon name; do
		case "${name}" in "${chip}")
			printf '%s' "${hwmon}"
			return 0
		esac
	done <<-EOF
		${chips}
		EOF

	return 1
}

read_value()
{
	local hwmon="${1}"
	local element="${2}"
	shift 2
	local hwmon_dir=
	local value=

	hwmon_dir="/sys/class/hwmon/${hwmon}"
	if [ -f "${hwmon_dir}/${element}_input" ]; then
		read -r value <"${hwmon_dir}/${element}_input"
	elif [ -f "${hwmon_dir}/device/${element}_input" ]; then
		read -r value <"${hwmon_dir}/device/${element}_input"
	else
		return 1
	fi

	printf '%s' "${value}"
	return 0
}

format_temp()
{
	local hwmon="${1}"
	local element="${2}"
	shift 2
	local value=

	value="$(read_value "${hwmon}" "${element}")" || return 1
	printf '%d C' "${value%???}"

	return 0
}

format_fan()
{
	local hwmon="${1}"
	local element="${2}"
	shift 2
	local value=

	value="$(read_value "${hwmon}" "${element}")" || return 1
	printf '%d RPM' "${value}"

	return 0
}

format_power()
{
	local hwmon="${1}"
	local element="${2}"
	shift 2
	local value=

	value="$(read_value "${hwmon}" "${element}")" || return 1
	printf '%d W' "${value%??????}"

	return 0
}

print_sensor()
{
	local label="${1}"
	local len=${2}
	local chip="${3}"
	local chips="${4}"
	local element="${5}"
	shift 5
	local hwmon=
	local value=
	local i=

	if ! hwmon="$(lookup_chip "${chip}" "${chips}")"; then
		err 'Unknown chip "%s"' "${chip}"
		return 1
	fi

	case "${element}" in
		'temp'*)
			value="$(format_temp "${hwmon}" "${element}")" || \
				return 1
			;;
		'fan'*)
			value="$(format_fan "${hwmon}" "${element}")" || \
				return 1
			;;
		'power'*)
			value="$(format_power "${hwmon}" "${element}")" || \
				return 1
			;;
		*)
			err 'Unsupported type of element "%s"' "${element}"
			return 1
			;;
	esac

	printf ' %s:' "${label}"
	i=${#label}
	while [ ${i} -lt ${len} ]; do
		printf ' '
		i=$((${i} + 1))
	done
	printf ' %s\n' "${value}"

	return 0
}

usage()
{
	printf 'Usage: %s [-d] %s [%s ...]\n' "${0}" \
		'<label>:<chip>/<element>' '<label>:<chip>/<element>'
}

main()
{
	local opt=
	local chips=
	local max_len=
	local sensor=
	local label=
	local len=
	local chip=
	local element=

	while getopts 'd' opt "${@}"; do
		case "${opt}" in
			'd') debug=1;;
			'?')
				usage 1>&2
				return 1
				;;
		esac
	done
	shift $((${OPTIND} - 1))

	if [ ${#} -lt 1 ]; then
		usage 1>&2
		return 1
	fi

	chips="$(lookup_chips)" || return 1

	max_len=0
	for sensor in "${@}"; do
		label="${sensor%%:*}"
		len=${#label}
		if [ ${len} -gt ${max_len} ]; then
			max_len=${len}
		fi
	done

	for sensor in "${@}"; do
		label="${sensor%%:*}"
		sensor="${sensor#*:}"
		case "${sensor}" in '')
			printf '%s\n' "${label}"
			continue
		esac
		chip="${sensor%/*}"
		element="${sensor##*/}"
		print_sensor "${label}" ${len} \
			"${chip}" "${chips}" "${element}" || return 1
	done

	return 0
}

main "${@}"