#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
void print_datetime(time_t now) {
struct tm *local = localtime(&now);
char buffer[20];
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M", local);
printf("%s", buffer);
}
void print_load() {
double loadavg[3];
if (getloadavg(loadavg, 3) == -1) {
perror("getloadavg failed");
return;
}
if(loadavg[0] > 1.0){
int result = system("ps aux | awk '$3 > 1.0' | sort -k3 -nr");
// int result = system("top -n 1");
(void)result;
}
printf("%.2f %.2f %.2f", loadavg[0], loadavg[1], loadavg[2]);
}
int main(int argc, char *argv[]) {
time_t start_time = time(NULL);
while (true) {
for(int i = 1; i < argc; i++){
printf("%s ", argv[i]);
}
if(argc == 1){
printf("%s", "\rUpdate ");
}
print_datetime(time(NULL));
printf("%s", " since ");
print_datetime(start_time);
printf("%s", ". ");
printf("%s", "Load: ");
print_load();
printf(".%s", "\n");
fflush(stdout);
usleep(59 * 1000 * 1000);
}
return 0;
}