#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "paramd.h" #define HOST_BUF_LEN 512 void get_net_x(netstat *ns) { unsigned long long tx_rate; unsigned long long rx_rate; FILE *fd; char * buffer = malloc(HOST_BUF_LEN); memset(buffer,0 , HOST_BUF_LEN); char *ptr; if ((fd = fopen("/proc/net/dev", "r")) == NULL) { printf("could not open /proc/net/dev!\n"); } fgets(buffer, HOST_BUF_LEN-1, fd); fgets(buffer, HOST_BUF_LEN-1, fd); fgets(buffer, HOST_BUF_LEN-1, fd); fgets(buffer, HOST_BUF_LEN-1, fd); //printf (buffer); ptr = strtok(buffer, ": "); ptr = strtok(NULL, " "); //tx_rate = atoll(ptr); //printf(">>>>%lld<<<<<<", tx_rate); //ns->tx_rate = &tx_rate; ns->tx_rate = atol(ptr); printf(">>>>%ld<<<<<<", ns->tx_rate); ptr = strtok(NULL, " "); ptr = strtok(NULL, " "); ptr = strtok(NULL, " "); ptr = strtok(NULL, " "); ptr = strtok(NULL, " "); ptr = strtok(NULL, " "); ptr = strtok(NULL, " "); ptr = strtok(NULL, " "); ns->rx_rate = atol(ptr); printf(">>>>%ld<<<<<<", ns->rx_rate); fclose(fd); free(buffer); } void get_mem_info(mem_info *mem){ FILE *fp; char buf[80]; unsigned long total, used, mfree, shared, buffers, cached; /* read memory info */ fp = fopen("/proc/meminfo", "r"); if (!fp) { perror("fopen('meminfo')"); return; } fgets(buf, sizeof(buf), fp); /* skip first line */ if (fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu", &total, &used, &mfree, &shared, &buffers, &cached) ) { mem->total=total; mem->used=used; mem->mfree=mfree; mem->shared=shared; mem->buffers=buffers; mem->cached=cached; } fclose(fp); } void get_cpu_info(cpu_info *cpu) { FILE *fd; char buffer[HOST_BUF_LEN+1]; int ret=0; char *tmp; unsigned int result; if (( fd = fopen("/proc/stat", "r"))<0 ) { printf("could not open /proc/stat!\n"); return; } else { fgets(buffer, HOST_BUF_LEN, fd); printf("found: %s\n", buffer); //'cpu' tmp = strtok(buffer, " "); //user tmp = strtok(NULL, " "); cpu->user = atol(tmp); //nice tmp = strtok(NULL, " "); cpu->nice = atol(tmp); // system tmp = strtok(NULL, " "); cpu->system = atol(tmp); // idle tmp = strtok(NULL, " "); cpu->idle = atol(tmp); fclose (fd); } } long get_disk_info() { struct statfs *s; char *device; const char mount_point[] ="/"; // struct statfs s; long blocks_used; long blocks_percent_used; if (statfs(mount_point, s) != 0) { //perror_msg("%s", mount_point); //return FALSE; } if (s->f_blocks > 0) { blocks_used = s->f_blocks - s->f_bfree; if(blocks_used == 0) return 0; else { return (long)(blocks_used * 100.0 / (blocks_used + s->f_bavail) + 0.5); } } } int check_sys_param(int fd, int graph) { char buf[HOST_BUF_LEN]; memset(buf,0,HOST_BUF_LEN); //struct statfs s; int days, hours, mins, num; netstat *ns =malloc(sizeof(netstat)); get_net_x(ns); cpu_info *cpu = malloc(sizeof(cpu_info)); get_cpu_info(cpu); mem_info *mem = malloc(sizeof(mem_info)); get_mem_info (mem); /*if(graph == 0){ sprintf(buf+strlen(buf), "Total Ram: %luk\tFree: %luk\n",mem->total/1024, mem->mfree/1024); //sys_info->totalram / 1024, sys_info->freeram / 1024); sprintf(buf+strlen(buf), "Shared Ram: %luk\n", mem->shared/1024);//sys_info->sharedram / 1024 ); sprintf( buf+strlen(buf), "Buffered Ram: %luk\n", mem->buffers/1024);//sys_info->bufferram / 1024); sprintf( buf+strlen(buf),"Tx rate: %lu\n", ns->tx_rate); printf(" tx %d\n",strlen(buf)) ; sprintf( buf+strlen(buf),"Rx rate: %lu\n" , ns->rx_rate); } else */ { sprintf(buf,"%lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu ", //sys_info->uptime % 60, mem->used, mem->mfree , mem->total , mem->buffers, mem->cached, ns->tx_rate,ns->rx_rate, get_disk_info(), //sys_info->loads[0], cpu->user, cpu->nice, cpu->system, cpu->idle ); free(ns); free(cpu); //free(sys_info); free(mem); } printf(" >>>") ; printf(buf); printf("<<<\n") ; if(send(fd,buf,strlen(buf),0)<=0) { close (fd); return -1; } return 1; } int init_server(int port, int proto) { struct sockaddr_in addr; int sd; int sock = SOCK_SEQPACKET; int family = PF_INET; if(proto == IPPROTO_TCP) { sock = SOCK_STREAM; family =AF_INET; } addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = htons(port); if( (sd=socket(family, sock, proto)) < 0 ) { perror("socket failed\n"); return(-1); } if( bind( sd, (struct sockaddr *)&addr,sizeof(addr) ) == -1 ) { perror("bind"); goto errout; exit(1); } if (listen(sd, 1)==-1) { printf("not listening...\n"); goto errout; } return sd; errout: //close(sd); return(-1); } void serve(int sd, int graph, int proto) { int fd, sent=0; int nodelay=1; signal(SIGPIPE,SIG_IGN); //buf = malloc (HOST_BUF_LEN); for (;;) { if ((fd = accept(sd, NULL, NULL)) < 0) { perror("accept"); } if ((fcntl(fd, F_SETFL, O_NONBLOCK)) < 0) { perror("fcntl"); } if(proto==IPPROTO_SCTP) { if ((setsockopt(fd, SOL_SCTP, SCTP_NODELAY, &nodelay, sizeof(nodelay))) < 0) { perror("setsockopt"); } } if(graph==1) { while(check_sys_param(fd, graph)>0) sleep(1); } sleep(1); close(fd); } } void usage() { } int main(int argc, char *argv[]) { int port = 10000; char *host; int proto=IPPROTO_SCTP; int graph=0; int option_index = 0; static struct option long_options[] = { {"port", required_argument, NULL, 'p'}, {"tcp", 0, NULL, 't' }, {"graph", required_argument, NULL, 'g' } }; int c = getopt_long(argc, argv, "p:h:tg", long_options, &option_index); while (c != -1) { switch (c) { case 'p': port = atoi(optarg); break; case 't': proto = IPPROTO_TCP; break; case 'g': graph = 1; break; default: usage(); exit; } c = getopt_long(argc, argv, "p:h:tg", long_options, &option_index); } int sd = init_server(port, proto); serve(sd, graph, proto); }