Untitled
unknown
plain_text
3 years ago
3.6 kB
5
Indexable
#include <ncurses.h>
#include <unistd.h>
#include <fstream>
#include <dirent.h>
#include <sys/sysinfo.h>
#include <sys/utsname.h>
int calculateMemoryPercentage()
{
struct sysinfo mem_info;
sysinfo(&mem_info);
int total_memory = mem_info.totalram;
int free_memory = mem_info.freeram;
int used_memory = total_memory - free_memory;
int memory_percentage = (used_memory * 100) / total_memory;
return memory_percentage;
}
void printMemory (int memory_percentage)
{
if (memory_percentage >= 75)
attron(COLOR_PAIR(2));
else
attron(COLOR_PAIR(1));
printw("Memory Usage: %d%%\n", memory_percentage);
attroff(COLOR_PAIR(1));
attroff(COLOR_PAIR(2));
}
void printCpuPercentage()
{
std::ifstream stat_file("/proc/stat");
std::string line;
getline(stat_file, line);
stat_file.close();
int user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice;
sscanf(line.c_str(), "cpu %d %d %d %d %d %d %d %d %d %d", &user, &nice, &system, &idle, &iowait, &irq, &softirq, &steal, &guest, &guest_nice);
int total_time = user + nice + system + idle + iowait + irq + softirq + steal + guest + guest_nice;
int usage = total_time - idle;
int cpu_percentage = (usage * 100) / total_time;
if (cpu_percentage >= 75)
attron(COLOR_PAIR(2));
else
attron(COLOR_PAIR(1));
printw("CPU Usage: %d%%\n", cpu_percentage);
attroff(COLOR_PAIR(1));
attroff(COLOR_PAIR(2));
}
void printProcesses()
{
printw("Processes:\n");
DIR* dir = opendir("/proc");
struct dirent* ent;
while ((ent = readdir(dir)) != NULL) {
if (ent->d_type == DT_DIR && isdigit(ent->d_name[0])) {
std::string pid = ent->d_name;
std::string status_path = "/proc/" + pid + "/status";
std::ifstream status_file(status_path);
std::string status_line;
while (getline(status_file, status_line)) {
if (status_line.substr(0, 4) == "Name") {
attron(COLOR_PAIR(4));
printw("%s (pid %s)\n", status_line.substr(6).c_str(), pid.c_str());
attroff(COLOR_PAIR(4));
break;
}
}
status_file.close();
}
}
closedir(dir);
}
void printSystemInformation()
{
time_t t = time(NULL);
struct tm tm = *localtime(&t);
attron(COLOR_PAIR(3));
printw("Time: %d:%d:%d Date: %d/%d/%d\n", tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_mday, tm.tm_mon + 1, tm.tm_year + 1900);
struct utsname unameData;
uname(&unameData);
attron(COLOR_PAIR(3));
printw("Operating System: %s\n", unameData.sysname);
printw("Kernel Version: %s\n", unameData.release);
attroff(COLOR_PAIR(3));
attron(COLOR_PAIR(5));
printw("Hostname: %s\n", unameData.nodename);
attroff(COLOR_PAIR(5));
}
void initCurses()
{
initscr();
noecho();
cbreak();
start_color();
init_pair(1, COLOR_GREEN, COLOR_BLACK);
init_pair(2, COLOR_RED, COLOR_BLACK);
init_pair(3, COLOR_YELLOW, COLOR_BLACK);
init_pair(4, COLOR_BLUE, COLOR_BLACK);
init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
}
int main() {
initCurses();
while(1) {
clear();
printSystemInformation();
printCpuPercentage();
printMemory(calculateMemoryPercentage());
printProcesses();
refresh();
sleep(1);
}
endwin();
return 0;
}
Editor is loading...