⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sysmonitor.c

📁 在linux下获取/proc中的系统信息
💻 C
📖 第 1 页 / 共 2 页
字号:
					total_mem = (float)mem_info.mem_total/1024/1024/1024;					printf("%6.2fGB \n ", total_mem);				}				else				{					printf("free memory outside");					return;				}	pthread_mutex_unlock(&mut_MEM);	getchar();		return;}void agent_disk_func(){	char fileSTR[1024],major[10],minor[10],rio[20],wio[20],ruse[20],wuse[20],block[20];	FILE *fp;	int i;	while( key )	{		pthread_mutex_lock(&mut_DISK);		if( (fp = fopen("/proc/partitions","rt"))==NULL)              //**** windows Test		{			printf("can not open /proc/partitions!");			return;		}		fgets(fileSTR,1024,fp);		fgets(fileSTR,1024,fp);		for(i=0;i<3;i++)		{			fgets(fileSTR,1024,fp);			if(fileSTR[0] == '\0')continue;			sscanf(fileSTR," %s %s %s %*s %s %*s %*s %s %s %*s %*s %s",major,minor,block,rio,ruse,wio,wuse);			disk_IO[i].prev_rio = disk_IO[i].curr_rio;			disk_IO[i].prev_ruse = disk_IO[i].curr_ruse;			disk_IO[i].prev_wio = disk_IO[i].curr_wio;			disk_IO[i].prev_wuse = disk_IO[i].curr_wuse;			disk_IO[i].major = atoi( major );			disk_IO[i].minor = atoi( minor );			disk_IO[i].block = atol( block );			disk_IO[i].curr_rio = atol( rio );			disk_IO[i].curr_ruse = atol( ruse );			disk_IO[i].curr_wio = atol( wio );			disk_IO[i].curr_wuse = atol( wuse );		}		fclose(fp);		pthread_mutex_unlock(&mut_DISK);		sleep(5);	}}void console_disk_func(){	printf("=========================================================\n");	printf("                 DiskIO Infomation\n");	printf("              type q and Enter to quit!\n");	printf("=========================================================\n\n");	printf("----------------------------------------------------------------------------\n");	printf("major minor block_size read_OP_NUM/Sec write_OP_NUM/Sec read_rate write_rate\n");	printf("----------------------------------------------------------------------------\n");	int i,flag=0;	float read_OP_NUM,write_OP_NUM,read_rate,write_rate;	while( key && rev == 4 )	{		pthread_mutex_lock(&mut_DISK);		for( i=0; i<3; i++ )		{			if( disk_IO[i].prev_rio != 0 )			{				printf("%5d ",disk_IO[i].major);				printf("%5d ",disk_IO[i].minor);				printf("%10d ",disk_IO[i].block);				read_OP_NUM = (float)( disk_IO[i].curr_rio - disk_IO[i].prev_rio )/ 5;				write_OP_NUM = (float)( disk_IO[i].curr_wio - disk_IO[i].prev_wio )/ 5;				read_rate = (float)( disk_IO[i].curr_ruse - disk_IO[i].prev_ruse )/ 5;				write_rate = (float)( disk_IO[i].curr_wuse - disk_IO[i].prev_wuse )/ 5;				printf("%15.2f %16.2f %9.2f %10.2f\n",read_OP_NUM,write_OP_NUM,read_rate,write_rate);			}			else			{				break;			}		}		printf("\n");		pthread_mutex_unlock(&mut_DISK);		sleep(5);	}}void console_net_func(){	printf("=========================================================\n");	printf("                 NetWork Infomation \n");	printf("              type q and Enter to quit!\n");	printf("=========================================================\n");	printf("---------------------------------------------------------\n");	printf("packet_IN_rate   packet_FWD_rate   total_packet_rate\n");	printf("---------------------------------------------------------\n");	float in,fwd,total;	while ( key && rev == 3 )	{				pthread_mutex_lock(&mut_NET);		if( network_info.prev_incoming )		{			in = (float)( network_info.curr_incoming - network_info.prev_incoming )/ 5;				fwd = (float)( network_info.curr_forwarding - network_info.prev_forwarding )/5;			total = (float)( network_info.curr_total - network_info.prev_total )/ 5;			printf("%14.2f   %15.2f   %17.2f\n",in,fwd,total);		}				pthread_mutex_unlock(&mut_NET);		sleep(5);	}	printf("\n");	}void agent_net_func(){	char netSTR[1024],forwarding[10],defaultTTL[10],inReceives[10],inHdrErrors[10],inAddrErrors[10],forwDatagrams[10],inUnknownProtos[10],inDiscards[10],inDeliver[10],netName[10];        int i;	FILE *net;//-----------------------------------------------		 while(key)		{		 pthread_mutex_lock(&mut_NET);		 if((net = fopen("/proc/net/snmp","rt"))==NULL)		 {			printf("can not open /proc/net/snmp!");			return;		 }		 //***********read the network info		 fgets(netSTR,1024,net);		 fgets(netSTR,1024,net);		 if(netSTR[0]!='I' & netSTR[1]!='p')		 {			fclose(net);			continue;		 }		 sscanf(netSTR,"%s %s %s %s %s %s %s %s %s %s",netName,forwarding,defaultTTL,inReceives,inHdrErrors,inAddrErrors,forwDatagrams,inUnknownProtos,inDiscards,inDeliver);		 network_info.prev_forwarding = network_info.curr_forwarding;		 network_info.prev_incoming = network_info.curr_incoming;		 network_info.prev_total = network_info.curr_total;		 network_info.curr_total = atol(inReceives);		 network_info.curr_forwarding = atol(forwarding);		 network_info.curr_incoming = atol(inDeliver);		// fclose(net);		 fclose(net);		 pthread_mutex_unlock(&mut_NET);		 sleep(5);        }}void agent_proc_func(){	int i = 0;	int index;	char dirName[20] = "/proc/";	proc *curproc = NULL;	char path[20];	char fileSTR[1024];	char pid[20],filename[50],utime[20],stime[20],rss[20];	DIR *dirVal;	struct dirent *dir;	FILE* fp;		while( key )	{		pthread_mutex_lock(&mut_PROC);		memset(procID,0,sizeof(procID));		dirVal = opendir(dirName);		while((dir = readdir(dirVal)) && i <= 1030)		{			if(atoi(dir->d_name)== 0) continue;			procID[i] = atoi(dir->d_name);			strcpy(path,dirName);			strcat(path,dir->d_name);			strcat(path,"/stat");//			printf("%s\n",path);//****test			if( (fp = fopen(path,"rt"))==NULL)              			{				printf("can not open /proc/partitions!");				return;			}						fgets(fileSTR,1024,fp);//			printf("%s\n",fileSTR);			fclose(fp);			sscanf(fileSTR,"%s %s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %s %s %*s %*s %*s %*s %*s %*s %*s %*s %s",pid,filename,utime,stime,rss);//			printf("%s\n",filename);			index = procID[i] % 1031;			if( (curproc=hash_search(index,procID[i])) )			{				curproc->pid = atol(pid);				strcpy(curproc->pname,filename);				curproc->cputime.prev_utime = curproc->cputime.curr_utime;				curproc->cputime.prev_stime = curproc->cputime.curr_stime;				curproc->cputime.curr_utime = atol(utime);				curproc->cputime.curr_stime = atol(stime);				curproc->rss = atol(rss);				curproc->seq++;				curproc = NULL;			}			else			{				hash_append(index,atol(pid),filename,0,0,atol(utime),atol(stime),atol(rss));			}			i++;		}		i = 0;		nowseq++;		pthread_mutex_unlock(&mut_PROC);		dirVal = NULL;		sleep(5);	}	}void console_proc_func(){	printf("=========================================================\n");	printf("                 Process Infomation \n");	printf("              type q and Enter to quit!\n");	printf("=========================================================\n");		int k,i = 0;	int index;	char flag = 1; 	ulong funsel;	proc *curproc = NULL;	ulong curr_s, prev_s, curr_u, prev_u,curr_rss;	float interval_u,interval_s;	while( key && rev == 5 && flag )	{		pthread_mutex_lock(&mut_PROC);		printf("--------------------------------------------------------------------------------\n");		printf("  PID      processName         PID      processName         PID      processName\n");		printf("--------------------------------------------------------------------------------\n");//		printf("%d %d %d \n",procID[0],procID[1],procID[2]);		while(procID[i] != 0)		{			for( k = 0; k < 3; k++ )			{				index = procID[i] % 1031;				if( (curproc=hash_search(index,procID[i])) == NULL )				{					i++;					k--;					continue;				}				if(k!=2)					printf("%5d %15s       ",curproc->pid,curproc->pname);				else					printf("%5d %15s",curproc->pid,curproc->pname);				i++;			}			k=0;			i++;			printf("\n");		}		printf("\nPlease type the PID to display the detail information. Type 0 to quit!:\n");		scanf("%d",&funsel);		if(funsel == 0)		{			key = 0;			return;		}		index = funsel % 1031;		if( (curproc=hash_search(index,funsel)) == NULL )		{			pthread_mutex_unlock(&mut_PROC);			sleep(1);			printf("The process is not existed!");			break;		}		printf("---------------------------------------------------\n");		printf("  PID      processName  userTime  sysTime  RSS \n");		printf("---------------------------------------------------\n");		curr_s = curproc->cputime.curr_stime;		prev_s = curproc->cputime.prev_stime;		curr_u = curproc->cputime.curr_utime;		prev_u = curproc->cputime.prev_utime;		curr_rss = curproc->rss;		interval_u = (float)(curr_u - prev_u)/100/5;		interval_s = (float)(curr_s - prev_s)/100/5;		printf("%5d %15s  %7.4f  %7.4f  %10d\n\n\n",curproc->pid,curproc->pname,interval_u,interval_s,curr_rss);		pthread_mutex_unlock(&mut_PROC);		i = 0;		funsel = 0;		flag = 0;		printf("Type c to continue or type q to quit!\n");		while( flag != 'c' )		{			scanf("%c",&flag);			if( flag == 'q' || key == 'Q' ) 			{				key = 0;				flag = 0;				return;			}			sleep(1);		}		flag = 1;	}}void control_func(){	while( key )	{		scanf("%c",&key);		if( key == 'q' || key == 'Q' ) key = 0;	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -