#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void jobs(char *arg1,FILE *fp) {
int i,ret;
char *p,*str,str1[1024]="0";
char readbuf[1024];
char c;
int n=0;
float max=0,value;
while(fgets(readbuf,1023,fp)!=NULL) {
if(strstr(readbuf,arg1)) {
n++;
for(p=strtok(readbuf," ");p;p=strtok(NULL," "))
str=p;
str[strlen(str)-2]=0;
/*
sprintf(&c,"%c",str[0]);
if(c=='.') {
*/
if(str[0]=='.') {
strcat(str1,str);
value=atof(str1);
// printf("%.2f\n",atof(str1));
}
else {
value=atof(str);
// printf("%.2f\n",atof(str));
}
if(max<value) max=value;
if(n%4==0) printf("%.2f\n",max);
strcpy(str1,"0");
}
}
fseek(fp,0,SEEK_SET);
printf("\n");
}
int main(int argc,char *argv[]) {
int i,ret;
FILE *fp;
if(argc<2) {
printf("%s filename...\n",argv[0]);
exit(-1);
}
for(i=1;i<=argc-1;i++) {
fp=fopen(argv[i],"r");
if(fp==NULL) {
fprintf(stderr,"fopen() error.\n");
exit(-1);
}
printf("file=%s\n%s max:\n",argv[i],"cpu");
jobs("cpu",fp);
//fseek(fp,0,SEEK_SET);
printf("%s max:\n","mem");
jobs("mem",fp);
//fseek(fp,0,SEEK_SET);
printf("%s max:\n","swap");
jobs("swap",fp);
//fseek(fp,0,SEEK_SET);
ret=fclose(fp);
if(ret==EOF) {
fprintf(stderr,"fopen() error.\n");
exit(-1);
}
printf("-------------------------\n");
}
exit(0);
}
文件内容:
$ head 0705.txt-bak (每15分钟一次)
07-05 00:00
the usage of cpu is 2.2%
the usage of mem is 16.678800%
the usage of swap is .411200%
07-05 00:15
the usage of cpu is .9%
the usage of mem is 16.378300%
the usage of swap is .394600%
07-05 00:30
the usage of cpu is 12.2%
the usage of mem is 17.535100%
the usage of swap is .394700%
07-05 00:45
the usage of cpu is 12.4%
the usage of mem is 17.657800%
the usage of swap is .406600%