Subversion Repositories DevTools

Rev

Rev 4219 | Details | Compare with Previous | 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.io.*;
10
 
11
//----------------------------------------------------------------------------
12
// CLASS              : NagiosThread
13
//
14
// DESCRIPTION        : A class to provide Nagios support
15
//
16
//
17
public class NagiosThread extends Thread {
18
    // Server socket
19
    private ServerSocket srv;
20
 
1361 dpurdie 21
    // BuildDaemon
22
    private BuildDaemon mBuildDaemon;
23
 
1350 dpurdie 24
    // Flag that indicates whether the poller is running or not.
25
    private volatile boolean isRunning = true;
26
 
27
    /**Logger
28
     * @attribute
29
     */
30
    private static final Logger mLogger = Logger.getLogger(NagiosThread.class);
31
 
32
    // Constructor.
1361 dpurdie 33
    public NagiosThread(ServerSocket srv, BuildDaemon bd) {
1350 dpurdie 34
        this.isRunning = true;
35
        this.srv = srv;
1361 dpurdie 36
        this.mBuildDaemon = bd;
1350 dpurdie 37
    }
38
 
39
    // Method for terminating the listener
40
    public void terminate() {
41
        mLogger.fatal("NagiosThread terminate");
42
        this.isRunning = false;
43
    }
44
 
45
    /**
46
    * This method start the thread and performs all the operations.
47
    */
48
    public void run() {
49
        mLogger.fatal("NagiosThread Run");
4123 dpurdie 50
        setName("Nagios");
1350 dpurdie 51
        try {
52
            // Wait for connection from client.
53
            while (isRunning) {
54
                Socket socket = srv.accept();
4123 dpurdie 55
                mLogger.warn("Nagios Socket Accepted");
56
 
1350 dpurdie 57
 
58
            // Open a reader to receive (and ignore the input)
59
            BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
60
 
4212 dpurdie 61
//          try {
62
//              String request = rd.readLine();
63
//              BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
64
//              wr.write("Build Daemon nagios inferface command:"+ request +" \n");
65
//              if (  request.equals( "quit") ) {
66
//                  wr.write("Got a QUIT\n");
67
//              }
68
//              else if (request.equals("status")) {
69
//                  wr.write("Got a STATUS\n");
70
//              }
71
//              else {
72
//                  wr.write("Unknown command\n");
73
//              }
74
//              wr.flush();
75
//          } catch (IOException e) {
76
//              mLogger.fatal("NagiosThread caught Exception reading from port:" + e.getMessage() );
77
//          }
78
 
79
 
1350 dpurdie 80
            // Write the status message to the outputstream
81
            // Currently this is too simple, but it is a start
82
            //
83
            try {
84
                BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
1361 dpurdie 85
                if ( mBuildDaemon == null ) {
86
                    wr.write("Build Daemon status: NULL\n");
87
                } else if ( mBuildDaemon.checkThreads() ) {
88
                    wr.write("Build Daemon status: OK\n");
89
                } else {
90
                    wr.write("Build Daemon status: NOT HAPPY\n");
4123 dpurdie 91
                    mLogger.fatal("NagiosThread: NOT HAPPY");
1361 dpurdie 92
                }
93
 
1350 dpurdie 94
                wr.write("Version: "+ this.getClass().getPackage().getImplementationVersion() + "\n");
4219 dpurdie 95
                wr.write("HostName: "+ BuildDaemon.mHostname + "\n");
1361 dpurdie 96
 
97
                File cwd = new File(".");
98
                wr.write( "Usable space: " + cwd.getUsableSpace() + "\n");
99
 
1350 dpurdie 100
                wr.flush();
101
            } catch (IOException e) {
102
                mLogger.fatal("NagiosThread caught Exception writing to port:" + e.getMessage() );
103
            }
104
 
105
            // Close the inputstream since we really don't care about it
106
            rd.close();
107
          }
108
        } catch (Exception e) {
109
            mLogger.fatal("NagiosThread caught Exception" + e.getMessage());
110
        }
111
    }
112
}
113