/* * Rick van der Zwet * 0433373 * OS Assigment 2 - Part BCD * Licence: BSD * $Id: partBCD.c 508 2008-03-02 14:18:40Z rick $ */ #include #include #include #include #include void usage(const int errno) { printf("Usage:\n"); printf("-h : This screen\n"); printf("-l : times to show loadavg\n"); printf("-s : Sleep seconds between every loadavg line\n"); exit(errno); } void partB() { char buffer[100]; long int uptime_seconds = 0; int i, days, hours, minutes, seconds; FILE * file; /* cpuinfo */ file = fopen("/proc/cpuinfo", "r"); /* move to right line */ for (i = 0; i <= 3; i++) while (fgetc(file) != '\n'); /* Fix offset */ fgets(buffer, 14, file); /* Read data */ fgets(buffer, 50, file); fclose(file); printf("CPU Info: %s", buffer); /* Kernel version */ file = fopen("/proc/sys/kernel/osrelease", "r"); fgets(buffer, 50, file); fclose(file); printf("Kernel version: %s", buffer); /* Pretty formatted uptime */ file = fopen("/proc/uptime", "r"); fscanf(file, "%li", &uptime_seconds); fclose(file); days = uptime_seconds / (24 * 3600); uptime_seconds = uptime_seconds % (24 * 3600); hours = uptime_seconds / 3600; uptime_seconds = uptime_seconds % 3600; minutes = uptime_seconds / 60; seconds = uptime_seconds % 60; printf("Uptime (dd:hh:mm:ss): %02i:%02i:%02i:%02i\n", days, hours, minutes, seconds ); } void partC() { char buffer[100]; long int cpu_user, cpu_user_nice, cpu_system, cpu_idle; long int disk_read_total, disk_write_total, disk_read, disk_write; int i, errno; time_t boot_time, now, uptime; FILE * file; /* User,system, idle mode */ file = fopen("/proc/stat", "r"); fscanf(file, "%*s %li %li %li %li", &cpu_user, &cpu_user_nice, &cpu_system, &cpu_idle); fclose(file); printf("Buffer: %s", buffer); printf("CPU User : %10li sec\n", cpu_user + cpu_user_nice); printf("CPU System : %10li sec\n", cpu_system); printf("CPU Idle : %10li sec\n", cpu_idle); /* Disk operations, read/write on all disks (sum) * Compatible with 2.6 kernel only Legenda at Documentation/iostats.txt * /proc/diskstat read: field 4, write field 8 * Partitions only has 7 fields and could easy be ignored */ disk_read = 0; disk_write = 0; file = fopen("/proc/diskstats", "r"); /* %*100c = dirty hack to move till EOL */ while ((errno = fscanf(file, "%*s %*s %*s %li %*s %*s %*s %li %*100c", &disk_read, &disk_write)) != EOF) { if (errno == 0) continue; disk_read_total += disk_read; disk_write_total += disk_write; } fclose(file); printf("Disk read operations : %li\n", disk_read_total); printf("Disk write operations : %li\n", disk_read_total); /* Context switches */ file = fopen("/proc/stat", "r"); for (i = 0; i <= 2; i++) while (fgetc(file) != '\n'); fgets(buffer, 5, file); fgets(buffer, 50, file); fclose(file); printf("Kernel context switches: %s", buffer); /* Time/Date last booted */ file = fopen("/proc/uptime", "r"); fscanf(file, "%li", &uptime); fclose(file); now = time(NULL); boot_time = now - uptime; printf("Time/Date last booted: %s", ctime(&boot_time)); /* Number of processes since boot */ file = fopen("/proc/stat", "r"); for (i = 0; i <= 4; i++) while (fgetc(file) != '\n'); fgets(buffer, 10, file); fgets(buffer, 50, file); fclose(file); printf("Processes since boot: %s", buffer); } void partDmem() { long int mem_available, mem_total; FILE * file; file = fopen("/proc/meminfo", "r"); fscanf(file, "%*s %li %*s", &mem_total); fscanf(file, "%*s %li %*s", &mem_available); fclose(file); printf("Memory total : %li kB\n", mem_total); printf("Memory available : %li kB\n", mem_available); } void partDloadavg(const int times, const int sleeptime) { float loadavg_min; int i; FILE * file; printf("Sleep timer: %i sec\n", sleeptime); for (i = 0; i < times; i++) { /* Might want do some forking over here to fake some load, by * now load needs to be generated by some external processes */ sleep(sleeptime); file = fopen("/proc/loadavg", "r"); fscanf(file, "%f", &loadavg_min); fclose(file); printf("%03i : %03.2f\n", i, loadavg_min); } } int main(int argc, char * argv[]) { int extendedinfoflag = 0; int loadavgtimes = 0; int loadavgsleep = 60; int ch; while ((ch = getopt(argc, argv, "hil:s:")) != -1) switch (ch) { case 'i': extendedinfoflag = 1; break; case 'l': loadavgtimes = atoi(optarg); break; case 's': loadavgsleep = atoi(optarg); break; case 'h': usage(EX_OK); /* NOTREACHED */ default: usage(EX_USAGE); /* NOTREACHED */ } partB(); if (extendedinfoflag == 1) partC(); partDmem(); if (loadavgtimes > 0) partDloadavg(loadavgtimes, loadavgsleep); return(EX_OK); }