Subversion Repositories DevTools

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1350 dpurdie 1
package com.erggroup.buildtool.daemon;
2
 
3
import com.erggroup.buildtool.daemon.BuildDaemon;
4
 
5
import org.apache.log4j.Logger;
6
import java.net.ServerSocket;
7
import java.net.Socket;
8
import java.lang.Thread;
9
import java.lang.Package;
10
import java.io.*;
11
 
12
//----------------------------------------------------------------------------
13
// CLASS              : NagiosThread
14
//
15
// DESCRIPTION        : A class to provide Nagios support
16
//
17
//
18
public class NagiosThread extends Thread {
19
    // Server socket
20
    private ServerSocket srv;
21
 
1361 dpurdie 22
    // BuildDaemon
23
    private BuildDaemon mBuildDaemon;
24
 
1350 dpurdie 25
    // Flag that indicates whether the poller is running or not.
26
    private volatile boolean isRunning = true;
27
 
28
    /**Logger
29
     * @attribute
30
     */
31
    private static final Logger mLogger = Logger.getLogger(NagiosThread.class);
32
 
33
    // Constructor.
1361 dpurdie 34
    public NagiosThread(ServerSocket srv, BuildDaemon bd) {
1350 dpurdie 35
        this.isRunning = true;
36
        this.srv = srv;
1361 dpurdie 37
        this.mBuildDaemon = bd;
1350 dpurdie 38
    }
39
 
40
    // Method for terminating the listener
41
    public void terminate() {
42
        mLogger.fatal("NagiosThread terminate");
43
        this.isRunning = false;
44
    }
45
 
46
    /**
47
    * This method start the thread and performs all the operations.
48
    */
49
    public void run() {
50
        mLogger.fatal("NagiosThread Run");
4123 dpurdie 51
        setName("Nagios");
1350 dpurdie 52
        try {
53
            // Wait for connection from client.
54
            while (isRunning) {
55
                Socket socket = srv.accept();
4123 dpurdie 56
                mLogger.warn("Nagios Socket Accepted");
57
 
1350 dpurdie 58
 
59
            // Open a reader to receive (and ignore the input)
60
            BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
61
 
62
            // Write the status message to the outputstream
63
            // Currently this is too simple, but it is a start
64
            //
65
            try {
66
                BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
1361 dpurdie 67
                if ( mBuildDaemon == null ) {
68
                    wr.write("Build Daemon status: NULL\n");
69
                } else if ( mBuildDaemon.checkThreads() ) {
70
                    wr.write("Build Daemon status: OK\n");
71
                } else {
72
                    wr.write("Build Daemon status: NOT HAPPY\n");
4123 dpurdie 73
                    mLogger.fatal("NagiosThread: NOT HAPPY");
1361 dpurdie 74
                }
75
 
1350 dpurdie 76
                wr.write("Version: "+ this.getClass().getPackage().getImplementationVersion() + "\n");
1361 dpurdie 77
                wr.write("HostName: "+ mBuildDaemon.mHostname + "\n");
78
 
79
                File cwd = new File(".");
80
                wr.write( "Usable space: " + cwd.getUsableSpace() + "\n");
81
 
1350 dpurdie 82
                wr.flush();
83
            } catch (IOException e) {
84
                mLogger.fatal("NagiosThread caught Exception writing to port:" + e.getMessage() );
85
            }
86
 
87
            // Close the inputstream since we really don't care about it
88
            rd.close();
89
          }
90
        } catch (Exception e) {
91
            mLogger.fatal("NagiosThread caught Exception" + e.getMessage());
92
        }
93
    }
94
}
95