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
 
4212 dpurdie 62
//          try {
63
//              String request = rd.readLine();
64
//              BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
65
//              wr.write("Build Daemon nagios inferface command:"+ request +" \n");
66
//              if (  request.equals( "quit") ) {
67
//                  wr.write("Got a QUIT\n");
68
//              }
69
//              else if (request.equals("status")) {
70
//                  wr.write("Got a STATUS\n");
71
//              }
72
//              else {
73
//                  wr.write("Unknown command\n");
74
//              }
75
//              wr.flush();
76
//          } catch (IOException e) {
77
//              mLogger.fatal("NagiosThread caught Exception reading from port:" + e.getMessage() );
78
//          }
79
 
80
 
1350 dpurdie 81
            // Write the status message to the outputstream
82
            // Currently this is too simple, but it is a start
83
            //
84
            try {
85
                BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
1361 dpurdie 86
                if ( mBuildDaemon == null ) {
87
                    wr.write("Build Daemon status: NULL\n");
88
                } else if ( mBuildDaemon.checkThreads() ) {
89
                    wr.write("Build Daemon status: OK\n");
90
                } else {
91
                    wr.write("Build Daemon status: NOT HAPPY\n");
4123 dpurdie 92
                    mLogger.fatal("NagiosThread: NOT HAPPY");
1361 dpurdie 93
                }
94
 
1350 dpurdie 95
                wr.write("Version: "+ this.getClass().getPackage().getImplementationVersion() + "\n");
1361 dpurdie 96
                wr.write("HostName: "+ mBuildDaemon.mHostname + "\n");
97
 
98
                File cwd = new File(".");
99
                wr.write( "Usable space: " + cwd.getUsableSpace() + "\n");
100
 
1350 dpurdie 101
                wr.flush();
102
            } catch (IOException e) {
103
                mLogger.fatal("NagiosThread caught Exception writing to port:" + e.getMessage() );
104
            }
105
 
106
            // Close the inputstream since we really don't care about it
107
            rd.close();
108
          }
109
        } catch (Exception e) {
110
            mLogger.fatal("NagiosThread caught Exception" + e.getMessage());
111
        }
112
    }
113
}
114