Linux下用C实现Ping监测与HTTP报文上传

     有一个数据中心监测项目,命名为CPing,它的主要原理通过WEB进行前台统一配置管理,后台定期对数据中心相关设备执行Ping操作,并将结果及时写入到数据库。

  该项目基于Linux平台部署,前端开发语言采用PHP,后台开发语言采用C,由于考量到项目的部署简洁性,后台开发的守护进程尽量不直接操作数据库,而是将需要写入的数据以HTTP的形式发送给PHP的WEB页面,由PHP完成写入操作。这样的好处是后台守护进程部署时不需要配置相关数据库接入环境。

  下面给出一段后台代码,作用是执行Ping操作,并将结果封装成HTTP报文发送至WEB端。

<span xmlns="http://www.w3.org/1999/xhtml" style="">//---------------------------------------------------------
//  HTTPSinge.c ./http www.ifeng.com  0/1 80 127.0.0.1 80 
// 用于执行HTTP命令,并将结果通过HTTP GET方式传至WEB数据库
//       my2005lb 2013-8-3
//
//---------------------------------------------------------
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

char* URLTOIP(char *argv)
{
	struct hostent *h;
	if((h=gethostbyname(argv))==NULL)
	{
		fprintf(stderr,"不能得到IP/n");
		exit(1);
	}
	printf("HostName :%s/n",h->h_name);
	printf("IP Address :%s/n",inet_ntoa(*((struct in_addr *)h->h_addr)));
	return inet_ntoa(*((struct in_addr *)h->h_addr));
}
// 执行PING 操作
int HTTPScan(char *argv,int nPort,int nType,double *pRet)
{
	struct sockaddr_in serverAddr;
	int clientSocket;
	int nCount = 0;
	char sendbuf[2000];
	char recvbuf[2000];
	if((clientSocket=socket(AF_INET,SOCK_STREAM,0)) < 0)
		return -1; 		
	
	serverAddr.sin_family=AF_INET;
	serverAddr.sin_port=htons(nPort);
	printf("%s\n",URLTOIP(argv));
	serverAddr.sin_addr.s_addr=inet_addr(URLTOIP(argv));
	if(connect(clientSocket,( struct sockaddr * )&serverAddr,sizeof(serverAddr)) < 0)
		return -1; 	

	//printf("%s\n",sendbuf);
	sprintf(sendbuf,"HEAD http://%s/ HTTP/1.1\r\nHOST: %s\r\nCONNECTION: CLOSED\r\n\r\n",argv,argv); 

	send(clientSocket,sendbuf,strlen(sendbuf),0);
	recv(clientSocket,recvbuf,sizeof(recvbuf),0);
	printf("%s\n",recvbuf);
	close(clientSocket);
	return nCount;
}
// 执行向远程数据中心上报数据
int ProcessSQLUpdate(char *strURL,char *strIP,int nPort,char *sendbuf)
{
   	struct sockaddr_in serverAddr;
	int clientSocket;
	char recvbuf[2000];
	if((clientSocket=socket(AF_INET,SOCK_STREAM,0)) < 0)
		return -1; 		
	
	serverAddr.sin_family=AF_INET;
	serverAddr.sin_port=htons(nPort);
	serverAddr.sin_addr.s_addr=inet_addr(strIP);
	if(connect(clientSocket,( struct sockaddr * )&serverAddr,sizeof(serverAddr)) < 0)
		return -1; 	

	//printf("%s\n",sendbuf);
	send(clientSocket,sendbuf,strlen(sendbuf),0);
	recv(clientSocket,recvbuf,sizeof(recvbuf),0);
	//printf("%s\n",recvbuf);
	close(clientSocket);
	return 0;
}
int main(int argc,char *argv[])
{	
	char strIP[250];
	int nIPPort = 80;
	char strURL[250];
	int nPort = 80;
	double pRet[6];
	int nType=0;
	int nCount = 0;
	char sendbuf[2000];

	if(argc != 6) return 0;
	// ./http www.sina.com.cn 1/0 80 127.0.0.1 80(1为HTTPS,0为HTTP)
	// url
	if(strlen(argv[1]) > 0 && strlen(argv[1]) < 200)
		sprintf(strURL,"%s",argv[1]);
	else
		return 0;
	
	// http https 
	if(strlen(argv[2]) > 0 && strlen(argv[2]) < 2)
		nType = atoi(argv[2]);
	else
		return 0;

	// scan port
	if(strlen(argv[3]) > 0 && strlen(argv[3]) < 6)
		nIPPort = atoi(argv[3]);
	else
		return 0;

	// ip 
	if(strlen(argv[4]) > 0 && strlen(argv[4]) < 160)
		sprintf(strIP,"%s",argv[4]);
	else
		sprintf(strIP,"127.0.0.1");

	if(strlen(argv[5]) > 0 && strlen(argv[5]) <= 5)
		nPort = atoi(argv[5]);
    printf("%s %d %d %s %d\n",strIP,nIPPort,nType,strURL,nPort);

	// process ping 
	nCount = HTTPScan(strURL,nIPPort,nType,pRet);

	if(nCount == 2)
		sprintf(sendbuf,"GET http://%s/CPing/update/http.php?name=%s&lost=%f HTTP/1.1\r\nHOST: %s\r\nCONNECTION: CLOSED\r\n\r\n",strIP,strURL,pRet[1],strIP); 
	else if(nCount == 6)
		sprintf(sendbuf,"GET http://%s/CPing/update/http.php?name=%s&lost=%f&avg=%f HTTP/1.1\r\nHOST: %s\r\nCONNECTION: CLOSED\r\n\r\n",strIP,strURL,pRet[1],pRet[3],strIP); 
	else 
		sprintf(sendbuf,"GET http://%s/CPing/update/http.php?name=%s&lost=-1&lost=-1 HTTP/1.1\r\nHOST: %s\r\nCONNECTION: CLOSED\r\n\r\n",strIP,strURL,strIP); 

//	ProcessSQLUpdate(strURL,strIP,nPort,sendbuf);
	/*
		printf("Min Value: %f\n",pRet[2]);
		printf("Avg Value: %f\n",pRet[3]);
		printf("Max Value: %f\n",pRet[4]);
		printf("MDev Value: %f\n",pRet[5]);
	*/
	return 0;
}</span>


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。