Socket Watching

The purpose of these scripts is to watch certain sockets where services should be running. If it finds that the socket does not exist, it starts the service. The scripts are written in such a way that they can easily be used even through telnet or ssh. This is why they are split to several small scripts instead of doing everything in one big program.

The scripts are used in production environment but they are not general enough. There is no warranty that they will be useful for anyone else. Most probably you will have to adapt them to your own needs.

The main script is sockwatch.cmd. It is started at the end of my startup.cmd after some delay so that I am sure that TCP/IP is already initialized. The script has its own initial delay and then it checks the spckets every delay seconds by issuing netstat -s. The stream sockets are considered running if there is at least one of them in state LISTEN or ESTABLISHED. I currently do not watch any DGRAM sockets. They are in UDP state. For higher security I do not use networking with MySQL. I have to check AF_OS2 address family. If MySQL runs, there is a socket name with "local name" assigned. In such case I set the state variable to 'LISTEN' in order to make the next commands easier.

Each service is defined by two variables: *sock and *server. The first variable defines the text which should appear in the 4th column. The exact text may vary, it depends on the contents of %ETC%\services. Try netstat -s when all services are running in order to see what your machine displays. The second variable contains the command which starts the server. It must start as a new process, therefore the command must either contain start explicitly or must call another script which issues start.

Port 22 is used by SSH daemon. I start it by simple command. Anyway, if it does not run, I have no chance for remote connection.

MySQL is started by mysql-start.cmd which is also included. I do not restart often, usually after some crash. There is a risk that the table may be corrupted, therefore I run check on all tables. there is an additional delay, without it Apache does not start correctly (I do not know why, this is just observation). Rexx cannot have wildcards for directory names, therefore I run the check from bash, bsh.cmd just sets environment for bash (I have different setting for PATH). The log is written to a directory which is available by https, so that I can view it from everywhere and even delete the log file by a button i a web browser. MySQL can be stopped by mysql -p shutdown.

Apache is started by ap-start.cmd which only calls apache.cmd. It was historically written in such a way that I could easily switch between normal Apache and ApacheSSL. It requires that the DLL modules are in two different directories, httpd.exe and httpd.dll are in their own directories and are copied before Apache starts. The configuration file is split into several small parts and the configuration file, which defines the location of the DLL files, is also copied to the place from where it will be included into the main config. I do not use this feature any more but it can serve for switching between two versions of Apache.

Sometimes it is necessary to stop some service for some time. However, sockwatch.cmd will soon restart it. In order to solve this problem I just temporarily rename the script which starts that service. It is manual work but it can be easily achieved even remotely through SSH.

Such actions are usually necessary if something does not work well. Therefore I did not develop any special method for disabling automatic start of services. If things do not work well, it is better to rename a file than rely upon sophisticated programs.

The scripts including this document are available from sockwatch.zip.

Z. Wagner, wagner@mbox.cesnet.cz