?? observer.c
字號:
/*************************************************************************** main.c - description ------------------- begin : 一 4月 4 21:01:11 CST 2003 copyright : (C) 2003 by 張鴻烈 Function : 觀察linux內核行為/***************************************************************************/ #include <stdio.h>#include <sys/time.h>#define LB_SIZE 80enum TYPE{STANDARD,SHORT,LONG};FILE *thisProcFile;struct timeval now;enum TYPE reportType;char repTypeName[16];char *lineBuf;int interval; int duration;int iteration;char c1,c2;void sampleLoadAvg(){ //觀察系統負荷 int i=0; thisProcFile = fopen("/proc/loadavg","r"); fgets(lineBuf,LB_SIZE+1,thisProcFile); while(*(lineBuf+i)==' ') ++i;//跳過前導空格 printf("\nOne minute average "); while(*(lineBuf+i)!=' ') {printf("%c",*(lineBuf+i)); i++;}//顯示行中第一字段 while(*(lineBuf+i)==' ') ++i;//跳過前導空格 printf("\nFive minute average "); while(*(lineBuf+i)!=' ') {printf("%c",*(lineBuf+i)); i++;}//顯示行中第二字段 while(*(lineBuf+i)==' ') ++i;//跳過前導空格 printf("\nTen minute average "); while(*(lineBuf+i)!=' ') {printf("%c",*(lineBuf+i)); i++;}//顯示行中第三字段 while(*(lineBuf+i)==' ') ++i;//跳過前導空格 printf("\nRunuing Task/Total Task "); while(*(lineBuf+i)!=' ') {printf("%c",*(lineBuf+i)); i++;}//顯示行中第四字段 while(*(lineBuf+i)==' ') ++i;//跳過前導空格 printf("\nLast Process ID "); while(*(lineBuf+i)!='\0') {printf("%c",*(lineBuf+i)); i++;}//顯示行中第五字段 printf("\n"); fclose(thisProcFile);}void sampleTime(){//觀察系統啟動時間 long uptime,idletime; int day,hour,minute,second; int i,j; char temp[80]; i=j=0; thisProcFile = fopen("/proc/uptime","r"); fgets(lineBuf,LB_SIZE+1,thisProcFile); printf("%s\n",lineBuf); while(*(lineBuf+i)==' ') ++i;//跳過前導空格 while(*(lineBuf+i)!=' ') { *(temp+j)=*(lineBuf+i); i++; j++;}//取行中第一字段 *(temp+j)='\0'; uptime = atoi(temp);//將啟動時間的秒數轉換為長整數 day = uptime/(3600*24);//求出日數 hour= uptime%(3600*24)/3600;//求出小時數 minute=(uptime%(3600*24)%3600)/60;//求出分鐘數 second=uptime%60;//求出秒數 printf("Time from Boot\n"); printf("%d:%d:%d:%d\n\n",day,hour,minute,second); j=0; while(*(lineBuf+i)==' ') ++i; while(*(lineBuf+i)!='\0') { *(temp+j)=*(lineBuf+i); i++; j++;}//取行中第一字段 *(temp+j)='\0'; idletime = atoi(temp);//將啟動時間的空閑秒數轉換為長整數 day = idletime/(3600*24);//求出日數 hour= idletime%(3600*24)/3600;//求出小時數 minute=(idletime%(3600*24)%3600)/60;//求出分鐘數 second=idletime%60;//求出秒數 printf("Idle Time from Boot\n"); printf("%d:%d:%d:%d\n\n",day,hour,minute,second); printf("System Efficiency=%f\n\n",(float)uptime/(float)idletime);}int main(int argc,char *argv[]){ lineBuf = (char *)malloc(LB_SIZE+1); reportType = STANDARD; strcpy(repTypeName,"Standard"); if(argc >1){ sscanf(argv[1],"%c%c",&c1,&c2);//取命令行選擇符 if(c1!='-'){ printf("usage:observer [-b] [-c] [-d int dur]\n"); exit(1); } if(c2 == 'b'){//觀察部分B printf("******PART B **********\n"); thisProcFile = fopen("/proc/meminfo","r"); //觀察內存信息 while(!feof(thisProcFile)){ fgets(lineBuf,LB_SIZE+1,thisProcFile); printf("%s",lineBuf); } printf("\n"); fclose(thisProcFile); sampleTime(); } else if(c2=='c'){//觀察部分C printf("******PART C**********\n"); thisProcFile = fopen("/proc/stat","r"); while(!feof(thisProcFile)){//取系統工作狀態信息 fgets(lineBuf,LB_SIZE+1,thisProcFile); printf("%s",lineBuf); } printf("\n"); fclose(thisProcFile); } else if(c2 == 'd'){ //觀察部分 D printf("******PART D **********\n"); if(argc<4){ printf("usage:observer [-b] [-c][-d int dur]\n"); exit(1); } reportType = LONG; strcpy(repTypeName,"Long"); interval = atoi(argv[2]);//時間間隔 duration = atoi(argv[3]);//時間段 iteration = 0; while(iteration<duration){ sleep(interval);//睡眠一段時間 sampleLoadAvg();//觀察1,5,10分鐘系統平均負荷 iteration += interval;//加一個時間片 } } } else{//觀察部分B printf("******PART A **********\n"); reportType = SHORT; strcpy(repTypeName,"Short"); gettimeofday(&now,NULL);//取系統時間 printf("Status report type %s at %s\n", repTypeName,ctime(&(now.tv_sec))); thisProcFile = fopen("/proc/sys/kernel/hostname","r"); fgets(lineBuf,LB_SIZE+1,thisProcFile);//取機器名 printf("Machine hostname:%s\n",lineBuf); fclose(thisProcFile); thisProcFile = fopen("/proc/cpuinfo","r"); while(!feof(thisProcFile)){//取CPU信息 fgets(lineBuf,LB_SIZE+1,thisProcFile); printf("%s",lineBuf); } printf("\n"); fclose(thisProcFile); thisProcFile = fopen("/proc/version","r"); while(!feof(thisProcFile)){//取OS版本號 fgets(lineBuf,LB_SIZE+1,thisProcFile); printf("%s",lineBuf); } printf("\n"); fclose(thisProcFile); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -