You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
2.9 KiB
C
113 lines
2.9 KiB
C
#include <dirent.h>
|
|
#include <limits.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/vfs.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#define INTERVAL 10
|
|
|
|
int get_cpu_usage_percentage(void);
|
|
int get_ram_usage_percentage(void);
|
|
void get_formatted_date_time(char *format, char *formattedDateTime);
|
|
double get_available_space_from_mountpoint_gb(const char *mountpoint);
|
|
double get_total_space_from_mountpoint_gb(const char *mountpoint);
|
|
double get_used_space_from_mountpoint_gb(const char *mountpoint);
|
|
double get_used_space_from_mountpoint_percentage(const char *mountpoint);
|
|
int get_number_of_processes(void);
|
|
|
|
#ifdef SYSINFO_IMPLEMENTATION
|
|
|
|
int get_cpu_usage_percentage(void)
|
|
{
|
|
int perc;
|
|
long double a[4], b[4];
|
|
FILE *fp;
|
|
|
|
fp = fopen("/proc/stat", "r");
|
|
if (fp == NULL) {
|
|
fprintf(stderr, "ERROR: failed to open file /proc/stat");
|
|
return 1;
|
|
}
|
|
fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
|
|
fclose(fp);
|
|
|
|
sleep(INTERVAL);
|
|
|
|
fp = fopen("/proc/stat", "r");
|
|
if (fp == NULL) {
|
|
fprintf(stderr, "ERROR: failed to open file /proc/stat");
|
|
return 1;
|
|
}
|
|
fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
|
|
fclose(fp);
|
|
|
|
perc = 100 * ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3]));
|
|
return (perc);
|
|
}
|
|
|
|
int get_ram_usage_percentage(void)
|
|
{
|
|
long total, free, buffers, cached;
|
|
FILE *fp;
|
|
|
|
fp = fopen("/proc/meminfo", "r");
|
|
if (fp == NULL) {
|
|
fprintf(stderr, "ERROR: failed to open file /proc/meminfo");
|
|
return 1;
|
|
}
|
|
fscanf(fp, "MemTotal: %ld kB\n", &total);
|
|
fscanf(fp, "MemFree: %ld kB\n", &free);
|
|
fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
|
|
fscanf(fp, "Cached: %ld kB\n", &cached);
|
|
fclose(fp);
|
|
|
|
return (100 * ((total - free) - (buffers + cached)) / total);
|
|
}
|
|
|
|
void get_formatted_date_time(char *format, char *formattedDateTime)
|
|
{
|
|
time_t rawtime;
|
|
struct tm *timeptr;
|
|
|
|
time(&rawtime);
|
|
timeptr = localtime(&rawtime);
|
|
strftime(formattedDateTime, 80, format, timeptr);
|
|
}
|
|
|
|
double get_available_space_from_mountpoint_gb(const char *mountpoint)
|
|
{
|
|
struct statfs mountpointStatfs;
|
|
|
|
statfs(mountpoint, &mountpointStatfs);
|
|
|
|
return ((long double)mountpointStatfs.f_bfree * mountpointStatfs.f_bsize)/1073741824;
|
|
}
|
|
|
|
double get_total_space_from_mountpoint_gb(const char *mountpoint)
|
|
{
|
|
struct statfs mountpointStatfs;
|
|
|
|
statfs(mountpoint, &mountpointStatfs);
|
|
|
|
return ((long double)mountpointStatfs.f_blocks * mountpointStatfs.f_bsize)/1073741824;
|
|
}
|
|
|
|
double get_used_space_from_mountpoint_gb(const char *mountpoint)
|
|
{
|
|
struct statfs mountpointStatfs;
|
|
|
|
statfs(mountpoint, &mountpointStatfs);
|
|
|
|
return (((long double)mountpointStatfs.f_blocks * mountpointStatfs.f_bsize) - ((long double)mountpointStatfs.f_bfree * mountpointStatfs.f_bsize))/1073741824;
|
|
}
|
|
|
|
double get_used_space_from_mountpoint_percentage(const char *mountpoint)
|
|
{
|
|
return (get_used_space_from_mountpoint_gb(mountpoint)/get_total_space_from_mountpoint_gb(mountpoint)) * 100;
|
|
}
|
|
|
|
#endif
|