In Linux, Ping monitoring and HTTP packet uploading are implemented using C,

Source: Internet
Author: User
Tags get ip web database htons

In Linux, Ping monitoring and HTTP packet uploading are implemented using C,

There is a data center monitoring project named CPing. Its main principle is to perform unified foreground configuration management through the WEB, and periodically perform Ping operations on the data center-related devices in the background, and write the results to the database in a timely manner.

This project is deployed based on the Linux platform. The frontend development language is PHP and the background development language is C. Due to the simplicity of the project deployment, the daemon process of background development does not directly operate the database, instead, the data to be written is sent to the php web page in the form of HTTP, And the write operation is completed by PHP. The advantage is that you do not need to configure the relevant database access environment when deploying the background daemon.

The following is a piece of background code used to perform the Ping operation and encapsulate the result into an HTTP packet and send it to the WEB end.

<Span xmlns = "http://www.w3.org/1999/xhtml" style = ""> // ----------------------------------------------------------- // HTTPSinge. c. /http www.ifeng.com 0/1 80 127.0.0.1 80 // used to execute the HTTP command, and pass the results to the WEB database through http get // my2005lb 2013-8-3 // ----------------------------------------------------------- # include <sys/stat. h> # include <fcntl. h> # include <errno. h> # include <netdb. h> # include <sys/types. h> # include <sys/sock Et. 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, "Cannot Get 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);} // execute PI NG operation 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, (stru Ct 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", argv, argv ); send (clientSocket, sendbuf, strlen (sendbuf), 0); recv (clientSocket, recvbuf, sizeof (recvbuf), 0); printf ("% s \ n", recvbuf ); close (clientSocket); return nCount;} // execute reporting data to the remote data center int ProcessSQLUpdate (char * strURL, char * strIP, int nP Ort, 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, s Trlen (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 is HTTPS, 0 is 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 (Rgv [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 \ 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 ", 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 ", 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 ", 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>





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.