From fb5b1690e6e8b6d9aad51ee7d861782583ddea39 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Mon, 15 Jul 2024 11:58:55 +0200 Subject: [PATCH] initial commit --- .gitignore | 2 + Makefile | 18 ++++++++ README.md | 15 +++++++ main.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 156 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c02e87e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/a.out +/measurer \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..19b6752 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +CC = gcc +CFLAGS = -Wall -g +SOURCE = main.c +TARGET = measurer + +# Default target +all: $(TARGET) + +# Rule to build the executable +$(TARGET): $(SOURCE) + $(CC) $(CFLAGS) -o $@ $< + +# Rule to clean up object files and the executable +clean: + rm -f $(TARGET) *.o + +# Phony targets +.PHONY: all clean \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..06666df --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +a lightweight script that logs the most important stuff about the raspberry pi + +# most important stuff what +- cpu temperature + +# warning +CPU temperature is read from `/sys/class/thermal/thermal_zone0/temp` but that should work on every Linux computer \ +Ambient temperature is read from `/sys/bus/w1/devices` so you must have 1wire enabled, if you don't have that set `SENSOR` to `0` + +# compilation +`make` or `gcc main.c -o measurer` \ +Should throw no warnings + +# why +like heavy ahh like databases grafana etc go away you're a performance monitor why'd you reduce performance \ No newline at end of file diff --git a/main.c b/main.c new file mode 100644 index 0000000..f06573d --- /dev/null +++ b/main.c @@ -0,0 +1,121 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define SENSOR 1 + +int read_temperature(char *filename) { + FILE *fp; + fp = fopen(filename, "r"); + + if (fp == NULL) { + fprintf(stderr, "Error opening file %s: %s\n", filename, strerror(errno)); // instead of perror + return INT_MIN; + } + + char buffer[6]; // because the file returns 5 digits + null terminator + + if (fgets(buffer, sizeof(buffer), fp) == NULL) { + fprintf(stderr, "Error reading from %s: %s\n", filename, strerror(errno)); + fclose(fp); + return INT_MIN; + } + + fclose(fp); + + int temperature = strtol(buffer, NULL, 10); + return temperature; +} + +int detect_ds18b20(char *w1_id) { + DIR *d; + struct dirent *dir; + d = opendir("/sys/bus/w1/devices"); + + bool success = false; + + if (d) { + while ((dir = readdir(d)) != NULL) { + strcpy(w1_id, dir->d_name); + if (strncmp("28", w1_id, 2) == 0) { + success = true; + } + } + closedir(d); + } else { + perror("Unable to open w1 directory"); + } + + if (success) return 0; + return 1; +} + + +int read_cpu_temperature() { + return read_temperature("/sys/class/thermal/thermal_zone0/temp"); +} + +int read_w1_temperature(char *w1_id) { + char *path; + if (asprintf(&path, "/sys/bus/w1/devices/%s/temperature", w1_id) == -1) { + fprintf(stderr, "Unable to get path for %s: %s\n", w1_id, strerror(errno)); + return INT_MIN; + } + return read_temperature(path); +} + +int main(int argc, char *argv[]) { + if (argc == 0) { + printf("Filename required\n"); + return 1; + } + + time_t now = time(0); + char *line; + + int cpu_temperature; + if ((cpu_temperature = read_cpu_temperature()) == INT_MIN) { + perror("Failed to read cpu temperature"); + return 1; + } + + if (SENSOR) { + char w1_id[16]; + if ((detect_ds18b20(w1_id)) == 1) + return 1; + printf("Using sensor: %s\n", w1_id); + + int env_temperature; + if ((env_temperature = read_w1_temperature(w1_id)) == INT_MIN) { + perror("Failed to read env temperature"); + return 1; + } + + asprintf(&line, "%ld,%d,%d\n", now, cpu_temperature, env_temperature); + printf("timestamp,cpu_temp,env_temp\n%s", line); + } else { + asprintf(&line, "%ld,%d\n", now, cpu_temperature); + printf("timestamp,cpu_temp\n%s", line); + } + + + FILE *fp; + fp = fopen(argv[0], "a"); + + if (fp == NULL) { + printf("Error opening output file"); + return 1; + } + + if (fprintf(fp, line) != 0) { + perror("Error writing to output file"); + fclose(fp); + return 1; + } + fclose(fp); +} \ No newline at end of file