[ Pobierz całość w formacie PDF ]
.Anotherdrawback is that there is no indication with /etc/inittab and the init command that the processhas exited and restarted unless the process continuously dies and init prints a message on theconsole.The message typically is something like Command is respawning too rapidly.Howcan you provide a system independent method of monitoring and restarting critical systemprocesses? The answer is procmon.Note BSD-based operating systems, such as SunOS, BSD/OS from BSD Inc., and FreeBSDdo not use /etc/inittab to control processes spawned by init.Handling Input and OutputAs with the shell, PERL uses the same three standard input and output files: standard input,known as STDIN; standard output, known as STDOUT; and standard error, known asSTDERR.Another similarity with the shell is the method of writing information into thesestreams.The printing of information to an open file descriptor such as STDOUT is accom-plished by using the PERL command print or printf, as shown in this example.printf STDOUT This is a test\n ;The PERL language has a C language syntax for many of its commands.The precedingexample prints the text This is a test followed by a newline on the standard output device.To print the same message only on standard error, use the command:printf STDERR This is a test\n ;Any file descriptor or file handle name can be used in place of STDOUT and STDERR,provided it has been opened for write first.If the corresponding file has not been opened, thetext cannot be seen anywhere.Understanding and Creating Daemons 67It may also be necessary to close one or more of the standard I/O streams to prevent unwantedtext from leaking into places where it is not desired.A good programmer would not allowthis to happen anyway.To close a file descriptor such as STDOUT in PERL, use the followingcommand:close(STDOUT);If these standard I/O file descriptors are not needed to communicate with the outside world,then they can be closed.This means that all output from the program must be directedspecifically to the location where you want it to go.Handling SignalsHandling the types of signals that can be generated by this daemon must be included in thePERL program.Failure to capture the signals may result in the program terminating early foran unknown reason.If, for example, your program uses pipe to communicate with otherprograms, it may terminate abnormally if the other program wasn t ready.Catching theSIGPIPE signal will prevent the program from terminating.Trapping a signal in PERL requires that you write a signal handler to respond to the signal, orset it to be ignored, as in the following example:$SIG{ PIPE } = IGNORE ; # signal value 13In this case, the SIGPIPE signal will be ignored.This means that if the program receives asignal SIGPIPE, it is ignored and does not cause the program to terminate.The procmon ProgramThe procmon program is a PERL script that is started during system startup and runs for thelife of the system.It has been written to be a system daemon, and it behaves as such.Thepurpose of this program is to monitor the operation of a set of defined processes; if they arenot present in the process list, procmon restarts them.This daemon is also designed to log itsactions, such as when the process fails and the time it is restarted.A sample of procmon smonitoring log, created by using the Unix syslog facility, is shown here:Feb 20 07:31:21 nic procmon[943]: Process Monitor startedFeb 20 07:31:21 nic procmon[943]: Loaded config file /etc/procmon.cfgFeb 20 07:31:22 nic procmon[943]: Command File: /etc/procmon.cmdFeb 20 07:31:22 nic procmon[943]: Loop Delay = 300Feb 20 07:31:22 nic procmon[943]: Adding named to stored process listFeb 20 07:31:22 nic procmon[943]: Monitoring : 1 processesFeb 20 07:31:22 nic procmon[943]: named running as PID 226Feb 20 07:36:22 nic procmon[943]: named running as PID 226Feb 20 07:41:23 nic procmon[943]: named running as PID 22668 Part I: Managing Internet SecurityThis syslog output shows procmon as it starts and records what it is doing with named.Thissetup is helpful for troubleshooting.You need to have as much logging information as possibleabout the process you were monitoring.The benefit of a program such as this one is most noticeable when the program is started atsystem boot time.How the program starts depends on the Unix variant you are using.OnSystem V systems, the command line shown here is added to /etc/rc2 or to a file in /etc/rc2.ddirectory, which is the preferred method.BSD-based systems use the same command, but inthe /etc/rc.local directory./usr/local/bin/procmon &procmon is in fact a daemon process.It handles all the system signals, and disconnects itselffrom a controlling terminal.When procmon starts, it prints a line indicating what configura-tion parameters it is using, and then quietly moves to the background.All logging at this pointis performed by the Unix syslog facility.The output that is printed when using theprocmon.cmd file is as follows:Found /etc/procmon.cfg.loading.When using the program defaults, this is what you will see:no config file.using defaults.Two configuration files are used by procmon: procmon.cfg and procmon.cmd.Only theprocmon.cmd file is absolutely necessary.If procmon
[ Pobierz całość w formacie PDF ]