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.

129 lines
3.1 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 MAX_MESSAGE_SIZE 1024
#define INTERVAL 10
char *string_insert(char *str1, const char *str2, int pos);
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);
int get_number_of_processes(void);
#ifdef SYSINFO_IMPLEMENTATION
char *string_insert(char *str1, const char *str2, int pos)
{
size_t l1 = strlen(str1);
size_t l2 = strlen(str2);
if (pos < 0) pos = 0;
if (pos > l1) pos = l1;
char *p = str1 + pos;
memmove(p + l2, p, l1 - pos);
memcpy (p, str2, l2);
return str1;
}
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(delay);
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_gb(const char *mountpoint)
{
return (mountpointUsedGB(mountpoint)/mountpointTotalGB(mountpoint)) * 100;
}
#endif