Subversion Repositories DevTools

Rev

Rev 1361 | Rev 4212 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.erggroup.buildtool.daemon;

import com.erggroup.buildtool.daemon.BuildDaemon;

import org.apache.log4j.Logger;
import java.net.ServerSocket;
import java.net.Socket;
import java.lang.Thread;
import java.lang.Package;
import java.io.*;

//----------------------------------------------------------------------------
// CLASS              : NagiosThread
//
// DESCRIPTION        : A class to provide Nagios support
//
//
public class NagiosThread extends Thread {
    // Server socket
    private ServerSocket srv;

    // BuildDaemon
    private BuildDaemon mBuildDaemon;

    // Flag that indicates whether the poller is running or not.
    private volatile boolean isRunning = true;

    /**Logger
     * @attribute
     */
    private static final Logger mLogger = Logger.getLogger(NagiosThread.class);
    
    // Constructor.
    public NagiosThread(ServerSocket srv, BuildDaemon bd) {
        this.isRunning = true;
        this.srv = srv;
        this.mBuildDaemon = bd;
    }

    // Method for terminating the listener
    public void terminate() {
        mLogger.fatal("NagiosThread terminate");
        this.isRunning = false;
    }

    /**
    * This method start the thread and performs all the operations.
    */
    public void run() {
        mLogger.fatal("NagiosThread Run");
        setName("Nagios");
        try {
            // Wait for connection from client.
            while (isRunning) {
                Socket socket = srv.accept();
                mLogger.warn("Nagios Socket Accepted");
                

            // Open a reader to receive (and ignore the input)
            BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            // Write the status message to the outputstream
            // Currently this is too simple, but it is a start
            //
            try {
                BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                if ( mBuildDaemon == null ) {
                    wr.write("Build Daemon status: NULL\n");
                } else if ( mBuildDaemon.checkThreads() ) {
                    wr.write("Build Daemon status: OK\n");
                } else {
                    wr.write("Build Daemon status: NOT HAPPY\n");
                    mLogger.fatal("NagiosThread: NOT HAPPY");
                }

                wr.write("Version: "+ this.getClass().getPackage().getImplementationVersion() + "\n");
                wr.write("HostName: "+ mBuildDaemon.mHostname + "\n");

                File cwd = new File(".");
                wr.write( "Usable space: " + cwd.getUsableSpace() + "\n");
                
                wr.flush();
            } catch (IOException e) {
                mLogger.fatal("NagiosThread caught Exception writing to port:" + e.getMessage() );
            }

            // Close the inputstream since we really don't care about it
            rd.close();
          }
        } catch (Exception e) {
            mLogger.fatal("NagiosThread caught Exception" + e.getMessage());
        }
    }
}