Monitoring Named

Martin McCormick martin at dc.cis.okstate.edu
Mon Mar 19 14:57:56 UTC 2001


	Here is a short program that compiles in gcc and will see
if named is running, try to start it if not, and
send you a predetermined number of attempt messages before giving
up.

	This code was written by others and me.
gcc -onamedverify namedverify.c

should compile it.  For maximum impact, you need incoming mail to
ring a bell or some other real-time alerting method and you want
to run it under cron with all parameters set to * so it runs each
minute.

#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
#include <errno.h>

main()
{ /*main start*/
int const verify = 0;
int const retries = 2; /*Set to any reasonable value.*/
char *bypass="/etc/namedverify.bypass"; /*Touch that to make
nothing happen upon execution.*/
char *filename="/var/run/named.pid"; /*Change to something else
if your named.pid is somewhere else.*/
char *bombcount="/var/tmp/namedverify.fail";
FILE *f;
FILE *g;
FILE *h;
pid_t pid;
int bombs = 0;

setuid(0);

 if ((f = fopen(filename,"r")) == NULL)  /*Is named.pid there?*/
{ /*This happens if you can't open named.pid.*/
  perror(filename); /*Bummer!*/
  exit(errno);
} /*This happens if you can't open named.pid.*/
 if (fscanf(f,"%d",&pid) != 1) 
{ /*This happens if you can't read the file.*/
  fprintf(stderr,"Error reading from %d\n",filename); /*Might
happen if the file is empty which is also bad news.*/
  perror(filename);
  exit(errno);
} /*This happens if you can't read the file.*/
 if(kill(pid,verify) != 0)  /*try a Signal 0 which is harmless on
that process.*/
{ /*Do if the process isn't running*/
 if ( ! (g = fopen(bombcount,"r")) == NULL)  /*How many failures?*/
{/*Get last count.*/
if (fscanf(g,"%d",&bombs) != 1) 
{ /*bad news if we can't read this file*/
  fprintf(stderr,"Error reading from %d\n",bombcount);
 } /*bad news*/
}/*Get last count.*/
fclose(g);
bombs++;
 if ((g = fopen(bombcount,"w")) == NULL) 
/*Put back a new error count.*/
{ /*We're hosed if we can't write this file.*/
  perror(bombcount);
  exit(errno);
} /*We're hosed if we can't write this file.*/
fprintf(g,"%d\n",bombs);
fclose(g);
if (bombs == retries) /*fresh out of luck.*/
{/*trouble*/
fprintf(stderr,"Have made %d attempts to restart named resulting in failure.\n",retries);
exit (1);
}/*trouble*/
if (bombs > retries)
{/*real trouble*/
exit (1);
}/*real trouble*/
fprintf(stderr,"Will attempt to restart named on this system.  Process has died.\n");
 /*  process has died, start a new one */
 if ((pid = fork()) == 0) 
{
   execl("/usr/local/sbin/named","/usr/local/sbin/named",(char *)0);
   printf("EXEC FAILED\n");  /*If it can't see itself, it didn't
start.*/
} /*Program bombed.*/
 else if (pid == -1) {
   perror("fork"); 
   exit(errno);
 }
  exit(errno);
} /*Do if the process isn't running*/
else
 {/*A OK*/
system (" exec /bin/rm -f /var/tmp/namedverify.fail");
}/*A OK*/
} /*main end*/

/*The idea is to send several messages and then silently keep
trying until the problem is resolved.  It then deletes the
failure count and is ready to resume normal operation until the
next crisis.
*/


More information about the bind-users mailing list