📄 web server+Ӧ
字号:
/*
by Network Wizards, 1995 (www.nw.com)
webcheck servername mailaddr
every minute,
sends GET / HTTP/1.0 to server, port 80
reads response
if no answer, calls pmail to send a page
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/time.h>
#include <signal.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#define FALSE 0
#define TRUE 1
#define PORT 80
#define MAXTMPLEN 128 /* max length of tmp array declared below */
#define BUFLEN 256
char buf[BUFLEN];
struct hostent *hp;
int skt;
FILE *fin, *fout;
struct itimerval tv;
void tout();
char tmp[MAXTMPLEN];
char *mailaddr;
main(argc, argv)
int argc;
char *argv[];
{
int i, c, down;
/* punt if syntax is wrong */
if (argc != 3)
{
fprintf(stderr,"usage: wwwtest server email\n");
exit(1);
}
if ((hp = gethostbyname(argv[1])) == NULL)
{
fprintf(stderr,"can't find host '%s'.\n",argv[1]);
exit(1);
}
mailaddr = argv[2];
if (fork())
exit(0);
for (i = 10; i >= 0; i--)
(void) close(i);
(void) open("/dev/null", O_RDONLY);
(void) dup2(0, 1);
(void) dup2(0, 2);
i = open("/dev/tty", O_RDWR);
if (i > 0)
{
(void) ioctl(i, TIOCNOTTY, (char *)NULL);
(void) close(i);
}
signal(SIGALRM,tout);
down = FALSE;
for (;;)
{
if (checkserver()) /* if server up */
{
if (down)
notify("up",argv[1]);
down = FALSE;
sleep(60);
continue;
}
/* if server down */
sleep(5*60);
if (checkserver()) /* try once more */
{
sleep(60);
continue;
}
notify("down",argv[1]);
down = TRUE;
sleep(3*60);
}
}
checkserver()
{
struct sockaddr_in sin;
u_long addr;
tv.it_interval.tv_sec = 0;
tv.it_interval.tv_usec = 0;
tv.it_value.tv_sec = 30;
tv.it_value.tv_usec = 0;
setitimer(ITIMER_REAL,&tv,NULL);
/* create a socket */
if ((skt = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{
cancel_timeout();
return(FALSE);
}
sin.sin_family = hp->h_addrtype;
bcopy (hp->h_addr, &sin.sin_addr, hp->h_length);
addr = ntohl(sin.sin_addr.s_addr);
sin.sin_port = htons(80);
/* Now, connect to that port */
if (connect(skt, (struct sockaddr *) &sin, sizeof(sin)) < 0)
{
close(skt);
cancel_timeout();
return(FALSE);
}
/* associate a STREAM to 'fin' and 'fout' from 'skt' */
if ((fin = fdopen(skt, "r")) == NULL ||
(fout = fdopen(skt, "w")) == NULL)
{
close(skt);
cancel_timeout();
return(FALSE);
}
/* write out the request to the server (remember to terminate
* with <return><newline>).
*/
fprintf(fout, "GET / HTTP/1.0\r\n\r\n");
fflush(fout);
/* read back results */
while (fgets(buf,BUFLEN,fin) != NULL)
{
if (!strncasecmp(buf,"HTTP/1.0 200 OK",15))
{
fclose(fin); fclose(fout);
close(skt);
cancel_timeout();
return(TRUE);
}
if (*buf == '\r') break;
if (*buf == '\n') break;
}
fclose(fin); fclose(fout);
close(skt);
cancel_timeout();
return(FALSE);
}
cancel_timeout()
{
tv.it_interval.tv_sec = 0;
tv.it_interval.tv_usec = 0;
tv.it_value.tv_sec = 0;
tv.it_value.tv_usec = 0;
setitimer(ITIMER_REAL,&tv,NULL);
}
void tout()
{
fclose(fin); fclose(fout);
close(skt);
skt = -1;
}
notify(msg,server)
char *msg, *server;
{
char buf[256];
sprintf(buf,"echo \"%s : %s\" | /usr/bin/Mail %s",
msg,server,mailaddr);
system(buf);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -