Linux下用C實現Ping監測與HTTP報文上傳,

來源:互聯網
上載者:User

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)// urlif(strlen(argv[1]) > 0 && strlen(argv[1]) < 200)sprintf(strURL,"%s",argv[1]);elsereturn 0;// http https if(strlen(argv[2]) > 0 && strlen(argv[2]) < 2)nType = atoi(argv[2]);elsereturn 0;// scan portif(strlen(argv[3]) > 0 && strlen(argv[3]) < 6)nIPPort = atoi(argv[3]);elsereturn 0;// ip if(strlen(argv[4]) > 0 && strlen(argv[4]) < 160)sprintf(strIP,"%s",argv[4]);elsesprintf(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>





相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.