-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·282 lines (235 loc) · 8.77 KB
/
install.sh
File metadata and controls
executable file
·282 lines (235 loc) · 8.77 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/bin/bash
# install.sh - Build and install sfp_monitor
#
# Usage: sudo ./install.sh [interface]
#
# If no interface is given, auto-detects ixgbe (Intel 82599) interfaces.
# If multiple are found, presents a menu. If none, prompts with tab completion.
#
# What this does:
# 1. Detects or prompts for the SFP+ interface
# 2. Compiles sfp_monitor.c → /usr/local/bin/sfp_monitor
# 3. Generates and installs a systemd service for the chosen interface
# 4. Enables and starts the service
# 5. Verifies sensor files are being written
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# ─── Root check ───
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}Error: run with sudo${NC}"
echo "Usage: sudo $0 [interface]"
exit 1
fi
# ─── Interface detection ───
# Find all ixgbe-driven interfaces (Intel 82599 / X520 / X540 / HP 560 etc.)
detect_ixgbe_interfaces() {
local ifaces=()
for path in /sys/class/net/*; do
local iface
iface=$(basename "$path")
local driver
driver=$(readlink "$path/device/driver" 2>/dev/null | xargs basename 2>/dev/null || true)
if [ "$driver" = "ixgbe" ]; then
ifaces+=("$iface")
fi
done
echo "${ifaces[@]}"
}
# Tab-completion prompt for manual interface entry
# Uses bash readline (bind -x) to hook Tab during read -e
read_interface_with_completion() {
local all_ifaces
all_ifaces=$(ls /sys/class/net/ 2>/dev/null | tr '\n' ' ')
_sfp_tab_complete() {
local cur="${READLINE_LINE}"
local matches
matches=$(compgen -W "$all_ifaces" -- "$cur")
local count
count=$(echo "$matches" | wc -w)
if [ "$count" -eq 1 ]; then
READLINE_LINE="$matches"
READLINE_POINT=${#READLINE_LINE}
elif [ "$count" -gt 1 ]; then
echo ""
echo "$matches" | tr ' ' '\n' | column
printf "Interface: %s" "$READLINE_LINE"
fi
}
bind -x '"\t": _sfp_tab_complete' 2>/dev/null
read -e -p "Interface: " REPLY < /dev/tty
bind '"\t": complete' 2>/dev/null
echo "$REPLY"
}
# Determine interface: CLI arg → auto-detect → interactive
select_interface() {
# 1. CLI argument
if [ -n "${1:-}" ]; then
echo "$1"
return
fi
# 2. Auto-detect ixgbe interfaces
local detected
detected=$(detect_ixgbe_interfaces)
local ifaces
read -ra ifaces <<< "$detected"
local count=${#ifaces[@]}
if [ "$count" -eq 0 ]; then
echo -e "${YELLOW}No ixgbe (Intel 82599) interfaces detected.${NC}" >&2
echo "Available interfaces:" >&2
ip -o link show | awk '{print " " $2}' | sed 's/://' >&2
echo "" >&2
echo -e "Enter the SFP+ interface name ${CYAN}(Tab to autocomplete)${NC}:" >&2
read_interface_with_completion
return
fi
if [ "$count" -eq 1 ]; then
echo -e "${GREEN}Found ixgbe interface: ${ifaces[0]}${NC}" >&2
# Quick sanity: does ethtool -m work on it?
if ethtool -m "${ifaces[0]}" &>/dev/null; then
echo -e "${GREEN} └─ SFP+ module detected (DDM available)${NC}" >&2
else
echo -e "${YELLOW} └─ No SFP+ module detected (ethtool -m failed)${NC}" >&2
fi
read -p "Use ${ifaces[0]}? (Y/n): " confirm < /dev/tty
if [[ "$confirm" =~ ^[Nn]$ ]]; then
echo -e "Enter the SFP+ interface name ${CYAN}(Tab to autocomplete)${NC}:" >&2
read_interface_with_completion
return
fi
echo "${ifaces[0]}"
return
fi
# Multiple ixgbe interfaces — show menu
echo -e "${CYAN}Found ${count} ixgbe interfaces:${NC}" >&2
for i in "${!ifaces[@]}"; do
local mod_status="no module"
if ethtool -m "${ifaces[$i]}" &>/dev/null; then
mod_status="SFP+ module detected"
fi
printf " %d) %-20s (%s)\n" $((i + 1)) "${ifaces[$i]}" "$mod_status" >&2
done
echo "" >&2
while true; do
read -p "Select interface [1-${count}]: " choice < /dev/tty
if [[ "$choice" =~ ^[0-9]+$ ]] && [ "$choice" -ge 1 ] && [ "$choice" -le "$count" ]; then
echo "${ifaces[$((choice - 1))]}"
return
fi
echo "Invalid selection." >&2
done
}
# ─── Select interface ───
INTERFACE=$(select_interface "${1:-}")
if [ -z "$INTERFACE" ]; then
echo -e "${RED}No interface selected. Aborting.${NC}"
exit 1
fi
# Validate interface exists
if ! ip link show "$INTERFACE" &>/dev/null; then
echo -e "${RED}Warning: interface '${INTERFACE}' not found in ip link.${NC}"
read -p "Continue anyway? (y/N): " confirm < /dev/tty
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
exit 1
fi
fi
SENSOR_DIR="/run/sfp-monitor/${INTERFACE}"
echo ""
echo -e "${CYAN}Installing sfp_monitor for interface: ${GREEN}${INTERFACE}${NC}"
echo ""
# ─── Compile ───
if ! command -v gcc &>/dev/null; then
echo -e "${YELLOW}gcc not found, installing...${NC}"
dnf install -y gcc
fi
echo -e "${CYAN}[1/4] Compiling sfp_monitor...${NC}"
gcc -O2 -Wall -Wextra -o /usr/local/bin/sfp_monitor "${SCRIPT_DIR}/sfp_monitor.c"
chmod 755 /usr/local/bin/sfp_monitor
echo -e "${GREEN}✓ Installed /usr/local/bin/sfp_monitor${NC}"
# ─── Generate and install service ───
echo -e "${CYAN}[2/4] Installing systemd service...${NC}"
cat > /etc/systemd/system/sfp-monitor.service << EOF
[Unit]
Description=SFP+ DDM Temperature Monitor (${INTERFACE})
Documentation=man:ethtool(8)
After=network-online.target
Wants=network-online.target
StartLimitIntervalSec=60
StartLimitBurst=5
[Service]
Type=simple
ExecStart=/usr/local/bin/sfp_monitor ${INTERFACE} /run/sfp-monitor/${INTERFACE} 5
RuntimeDirectory=sfp-monitor/${INTERFACE}
RuntimeDirectoryMode=0755
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes
NoNewPrivileges=yes
ReadWritePaths=/run/sfp-monitor
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
echo -e "${GREEN}✓ Generated and installed /etc/systemd/system/sfp-monitor.service${NC}"
# ─── Enable and start ───
echo -e "${CYAN}[3/4] Enabling and starting service...${NC}"
systemctl enable sfp-monitor.service
systemctl restart sfp-monitor.service
echo -e "${GREEN}✓ Service started${NC}"
# ─── Verify ───
echo -e "${CYAN}[4/4] Verifying sensor output...${NC}"
sleep 6
if [ -f "${SENSOR_DIR}/temp1_input" ]; then
TEMP_RAW=$(cat "${SENSOR_DIR}/temp1_input")
TEMP_C=$(echo "scale=2; ${TEMP_RAW} / 1000" | bc)
echo -e "${GREEN}✓ Temperature: ${TEMP_C}°C (${TEMP_RAW} mC)${NC}"
else
echo -e "${RED}✗ No temperature data yet. Check: journalctl -u sfp-monitor -f${NC}"
fi
if [ -f "${SENSOR_DIR}/in1_input" ]; then
VOLT_RAW=$(cat "${SENSOR_DIR}/in1_input")
VOLT_V=$(echo "scale=3; ${VOLT_RAW} / 1000" | bc)
echo -e "${GREEN}✓ Voltage: ${VOLT_V}V (${VOLT_RAW} mV)${NC}"
fi
if [ -f "${SENSOR_DIR}/temp1_crit" ]; then
CRIT_RAW=$(cat "${SENSOR_DIR}/temp1_crit")
CRIT_C=$(echo "scale=2; ${CRIT_RAW} / 1000" | bc)
echo -e "${GREEN}✓ Critical threshold: ${CRIT_C}°C${NC}"
fi
# ─── Summary ───
echo ""
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo -e "${CYAN} CoolerControl Configuration${NC}"
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo ""
echo "To add the SFP+ temperature sensor in CoolerControl:"
echo ""
echo " 1. Open CoolerControl UI (http://localhost:11987 or via SSH tunnel)"
echo " 2. Go to Custom Sensors"
echo " 3. Add a File Sensor:"
echo " Name: SFP+ ${INTERFACE} Temperature"
echo " Path: ${SENSOR_DIR}/temp1_input"
echo " Unit: millidegrees (÷1000 = °C)"
echo ""
echo " 4. Optionally add voltage sensor:"
echo " Name: SFP+ ${INTERFACE} Voltage"
echo " Path: ${SENSOR_DIR}/in1_input"
echo ""
echo " 5. Create a fan profile using the temperature sensor"
echo " to drive the SFP+ cage cooling fan"
echo ""
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo ""
echo "Useful commands:"
echo " journalctl -u sfp-monitor -f # live logs"
echo " systemctl status sfp-monitor # service status"
echo " cat ${SENSOR_DIR}/temp1_input # current temp (mC)"
echo " cat ${SENSOR_DIR}/in1_input # current voltage (mV)"
echo ""
echo -e "${GREEN}Done!${NC}"