#include "collect.h"
#include <errno.h>
pthread_mutex_t work_mutex;
/*this is collect the info*/
int collecter(conf *head, pid_t pid_record){
pthread_t thread_cpu_id;
pthread_t thread_mem_id;
pthread_t thread_disk_id;
pthread_t thread_net_id;
p_head_pid p_info;
p_info.head=head;
p_info.record_pid=pid_record;
/*create the pthread to collect the infomation*/
pthread_create(&thread_cpu_id, NULL, (void *)*cpu_info, &p_info);
// pthread_create(&thread_mem_id, NULL, (void *)*mem_info, &p_info);
// pthread_create(&thread_disk_id, NULL, (void *)*disk_info, &p_info);
// pthread_create(&thread_net_id, NULL, (void *)*net_info, &p_info);
/*wait the signal*/
pause();
}
/*
*
*collet the cpu infomation we read the file "/proc/stats" twice
*at the time we sleep 5 second when first read finish
*
*/
void *cpu_info(p_head_pid *p_info){
float user[2],nice[2],system[2],idle[2];
char info[100];
FILE *cpu_fp;
char x_info[30];
char cz_path[50];
char res_path[50];
int cz_fifo_fp;
int res_fifo_fp;
char res_fifo;
cpu_fp=fopen(CPU_INFO_PATH, "r");
memset(cz_path, '\0', 50);
memset(res_path, '\0', 50);
strncpy(cz_path, p_info->head->journal.tmp_path, strlen(p_info->head->journal.tmp_path));
strncpy(res_path, p_info->head->journal.tmp_path, strlen(p_info->head->journal.tmp_path));
// strcpy(cz_path, "./tmp"));
// strcpy(res_path, "./tmp"));
strcat(cz_path, "fifo_cz");
strcat(res_path, "fifo_res");
// printf("cz: %s\n res: %s\n",cz_path,res_path);
cz_fifo_fp=open("./tmp/fifo_cz", O_RDWR);
res_fifo_fp=open("./tmp/fifo_res", O_RDWR);
// printf("finish open the fifo \n");
while(1){
printf("cpu 1\n");
user[0]=0.0;
user[1]=0.0;
nice[1]=0.0;
nice[0]=0.0;
system[0]=0.0;
system[1]=0.0;
idle[0]=0.0;
idle[1]=0.0;
memset(x_info, '\0', sizeof(x_info));
memset(info, '\0', sizeof(info));
strncpy(info,"#ID:CPU",7);
fscanf(cpu_fp,"cpu %f %f %f %f \n", &user[0], &nice[0], &system[0], &idle[0]);
sleep(5);
rewind(cpu_fp);
//rewind(cz_fifo_fp);
//rewind(res_fifo_fp);
fscanf(cpu_fp,"cpu %f %f %f %f \n", &user[1], &nice[1], &system[1], &idle[1]);
user[0]=user[1]-user[0];
nice[0]=nice[1]-nice[0];
system[0]=system[1]-system[0];
idle[0]=idle[1]-idle[0];
if(p_info->head->cpu.collet_total == 'y' || p_info->head->cpu.collet_total == 'Y'){
sprintf(x_info, "#total:%.2f",(user[1]+nice[1]+system[1]+idle[1]));
strcat(info,x_info);
memset(x_info, '\0', sizeof(x_info));
}
if(p_info->head->cpu.collet_work == 'y' || p_info->head->cpu.collet_work == 'Y'){
sprintf(x_info,"#work:%.2f%%", 100*((user[0]+nice[0]+system[0])/(user[0]+nice[0]+system[0]+idle[0])));
strcat(info,x_info);
memset(x_info, '\0', sizeof(x_info));
}
if(p_info->head->cpu.collet_free == 'y' || p_info->head->cpu.collet_free == 'y'){
sprintf(x_info,"#free:%.2f%%", 100*(1-((user[0]+nice[0]+system[0])/(user[0]+nice[0]+system[0]+idle[0]))));
strcat(info,x_info);
memset(x_info, '\0', sizeof(x_info));
}
if(p_info->head->cpu.journal == 'y' || p_info->head->cpu.journal == 'Y')
send_record(info, 1, p_info->head, p_info->record_pid);
strcat(info,"#&");
printf("the cpu info:%s\n",info);
pthread_mutex_lock(&work_mutex);
write(cz_fifo_fp,info,strlen(info));
read(res_fifo_fp,&res_fifo, 1);
if(res_fifo == 'y'){
pthread_mutex_unlock(&work_mutex);
res_fifo == 'n';
}
sleep(p_info->head->cpu.collet_time);
}
}
/*
*
*read the "/proc/meminfo" read the "total" "free" "buf" "cached"
*
*
*
*/
void *mem_info(p_head_pid *p_info){
float total,free,buff,cached;
char info[100];
FILE *mem_fp;
char x_info[30];
char cz_path[50];
char res_path[50];
int cz_fifo_fp;
int res_fifo_fp;
char res_fifo;
mem_fp=fopen(MEM_INFO_PATH, "r");
memset(cz_path, '\0', 50);
memset(res_path, '\0', 50);
strncpy(cz_path, p_info->head->journal.tmp_path, strlen(p_info->head->journal.tmp_path));
strncpy(res_path, p_info->head->journal.tmp_path, strlen(p_info->head->journal.tmp_path));
// strcpy(cz_path, "./tmp"));
// strcpy(res_path, "./tmp"));
strcat(cz_path, "fifo_cz");
strcat(res_path, "fifo_res");
// printf("cz: %s\n res: %s\n",cz_path,res_path);
cz_fifo_fp=open("./tmp/fifo_cz", O_RDWR);
res_fifo_fp=open("./tmp/fifo_res", O_RDWR);
// printf("finish open the fifo \n");
while(1){
printf("mem 1\n");
total=0.0;
free=0.0;
buff=0.0;
cached=0.0;
memset(x_info, '\0', sizeof(x_info));
memset(info, '\0', sizeof(info));
strncpy(info,"#ID:MEM",7);
fscanf(mem_fp, "MemTotal: %f kB\n", &total);
fscanf(mem_fp, "MemFree: %f kB\n", &free);
fscanf(mem_fp, "Buffers: %f kB\n",&buff);
fscanf(mem_fp, "Cached: %f kB\n", &cached);
printf("info:%s \n",info);
rewind(mem_fp);
//rewind(cz_fifo_fp);
//rewind(res_fifo_fp);
if(p_info->head->mem.collet_total == 'y' || p_info->head->mem.collet_total == 'Y'){
sprintf(x_info, "#total:%.2f kB",total);
strcat(info,x_info);
memset(x_info, '\0', sizeof(x_info)); printf("info:%s \n",info);
}
if(p_info->head->mem.collet_work == 'y' || p_info->head->mem.collet_work == 'Y'){
sprintf(x_info,"#work:%.2f%%", 100*(cached/total));
strcat(info,x_info);
memset(x_info, '\0', sizeof(x_info));printf("info:%s \n",info);
}
if(p_info->head->mem.collet_free == 'y' || p_info->head->mem.collet_free == 'y'){
sprintf(x_info,"#free:%.2f%%", 100*(free/total));
strcat(info,x_info);
memset(x_info, '\0', sizeof(x_info));printf("info:%s \n",info);
}
if(p_info->head->mem.journal == 'y' || p_info->head->mem.journal == 'Y')
send_record(info, 1, p_info->head, p_info->record_pid);
strcat(info, "#&");
printf("the mem info:%s\n",info);
pthread_mutex_lock(&work_mutex);
write(cz_fifo_fp,info,strlen(info));
read(res_fifo_fp,&res_fifo, 1);
if(res_fifo == 'y'){
pthread_mutex_unlock(&work_mutex);
res_fifo == 'n';
}
sleep(p_info->head->mem.collet_time);
}
}
void *disk_info(p_head_pid *p_info){
char info[300];
char x_info[30];
char cz_path[50];
char res_path[50];
int cz_fifo_fp;
int res_fifo_fp;
char res_fifo;
char *aim=NULL;
int len=0;
FILE *disk_fp=NULL;
char name[20];
char total[10];
char used[10];
char avail[10];
char freed[10];
char mount[20];
if( (disk_fp=popen("df -h", "r")) == NULL){
printf("the commed error 'df -h'\n");
}
getline(&aim, &len, disk_fp);
free(aim);
aim=NULL;
len=0;
memset(cz_path, '\0', 50);
memset(res_path, '\0', 50);
strncpy(cz_path, p_info->head->journal.tmp_path, strlen(p_info->head->journal.tmp_path));
strncpy(res_path, p_info->head->journal.tmp_path, strlen(p_info->head->journal.tmp_path));
// strcpy(cz_path, "./tmp"));
// strcpy(res_path, "./tmp"));
strcat(cz_path, "fifo_cz");
strcat(res_path, "fifo_res");
printf("cz: %s\n res: %s\n",cz_path,res_path);
cz_fifo_fp=open("./tmp/fifo_cz", O_RDWR);
res_fifo_fp=open("./tmp/fifo_res", O_RDWR);
printf("finish open the fifo \n");
while(1){
memset(info,'\0',300);
memset(x_info, '\0', 30);
while(getline(&aim, &len, disk_fp) != -1){
printf("disk\n");
memset(name, '\0', 20);
memset(total, '\0', 10);
memset(used, '\0', 10);
memset(avail, '\0', 10);
memset(freed, '\0', 10);
memset(mount, '\0', 20);
memset(x_info, '\0', sizeof(x_info));
memset(info, '\0', sizeof(info));
strncpy(info,"#ID:DISK",8);
sscanf(aim,"%s %s %s %s %s %s", name, total, used, avail, freed, mount);
strcat(info,"#name:");
strcat(info,name);
if(p_info->head->disk.collet_total == 'y' || p_info->head->disk.collet_total == 'Y'){
sprintf(x_info, "#total:%s",total);
strcat(info,x_info);
memset(x_info, '\0', sizeof(x_info));
}
if(p_info->head->disk.collet_work == 'y' || p_info->head->disk.collet_work == 'Y'){
sprintf(x_info,"#used:%s", used);
strcat(info,x_info);
memset(x_info, '\0', sizeof(x_info));
}
if(p_info->head->disk.collet_free == 'y' || p_info->head->disk.collet_free == 'y'){
sprintf(x_info,"#free:%s", avail