[Date Prev]   [Date Next] [Thread Prev]   [Thread Next] [Date Index]   [Thread Index]

 

     [nocol-users] Radiusmon Patch for SIGHUP

The attached patch provides sighup functionality to radiusmon. It was
built on nocol-4.2.1, so it may not merge smoothly with the newest beta.

Basically, it removes the temporary event variable "v" and stores it in
site_info, so each node in the linked list has it's own event variable.
Then rather than reading through the file, it loops through the linked
list and writes to the data file.

I've tested this quite a bit and it seems to work fine, but I'd love to
have someone else test it as well. If desire warrants it, I will (later
on) update this for the newest beta at the time.

--Nathan

____________________________________________________________________
Nathan Clemons, Systems Engineer
WinStar Internet and Hosting Services

800 South Main St. Mansfield, MA  02048
____________________________________________________________________  
nclemons@winstar.com    www.winstar.com    (v) 800-234-0002 ext.1109
nathan@ici.net                             (f) 508-261-0430

diff -c /files/nocol/source/nocol-4.2.1/radiusmon/main.c /files/nocol/nocol-4.2.1/radiusmon/main.c
*** /files/nocol/source/nocol-4.2.1/radiusmon/main.c	Thu Jan 20 15:17:02 2000
--- /files/nocol/nocol-4.2.1/radiusmon/main.c	Fri Jan 21 11:04:10 2000
***************
*** 26,31 ****
--- 26,32 ----
  
  #include <stdio.h>
  #include <fcntl.h>
+ #include <signal.h>
  
  #include "nocol.h"
  #include "radiusmon.h"
***************
*** 39,54 ****
  struct site_info *site_info_list = NULL;	
  
  void free_site_list();
  static char *Strdup();			/* replacement for strdup() */
  
  static time_t	pollinterval ;		/* Time between polls */
  int debug = 0;
  
  int main(argc, argv)
    int argc;
    char **argv;
  {
-   int fdout = 0;
    time_t starttm, polltime ;
    extern char *optarg;
    extern int optind;
--- 40,59 ----
  struct site_info *site_info_list = NULL;	
  
  void free_site_list();
+ int copy_site_list(struct site_info **si_list, struct site_info **new_si_list);
  static char *Strdup();			/* replacement for strdup() */
  
  static time_t	pollinterval ;		/* Time between polls */
  int debug = 0;
+ //int debug = 10;
+ int fdout = 0;
  
+ void sighuphandler();		/* for HUP signalling to reread config */
+ 
  int main(argc, argv)
    int argc;
    char **argv;
  {
    time_t starttm, polltime ;
    extern char *optarg;
    extern int optind;
***************
*** 110,115 ****
--- 115,123 ----
      if (init_sites(fdout, configfile) == -1 )
        goto Cleanup ;
  
+     /* Set up the signal handler */
+     (void) signal(SIGHUP, sighuphandler);
+ 
      if (!pollinterval)
        pollinterval = POLLINTERVAL ;		/* default value */
  
***************
*** 155,165 ****
    time_t locclock ;			/* Careful, don't use 'long'	*/
    FILE *p_nodes ;
    char record[MAXLINE];
-   EVENT v;				/* Defined in NOCOL.H		*/
    struct site_info *lastnode = NULL, *newnode;
  
!   if(site_info_list)
!     free_site_list(&site_info_list);	/* In case rereading confg file */
  
    if ((p_nodes = fopen(configfile, "r")) == NULL)
    {
--- 163,177 ----
    time_t locclock ;			/* Careful, don't use 'long'	*/
    FILE *p_nodes ;
    char record[MAXLINE];
    struct site_info *lastnode = NULL, *newnode;
  
!   if (site_info_list) {
!      free_site_list(&site_info_list);	/* In case rereading confg file */
!      if ( lseek(fdout, (off_t)0, SEEK_SET) == -1) {
!         perror (prognm);
!         return (-1);
!      } 
!   }
  
    if ((p_nodes = fopen(configfile, "r")) == NULL)
    {
***************
*** 171,200 ****
    /*
     * Fill in the static data stuff
     */
!     bzero (&v, sizeof(v)) ;
!     locclock = time((time_t *)0);
!     loctime = localtime((long *)&locclock);
  
-     v.mon = loctime->tm_mon + 1;	v.day = loctime->tm_mday;
-     v.hour = loctime->tm_hour;		v.min = loctime->tm_min;
- 
-     /*
-      * in the following strncpy's, the NULL is already appended because
-      * of the bzero, and copying one less than size of the arrays.
-      */
-     strncpy (v.sender, sender, sizeof(v.sender) - 1);
- 
-     strncpy (v.var.name, VARNM, sizeof (v.var.name) - 1);
-     strncpy (v.var.units, VARUNITS, sizeof (v.var.units) - 1);
-     v.var.value = 0 ; v.var.threshold = 0 ;	/* threshold not used */
-     v.nocop = SETF_UPDOUN (v.nocop, n_UNKNOWN);	/* Set all to UNKNOWN	*/
-     v.severity = E_INFO ;
- 
    while(fgetLine(p_nodes,record,MAXLINE) > 0 ) 
    {
      int rc;						/* return code	*/
  
-     v.nocop = 0 ;				/* Init options to zero	*/
      *w1 = *w2 = *w3 = *w4 = *w5 = *w6 = '\0' ;
      rc = sscanf(record,"%s %s %s %s %s %s", w1, w2, w3, w4, w5, w6);
      if (rc == 0 || *w1 == '\0' || *w1 == '#')  /* Comment or blank 	*/
--- 183,195 ----
    /*
     * Fill in the static data stuff
     */
!   locclock = time((time_t *)0);
!   loctime = localtime((long *)&locclock);
  
    while(fgetLine(p_nodes,record,MAXLINE) > 0 ) 
    {
      int rc;						/* return code	*/
  
      *w1 = *w2 = *w3 = *w4 = *w5 = *w6 = '\0' ;
      rc = sscanf(record,"%s %s %s %s %s %s", w1, w2, w3, w4, w5, w6);
      if (rc == 0 || *w1 == '\0' || *w1 == '#')  /* Comment or blank 	*/
***************
*** 215,223 ****
        continue;
      }
      
!     /* Only <name> <addr> <port> <secret> <username> <password>  remain */
!     strncpy(v.site.name, w1, sizeof(v.site.name) - 1);
!     strncpy(v.site.addr, w2, sizeof(v.site.addr) - 1);	/* no checks */
      
      if (inet_addr(w2) == -1)	/* bad address */
      {
--- 210,235 ----
        continue;
      }
      
!     /*
!      * Create a site info node and add to the tail of linked list
!      */
!     newnode = (struct site_info *)calloc(1, sizeof(struct site_info));
!     if(!newnode)
!     {
!       fprintf(stderr,"%s: Out of Memory ",prognm);
!       return -1;
!     }
! 
!     /*
!      * in the following strncpy's, the NULL is already appended because
!      * of the calloc, and copying one less than size of the arrays.
!      */
!     strncpy (newnode->v.sender, sender, sizeof(newnode->v.sender) - 1);
!     strncpy(newnode->v.site.name, w1, sizeof(newnode->v.site.name) - 1);
!     /* no checks */
!     strncpy(newnode->v.site.addr, w2, sizeof(newnode->v.site.addr) - 1);	
!     strncpy(newnode->v.var.name, VARNM, sizeof(newnode->v.var.name) - 1);
!     strncpy(newnode->v.var.units, VARUNITS, sizeof(newnode->v.var.units) - 1);
      
      if (inet_addr(w2) == -1)	/* bad address */
      {
***************
*** 224,229 ****
--- 236,242 ----
        fprintf(stderr,
  	      "	(%s): Error in address '%s' for site '%s', ignoring\n",
  	      prognm, w2, w1);
+       free(newnode);
        continue ;
      }
  
***************
*** 231,257 ****
      {
        fprintf(stderr, "%s: invalid number of fields\n", prognm);
        fprintf(stderr, "\t '%s'\n", record);
        continue;
      }
      if (atoi(w3) <= 0) {
        fprintf(stderr, "%s: invalid port number '%s', skipping\n", prognm, w3);
        continue;
      }
  
!     /*
!      * Create a site info node and add to the tail of linked list
!      */
!     newnode = (struct site_info *)calloc(1, sizeof(struct site_info));
!     if(!newnode)
!     {
!       fprintf(stderr,"%s: Out of Memory ",prognm);
!       return -1;
!     }
  
!     newnode->port = atoi(w3);
!     newnode->secret = Strdup(w4);    
!     newnode->user = Strdup(w5);    
!     newnode->pass = Strdup(w6);
      newnode->next = NULL;
  	
      if(!site_info_list)
--- 244,273 ----
      {
        fprintf(stderr, "%s: invalid number of fields\n", prognm);
        fprintf(stderr, "\t '%s'\n", record);
+       free(newnode);
        continue;
      }
      if (atoi(w3) <= 0) {
        fprintf(stderr, "%s: invalid port number '%s', skipping\n", prognm, w3);
+       free(newnode);
        continue;
      }
  
!     newnode->port    = atoi(w3);	newnode->secret   = Strdup(w4);    
!     newnode->user    = Strdup(w5);    	newnode->pass     = Strdup(w6);
  
!     /* Set all to UNKNOWN	*/
!     newnode->v.nocop = SETF_UPDOUN (newnode->v.nocop, n_UNKNOWN);
!     // should the loglevel be initialized, and if so, what to? --NC
!     newnode->v.loglevel = 0 ;
!     newnode->v.mon      = loctime->tm_mon + 1;
!     newnode->v.day      = loctime->tm_mday;
!     newnode->v.hour     = loctime->tm_hour;	
!     newnode->v.min      = loctime->tm_min;
!     newnode->v.var.value     = 0 ; 
!     newnode->v.var.threshold = 0 ;	/* threshold not used */
!     newnode->v.nocop         = 0 ;	/* Init options to zero	*/
!     newnode->v.severity      = E_INFO ;
      newnode->next = NULL;
  	
      if(!site_info_list)
***************
*** 261,267 ****
  	  
      lastnode = newnode;
  
!     write (fdout, (char *)&v, sizeof(v)) ;
    }	/* end: while			*/
  }
  
--- 277,283 ----
  	  
      lastnode = newnode;
  
!     write (fdout, (char *)&(newnode->v), sizeof(newnode->v)) ;
    }	/* end: while			*/
  }
  
***************
*** 285,337 ****
  
    /* 
     * until end of the file or erroneous data... one entire pass
     */
  
!   curnode = site_info_list;	/* Point to the head of Linked List */
! 
!   while ( (bufsize = read (fdout, (char *)&v, sizeof(v))) == sizeof(v) ) 
    {
!       if (debug) {
!          fprintf(stderr, "sender: %s\nsite.name: %s\nsite.addr: %s\n",
!             v.sender, v.site.name, v.site.addr);
!          fprintf(stderr, "var.name: %s\nvar.value: %d\nvar.threshold: %d\n",
!             v.var.name, v.var.value, v.var.threshold);    
!          fprintf(stderr, "var.units: %s\nmon: %d\nday: %d\n",
!             v.var.units, v.mon, v.day);
!          fprintf(stderr, "hour: %d\nmin: %d\nseverity: %d\n",
!             v.hour, v.min, v.severity);
!          fprintf(stderr, "loglevel: %d\nnocop: %d\n\n",
!             v.loglevel, v.nocop);
!       }  
! 
!    status = radiusmon(v.site.addr, curnode->port, curnode->secret,
  		      curnode->user, curnode->pass);
!    if (debug)
!    {
!      fprintf(stderr, "(debug) %s: Radius down %s : %d\n", prognm,
! 	     v.site.name, curnode->port);
!      fflush(stderr);
!    }
  
!    update_event(&v, status, /* VALUE */ status, /* max severity */E_CRITICAL);
!       
!    /* Now rewind to start of present record and write to the file  */
!    lseek(fdout,-(off_t)sizeof(v), SEEK_CUR);
!    write (fdout, (char *)&v, sizeof(v));
  
!    curnode = curnode->next;
!      
!   }	/* end of:    while (read..)	*/
!     
!   /**** Now determine why we broke out of the above loop *****/
!     
!   if (bufsize == 0)			/* reached end of the file	*/
!     return (1);
!   else {				/* error in output data file 	*/
!     fprintf (stderr, "%s: Insufficient read data in output file", prognm);
!     return (-1);
!   }
! 
  }	/* end of:  poll_sites		*/
  
  help()
--- 301,334 ----
  
    /* 
     * until end of the file or erroneous data... one entire pass
+    *
+    * re-written to pass through the linked list rather than reading through
+    * the file... --NPC 1/20/00
     */
  
!   for (curnode = site_info_list; curnode != NULL; curnode = curnode->next)
    {
!    status = radiusmon(curnode->v.site.addr, curnode->port, curnode->secret,
  		      curnode->user, curnode->pass);
!    update_event(&(curnode->v), status, /*VALUE*/status, /*maxseverity*/E_CRITICAL);
!    if (status != 1) {
!      if (debug) {
!        fprintf(stderr, "(debug) %s: Radius down %s : %d\n", prognm,
! 	       curnode->v.site.name, curnode->port);
!        fflush(stderr);
!      }
!    } else {  
!      if (debug) {  
!        fprintf(stderr, "(debug) %s: Radius fine on %s : %d(%d)(%d)\n", prognm,
!                curnode->v.site.name, curnode->port, status, curnode->v.var.value);
!        fflush(stderr);
!      }
!    } 
  
!    write (fdout, (char *)&(curnode->v), sizeof(curnode->v));
!   }	/* end of:    for (curnode=..)	*/
  
!   return (1) ;
  }	/* end of:  poll_sites		*/
  
  help()
***************
*** 371,374 ****
--- 368,471 ----
    }
  
    return (t);
+ }
+ 
+ int copy_site_list(si_list, new_si_list)
+    struct site_info **si_list;
+    struct site_info **new_si_list;
+ {
+    struct site_info *curptr, *newptr, *lastptr = NULL;
+    int nodenum = 0;
+ 
+    for (curptr = *si_list; curptr; curptr = curptr->next)
+    {
+       newptr = (struct site_info *)calloc(1, sizeof(struct site_info));
+       if (!newptr)
+       {
+          fprintf(stderr, "%s: Out of memory!", prognm);
+          return -1;
+       }
+ 
+       newptr->port            = curptr->port;
+       newptr->secret          = Strdup(curptr->secret);
+       newptr->user            = Strdup(curptr->user);
+       newptr->pass            = Strdup(curptr->pass);
+       strncpy(newptr->v.sender, curptr->v.sender, sizeof(newptr->v.sender) - 1);
+       strncpy(newptr->v.site.name, curptr->v.site.name, sizeof(newptr->v.site.name) - 1);
+       strncpy(newptr->v.site.addr, curptr->v.site.addr, sizeof(newptr->v.site.addr) - 1);
+       if (debug) {
+          fprintf(stderr, "Copying node #%d -- %s (%s)\n", nodenum, curptr->v.site.name, curptr->v.site.addr);
+       }
+       strncpy(newptr->v.var.name, curptr->v.var.name, sizeof(newptr->v.var.name) - 1);
+       newptr->v.var.value     = curptr->v.var.value;
+       newptr->v.var.threshold = curptr->v.var.threshold;
+       strncpy(newptr->v.var.units, curptr->v.var.units, sizeof(newptr->v.var.units) - 1);
+       newptr->v.mon           = curptr->v.mon;
+       newptr->v.day           = curptr->v.day;
+       newptr->v.hour          = curptr->v.hour;
+       newptr->v.min           = curptr->v.min ;
+       newptr->v.severity      = curptr->v.severity ;
+       newptr->v.loglevel      = curptr->v.loglevel ;
+       newptr->v.nocop         = curptr->v.nocop ;
+       newptr->next   = NULL;
+ 
+       if (!(*new_si_list))
+          *new_si_list = newptr;
+       else
+          lastptr->next = newptr;
+ 
+       lastptr = newptr;
+       nodenum++;
+    }
+ }
+ 
+ void update_event_nodes(old_si_list, new_si_list)
+    struct site_info *old_si_list;
+    struct site_info *new_si_list;
+ {
+    struct site_info *curptr_new, *curptr_old;
+ 
+    for (curptr_new = new_si_list; curptr_new; curptr_new=curptr_new->next) {
+        for (curptr_old = old_si_list; curptr_old; curptr_old=curptr_old->next) {
+            if (!strcmp(curptr_old->v.site.name, curptr_new->v.site.name) &&
+                !strcmp(curptr_old->v.site.addr, curptr_new->v.site.addr) &&
+                !strcmp(curptr_old->user, curptr_new->user)) {
+               if (debug) {
+                  fprintf(stderr, "(debug) saving state information for %s\n",
+                          curptr_old->v.site.name);
+               }
+               curptr_new->v.severity = curptr_old->v.severity;
+               curptr_new->v.loglevel = curptr_old->v.loglevel;
+               curptr_new->v.nocop    = curptr_old->v.nocop;
+               break;
+            }
+        }
+    }
+ }
+ 
+ void sighuphandler()
+ {
+    struct site_info *temp_site_list = NULL;
+    int retval;
+ 
+    if (debug) {
+       fprintf(stderr, "(debug) Received SIGHUP, going to reread config.\n");
+       fprintf(stderr, "(debug)  copying site information and events...");
+    }
+    retval = copy_site_list(&site_info_list, &temp_site_list);
+    if (retval >= 0) {
+       if (debug) {
+          fprintf(stderr, "done.\n(debug) rereading config file...");
+       }
+       init_sites(fdout, configfile);
+       if (debug) {
+          fprintf(stderr, "done.\n(debug) copying state information back...\n");
+       }
+       update_event_nodes(temp_site_list, site_info_list);
+       if (debug) {
+          fprintf(stderr, "done.\n");
+       }
+    } else if (debug) {
+       fprintf(stderr, " ABORTED.\n");
+    }
  }
Only in /files/nocol/source/nocol-4.2.1/radiusmon: main.o
Only in /files/nocol/source/nocol-4.2.1/radiusmon: md5.o
Only in /files/nocol/source/nocol-4.2.1/radiusmon: radiusmon
Only in /files/nocol/source/nocol-4.2.1/radiusmon: radiusmon.o
*** /files/nocol/source/nocol-4.2.1/include/radiusmon.h	Fri Jul 31 14:20:24 1998
--- /files/nocol/nocol-4.2.1/include/radiusmon.h	Thu Jan 20 13:33:16 2000
***************
*** 42,47 ****
--- 42,48 ----
    char *secret;
    char *user;
    char *pass;    /* password */
+   EVENT v;
    struct site_info *next;
  };