proc observer

  • T1_218918
    了解作者
  • 5.8KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-08 05:14
    上传日期
使用proc文件系统查看linux的系统信息,包括cpu,内存,磁盘调用等
observer.rar
  • observer
  • observer
    13KB
  • makefile
    75B
  • observer.c
    3.6KB
内容介绍
#include <stdlib.h> #include <stdio.h> #include <getopt.h> #include <string.h> #include <sys/time.h> #include <fcntl.h> #include <unistd.h> #define MAXBUFF 1024 char* const short_options="cutmdCplh"; struct option long_options[]={ {"cpu",0,NULL,'c'}, {"uptime",0,NULL,'u'}, {"time",0,NULL,'t'}, {"memory",0,NULL,'m'}, {"diskrequest",0,NULL,'d'}, {"ctxt",0,NULL,'C'}, {"processes",0,NULL,'p'}, {"loadavg",0,NULL,'l'}, {"help",0,NULL,'h'} }; void print_help(){ fprintf(stdout,"help for options:\n"\ "-h --help to display this information.\n"\ "-c --cpu to view cpu type and version of linux kernel.\n"\ "-u --uptime to view the uptime since boot(dd:hh:mm:ss).\n"\ "-t --time to view cpu time for usr sys and idle.\n"\ "-m --memory to view total and free memory.\n"\ "-d --diskrequest to view disk request system received.\n"\ "-C --ctxt to view times of context switch.\n"\ "-p --processse to view the number of processes created since boot.\n"\ "-l --loadavg to view the load average of system.\n"); } void print_info(char const *path,char const *name){ FILE* fp; char buff[MAXBUFF]; char *line; if((fp=fopen(path,"r"))==NULL){ fprintf(stderr,"can't open file %s",path); exit(1); } while((line=fgets(buff,MAXBUFF,fp))!=NULL){ if(strncmp(line,name,strlen(name))==0){ fputs(buff,stdout); } } fclose(fp); } void print_uptime(){ FILE* fp; char buff[MAXBUFF]; char *line; fputs("Uptime:\ndd:hh:mm:ss\n",stdout); if((fp=fopen("/proc/uptime","r"))==NULL){ fprintf(stderr,"can't open file /proc/uptime"); exit(1); } line=fgets(buff,MAXBUFF,fp); char *token=strtok(line," "); char *endptr; double uptime=strtod(token,&endptr); int second=((int)uptime)%60; int rest=uptime/60; int minute=rest%60; rest/=60; int hour=rest%60; int day=rest/24; fprintf(stdout,"%02d:%02d:%02d:%02d\n",day,hour,minute,second); fclose(fp); } void print_stat(){ FILE* fp; char buff[MAXBUFF]; char *line; fputs("CPU time:\nuser\tsystem\tidle\n",stdout); if((fp=fopen("/proc/stat","r"))==NULL){ fprintf(stderr,"can't open file /proc/stat"); exit(1); } line=fgets(buff,MAXBUFF,fp); char *token=strtok(line," "); char *user=strtok(NULL," "); token=strtok(NULL," "); char *system=strtok(NULL," "); char *idle=strtok(NULL," "); fprintf(stdout,"%s\t%s\t%s\n",user,system,idle); fclose(fp); } void print_loadavg(){ FILE* fp; char buff[MAXBUFF]; char *line; fputs("load average per minute :",stdout); if((fp=fopen("/proc/loadavg","r"))==NULL){ fprintf(stderr,"can't open file /proc/loadavg"); exit(1); } line=fgets(buff,MAXBUFF,fp); char *token=strtok(line," "); fprintf(stdout,"%s\n",token); fclose(fp); } int main(int argc,char *argv[]){ int option; while((option=getopt_long (argc, argv, short_options, long_options, NULL)) != -1){ switch(option){ case 'c': fputs("CPU infomations:\n",stdout); print_info("/proc/cpuinfo","model name"); fputs("Version of Linux kernel:\n",stdout); print_info("/proc/version",""); break; case 'u': print_uptime(); break; case 't': print_stat(); break; case 'm': fputs("Memory information:\n",stdout); print_info("/proc/meminfo","MemTotal"); print_info("/proc/meminfo","MemFree"); break; case 'd': print_info("/proc/diskstats",""); break; case 'C': fputs("Context:\n",stdout); print_info("/proc/stat","ctxt"); break; case 'p': fputs("Number of processes:\n",stdout); print_info("/proc/stat","processes"); break; case 'l': print_loadavg(); break; case 'h': print_help(); break; case -1: break; case '?': print_help(); break; } } return 0; }
评论