Subversion Repositories DevTools

Rev

Rev 6914 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6914 Rev 7033
Line 1... Line 1...
1
package com.erggroup.buildtool.daemon;
1
package com.erggroup.buildtool.daemon;
2
 
2
 
3
import com.erggroup.buildtool.daemon.BuildDaemon;
3
import com.erggroup.buildtool.daemon.BuildDaemon;
4
 
4
 
5
import org.apache.log4j.Logger;
5
import org.slf4j.Logger;
-
 
6
import org.slf4j.LoggerFactory;
6
 
7
 
7
import java.text.SimpleDateFormat;
8
import java.text.SimpleDateFormat;
8
import java.util.Calendar;
9
import java.util.Calendar;
9
import java.util.LinkedHashMap;
10
import java.util.LinkedHashMap;
10
import java.util.Map.Entry;
11
import java.util.Map.Entry;
Line 43... Line 44...
43
    private final String startString = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(Calendar.getInstance().getTime());
44
    private final String startString = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(Calendar.getInstance().getTime());
44
 
45
 
45
    /**Logger
46
    /**Logger
46
     * @attribute
47
     * @attribute
47
     */
48
     */
48
    private static final Logger mLogger = Logger.getLogger(NagiosThread.class);
49
    private static final Logger mLogger = LoggerFactory.getLogger(NagiosThread.class);
49
    
50
    
50
    // Constructor.
51
    // Constructor.
51
    public NagiosThread(ServerSocket srv, BuildDaemon bd) {
52
    public NagiosThread(ServerSocket srv, BuildDaemon bd) {
52
        this.isRunning = true;
53
        this.isRunning = true;
53
        this.srv = srv;
54
        this.srv = srv;
54
        this.mBuildDaemon = bd;
55
        this.mBuildDaemon = bd;
55
    }
56
    }
56
 
57
 
57
    // Method for terminating the listener
58
    // Method for terminating the listener
58
    public void terminate() {
59
    public void terminate() {
59
        mLogger.fatal("NagiosThread terminate");
60
        mLogger.error("NagiosThread terminate");
60
        this.isRunning = false;
61
        this.isRunning = false;
61
        try  {
62
        try  {
62
            srv.close();
63
            srv.close();
63
        }
64
        }
64
        catch (IOException e) {
65
        catch (IOException e) {
65
            mLogger.fatal("NagiosThread terminate Exception on srv.close: " + e.getMessage());
66
            mLogger.error("NagiosThread terminate Exception on srv.close: " + e.getMessage());
66
        }
67
        }
67
    }
68
    }
68
   
69
   
69
    /** 
70
    /** 
70
     * Sleep and handle exceptions
71
     * Sleep and handle exceptions
Line 81... Line 82...
81
    /**
82
    /**
82
     * This method start the thread and performs all the operations.
83
     * This method start the thread and performs all the operations.
83
     */
84
     */
84
    @Override
85
    @Override
85
    public void run() {
86
    public void run() {
86
        mLogger.fatal("NagiosThread Run");
87
        mLogger.error("NagiosThread Run");
87
        setName("Nagios");
88
        setName("Nagios");
88
 
89
 
89
        // Wait for connection from client.
90
        // Wait for connection from client.
90
        // Process one command than then close the socket
91
        // Process one command than then close the socket
91
        // This is not an interactive session
92
        // This is not an interactive session
Line 97... Line 98...
97
                mLogger.info("Nagios Socket Accepted");
98
                mLogger.info("Nagios Socket Accepted");
98
                processSocketRequest(socket);
99
                processSocketRequest(socket);
99
                try {
100
                try {
100
                    socket.close();
101
                    socket.close();
101
                } catch (IOException e) {
102
                } catch (IOException e) {
102
                    mLogger.fatal("NagiosThread Exception on socket opr: " + e.getMessage());
103
                    mLogger.error("NagiosThread Exception on socket opr: " + e.getMessage());
103
                    sleep(1000);
104
                    sleep(1000);
104
                }
105
                }
105
            }
106
            }
106
 
107
 
107
            catch (IOException e) {
108
            catch (IOException e) {
108
                mLogger.fatal("NagiosThread Exception on srv.accept: " + e.getMessage());
109
                mLogger.error("NagiosThread Exception on srv.accept: " + e.getMessage());
109
                sleep(1000);
110
                sleep(1000);
110
            }
111
            }
111
        }
112
        }
112
        
113
        
113
        //
114
        //
Line 134... Line 135...
134
        //      Insert a dummy 'status' command if no input is received in 3 seconds
135
        //      Insert a dummy 'status' command if no input is received in 3 seconds
135
        //
136
        //
136
        try {
137
        try {
137
            socket.setSoTimeout(3000);
138
            socket.setSoTimeout(3000);
138
        } catch (SocketException e) {
139
        } catch (SocketException e) {
139
            mLogger.fatal("NagiosThread Exception on setSoTimeout: " + e.getMessage());
140
            mLogger.error("NagiosThread Exception on setSoTimeout: " + e.getMessage());
140
        }
141
        }
141
        
142
        
142
        ;
143
        ;
143
        try {
144
        try {
144
            BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
145
            BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
145
            processRequest(socket, rd);
146
            processRequest(socket, rd);
146
            rd.close();
147
            rd.close();
147
        } catch (IOException e) {
148
        } catch (IOException e) {
148
            mLogger.fatal("NagiosThread Exception on getInputStream: " + e.getMessage());
149
            mLogger.error("NagiosThread Exception on getInputStream: " + e.getMessage());
149
        }
150
        }
150
    }
151
    }
151
    
152
    
152
    /** Process the request
153
    /** Process the request
153
     *  Have a BufferedReader that will contain the socket data
154
     *  Have a BufferedReader that will contain the socket data
Line 186... Line 187...
186
                    requestType = requestTokens[0];
187
                    requestType = requestTokens[0];
187
                    requestTokens = requestTokens[1].substring(1).split("/");
188
                    requestTokens = requestTokens[1].substring(1).split("/");
188
                    request = requestTokens[0];
189
                    request = requestTokens[0];
189
                }
190
                }
190
                catch (IndexOutOfBoundsException e1) {
191
                catch (IndexOutOfBoundsException e1) {
191
                    mLogger.fatal("NagiosThread cannot parse HTTP header");
192
                    mLogger.error("NagiosThread cannot parse HTTP header");
192
                    return; 
193
                    return; 
193
                }
194
                }
194
            }
195
            }
195
        }
196
        }
196
        catch ( SocketTimeoutException s)
197
        catch ( SocketTimeoutException s)
197
        {
198
        {
198
            mLogger.warn("Nagios Socket read timeout");
199
            mLogger.warn("Nagios Socket read timeout");
199
            request = "status";
200
            request = "status";
200
            
201
            
201
        } catch (IOException e) {
202
        } catch (IOException e) {
202
            mLogger.fatal("NagiosThread Exception on processRequest: " + e.getMessage());
203
            mLogger.error("NagiosThread Exception on processRequest: " + e.getMessage());
203
            return;
204
            return;
204
        }
205
        }
205
       
206
       
206
        //  Process the users request and generate a response
207
        //  Process the users request and generate a response
207
        
208
        
Line 228... Line 229...
228
            //  Flush out the input stream
229
            //  Flush out the input stream
229
            wr.flush();
230
            wr.flush();
230
            wr.close();
231
            wr.close();
231
 
232
 
232
        } catch (IOException e) {
233
        } catch (IOException e) {
233
            mLogger.fatal("NagiosThread caught Exception writing to port:" + e.getMessage() );
234
            mLogger.error("NagiosThread caught Exception writing to port:" + e.getMessage() );
234
        }
235
        }
235
    }
236
    }
236
    
237
    
237
    /**
238
    /**
238
     * Generate a response to the Nagios (or user ) request 
239
     * Generate a response to the Nagios (or user ) request 
Line 252... Line 253...
252
        int reasonCount = nagInfo.extendedReason.size();
253
        int reasonCount = nagInfo.extendedReason.size();
253
        if (  reasonCount <= 0) {
254
        if (  reasonCount <= 0) {
254
            resp.append("Build Daemon status: OK\n");
255
            resp.append("Build Daemon status: OK\n");
255
        } else {
256
        } else {
256
            resp.append("Build Daemon status: NOT HAPPY\n");
257
            resp.append("Build Daemon status: NOT HAPPY\n");
257
            mLogger.fatal("NagiosThread: NOT HAPPY");
258
            mLogger.error("NagiosThread: NOT HAPPY");
258
            
259
            
259
            // Extended information - Reasons for being not happy
260
            // Extended information - Reasons for being not happy
260
 
261
 
261
            if (reasonCount > 0)
262
            if (reasonCount > 0)
262
            {
263
            {
Line 321... Line 322...
321
        String outBody = dumpJson(status, "");
322
        String outBody = dumpJson(status, "");
322
        resp.append(outBody);
323
        resp.append(outBody);
323
    }
324
    }
324
    else if ( request.equals( "shutdown") ) {
325
    else if ( request.equals( "shutdown") ) {
325
        resp.append("Gracefull shutdown\n");
326
        resp.append("Gracefull shutdown\n");
326
        mLogger.fatal("Gracefull shutdown requested");
327
        mLogger.error("Gracefull shutdown requested");
327
        BuildDaemon.mShutDown = true;
328
        BuildDaemon.mShutDown = true;
328
    }
329
    }
329
    else if ( request.equals( "kill") ) {
330
    else if ( request.equals( "kill") ) {
330
        resp.append("Will commit suicide\n");
331
        resp.append("Will commit suicide\n");
331
        mLogger.fatal("Immediate program termination");
332
        mLogger.error("Immediate program termination");
332
        mustDie = true;
333
        mustDie = true;
333
        isRunning = false;
334
        isRunning = false;
334
    }
335
    }
335
    else {
336
    else {
336
        String eMgs = "Build Daemon Command Interface: Unknown command:" + request;
337
        String eMgs = "Build Daemon Command Interface: Unknown command:" + request;