#include <iostream> #include <unistd.h> #include <string.h> #include <sstream> #include <fstream> #include <stdlib.h> using namespace std; static const int BUF_SIZE=512; static char *eff=NULL; char buf[BUF_SIZE]; /** *Interprete the command arguments. */ void interprete(char); /** *Get a integer or double from a file. */ template<typename T> T get_i(const char *file,const char *attribute=NULL,int column=0); /** *Get a integer or double from a line. *This method will be invoked by get_i. */ template<typename T> T retrieve_i(const char *attr,int column); /** *Get a string. */ const char* get_a(const char *file,const char *attribute=NULL,int column=0); /** *Get a string from a line. *This method will be invoked by get_a. */ const char* retrieve_a(const char *attr,int column); /** *Search a line that includes the attribute from a input stream. */ const char* search(istream &in,const char *attr); /** *Translate a time value represented by an integer to a string. */ const char* itot(int time); int main(int argc,char **argv) { if(argc<2) { cerr<<"Usage: Observer -option!"<<endl; return -127; } char **sptr=argv; while((--argc>0) && (**(++sptr)=='-')) { char *ptr=*sptr; while(*(++ptr)!='/0') { interprete(*ptr); } } } const char* search(istream &in,const char *attr) { bool not_found=true; while(not_found) { in.getline(buf,BUF_SIZE); if(attr==NULL) { eff=buf; break; } if((eff=strstr(buf,attr))!=NULL) { not_found=false; } } return eff; } template<typename T> T retrieve_i(const char *attr,int column) { int len=attr!=NULL?strlen(attr):0; stringstream ss(stringstream::in | stringstream::out); char *ptr=eff+len; if(attr!=NULL) { char *index=strchr(eff,':'); ptr= index == NULL ? eff+len:++index; } ss<<ptr; T result=0; while(column-->=0) { ss>>result; } return result; } const char* retrieve_a(const char *attr,int column) { int len=attr!=NULL?strlen(attr):0; stringstream ss(stringstream::in | stringstream::out); char *ptr=eff+len; if(attr!=NULL) { char *index=strchr(eff,':'); ptr= index == NULL ? eff+len:++index; } ss<<ptr; if(column==-1) { return ptr; } char tmp[100]; while(column-->=0) { ss>>tmp; } strcpy(buf,tmp); return buf; } template<typename T> T get_i(const char *file,const char *attribute,int column) { ifstream in; in.open(file); search(in,attribute); return retrieve_i<T>(attribute,column); } const char* get_a(const char *file,const char *attribute,int column) { ifstream in; in.open(file); search(in,attribute); return retrieve_a(attribute,column); } const char* itot(int time) { int sec=time%60; time/=60; int min=time%60; time/=60; int hour=time%24; int day=time/24; sprintf(buf,"%d:%d:%d:%d",day,hour,min,sec); return buf; } void interprete(char ch) { const char *result; switch(ch) { case 'u': result=get_a("/proc/cpuinfo","model name",-1); cout<<"CPU Type:/t"<<result<<endl; break; case 'k': result=get_a("/proc/version",NULL,2); cout<<"Kernel Version:/t"<<result<<endl; break; case 'b': int time; time=get_i<int>("/proc/uptime"); cout<<"Launch Time:/t"<<itot(time)<<endl; break; case 't': int user; user=get_i<int>("/proc/stat","cpu",1); int sys; sys=get_i<int>("/proc/stat","cpu",3); int idle; idle=get_i<int>("/proc/stat","cpu",4); cout<<"User CPU:/t"<<user<<'/t'; cout<<"System CPU:/t"<<sys<<'/t'; cout<<"Idel CPU:/t"<<idle<<endl; break; case 'm': int total; total=get_i<int>("/proc/meminfo","MemTotal"); int free; free=get_i<int>("/proc/meminfo","MemFree"); cout<<"MemTotal:/t"<<total<<" KB/t"; cout<<"MemFree:/t"<<free<<" KB"<<endl; break; case 'd': int read; read=get_i<int>("/proc/diskstats","sda",1); int write; write=get_i<int>("/proc/diskstats","sda",5); cout<<"IO Request:/t"<<read+write<<endl; break; case 'c': int ctxt; system("grep ctxt /proc/stat > /tmp/jmj"); ctxt=get_i<int>("/tmp/jmj","ctxt"); cout<<"Context:/t"<<ctxt<<endl; break; case 'p': int pros; system("grep processes /proc/stat > /tmp/jmj"); pros=get_i<int>("/tmp/jmj","processes"); cout<<"Processes:/t"<<pros<<endl; break; case 'l': double avg; avg=get_i<double>("/proc/loadavg"); cout<<"Loadavg:/t"<<avg<<endl; break; default: cout<<"The options is specified as follows:"<<endl; cout<<"-l/tPrint the load average."<<endl; cout<<"-p/tPrint the processed."<<endl; cout<<"-c/tPrint the context."<<endl; cout<<"-d/tPrint the IO request."<<endl; cout<<"-m/tPrint the memory status."<<endl; cout<<"-t/tPrint the time of CPU at every kind of status."<<endl; cout<<"-k/tPrint the version of kernel."<<endl; cout<<"-u/tPrint the type of CPU."<<endl; } } 用来观察系统的相应参数~