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");
51
        try {
52
            // Wait for connection from client.
53
            while (isRunning) {
54
                Socket socket = srv.accept();
55
 
56
            // Open a reader to receive (and ignore the input)
57
            BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
58
 
59
            // Write the status message to the outputstream
60
            // Currently this is too simple, but it is a start
61
            //
62
            try {
63
                BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
1361 dpurdie 64
                if ( mBuildDaemon == null ) {
65
                    wr.write("Build Daemon status: NULL\n");
66
                } else if ( mBuildDaemon.checkThreads() ) {
67
                    wr.write("Build Daemon status: OK\n");
68
                } else {
69
                    wr.write("Build Daemon status: NOT HAPPY\n");
70
                }
71
 
1350 dpurdie 72
                wr.write("Version: "+ this.getClass().getPackage().getImplementationVersion() + "\n");
1361 dpurdie 73
                wr.write("HostName: "+ mBuildDaemon.mHostname + "\n");
74
 
75
                File cwd = new File(".");
76
                wr.write( "Usable space: " + cwd.getUsableSpace() + "\n");
77
 
1350 dpurdie 78
                wr.flush();
79
            } catch (IOException e) {
80
                mLogger.fatal("NagiosThread caught Exception writing to port:" + e.getMessage() );
81
            }
82
 
83
            // Close the inputstream since we really don't care about it
84
            rd.close();
85
          }
86
        } catch (Exception e) {
87
            mLogger.fatal("NagiosThread caught Exception" + e.getMessage());
88
        }
89
    }
90
}
91