| 项目搜索 |
| |
|
代码片段库:
查看代码片段
浏览
| 提交新的代码片段
| 创建代码包
Web Server Monitoring Software
|
类型:
Full Script |
类别:
UNIX Admin
|
许可证:
GNU General Public License |
语言:
C
|
描述:
This Unix C code monitors a web server every few minutes by trying to retrieve its home page. It sends you email when it can't connect, and every so often while the server is still down. It sends a final message when it comes back up. If you have email paging, just direct the email to your pager address.
|
该代码片段的版本系列:
片段ID |
下载版本 |
提交时间 |
提交人 |
删除 |
10 | 0.1 | 2001-07-26 13:32 | bbsadmin | |
点击"下载版本"来下载该代码片段.
最新版本的代码片段: 0.1
/*
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);
}
如果您修改了一个代码片段并且觉得很应该让别人共享,您可以把这作为这个代码片段的最新版本提交上来. |
|