All pastes #1052454 Raw Edit

Someone

public text v1 · immutable
#1052454 ·published 2008-06-21 14:07 UTC
rendered paste body
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/poll.h>
#include <sys/time.h>
#include <signal.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <sys/uio.h>
#include <sys/sysinfo.h>
#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;
	//	             s_ticks_o = 0, i_ticks_o = 0;
	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);	
	}
}
int check_sys_param(int fd, int graph)
{
	char buf[HOST_BUF_LEN];
	memset(buf,0,HOST_BUF_LEN);
	int days, hours, mins, num;
	//struct sysinfo *sys_info = malloc (sizeof(sysinfo));
	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, 
			0,
			//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);
	}
	//else printf("binded\n");
	if (listen(sd, 1)==-1) {
		printf("not listening...\n");
       	        goto errout;
	}
       	//else printf("listening...\n");
	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);
}