Files
Trashbox/status.c
2025-11-20 16:05:18 +08:00

56 lines
1.4 KiB
C
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// /home/romkazvo/www/cgi-bin/status.c
// Отрисовка статистики
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
long get_nginx_memory() {
long total_kb = 0;
char command[] = "ps -o rss= -C nginx 2>/dev/null";
FILE *ps = popen(command, "r");
if (!ps) return 0;
char line[64];
while (fgets(line, sizeof(line), ps)) {
long kb;
if (sscanf(line, "%ld", &kb) == 1) {
total_kb += kb;
}
}
pclose(ps);
return total_kb;
}
int get_process_memory(const char *process_name, long *memory_kb) {
char command[256];
snprintf(command, sizeof(command), "ps -o rss= -C %s 2>/dev/null | head -1", process_name);
FILE *ps = popen(command, "r");
if (!ps) return 0;
int result = fscanf(ps, "%ld", memory_kb);
pclose(ps);
return result == 1;
}
int main() {
printf("Content-type: text/plain; charset=utf-8\n\n");
// Получаем память процессов
long busybox_kb = 0, nginx_kb = 0;
get_process_memory("busybox", &busybox_kb);
nginx_kb = get_nginx_memory();
// Выводим в одну строку
printf("BusyBox: %ld KB | Nginx: %ld MB | Всего: %.1f MB",
busybox_kb,
nginx_kb / 1024,
(busybox_kb + nginx_kb) / 1024.0);
return 0;
}