-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsfp_monitor.c
More file actions
424 lines (352 loc) · 12.8 KB
/
sfp_monitor.c
File metadata and controls
424 lines (352 loc) · 12.8 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
* sfp_monitor.c - Ultra-lean SFP+ DDM monitor daemon
*
* Polls ethtool -m for standard SFP+ DDM (Digital Diagnostics Monitoring)
* data and writes module temperature and supply voltage to hwmon-format
* files for CoolerControl to read as a custom file sensor.
*
* Works with any SFP+ transceiver that supports DDM — copper RJ45
* (RealHD, MikroTik S+RJ10, etc.), SR/LR optical, DAC, AOC.
* Parses only standard DDM fields; ignores spoofed optical data
* from copper transceivers that fake laser/optical identity.
*
* Compile: gcc -O2 -Wall -o sfp_monitor sfp_monitor.c
* Install: sudo cp sfp_monitor /usr/local/bin/
* Run: sudo sfp_monitor <interface> [output_dir] [poll_interval] [-v]
*
* hwmon output files created:
* <output_dir>/name - device name string
* <output_dir>/temp1_input - module temperature (millidegrees C)
* <output_dir>/temp1_label - "SFP+ Module"
* <output_dir>/temp1_max - high warning threshold (millidegrees C)
* <output_dir>/temp1_crit - high alarm threshold (millidegrees C)
* <output_dir>/in1_input - supply voltage (millivolts)
* <output_dir>/in1_label - "SFP+ Voltage"
* <output_dir>/in1_max - voltage high alarm (millivolts)
* <output_dir>/in1_min - voltage low alarm (millivolts)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/stat.h>
#include <time.h>
#define MAX_LINE 256
#define MAX_PATH 512
#define MAX_FILEPATH (MAX_PATH + 32) /* dir + "/temp1_input.tmp" etc. */
/* Configuration */
typedef struct {
char interface[64];
char output_dir[MAX_PATH];
int poll_interval;
int verbose;
} config_t;
/* Sensor data */
typedef struct {
int temp_millidegrees; /* Module temperature in millidegrees C */
int voltage_millivolts; /* Supply voltage in millivolts */
int temp_warn_high; /* High warning threshold (millidegrees) */
int temp_alarm_high; /* High alarm threshold (millidegrees) */
int volt_alarm_high; /* Voltage high alarm (millivolts) */
int volt_alarm_low; /* Voltage low alarm (millivolts) */
int valid; /* Data valid flag */
int thresholds_valid; /* Thresholds parsed flag */
} sensor_data_t;
/* Global for signal handling */
static volatile int keep_running = 1;
static void signal_handler(int signum) {
(void)signum;
keep_running = 0;
}
/* Atomic write integer to file (write tmp, rename) */
static int write_sensor_file(const char *path, int value) {
char tmp_path[MAX_FILEPATH];
FILE *fp;
snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
fp = fopen(tmp_path, "w");
if (!fp) {
perror("fopen");
return -1;
}
fprintf(fp, "%d\n", value);
fclose(fp);
if (rename(tmp_path, path) == -1) {
perror("rename");
unlink(tmp_path);
return -1;
}
chmod(path, 0444);
return 0;
}
/* Write string to file (for labels, name) */
static int write_string_file(const char *path, const char *value) {
FILE *fp = fopen(path, "w");
if (!fp) {
perror("fopen");
return -1;
}
fprintf(fp, "%s\n", value);
fclose(fp);
chmod(path, 0444);
return 0;
}
/* Recursive mkdir — create all path components (like mkdir -p) */
static int mkdir_p(const char *dir, mode_t mode) {
char tmp[MAX_PATH];
char *p;
snprintf(tmp, sizeof(tmp), "%s", dir);
for (p = tmp + 1; *p; p++) {
if (*p == '/') {
*p = '\0';
if (mkdir(tmp, mode) == -1 && errno != EEXIST)
return -1;
*p = '/';
}
}
if (mkdir(tmp, mode) == -1 && errno != EEXIST)
return -1;
return 0;
}
/* Create output directory and static files */
static int create_output_structure(const char *dir, const char *iface) {
char path[MAX_FILEPATH];
char device_name[128];
/* Create directory tree (works for both systemd and manual invocation) */
if (mkdir_p(dir, 0755) == -1) {
perror("mkdir_p");
return -1;
}
/* name file */
snprintf(device_name, sizeof(device_name), "sfp_%s", iface);
snprintf(path, sizeof(path), "%s/name", dir);
write_string_file(path, device_name);
/* Labels */
snprintf(path, sizeof(path), "%s/temp1_label", dir);
write_string_file(path, "SFP+ Module");
snprintf(path, sizeof(path), "%s/in1_label", dir);
write_string_file(path, "SFP+ Voltage");
return 0;
}
/* Convert float to milliunit int with proper rounding (not truncation) */
static inline int to_milli(float val) {
return (int)(val * 1000.0f + 0.5f);
}
/* Parse ethtool -m output for DDM values */
static int parse_ethtool_output(const char *output, sensor_data_t *data) {
char line[MAX_LINE];
const char *ptr = output;
const char *end;
float fval;
data->valid = 0;
data->thresholds_valid = 0;
while (*ptr) {
end = strchr(ptr, '\n');
if (!end) end = ptr + strlen(ptr);
int len = end - ptr;
if (len >= MAX_LINE - 1) len = MAX_LINE - 2;
strncpy(line, ptr, len);
line[len] = '\0';
/* Current readings — reject threshold/alarm/warning variants */
if (strstr(line, "Module temperature") && !strstr(line, "threshold") &&
!strstr(line, "alarm") && !strstr(line, "warning")) {
if (sscanf(line, "%*[^:]: %f", &fval) == 1) {
/* Sanity: reject readings outside -20°C to 120°C */
if (fval >= -20.0f && fval <= 120.0f) {
data->temp_millidegrees = to_milli(fval);
data->valid = 1;
}
}
}
if (strstr(line, "Module voltage") && !strstr(line, "threshold") &&
!strstr(line, "alarm") && !strstr(line, "warning")) {
if (sscanf(line, "%*[^:]: %f", &fval) == 1) {
/* Sanity: reject readings outside 0V to 5V */
if (fval >= 0.0f && fval <= 5.0f) {
data->voltage_millivolts = to_milli(fval);
}
}
}
/* Thresholds (written once, used by CoolerControl for limit display) */
if (strstr(line, "temperature high alarm threshold")) {
if (sscanf(line, "%*[^:]: %f", &fval) == 1) {
data->temp_alarm_high = to_milli(fval);
data->thresholds_valid = 1;
}
}
if (strstr(line, "temperature high warning threshold")) {
if (sscanf(line, "%*[^:]: %f", &fval) == 1) {
data->temp_warn_high = to_milli(fval);
}
}
if (strstr(line, "voltage high alarm threshold")) {
if (sscanf(line, "%*[^:]: %f", &fval) == 1) {
data->volt_alarm_high = to_milli(fval);
}
}
if (strstr(line, "voltage low alarm threshold")) {
if (sscanf(line, "%*[^:]: %f", &fval) == 1) {
data->volt_alarm_low = to_milli(fval);
}
}
ptr = (*end) ? end + 1 : end;
}
return data->valid ? 0 : -1;
}
/* Execute ethtool and read output */
static int read_sfp_data(const char *interface, sensor_data_t *data) {
char cmd[MAX_LINE];
char output[8192];
FILE *fp;
size_t bytes_read;
snprintf(cmd, sizeof(cmd), "ethtool -m %s 2>/dev/null", interface);
fp = popen(cmd, "r");
if (!fp) {
perror("popen");
return -1;
}
bytes_read = fread(output, 1, sizeof(output) - 1, fp);
output[bytes_read] = '\0';
pclose(fp);
return parse_ethtool_output(output, data);
}
/* Update all sensor files */
static int update_sensor_files(const char *dir, const sensor_data_t *data) {
char path[MAX_FILEPATH];
snprintf(path, sizeof(path), "%s/temp1_input", dir);
if (write_sensor_file(path, data->temp_millidegrees) == -1)
return -1;
snprintf(path, sizeof(path), "%s/in1_input", dir);
if (write_sensor_file(path, data->voltage_millivolts) == -1)
return -1;
return 0;
}
/* Write threshold files (only once, they don't change) */
static int write_threshold_files(const char *dir, const sensor_data_t *data) {
char path[MAX_FILEPATH];
if (!data->thresholds_valid)
return 0;
snprintf(path, sizeof(path), "%s/temp1_max", dir);
write_sensor_file(path, data->temp_warn_high);
snprintf(path, sizeof(path), "%s/temp1_crit", dir);
write_sensor_file(path, data->temp_alarm_high);
snprintf(path, sizeof(path), "%s/in1_max", dir);
write_sensor_file(path, data->volt_alarm_high);
snprintf(path, sizeof(path), "%s/in1_min", dir);
write_sensor_file(path, data->volt_alarm_low);
return 0;
}
static void get_timestamp(char *buf, size_t len) {
time_t now = time(NULL);
struct tm *tm = localtime(&now);
strftime(buf, len, "%H:%M:%S", tm);
}
/* Main monitoring loop */
static int monitor_loop(const config_t *config) {
sensor_data_t data;
char timestamp[32];
int errors = 0;
int thresholds_written = 0;
const int max_errors = 10;
fprintf(stderr, "sfp_monitor: interface=%s dir=%s poll=%ds\n",
config->interface, config->output_dir, config->poll_interval);
while (keep_running) {
if (read_sfp_data(config->interface, &data) == 0) {
if (update_sensor_files(config->output_dir, &data) == 0) {
errors = 0;
/* Write thresholds once on first successful read */
if (!thresholds_written && data.thresholds_valid) {
write_threshold_files(config->output_dir, &data);
thresholds_written = 1;
}
if (config->verbose) {
get_timestamp(timestamp, sizeof(timestamp));
fprintf(stderr, "[%s] Temp: %.2f°C Voltage: %.3fV\n",
timestamp,
data.temp_millidegrees / 1000.0,
data.voltage_millivolts / 1000.0);
}
} else {
fprintf(stderr, "sfp_monitor: error updating sensor files\n");
errors++;
}
} else {
fprintf(stderr, "sfp_monitor: error reading SFP+ DDM from %s\n",
config->interface);
errors++;
}
if (errors >= max_errors) {
fprintf(stderr, "sfp_monitor: too many consecutive errors, exiting\n");
return 1;
}
sleep(config->poll_interval);
}
fprintf(stderr, "sfp_monitor: shutting down\n");
return 0;
}
static void print_usage(const char *prog) {
fprintf(stderr,
"Usage: %s <interface> [output_dir] [poll_interval] [-v]\n"
"\n"
"Arguments:\n"
" interface Network interface with SFP+ module (required)\n"
" output_dir Directory for sensor files (default: /run/sfp-monitor/<interface>)\n"
" poll_interval Seconds between polls (default: 5)\n"
"\n"
"Options:\n"
" -v Verbose: log every poll to stderr\n"
" -h Show this help\n"
"\n"
"Example:\n"
" %s enp3s0f0 /run/sfp-monitor/enp3s0f0 5 -v\n"
"\n", prog, prog);
}
int main(int argc, char *argv[]) {
config_t config = {
.interface = "",
.output_dir = "",
.poll_interval = 5,
.verbose = 0
};
/* Parse arguments */
if (argc < 2 || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
print_usage(argv[0]);
return (argc < 2) ? 1 : 0;
}
if (argv[1][0] != '-') {
strncpy(config.interface, argv[1], sizeof(config.interface) - 1);
config.interface[sizeof(config.interface) - 1] = '\0';
}
if (config.interface[0] == '\0') {
fprintf(stderr, "sfp_monitor: interface name is required\n");
print_usage(argv[0]);
return 1;
}
if (argc >= 3 && argv[2][0] != '-') {
strncpy(config.output_dir, argv[2], sizeof(config.output_dir) - 1);
config.output_dir[sizeof(config.output_dir) - 1] = '\0';
}
if (argc >= 4 && argv[3][0] != '-') {
config.poll_interval = atoi(argv[3]);
if (config.poll_interval < 1) config.poll_interval = 5;
}
/* Scan for flags */
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-v") == 0) config.verbose = 1;
}
/* Default output dir: /run/sfp-monitor/<interface> */
if (config.output_dir[0] == '\0') {
snprintf(config.output_dir, sizeof(config.output_dir),
"/run/sfp-monitor/%s", config.interface);
}
/* Signal handlers */
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
/* Create output structure */
if (create_output_structure(config.output_dir, config.interface) == -1) {
fprintf(stderr, "sfp_monitor: failed to create %s\n", config.output_dir);
return 1;
}
return monitor_loop(&config);
}