/* do the actual work, get and print stats if verbose */ void get_iface_stats_libstat (char verbose) { sg_network_io_stats *network_stats=NULL; int num_network_stats,current_if_num=0,hidden_if=0; t_iface_speed_stats stats; /* local struct, used to calc total values */ t_iface_speed_stats tmp_if_stats; memset(&stats,0,(size_t)sizeof(t_iface_speed_stats)); /* init it */ network_stats = sg_get_network_io_stats(&num_network_stats); if (network_stats == NULL){ deinit(1, "libstatgrab error!\n"); } for (current_if_num=0;current_if_num<num_network_stats;current_if_num++) { tmp_if_stats.bytes.in=network_stats->rx; tmp_if_stats.bytes.out=network_stats->tx; tmp_if_stats.packets.in=network_stats->ipackets; tmp_if_stats.packets.out=network_stats->opackets; tmp_if_stats.errors.in=network_stats->ierrors; tmp_if_stats.errors.out=network_stats->oerrors; hidden_if = process_if_data (hidden_if, tmp_if_stats, &stats, network_stats->interface_name, current_if_num, verbose #ifdef IOCTL ,check_if_up(network_stats->interface_name) #else ,1 #endif ); network_stats++; } finish_iface_stats (verbose, stats, hidden_if,current_if_num); return; }
void get_iface_stats_libstatdisk (char verbose) { sg_disk_io_stats *disk_stats=NULL; int num_disk_stats,current_if_num=0,hidden_if=0; t_iface_speed_stats stats; /* local struct, used to calc total values */ t_iface_speed_stats tmp_if_stats; memset(&stats,0,(size_t)sizeof(t_iface_speed_stats)); /* init it */ disk_stats = sg_get_disk_io_stats(&num_disk_stats); if (disk_stats == NULL){ deinit(1, "libstatgrab error!\n"); } for (current_if_num=0;current_if_num<num_disk_stats;current_if_num++) { tmp_if_stats.bytes.in=disk_stats->read_bytes; tmp_if_stats.bytes.out=disk_stats->write_bytes; tmp_if_stats.packets.in=0; tmp_if_stats.packets.out=0; tmp_if_stats.errors.in=0; tmp_if_stats.errors.out=0; hidden_if = process_if_data (hidden_if, tmp_if_stats, &stats, disk_stats->disk_name, current_if_num, verbose,1); disk_stats++; } finish_iface_stats (verbose, stats, hidden_if,current_if_num); return; }
/* do the actual work, get and print stats if verbose */ void get_iface_stats_proc (char verbose) { char *ptr; FILE *f=NULL; char *buffer=NULL,*name=NULL; int hidden_if=0,current_if_num=0; t_iface_speed_stats stats; /* local struct, used to calc total values */ t_iface_speed_stats tmp_if_stats; memset(&stats,0,(size_t)sizeof(t_iface_speed_stats)); /* init it */ /* dont open proc_net_dev if netstat_i is requested, else try to open and if it fails fallback */ if (!(f=fopen(PROC_FILE,"r"))) { deinit(1, "open of procfile failed: %s\n",strerror(errno)); } buffer=(char *)malloc(MAX_LINE_BUFFER); /* we skip first 2 lines if not bsd at any mode */ if ((fgets(buffer,MAX_LINE_BUFFER,f) == NULL ) || (fgets(buffer,MAX_LINE_BUFFER,f) == NULL )) deinit(1, "read of proc failed: %s\n",strerror(errno)); name=(char *)malloc(MAX_LINE_BUFFER); while ( (fgets(buffer,MAX_LINE_BUFFER,f) != NULL) ) { /* get the name */ ptr=strchr(buffer,':'); /* wrong format */ if (ptr==NULL) { deinit(1, "wrong format of input stream\n"); } /* set : to end_of_string and move to first char of "next" string (to first data) */ *ptr++ = 0; sscanf(ptr,"%llu%llu%llu%*i%*i%*i%*i%*i%llu%llu%llu",&tmp_if_stats.bytes.in,&tmp_if_stats.packets.in,&tmp_if_stats.errors.in,&tmp_if_stats.bytes.out,&tmp_if_stats.packets.out,&tmp_if_stats.errors.out); sscanf(buffer,"%s",name); /* init new interfaces and add fetched data to old or new one */ hidden_if = process_if_data (hidden_if, tmp_if_stats, &stats, name, current_if_num, verbose #ifdef IOCTL ,check_if_up(name) #else ,1 #endif ); current_if_num++; } /* fgets done (while) */ /* add to total stats and output current stats if verbose */ finish_iface_stats (verbose, stats, hidden_if,current_if_num); /* clean buffers */ free(buffer); free(name); /* close input stream */ fclose(f); return; }
void get_iface_stats_win32 (char verbose) { PMIB_IFTABLE if_table,tmp; unsigned long tableSize; int i,current_if_num,hidden_if=0,err; char name[MAX_INTERFACE_NAME_LEN]; t_iface_speed_stats tmp_if_stats; t_iface_speed_stats stats; /* local struct, used to calc total values */ memset(&stats,0,(size_t)sizeof(t_iface_speed_stats)); /* init it */ tableSize=sizeof(MIB_IFTABLE); if_table = malloc(sizeof(MIB_IFTABLE)); if (if_table==NULL) return; /* get table size or data if table is big enough */ if ((err=GetIfTable(if_table, &tableSize, 0)==ERROR_INSUFFICIENT_BUFFER)) { tmp = realloc(if_table, tableSize); if (tmp==NULL) { free(if_table); return; } if_table=tmp; /* get data */ err=GetIfTable(if_table, &tableSize, 0); } if (err != NO_ERROR) { free(if_table); return; } current_if_num=0; for (i=0; i<if_table->dwNumEntries; i++) { strncpy(name,(char*)(if_table->table[i].bDescr),MAX_INTERFACE_NAME_LEN); name[MAX_INTERFACE_NAME_LEN-1]='\0'; tmp_if_stats.bytes.in=if_table->table[i].dwInOctets; tmp_if_stats.bytes.out=if_table->table[i].dwOutOctets; tmp_if_stats.packets.in=if_table->table[i].dwInUcastPkts + if_table->table[i].dwInNUcastPkts; tmp_if_stats.packets.out=if_table->table[i].dwOutUcastPkts + if_table->table[i].dwOutNUcastPkts; tmp_if_stats.errors.in=if_table->table[i].dwInErrors; tmp_if_stats.errors.out=if_table->table[i].dwOutErrors; /* init new interfaces and add fetched data to old or new one */ hidden_if = process_if_data (hidden_if, tmp_if_stats, &stats, name, current_if_num, verbose, 1); current_if_num++; } /* add to total stats and output current stats if verbose */ finish_iface_stats (verbose, stats, hidden_if,current_if_num); free(if_table); return; }
/* do the actual work, get and print stats if verbose */ void get_iface_stats_netstat (char verbose) { int current_if_num=0,hidden_if=0; char *buffer=NULL,*name=NULL; #if NETSTAT_NETBSD char *str_buf=NULL; char *test_buf; char *buffer2=NULL; FILE *f2=NULL; #endif #if NETSTAT_BSD || NETSTAT_BSD_BYTES || NETSTAT_SOLARIS || NETSTAT_NETBSD char *last_name=NULL; #endif FILE *f=NULL; t_iface_speed_stats stats; /* local struct, used to calc total values */ t_iface_speed_stats tmp_if_stats; memset(&stats,0,(size_t)sizeof(t_iface_speed_stats)); /* init it */ if (!(f=(FILE *)popen( #if NETSTAT_BSD || NETSTAT_BSD_BYTES #if NETSTAT_BSD_LINK NETSTAT_PATH " -iW -f link" #else NETSTAT_PATH " -iW" #endif #if NETSTAT_BSD_BYTES " -b" #endif #endif #if NETSTAT_LINUX show_all_if ? NETSTAT_PATH " -ia" : NETSTAT_PATH " -i" #endif #if NETSTAT_SOLARIS NETSTAT_PATH " -i -f inet -f inet6" #endif #if NETSTAT_NETBSD NETSTAT_PATH " -ib" #endif ,"r"))) deinit(1, "no input stream found: %s\n",strerror(errno)); #if NETSTAT_NETBSD if (!(f2=popen( NETSTAT_PATH " -i","r"))) deinit(1, "no input stream found: %s\n",strerror(errno)); buffer2=(char *)malloc(MAX_LINE_BUFFER); if ((fgets(buffer2,MAX_LINE_BUFFER,f2) == NULL )) deinit(1, "read of netstat failed: %s\n",strerror(errno)); str_buf=(char *)malloc(MAX_LINE_BUFFER); #endif buffer=(char *)malloc(MAX_LINE_BUFFER); #ifdef NETSTAT_LINUX /* we skip first 2 lines if not bsd at any mode */ if ((fgets(buffer,MAX_LINE_BUFFER,f) == NULL ) || (fgets(buffer,MAX_LINE_BUFFER,f) == NULL )) deinit(1, "read of netstat failed: %s\n",strerror(errno)); #endif #if NETSTAT_BSD || NETSTAT_BSD_BYTES || NETSTAT_SOLARIS || NETSTAT_NETBSD last_name=(char *)malloc(MAX_LINE_BUFFER); last_name[0]='\0'; /* init */ if ((fgets(buffer,MAX_LINE_BUFFER,f) == NULL )) deinit(1, "read of netstat failed: %s\n",strerror(errno)); #endif name=(char *)malloc(MAX_LINE_BUFFER); /* loop and read each line */ while ( (fgets(buffer,MAX_LINE_BUFFER,f) != NULL && buffer[0]!='\n') ) { memset(&tmp_if_stats,0,(size_t)sizeof(t_iface_speed_stats)); /* reinit it to zero */ #ifdef NETSTAT_LINUX sscanf(buffer,"%s%*i%*i%llu%llu%*i%*i%llu%llu",name,&tmp_if_stats.packets.in,&tmp_if_stats.errors.in,&tmp_if_stats.packets.out,&tmp_if_stats.errors.out); #endif #if NETSTAT_BSD_BYTES if (count_tokens(buffer)>10) /* including address */ sscanf(buffer,"%s%*i%*s%*s%llu%llu%llu%llu%llu%llu",name,&tmp_if_stats.packets.in,&tmp_if_stats.errors.in,&tmp_if_stats.bytes.in,&tmp_if_stats.packets.out,&tmp_if_stats.errors.out,&tmp_if_stats.bytes.out); else /* w/o address */ sscanf(buffer,"%s%*i%*s%llu%llu%llu%llu%llu%llu",name,&tmp_if_stats.packets.in,&tmp_if_stats.errors.in,&tmp_if_stats.bytes.in,&tmp_if_stats.packets.out,&tmp_if_stats.errors.out,&tmp_if_stats.bytes.out); #endif #if NETSTAT_BSD || NETSTAT_SOLARIS if (count_tokens(buffer)>=8) /* including address */ sscanf(buffer,"%s%*i%*s%*s%llu%llu%llu%llu",name,&tmp_if_stats.packets.in,&tmp_if_stats.errors.in,&tmp_if_stats.packets.out,&tmp_if_stats.errors.out); else /* w/o address */ sscanf(buffer,"%s%*i%*s%llu%llu%llu%llu",name,&tmp_if_stats.packets.in,&tmp_if_stats.errors.in,&tmp_if_stats.packets.out,&tmp_if_stats.errors.out); #endif #if NETSTAT_NETBSD test_buf=fgets(buffer2,MAX_LINE_BUFFER,f2); if (count_tokens(buffer)>=6) { /* including address */ if (test_buf) sscanf(buffer2,"%s%s%s%s%llu%llu%llu%llu",str_buf,str_buf,str_buf,str_buf,&tmp_if_stats.packets.in,&tmp_if_stats.errors.in,&tmp_if_stats.packets.out,&tmp_if_stats.errors.out); sscanf(buffer,"%s%s%s%s%llu%llu",name,str_buf,str_buf,str_buf,&tmp_if_stats.bytes.in,&tmp_if_stats.bytes.out); } else { if (test_buf) sscanf(buffer2,"%s%s%s%llu%llu%llu%llu",str_buf,str_buf,str_buf,&tmp_if_stats.packets.in,&tmp_if_stats.errors.in,&tmp_if_stats.packets.out,&tmp_if_stats.errors.out); sscanf(buffer,"%s%s%s%llu%llu",name,str_buf,str_buf,&tmp_if_stats.bytes.in,&tmp_if_stats.bytes.out); } #endif #if NETSTAT_BSD || NETSTAT_BSD_BYTES || NETSTAT_SOLARIS || NETSTAT_NETBSD /* check if we have a new iface or if its only a second line of the same one */ if (!strcmp(last_name,name)) continue; /* skip this line */ strlcpy(last_name,name,MAX_LINE_BUFFER); #endif /* init new interfaces and add fetched data to old or new one */ hidden_if = process_if_data (hidden_if, tmp_if_stats, &stats, name, current_if_num, verbose, #if NETSTAT_BSD || NETSTAT_BSD_BYTES || NETSTAT_NETBSD (name[strlen(name)-1]!='*') #else 1 #endif ); current_if_num++; } /* fgets done (while) */ /* add to total stats and output current stats if verbose */ finish_iface_stats (verbose, stats, hidden_if,current_if_num); /* clean buffers */ free(buffer); #if NETSTAT_NETBSD free(buffer2); free(str_buf); pclose(f2); #endif #if NETSTAT_BSD || NETSTAT_NETBSD || NETSTAT_BSD_BYTES || NETSTAT_SOLARIS free(last_name); #endif free(name); /* close input stream */ pclose(f); return; }
void get_iface_stats_kstat (char verbose) { kstat_ctl_t *kc; kstat_t *ksp; kstat_io_t kio; kstat_named_t *i_bytes=NULL,*o_bytes=NULL,*i_packets=NULL,*o_packets=NULL,*i_errors=NULL,*o_errors=NULL; char *name; int hidden_if=0,current_if_num=0,my_errno=0; t_iface_speed_stats tmp_if_stats; t_iface_speed_stats stats; /* local struct, used to calc total values */ memset(&stats,0,(size_t)sizeof(t_iface_speed_stats)); /* init it */ kc = kstat_open(); if (kc==NULL) deinit(1, "kstat failed: %s\n",strerror(my_errno)); name=(char *)malloc(KSTAT_STRLEN); if (!name) { kstat_close(kc); deinit(1,"mem alloc failed: %s\n",strerror(errno)); } /* loop for interfaces */ for (ksp = kc->kc_chain;ksp != NULL;ksp = ksp->ks_next) { if ((strcmp(ksp->ks_class, "net") != 0 && input_method==KSTAT_IN) || (strcmp(ksp->ks_class, "disk") != 0 && input_method==KSTATDISK_IN && ksp->ks_type != KSTAT_TYPE_IO)) continue; /* skip all other stats */ strncpy(name,ksp->ks_name,KSTAT_STRLEN); name[KSTAT_STRLEN-1]='\0'; if (KSTAT_IN==input_method) { kstat_read(kc, ksp, NULL); i_bytes=(kstat_named_t *)kstat_data_lookup(ksp, "rbytes"); o_bytes=(kstat_named_t *)kstat_data_lookup(ksp, "obytes"); i_packets=(kstat_named_t *)kstat_data_lookup(ksp, "ipackets"); o_packets=(kstat_named_t *)kstat_data_lookup(ksp, "opackets"); i_errors=(kstat_named_t *)kstat_data_lookup(ksp, "ierrors"); o_errors=(kstat_named_t *)kstat_data_lookup(ksp, "oerrors"); if (!i_bytes || !o_bytes || !i_packets || !o_packets || !i_errors || !o_errors) continue; /* use ui32 values, the 64 bit values return strange (very big) differences */ tmp_if_stats.bytes.in=i_bytes->value.ui32; tmp_if_stats.bytes.out=o_bytes->value.ui32; tmp_if_stats.packets.in=i_packets->value.ui32; tmp_if_stats.packets.out=o_packets->value.ui32; tmp_if_stats.errors.in=i_errors->value.ui32; tmp_if_stats.errors.out=o_errors->value.ui32; } else if (KSTATDISK_IN==input_method) { kstat_read(kc, ksp, &kio); tmp_if_stats.bytes.in=kio.nread; tmp_if_stats.bytes.out=kio.nwritten; tmp_if_stats.packets.in=kio.reads; tmp_if_stats.packets.out=kio.writes; tmp_if_stats.errors.in=tmp_if_stats.errors.out=0; } else { free(name); kstat_close(kc); deinit(1,"im confused about kstat input methods!\n"); } /* init new interfaces and add fetched data to old or new one */ hidden_if = process_if_data (hidden_if, tmp_if_stats, &stats, name, current_if_num, verbose, (tmp_if_stats.bytes.in != 0 || tmp_if_stats.bytes.out != 0)); current_if_num++; } /* add to total stats and output current stats if verbose */ finish_iface_stats (verbose, stats, hidden_if,current_if_num); /* clean buffers */ free(name); kstat_close(kc); return; }
/* do the actual work, get and print stats if verbose */ void get_disk_stats_proc (char verbose) { FILE *f=NULL,*f_s=NULL; char *buffer=NULL,*name=NULL,*ptr; ullong tmp_long; int n,major,minor,maj_s,min_s; static char diskstats_works = 1; static char proc_stat[PATH_MAX] = ""; int hidden_if=0,current_if_num=0; t_iface_speed_stats stats; /* local struct, used to calc total values */ t_iface_speed_stats tmp_if_stats; memset(&stats,0,(size_t)sizeof(t_iface_speed_stats)); /* init it */ /* dont open proc_net_dev if netstat_i is requested, else try to open and if it fails fallback */ if (diskstats_works && !(f=fopen(PROC_DISKSTATS_FILE,"r"))) { diskstats_works = 0; } buffer=(char *)malloc(MAX_LINE_BUFFER); name=(char *)malloc(MAX_LINE_BUFFER); if (!name || !buffer) { if (name) free(name); if (buffer) free(buffer); if (f) fclose(f); deinit(1,"mem alloc failed: %s\n",strerror(errno)); } if (!diskstats_works) { if (!(f=fopen(PROC_PARTITIONS_FILE,"r"))) { diskstats_works = 1; } else { /* skip first two lines in /proc/partitions */ fgets(buffer,MAX_LINE_BUFFER,f); fgets(buffer,MAX_LINE_BUFFER,f); } } while ( (fgets(buffer,MAX_LINE_BUFFER,f) != NULL) ) { n = sscanf(buffer, (diskstats_works ? "%i %i %s %llu%llu%llu%llu%llu%llu%llu%*i" : "%i %i %*i %s %llu%llu%llu%llu%llu%llu%llu%*i"), &major,&minor,name,&tmp_if_stats.packets.in,&tmp_if_stats.errors.in,&tmp_if_stats.bytes.in,&tmp_long,&tmp_if_stats.packets.out,&tmp_if_stats.errors.out,&tmp_if_stats.bytes.out); /* skip loop devices, we dont see stats anyway */ if (major == 7) continue; if (n == 7) { /* its a partition with shorter stats, move all to correct fields */ tmp_if_stats.packets.out=tmp_if_stats.bytes.in; tmp_if_stats.bytes.in=tmp_if_stats.errors.in; tmp_if_stats.bytes.out=tmp_long; tmp_if_stats.errors.in=0; tmp_if_stats.errors.out=0; } else { /* having 10 fields all is just fine, else check for other formats */ if (n != 10) { if (diskstats_works == 0 && n == 3) { /* we are reading /proc/partitions and have the * old 2.4 partitions format, look in /proc/stat for block devince data */ if (proc_stat[0] == 0) { /* build /proc/stat path */ strcpy(proc_stat,PROC_PARTITIONS_FILE); if ((ptr=strrchr(proc_stat,'/'))) { ptr++; strcpy(ptr,"stat"); } else { free(name); free(buffer); deinit(1, "strange /proc/partitions name, couldnt build /proc/stats name\n"); } } if (!(f_s=fopen(proc_stat,"r"))) { free(name); free(buffer); deinit(1, "couldnt open %s: %s\n",proc_stat,strerror(errno)); } while ( (fgets(buffer,MAX_LINE_BUFFER,f_s) != NULL)) { if (!strncmp("disk_io:",buffer,8)) { ptr=buffer+9; while (ptr[0]!=0) { n = sscanf(ptr,"(%i,%i): (%*i,%llu,%llu,%llu,%llu)",&maj_s,&min_s,&tmp_if_stats.packets.in,&tmp_if_stats.bytes.in,&tmp_if_stats.packets.out,&tmp_if_stats.bytes.out); if (maj_s==major && min_s==minor) { /* we found the correct device */ fclose(f_s); f_s=NULL; break; } if (!(ptr=strchr(ptr,' '))) break; ptr++; } if (!f_s) break; } } if (f_s) { fclose(f_s); /* couldnt find the entry, prolly a partition */ continue; } } else { /* neither new nor old /proc/partitions nor /proc/diskstats */ free(name); free(buffer); deinit(1, "wrong format of procfile. %i: %s\n",n,buffer); } } } /* stats are in 512 byte blocks */ tmp_if_stats.bytes.in*=512; tmp_if_stats.bytes.out*=512; /* convert devfs name to a short abr */ get_short_devfs_name(name); /* init new interfaces and add fetched data to old or new one */ hidden_if = process_if_data (hidden_if, tmp_if_stats, &stats, name, current_if_num, verbose,(n==10 || proc_stat[0] != 0)); current_if_num++; } /* fgets done (while) */ /* add to total stats and output current stats if verbose */ finish_iface_stats (verbose, stats, hidden_if,current_if_num); /* clean buffers */ free(buffer); free(name); /* close input stream */ fclose(f); return; }