📄 sysmonitor.c
字号:
#include<stdio.h>#include<stdlib.h>#include<pthread.h>#include<sys/time.h>#include<dirent.h>#include<sys/types.h>#include<unistd.h>#include"rpm.h"#include"linktable.c"void agent_cpu_func();void agent_mem_func();void agent_disk_func();void agent_net_func();void agent_proc_func();void console_disk_func();void console_cpu_func();void console_mem_func();void console_disk_func();void console_net_func();void console_proc_func();void control_func();pthread_mutex_t mut_CPU = PTHREAD_MUTEX_INITIALIZER;pthread_mutex_t mut_MEM = PTHREAD_MUTEX_INITIALIZER;pthread_mutex_t mut_DISK = PTHREAD_MUTEX_INITIALIZER;pthread_mutex_t mut_NET = PTHREAD_MUTEX_INITIALIZER;pthread_mutex_t mut_PROC = PTHREAD_MUTEX_INITIALIZER;char key = 0;int rev = 0;int nowseq = 0;proc *hashproc[1031],*head[1031],*tail[1031],*p[1031];uint procID[1031];int main(){ int k; printf("***************************************************\n"); printf("** Resource Performance Management Under Linux **\n"); printf("***************************************************\n"); hash_init(); while( key == 0 ) { printf("\nWhat type of information do you want?\n"); printf("1-CPU\n2-Memory\n3-Network\n4-Disk\n5-Process\n6-Quit\n:\t"); scanf("%d",&rev); //*****cpu if(rev==1) { key = 1; pthread_t pid; pthread_create(&pid,NULL,(void *)agent_cpu_func,NULL); pthread_create(&pid,NULL,(void *)console_cpu_func,NULL); pthread_create(&pid,NULL,(void *)control_func,NULL); pthread_join(pid,NULL); } //*****memory if(rev==2) { key = 1; pthread_t pid; pthread_create(&pid,NULL,(void *)agent_mem_func,NULL); pthread_create(&pid,NULL,(void *)console_mem_func,NULL); pthread_create(&pid,NULL,(void *)control_func,NULL); pthread_join(pid,NULL); } //*****network if(rev==3) { key = 1; pthread_t pid; pthread_create(&pid,NULL,(void *)agent_net_func,NULL); pthread_create(&pid,NULL,(void *)console_net_func,NULL); pthread_create(&pid,NULL,(void *)control_func,NULL); pthread_join(pid,NULL); } //*****disk if(rev==4) { key = 1; pthread_t pid; pthread_create(&pid,NULL,(void *)agent_disk_func,NULL); pthread_create(&pid,NULL,(void *)console_disk_func,NULL); pthread_create(&pid,NULL,(void *)control_func,NULL); pthread_join(pid,NULL); } //*****process if(rev==5) { key = 1; pthread_t pid1,pid2; pthread_create(&pid1,NULL,(void *)agent_proc_func,NULL); pthread_create(&pid2,NULL,(void *)console_proc_func,NULL); pthread_join(pid1,NULL); pthread_join(pid2,NULL); } //*****quit if(rev==6){ for(k = 0; k < 1031; k++) free(hashproc[k]); printf("You have safely exited!\n"); return 0; } if(rev < 0&& rev >9) { for(k = 0; k < 1031; k++) free(hashproc[k]); printf("Bad Input makes error!\n"); return 0; } sleep(0.2); } }void console_cpu_func(){ printf("\n"); printf("=========================================================\n"); printf(" CPUs Information\n"); printf(" Type q and Enter to quit!\n"); printf("=========================================================\n"); printf("---------------------------------------------------------\n"); printf("CPU_TIMEMODE TIME(secs) PERCENT GRAPHIC\n"); printf("---------------------------------------------------------\n"); ulong curr_s, prev_s, curr_u, prev_u; //receive the current time and previous time from the struct double sys_time, user_time; //time interval on system mode and usermode double per_s,per_u; //the percentage double curr_t, prev_t, interval_t; //the totle current time, previous time and the interval time struct timeval tv; struct timezone tz; int i,flag=0; while( key && rev == 1 ) { pthread_mutex_lock(&mut_CPU); gettimeofday(&tv,&tz);//get the current time prev_t = curr_t; curr_t = tv.tv_sec + (double)tv.tv_usec/1000000; interval_t = curr_t - prev_t; curr_s = rpm_cpus[4].curr_stime; prev_s = rpm_cpus[4].prev_stime; curr_u = rpm_cpus[4].curr_utime; prev_u = rpm_cpus[4].prev_utime; if(rpm_cpus[4].prev_stime!=0) { sys_time = (double)(curr_s - prev_s)/100; //the time interval on the system mode user_time = (double)(curr_u - prev_u)/100; //the time interval on the user mode // printf("%f:%f/%f\n:",sys_time,user_time,interval_t); per_s = (double)sys_time/interval_t; //compute the percentage of the system mode time per_u = (double)user_time/interval_t; //compute the percentage of the user mode time flag = 1; //open output } else { flag = 0; } pthread_mutex_unlock(&mut_CPU); if(flag) { printf("System time: %9.3f %7.3f% \t ", sys_time,per_s*100); for( i = 0; i <= sys_time; i++ ) printf("*"); printf("\n"); printf("User time: %9.3f %7.3f% \t ", user_time,per_u*100); for( i = 0; i <= user_time/50; i++ ) printf(">"); printf("\n"); fflush(stdout); } sleep(5); }}void agent_cpu_func(){ char fileSTR[60],CPUname[10],userMode[10],userModeL[10],sysMode[10]; int i; FILE *fp; while ( key ) { pthread_mutex_lock(&mut_CPU); if( (fp = fopen("/proc/stat","rt"))==NULL) { printf("can not open /proc/stat!"); return; } //**** read the total cpu info fgets(fileSTR,60,fp); if(fileSTR[0]!='c'&fileSTR[1]!='p'&fileSTR[2]!='u') { sleep(5); fclose(fp); continue; } sscanf(fileSTR,"%s %s %s %s",CPUname,userMode,userModeL,sysMode); rpm_cpus[4].prev_stime = rpm_cpus[4].curr_stime; rpm_cpus[4].prev_utime = rpm_cpus[4].curr_utime; rpm_cpus[4].curr_utime = atol(userMode) + atol(userModeL); rpm_cpus[4].curr_stime = atol(sysMode); //**** get each cpu info for( i = 0;i < 4;i++) { fgets(fileSTR,50,fp); if(fileSTR[0]!='c'&fileSTR[1]!='p'&fileSTR[2]!='u') continue; sscanf(fileSTR,"%s %s %s %s",CPUname,userMode,userModeL,sysMode); rpm_cpus[i].prev_stime = rpm_cpus[i].curr_stime; rpm_cpus[i].prev_utime = rpm_cpus[i].curr_utime; rpm_cpus[i].curr_utime = atol(userMode) + atol(userModeL); rpm_cpus[i].curr_stime = atol(sysMode); } fclose(fp); pthread_mutex_unlock(&mut_CPU); sleep(5); }}void agent_mem_func(){ char memSTR[1024],memTotal[20],memFree[20],memUsed[20],memName[20]; FILE *fp; while( key ) { pthread_mutex_lock(&mut_MEM); if( (fp = fopen("/proc/meminfo","rt"))==NULL) //**** windows Test { printf("can not open /proc/meminfo!"); return; } //********read the memory info fgets(memSTR,1024,fp); fgets(memSTR,1024,fp); if(memSTR[0]!='M'&memSTR[1]!='e'&memSTR[2]!='m') { fclose(fp); pthread_mutex_unlock(&mut_MEM); key = 0; return; } sscanf(memSTR,"%s %s %s %s ",memName,memTotal,memUsed,memFree); mem_info.mem_total = atol(memTotal); mem_info.mem_free = atol(memFree); fclose(fp); pthread_mutex_unlock(&mut_MEM); sleep(5); }}void console_mem_func(){ float free_mem,total_mem; printf("\n"); printf("=========================================================\n"); printf(" Memory Infomation\n"); printf(" type q and Enter to quit!\n"); printf("=========================================================\n"); printf("---------------------------------------------------------\n"); printf("\t Free-Mem \tTotal-Mem\n"); printf("---------------------------------------------------------\n"); pthread_mutex_lock(&mut_MEM); if( mem_info.mem_free < 1024 ) printf("\t %7dB \t ",mem_info.mem_free); else if( mem_info.mem_free >= 1024 && mem_info.mem_free < 1024*1024 ) { free_mem = (float)mem_info.mem_free/1024; printf("\t %6.2fKB \t ", free_mem); } else if( mem_info.mem_free >= 1024*1024 && mem_info.mem_free < 1024*1024*1024 ) { free_mem = (float)mem_info.mem_free/1024/1024; printf("\t %6.2fMB \t ", free_mem); } else if( mem_info.mem_free >= 1024*1024*1024) { free_mem = (float)mem_info.mem_free/1024/1024/1024; printf("\t %6.2fGB \t ", free_mem); } else { printf("free memory outside"); } if( mem_info.mem_total < 1024 ) printf("%7dB \n ",mem_info.mem_total); else if( mem_info.mem_total >= 1024 && mem_info.mem_total < 1024*1024 ) { total_mem = (float)mem_info.mem_total/1024; printf("%6.2fKB \n ", total_mem); } else if( mem_info.mem_total >= 1024*1024 && mem_info.mem_total < 1024*1024*1024 ) { total_mem = (float)mem_info.mem_total/1024/1024; printf("%6.2fMB \n ", total_mem); } else if( mem_info.mem_total >= 1024*1024*1024) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -