aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/status/LegacyNodePageRequestHandler.java
blob: ae29f50e0979c2e0e4896187957f595e1a5f6693 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core.status;

import com.yahoo.vdslib.state.Node;
import com.yahoo.vdslib.state.NodeType;
import com.yahoo.vespa.clustercontroller.core.*;
import com.yahoo.vespa.clustercontroller.core.status.statuspage.StatusPageResponse;
import com.yahoo.vespa.clustercontroller.core.status.statuspage.StatusPageServer;

import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author Haakon Humberset
*/
public class LegacyNodePageRequestHandler implements StatusPageServer.RequestHandler {

    private static final Pattern nodePattern = Pattern.compile("/node=([a-z]+)\\.(\\d+)");
    private final Timer timer;
    private final EventLog eventLog;
    private final ContentCluster cluster;

    public LegacyNodePageRequestHandler(Timer timer, EventLog eventLog, ContentCluster cluster) {
        this.timer = timer;
        this.eventLog = eventLog;
        this.cluster = cluster;
    }

    @Override
    public StatusPageResponse handle(StatusPageServer.HttpRequest request) {
        Matcher m = nodePattern.matcher(request.getPath());
        if (!m.matches()) {
            throw new IllegalStateException("Node request handler invoked but failed to match path");
        }
        TimeZone tz = TimeZone.getTimeZone("UTC");
        long currentTime = timer.getCurrentTimeInMillis();
        NodeType nodeType = NodeType.get(m.group(1));
        int index = Integer.parseInt(m.group(2));
        Node node = new Node(nodeType, index);

        StatusPageResponse response = new StatusPageResponse();
        response.setContentType("text/html");
        StringBuilder content = new StringBuilder();
        content.append("<!-- Answer to request " + request + " -->\n");
        response.writeHtmlHeader(content, "Cluster Controller Status Page - Node status for " + node);
        content.append("<p>UTC time when creating this page: ").append(RealTimer.printDateNoMilliSeconds(currentTime, tz)).append("</p>");
        content.append("[ <a href=\"..\">Back to cluster overview</a> ] <br><br>");
        eventLog.writeHtmlState(content, node);
        NodeInfo nodeInfo = cluster.getNodeInfo(node);
        content.append("<h2>Host info</h2>\n");
        if (nodeInfo.getHostInfo() != null) {
            content.append("<pre>\n").append(nodeInfo.getHostInfo().getRawCreationString()).append("\n</pre>\n");
        } else {
            content.append("Not retrieved\n");
        }
        response.writeHtmlFooter(content, "");
        response.writeContent(content.toString());
        return response;
    }

    @Override
    public String pattern() { return "^/node=([a-z]+)\\.(\\d+)$"; }

}