навел поряд в структуре
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
// /home/romkazvo/www/cgi-bin/src/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;
|
||||
}
|
||||
|
||||
float get_cpu_load() {
|
||||
FILE *fp = fopen("/proc/loadavg", "r");
|
||||
if (!fp) return 0.0f;
|
||||
|
||||
float load;
|
||||
fscanf(fp, "%f", &load);
|
||||
fclose(fp);
|
||||
|
||||
return load;
|
||||
}
|
||||
|
||||
void get_memory_usage(long *used_mb, long *total_mb) {
|
||||
FILE *fp = fopen("/proc/meminfo", "r");
|
||||
if (!fp) {
|
||||
*used_mb = 0;
|
||||
*total_mb = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
char line[128];
|
||||
long total = 0, available = 0;
|
||||
|
||||
while (fgets(line, sizeof(line), fp)) {
|
||||
if (sscanf(line, "MemTotal: %ld kB", &total) == 1) {
|
||||
// сохраняем
|
||||
} else if (sscanf(line, "MemAvailable: %ld kB", &available) == 1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
*total_mb = total / 1024;
|
||||
*used_mb = (total - available) / 1024;
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
double busybox_mb = busybox_kb / 1024.0;
|
||||
double nginx_mb = nginx_kb / 1024.0;
|
||||
double total_mb = busybox_mb + nginx_mb;
|
||||
|
||||
float cpu_load = get_cpu_load();
|
||||
|
||||
long used_mb = 0, total_sys_mb = 0;
|
||||
get_memory_usage(&used_mb, &total_sys_mb);
|
||||
|
||||
// Две строки
|
||||
printf("BusyBox: %.1f MB | Nginx: %.1f MB | Всего: %.1f MB\n",
|
||||
busybox_mb, nginx_mb, total_mb);
|
||||
printf("CPU Load: %.2f | Память: %ld / %ld MB (%.0f%%)",
|
||||
cpu_load, used_mb, total_sys_mb, total_sys_mb > 0 ? (float)used_mb / total_sys_mb * 100 : 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user