[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] NTP protocol test
From: |
Michel Marti |
Subject: |
[PATCH] NTP protocol test |
Date: |
Fri, 21 Jan 2005 11:44:05 +0100 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.3) Gecko/20041007 Debian/1.7.3-5 |
Attached is a patch that adds simple NTP protocol testing. I've tested this
against pool.ntp.org.
It also contains a fix in protocol.c:create_generic() - this function was
operating on myrsync instead of mygeneric.
I added a new flag to the Protocol_T structure that will be used to determine if
socket_is_ready(...) must be called. I added this since I wanted to prevent
monit from sending a single byte to the NTP server before check_ntp is called.
What do you think?
Michel
diff -urN --exclude=CVS monit-cvs-start/l.l monit-cvs/l.l
--- monit-cvs-start/l.l 2005-01-20 19:25:44.000000000 +0100
+++ monit-cvs/l.l 2005-01-21 09:38:06.000000000 +0100
@@ -181,6 +181,7 @@
smtp { return SMTP; }
pop { return POP; }
imap { return IMAP; }
+ntp { return NTP; }
nntp { return NNTP; }
ssh { return SSH; }
dwp { return DWP; }
diff -urN --exclude=CVS monit-cvs-start/monitor.h monit-cvs/monitor.h
--- monit-cvs-start/monitor.h 2005-01-06 21:51:49.000000000 +0100
+++ monit-cvs/monitor.h 2005-01-21 11:29:12.000000000 +0100
@@ -404,6 +404,7 @@
typedef struct myprotocol {
const char *name; /**< Protocol name */
int(*check)(Socket_T); /**< Protocol verification function */
+ int check_isready; /**< call socket_is_ready()? */
} *Protocol_T;
diff -urN --exclude=CVS monit-cvs-start/p.y monit-cvs/p.y
--- monit-cvs-start/p.y 2005-01-20 19:25:44.000000000 +0100
+++ monit-cvs/p.y 2005-01-21 11:29:12.000000000 +0100
@@ -240,7 +240,7 @@
%token HOST PORT TYPE UDP TCP TCPSSL PROTOCOL CONNECTION
%token ALERT MAILFORMAT UNIXSOCKET SIGNATURE
%token TIMEOUT RESTART CHECKSUM EXPECT EVERY
-%token DEFAULT HTTP APACHESTATUS FTP SMTP POP IMAP NNTP
+%token DEFAULT HTTP APACHESTATUS FTP SMTP POP IMAP NNTP NTP
%token SSH DWP LDAP2 LDAP3 RDATE RSYNC
%token <string> STRING PATH MAILADDR MAILFROM MAILSUBJECT
%token <string> MAILBODY SERVICENAME
@@ -762,6 +762,8 @@
| PROTOCOL LDAP3 { portset.protocol= addprotocol(P_LDAP3); }
| PROTOCOL RDATE { portset.protocol= addprotocol(P_RDATE); }
| PROTOCOL RSYNC { portset.protocol= addprotocol(P_RSYNC); }
+ | PROTOCOL NTP { portset.protocol= addprotocol(P_NTP);
+ portset.type= SOCK_DGRAM; }
| sendexpectlist { portset.protocol= addprotocol(P_GENERIC); }
;
@@ -2007,6 +2009,7 @@
case P_RSYNC: return create_rsync();
case P_GENERIC: return create_generic();
case P_APACHESTATUS: return create_apache_status();
+ case P_NTP: return create_ntp();
}
return create_default();
diff -urN --exclude=CVS monit-cvs-start/protocols/ntp.c
monit-cvs/protocols/ntp.c
--- monit-cvs-start/protocols/ntp.c 1970-01-01 01:00:00.000000000 +0100
+++ monit-cvs/protocols/ntp.c 2005-01-21 11:25:16.000000000 +0100
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C), 2000-2005 by the monit project group.
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <string.h>
+#include <config.h>
+
+#include "protocol.h"
+
+#define NTPLEN 48
+#define NTP_VERSION 3
+
+/**
+ * NTP (Network time procol) test
+ *
+ * @author Michel Marti, <address@hidden>
+ *
+ * @version \$Id: default.c,v 1.12 2005/01/06 20:51:50 martinp Exp $
+ *
+ */
+int check_ntp(Socket_T s)
+{
+ char ntpRequest[NTPLEN];
+ int br;
+
+ ASSERT(s);
+
+ /* Prepare NTP request */
+ memset(ntpRequest,0,NTPLEN);
+ ntpRequest[0]=0xc3|(NTP_VERSION<<3); /* Flags: clock not synched,
+ mode client, version indicator */
+
+
+ /* Send request to NTP server */
+ if( (br=socket_write(s, ntpRequest, NTPLEN)) <= 0 ) {
+ log("NTP: error sending NTP request -- %s\n", STRERROR);
+ return FALSE;
+ }
+
+ /* Receive and validate response */
+ memset(ntpRequest,0,NTPLEN);
+ if( (br=socket_read(s, ntpRequest, NTPLEN)) <= 0) {
+ log("NTP: did not receive answer form server -- %s\n", STRERROR);
+ return FALSE;
+ }
+
+ if( br != NTPLEN ) {
+ log("NTP: Received %d bytes from server, expected %d bytes\n", br, NTPLEN);
+ return FALSE;
+ }
+
+ if( !ntpRequest[0]&4 ) { /* Check for server mode flag */
+ log("NTP: Server mode flag not present in response\n");
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
diff -urN --exclude=CVS monit-cvs-start/protocols/protocol.c
monit-cvs/protocols/protocol.c
--- monit-cvs-start/protocols/protocol.c 2005-01-06 21:51:50.000000000
+0100
+++ monit-cvs/protocols/protocol.c 2005-01-21 11:32:19.000000000 +0100
@@ -49,6 +49,7 @@
static Protocol_T myrsync= NULL;
static Protocol_T mygeneric= NULL;
static Protocol_T myapache_status= NULL;
+static Protocol_T myntp= NULL;
/**
@@ -83,6 +84,7 @@
FREE(myrsync);
FREE(mygeneric);
FREE(myapache_status);
+ FREE(myntp);
}
@@ -92,6 +94,7 @@
NEW(mydefault);
mydefault->name= "DEFAULT";
mydefault->check= check_default;
+ mydefault->check_isready = TRUE;
}
return mydefault;
}
@@ -102,6 +105,7 @@
NEW(myhttp);
myhttp->name= "HTTP";
myhttp->check= check_http;
+ myhttp->check_isready = TRUE;
}
return myhttp;
}
@@ -112,6 +116,7 @@
NEW(myapache_status);
myapache_status->name= "APACHESTATUS";
myapache_status->check= check_apache_status;
+ myapache_status->check_isready = TRUE;
}
return myapache_status;
}
@@ -122,6 +127,7 @@
NEW(myftp);
myftp->name= "FTP";
myftp->check= check_ftp;
+ myftp->check_isready = TRUE;
}
return myftp;
}
@@ -132,6 +138,7 @@
NEW(mysmtp);
mysmtp->name= "SMTP";
mysmtp->check= check_smtp;
+ mysmtp->check_isready = TRUE;
}
return mysmtp;
}
@@ -142,6 +149,7 @@
NEW(mypop);
mypop->name= "POP";
mypop->check= check_pop;
+ mypop->check_isready = TRUE;
}
return mypop;
}
@@ -152,6 +160,7 @@
NEW(myimap);
myimap->name= "IMAP";
myimap->check= check_imap;
+ myimap->check_isready = TRUE;
}
return myimap;
}
@@ -162,6 +171,7 @@
NEW(mynntp);
mynntp->name= "NNTP";
mynntp->check= check_nntp;
+ mynntp->check_isready = TRUE;
}
return mynntp;
}
@@ -172,6 +182,7 @@
NEW(myssh);
myssh->name= "SSH";
myssh->check= check_ssh;
+ myssh->check_isready = TRUE;
}
return myssh;
}
@@ -182,6 +193,7 @@
NEW(mydwp);
mydwp->name= "DWP";
mydwp->check= check_dwp;
+ mydwp->check_isready = TRUE;
}
return mydwp;
}
@@ -192,6 +204,7 @@
NEW(myldap2);
myldap2->name= "LDAP2";
myldap2->check= check_ldap2;
+ myldap2->check_isready = TRUE;
}
return myldap2;
}
@@ -202,6 +215,7 @@
NEW(myldap3);
myldap3->name= "LDAP3";
myldap3->check= check_ldap3;
+ myldap3->check_isready = TRUE;
}
return myldap3;
}
@@ -212,6 +226,7 @@
NEW(myrdate);
myrdate->name= "RDATE";
myrdate->check= check_rdate;
+ myrdate->check_isready = TRUE;
}
return myrdate;
}
@@ -222,17 +237,28 @@
NEW(myrsync);
myrsync->name= "RSYNC";
myrsync->check= check_rsync;
+ myrsync->check_isready = TRUE;
}
return myrsync;
}
void *create_generic() {
- if(myrsync == NULL) {
- NEW(myrsync);
- myrsync->name= "generic";
- myrsync->check= check_generic;
+ if(mygeneric == NULL) {
+ NEW(mygeneric);
+ mygeneric->name= "generic";
+ mygeneric->check= check_generic;
+ mygeneric->check_isready = TRUE;
+ }
+ return mygeneric;
+}
+
+void *create_ntp() {
+ if(myntp == NULL) {
+ NEW(myntp);
+ myntp->name= "NTP";
+ myntp->check= check_ntp;
+ myntp->check_isready = FALSE;
}
- return myrsync;
+ return myntp;
}
-
diff -urN --exclude=CVS monit-cvs-start/protocols/protocol.h
monit-cvs/protocols/protocol.h
--- monit-cvs-start/protocols/protocol.h 2005-01-06 21:51:50.000000000
+0100
+++ monit-cvs/protocols/protocol.h 2005-01-21 09:37:06.000000000 +0100
@@ -42,6 +42,7 @@
#define P_RSYNC 13
#define P_GENERIC 14
#define P_APACHESTATUS 15
+#define P_NTP 16
void gc_protocols();
@@ -61,6 +62,7 @@
void* create_rsync();
void* create_generic();
void* create_apache_status();
+void* create_ntp();
/* "Package" locale Protocol routines */
int check_default(Socket_T);
@@ -78,6 +80,7 @@
int check_rsync(Socket_T);
int check_generic(Socket_T);
int check_apache_status(Socket_T);
+int check_ntp(Socket_T);
#endif
diff -urN --exclude=CVS monit-cvs-start/validate.c monit-cvs/validate.c
--- monit-cvs-start/validate.c 2005-01-20 19:25:44.000000000 +0100
+++ monit-cvs/validate.c 2005-01-21 11:29:12.000000000 +0100
@@ -465,7 +465,7 @@
}
/* Verify that the socket is ready for i|o */
- if(! socket_is_ready(socket)) {
+ if(p->protocol->check_isready && ! socket_is_ready(socket)) {
snprintf(report, STRLEN,
"'%s' failed, the socket at %s is not ready for i|o -- %s",
s->name, p->address, STRERROR);
- [PATCH] NTP protocol test,
Michel Marti <=